|
rllib
1
|
#include <rlinifile.h>

Classes | |
| struct | _rlSection_ |
| struct | _rlSectionName_ |
Public Member Functions | |
| rlIniFile () | |
| virtual | ~rlIniFile () |
| int | read (const char *filename) |
| int | write (const char *filename) |
| const char * | filename () |
| const char * | text (const char *section, const char *name) |
| void | setText (const char *section, const char *name, const char *text) |
| int | printf (const char *section, const char *name, const char *format,...) |
| void | remove (const char *section) |
| void | remove (const char *section, const char *name) |
| const char * | firstSection () |
| const char * | nextSection () |
| const char * | firstName (const char *section) |
| const char * | nextName (const char *section) |
| void | setDefaultSection (const char *section) |
| const char * | defaultSection () |
| const char * | i18n (const char *tag, const char *default_text) |
| const char * | tr (const char *txt) |
Private Types | |
| typedef struct rlIniFile::_rlSectionName_ | rlSectionName |
| typedef struct rlIniFile::_rlSection_ | rlSection |
Private Member Functions | |
| void | copyIdentifier (char *buf, const char *line) |
| void | copyName (char *buf, const char *line) |
| void | copyParam (char *buf, const char *line) |
| void | deleteSectionNames (rlSection *section) |
Private Attributes | |
| rlSection * | _firstSection |
| int | currentSection |
| int | currentName |
| rlString | fname |
| rlString | default_section |
class for INI files as known from Windows.
Definition at line 25 of file rlinifile.h.
typedef struct rlIniFile::_rlSection_ rlIniFile::rlSection [private] |
typedef struct rlIniFile::_rlSectionName_ rlIniFile::rlSectionName [private] |
| rlIniFile::rlIniFile | ( | ) |
Definition at line 29 of file rlinifile.cpp.
{
_firstSection = new rlSection; // first section holds names with section name = null_string
_firstSection->nextSection = NULL;
_firstSection->firstName = NULL;
_firstSection->name = new char[1];
_firstSection->name[0] = '\0';
currentSection = currentName = -1;
}
| rlIniFile::~rlIniFile | ( | ) | [virtual] |
Definition at line 39 of file rlinifile.cpp.
{
rlSection *section, *last_section;
section = _firstSection;
while(section != NULL)
{
deleteSectionNames(section);
last_section = section;
section = section->nextSection;
delete last_section;
}
}
| void rlIniFile::copyIdentifier | ( | char * | buf, |
| const char * | line | ||
| ) | [private] |
Definition at line 70 of file rlinifile.cpp.
{
int i = 1;
buf[0] = '\0';
if(line[0] != '[') return;
while(line[i] != ']' && line[i] != '\0')
{
*buf++ = line[i++];
}
*buf = '\0';
}
| void rlIniFile::copyName | ( | char * | buf, |
| const char * | line | ||
| ) | [private] |
Definition at line 82 of file rlinifile.cpp.
{
int i = 0;
buf[0] = '\0';
//while(line[i] > ' ' && line[i] != '=')
while(line[i] != '\0' && line[i] != '=')
{
*buf++ = line[i++];
}
*buf = '\0';
i--; // eventually delete spaces
while(i>=0 && (buf[i] == ' ' || buf[i] == '\t'))
{
buf[i--] = '\0';
}
}
| void rlIniFile::copyParam | ( | char * | buf, |
| const char * | line | ||
| ) | [private] |
Definition at line 99 of file rlinifile.cpp.
{
const char *cptr;
buf[0] = '\0';
cptr = strchr(line,'=');
if(cptr == NULL) return;
cptr++;
while((*cptr == ' ' || *cptr == '\t') && *cptr != '\0') cptr++;
if(*cptr == '\0') return;
while(*cptr != '\0' && *cptr != '\n') *buf++ = *cptr++;
*buf = '\0';
}
| const char * rlIniFile::defaultSection | ( | ) |
Definition at line 480 of file rlinifile.cpp.
{
return default_section.text();
}
| void rlIniFile::deleteSectionNames | ( | rlSection * | section | ) | [private] |
Definition at line 53 of file rlinifile.cpp.
{
rlSectionName *name, *last_name;
if(section == NULL) return;
name = section->firstName;
while(name != NULL)
{
if(name->name != NULL) delete [] name->name;
if(name->param != NULL) delete [] name->param;
last_name = name;
name = name->nextName;
delete [] last_name;
}
if(section->name != NULL) delete [] section->name;
}
| const char * rlIniFile::filename | ( | ) |
Definition at line 199 of file rlinifile.cpp.
| const char * rlIniFile::firstName | ( | const char * | section | ) |
Definition at line 428 of file rlinifile.cpp.
{
rlSection *s;
rlSectionName *n;
s = _firstSection;
while(s != NULL)
{
if(strcmp(section,s->name) == 0)
{
n = s->firstName;
currentName = 0;
return n->name;
}
s = s->nextSection;
}
return NULL;
}
| const char * rlIniFile::firstSection | ( | ) |
Definition at line 403 of file rlinifile.cpp.
{
currentSection = 0;
return _firstSection->name;
}
| const char * rlIniFile::i18n | ( | const char * | tag, |
| const char * | default_text | ||
| ) |
Use this method for translating text within your application.
Example:
ini->setDefaultSection("german");
printf("german_text=%s\n", ini->i18n("text1","This is text1") );
Definition at line 485 of file rlinifile.cpp.
{
const char *cptr = text(default_section.text(), tag);
if(strcmp(cptr,null_string) == 0) return default_text;
else return cptr;
}
| const char * rlIniFile::nextName | ( | const char * | section | ) |
Definition at line 447 of file rlinifile.cpp.
{
rlSection *s;
rlSectionName *n;
int i;
if(currentName < 0) return NULL;
currentName++;
i = 0;
s = _firstSection;
while(s != NULL)
{
if(strcmp(section,s->name) == 0)
{
n = s->firstName;
while(n != NULL)
{
if(i == currentName) return n->name;
i++;
n = n->nextName;
}
return NULL;
}
s = s->nextSection;
}
return NULL;
}
| const char * rlIniFile::nextSection | ( | ) |
Definition at line 409 of file rlinifile.cpp.
{
rlSection *s;
int i;
if(currentSection < 0) return NULL;
currentSection++;
i = 0;
s = _firstSection;
while(s != NULL)
{
if(i == currentSection) return s->name;
s = s->nextSection;
i++;
}
currentSection = -1;
return NULL;
}
| int rlIniFile::printf | ( | const char * | section, |
| const char * | name, | ||
| const char * | format, | ||
| ... | |||
| ) |
Definition at line 339 of file rlinifile.cpp.
{
int ret;
char buf[rl_PRINTF_LENGTH]; // should be big enough
va_list ap;
va_start(ap,format);
ret = rlvsnprintf(buf, rl_PRINTF_LENGTH - 1, format, ap);
va_end(ap);
if(ret > 0) setText(section, name, buf);
return ret;
}
| int rlIniFile::read | ( | const char * | filename | ) |
Definition at line 112 of file rlinifile.cpp.
{
FILE *fp;
char line[rl_PRINTF_LENGTH],
name_section[rl_PRINTF_LENGTH],
name_name[rl_PRINTF_LENGTH],
name_param[rl_PRINTF_LENGTH],
*cptr;
rlSection *s, *s_old;
// delete old content
s = _firstSection;
while(s != NULL)
{
deleteSectionNames(s);
s_old = s;
s = s->nextSection;
delete s_old;
}
// read the file
fname.setText(filename);
_firstSection = new rlSection; // first section holds names with section name = null_string
_firstSection->nextSection = NULL;
_firstSection->firstName = NULL;
_firstSection->name = new char[1];
_firstSection->name[0] = '\0';
fp = fopen(filename,"r");
if(fp == NULL) return -1;
name_section[0] = name_name[0] = name_param[0] = '\0';
while(fgets(line,sizeof(line)-1,fp) != NULL)
{
cptr = strchr(line,0x0d);
if(cptr != NULL) *cptr = '\0';
cptr = strchr(line,0x0a);
if(cptr != NULL) *cptr = '\0';
if(line[0] == '[') // section identifier
{
copyIdentifier(name_section,line);
setText(name_section, NULL, NULL);
}
else if(line[0] > ' ' && line[0] != '\t' && line[0] != '#') // name identifier
{
copyName(name_name,line);
copyParam(name_param,line);
setText(name_section, name_name, name_param);
}
else // it must be a comment line
{
setText(name_section, line, NULL);
}
}
fclose(fp);
return 0;
}
| void rlIniFile::remove | ( | const char * | section | ) |
Definition at line 352 of file rlinifile.cpp.
{
rlSection *s, *last;
last = NULL;
s = _firstSection;
while(s != NULL)
{
if(strcmp(section,s->name) == 0)
{
deleteSectionNames(s);
if(last != NULL) last->nextSection = s->nextSection;
delete s;
return;
}
last = s;
s = s->nextSection;
}
}
| void rlIniFile::remove | ( | const char * | section, |
| const char * | name | ||
| ) |
Definition at line 372 of file rlinifile.cpp.
{
rlSection *s;
rlSectionName *n, *last;
s = _firstSection;
while(s != NULL)
{
if(strcmp(section,s->name) == 0)
{
last = NULL;
n = s->firstName;
while(n != NULL)
{
if(strcmp(name,n->name) == 0)
{
if(n->name != NULL) delete [] n->name;
if(n->param != NULL) delete [] n->param;
if(last != NULL) last->nextName = n->nextName;
delete n;
return;
}
last = n;
n = n->nextName;
}
return;
}
s = s->nextSection;
}
}
| void rlIniFile::setDefaultSection | ( | const char * | section | ) |
Definition at line 475 of file rlinifile.cpp.
{
default_section.setText(section);
}
| void rlIniFile::setText | ( | const char * | section, |
| const char * | name, | ||
| const char * | text | ||
| ) |
Definition at line 230 of file rlinifile.cpp.
{
rlSection *s, *last_section;
rlSectionName *n, *last_name;
if(section == NULL) return;
last_section = NULL;
last_name = NULL;
s = _firstSection;
while(s != NULL)
{
if(strcmp(section,s->name) == 0)
{
last_name = NULL;
n = s->firstName;
while(n != NULL)
{
if(name != NULL && name[0] != '#' && name[0] != '\0' && strcmp(name,n->name) == 0)
{
if(n->param != NULL) delete [] n->param;
if(text == NULL)
{
n->param = new char[1];
n->param[0] = '\0';
}
else
{
n->param = new char[strlen(text)+1];
strcpy(n->param,text);
}
return;
}
last_name = n;
n = n->nextName;
}
if(last_name == NULL)
{
s->firstName = new rlSectionName;
n = s->firstName;
}
else
{
last_name->nextName = new rlSectionName;
n = last_name->nextName;
}
if(name == NULL)
{
n->name = new char[1];
n->name[0] = '\0';
}
else
{
n->name = new char[strlen(name)+1];
strcpy(n->name,name);
}
if(text == NULL)
{
n->param = new char[1];
n->param[0] = '\0';
}
else
{
n->param = new char[strlen(text)+1];
strcpy(n->param,text);
}
n->nextName = NULL;
return;
}
last_section = s;
s = s->nextSection;
}
if(last_section == NULL)
{
_firstSection = new rlSection;
last_section = _firstSection;
}
else
{
last_section->nextSection = new rlSection;
last_section = last_section->nextSection;
}
last_section->name = new char[strlen(section)+1];
strcpy(last_section->name,section);
last_section->nextSection = NULL;
if(name == NULL)
{
last_section->firstName = NULL;
}
else
{
last_section->firstName = new rlSectionName;
n = last_section->firstName;
n->name = new char[strlen(name)+1];
strcpy(n->name,name);
if(text == NULL)
{
n->param = new char[1];
n->param[0] = '\0';
}
else
{
n->param = new char[strlen(text)+1];
strcpy(n->param,text);
}
n->nextName = NULL;
}
return;
}
| const char * rlIniFile::text | ( | const char * | section, |
| const char * | name | ||
| ) |
Definition at line 204 of file rlinifile.cpp.
{
rlSection *s;
rlSectionName *n;
s = _firstSection;
while(s != NULL)
{
if(strcmp(section,s->name) == 0)
{
n = s->firstName;
while(n != NULL)
{
if(n->name != NULL && strcmp(name,n->name) == 0)
{
return n->param;
}
n = n->nextName;
}
return null_string;
}
s = s->nextSection;
}
return null_string;
}
| const char * rlIniFile::tr | ( | const char * | txt | ) |
Use this method for translating text within your application.
Example:
#define TR(txt) d->translator.tr(txt)
typedef struct // (todo: define your data structure here)
{
rlIniFile translator;
}
DATA; static int slotInit(PARAM *p, DATA *d)
{
if(p == NULL || d == NULL) return -1;
d->translator.read("text.ini");
d->translator.setDefaultSection("DEUTSCH");
printf("test1=%s\n", TR("umlaute"));
printf("test2=%s\n", TR("Xumlaute"));
d->translator.setDefaultSection("ENGLISH");
printf("test1=%s\n", TR("umlaute"));
printf("test2=%s\n", TR("Xumlaute"));
}With text.ini: [DEUTSCH] hello=Hallo world=Welt umlaute=äöüß
[ENGLISH] hello=Hello world=World umlaute=german_umlaute=äöüß
Definition at line 492 of file rlinifile.cpp.
{
if(txt == NULL) return "ERROR:txt=NULL";
char default_text[1024];
if(strlen(txt) < (sizeof(default_text) - 40)) sprintf(default_text,"tr_error:%s", txt);
else strcpy(default_text,"tr_error:text too long");
return i18n(txt,default_text);
}
| int rlIniFile::write | ( | const char * | filename | ) |
Definition at line 168 of file rlinifile.cpp.
{
FILE *fp;
rlSection *s;
rlSectionName *n;
fp = fopen(filename,"w");
if(fp == NULL) return -1;
s = _firstSection;
while(s != NULL)
{
if (s->name[0] == '#') fprintf(fp,"%s\n",s->name);
else if(s->name[0] == '\0') ;
else fprintf(fp,"[%s]\n",s->name);
n = s->firstName;
while(n != NULL)
{
if (n->name[0] == '#') fprintf(fp,"%s\n",n->name);
else if(n->name[0] == '\0') fprintf(fp,"\n");
else if(n->param[0] == '\0') fprintf(fp,"\n");
else fprintf(fp,"%s=%s\n",n->name,n->param);
n = n->nextName;
}
s = s->nextSection;
}
fclose(fp);
return 0;
}
rlSection* rlIniFile::_firstSection [private] |
Definition at line 108 of file rlinifile.h.
int rlIniFile::currentName [private] |
Definition at line 109 of file rlinifile.h.
int rlIniFile::currentSection [private] |
Definition at line 109 of file rlinifile.h.
rlString rlIniFile::default_section [private] |
Definition at line 111 of file rlinifile.h.
rlString rlIniFile::fname [private] |
Definition at line 110 of file rlinifile.h.
1.7.5.1