rllib  1
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros
Classes | Public Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
rlIniFile Class Reference

#include <rlinifile.h>

Collaboration diagram for rlIniFile:
Collaboration graph
[legend]

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
 

Detailed Description

class for INI files as known from Windows.

Definition at line 25 of file rlinifile.h.

Member Typedef Documentation

Constructor & Destructor Documentation

rlIniFile::rlIniFile ( )

Definition at line 29 of file rlinifile.cpp.

{
_firstSection = new rlSection; // first section holds names with section name = null_string
_firstSection->name = new char[1];
_firstSection->name[0] = '\0';
}
rlIniFile::~rlIniFile ( )
virtual

Definition at line 39 of file rlinifile.cpp.

{
rlSection *section, *last_section;
section = _firstSection;
while(section != NULL)
{
last_section = section;
section = section->nextSection;
delete last_section;
}
}

Member Function Documentation

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] != '=' || (line[i] == '=' && line[i-1] == '\\')))
{
*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 = line;
buf[0] = '\0';
while(1)
{
cptr = strchr(cptr,'=');
if(cptr == NULL) return;
cptr++;
if(cptr[-2] != '\\') break;
}
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 484 of file rlinifile.cpp.

{
}
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 203 of file rlinifile.cpp.

{
return fname.text();
}
const char * rlIniFile::firstName ( const char *  section)

Definition at line 432 of file rlinifile.cpp.

{
while(s != NULL)
{
if(strcmp(section,s->name) == 0)
{
n = s->firstName;
return n->name;
}
s = s->nextSection;
}
return NULL;
}
const char * rlIniFile::firstSection ( )

Definition at line 407 of file rlinifile.cpp.

{
}
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 489 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 451 of file rlinifile.cpp.

{
int i;
if(currentName < 0) return NULL;
i = 0;
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 413 of file rlinifile.cpp.

{
int i;
if(currentSection < 0) return NULL;
i = 0;
while(s != NULL)
{
if(i == currentSection) return s->name;
s = s->nextSection;
i++;
}
return NULL;
}
int rlIniFile::printf ( const char *  section,
const char *  name,
const char *  format,
  ... 
)

Definition at line 343 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 116 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
while(s != NULL)
{
s_old = s;
s = s->nextSection;
delete s_old;
}
// read the file
_firstSection = new rlSection; // first section holds names with section name = null_string
_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 356 of file rlinifile.cpp.

{
rlSection *s, *last;
last = NULL;
while(s != NULL)
{
if(strcmp(section,s->name) == 0)
{
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 376 of file rlinifile.cpp.

{
rlSectionName *n, *last;
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 479 of file rlinifile.cpp.

{
}
void rlIniFile::setText ( const char *  section,
const char *  name,
const char *  text 
)

Definition at line 234 of file rlinifile.cpp.

{
rlSection *s, *last_section;
rlSectionName *n, *last_name;
if(section == NULL) return;
last_section = NULL;
last_name = NULL;
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)
{
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 208 of file rlinifile.cpp.

{
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 496 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 172 of file rlinifile.cpp.

{
FILE *fp;
fp = fopen(filename,"w");
if(fp == NULL) return -1;
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;
}

Member Data Documentation

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.


The documentation for this class was generated from the following files: