rllib  1
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

◆ rlSection

◆ rlSectionName

Constructor & Destructor Documentation

◆ rlIniFile()

rlIniFile::rlIniFile ( )

Definition at line 29 of file rlinifile.cpp.

30 {
31  _firstSection = new rlSection; // first section holds names with section name = null_string
32  _firstSection->nextSection = NULL;
33  _firstSection->firstName = NULL;
34  _firstSection->name = new char[1];
35  _firstSection->name[0] = '\0';
37 }
int currentSection
Definition: rlinifile.h:109
int currentName
Definition: rlinifile.h:109
rlSectionName * firstName
Definition: rlinifile.h:100
_rlSection_ * nextSection
Definition: rlinifile.h:99
rlSection * _firstSection
Definition: rlinifile.h:108
struct rlIniFile::_rlSection_ rlSection

◆ ~rlIniFile()

rlIniFile::~rlIniFile ( )
virtual

Definition at line 39 of file rlinifile.cpp.

40 {
41  rlSection *section, *last_section;
42 
43  section = _firstSection;
44  while(section != NULL)
45  {
46  deleteSectionNames(section);
47  last_section = section;
48  section = section->nextSection;
49  delete last_section;
50  }
51 }
void deleteSectionNames(rlSection *section)
Definition: rlinifile.cpp:53
rlSection * _firstSection
Definition: rlinifile.h:108
struct rlIniFile::_rlSection_ rlSection

Member Function Documentation

◆ copyIdentifier()

void rlIniFile::copyIdentifier ( char *  buf,
const char *  line 
)
private

Definition at line 70 of file rlinifile.cpp.

71 {
72  int i = 1;
73  buf[0] = '\0';
74  if(line[0] != '[') return;
75  while(line[i] != ']' && line[i] != '\0')
76  {
77  *buf++ = line[i++];
78  }
79  *buf = '\0';
80 }

◆ copyName()

void rlIniFile::copyName ( char *  buf,
const char *  line 
)
private

Definition at line 82 of file rlinifile.cpp.

83 {
84  int i = 0;
85  buf[0] = '\0';
86  //while(line[i] > ' ' && line[i] != '=')
87  while(line[i] != '\0' && (line[i] != '=' || (line[i] == '=' && line[i-1] == '\\')))
88  {
89  *buf++ = line[i++];
90  }
91  *buf = '\0';
92  i--; // eventually delete spaces
93  while(i>=0 && (buf[i] == ' ' || buf[i] == '\t'))
94  {
95  buf[i--] = '\0';
96  }
97 }

◆ copyParam()

void rlIniFile::copyParam ( char *  buf,
const char *  line 
)
private

Definition at line 99 of file rlinifile.cpp.

100 {
101  const char *cptr = line;
102  buf[0] = '\0';
103  while(1)
104  {
105  cptr = strchr(cptr,'=');
106  if(cptr == NULL) return;
107  cptr++;
108  if(cptr[-2] != '\\') break;
109  }
110  while((*cptr == ' ' || *cptr == '\t') && *cptr != '\0') cptr++;
111  if(*cptr == '\0') return;
112  while(*cptr != '\0' && *cptr != '\n') *buf++ = *cptr++;
113  *buf = '\0';
114 }

◆ defaultSection()

const char * rlIniFile::defaultSection ( )

Definition at line 488 of file rlinifile.cpp.

489 {
490  return default_section.text();
491 }
char * text()
Definition: rlstring.cpp:126
rlString default_section
Definition: rlinifile.h:111

◆ deleteSectionNames()

void rlIniFile::deleteSectionNames ( rlSection section)
private

Definition at line 53 of file rlinifile.cpp.

54 {
55  rlSectionName *name, *last_name;
56 
57  if(section == NULL) return;
58  name = section->firstName;
59  while(name != NULL)
60  {
61  if(name->name != NULL) delete [] name->name;
62  if(name->param != NULL) delete [] name->param;
63  last_name = name;
64  name = name->nextName;
65  delete [] last_name;
66  }
67  if(section->name != NULL) delete [] section->name;
68 }
struct rlIniFile::_rlSectionName_ rlSectionName

◆ filename()

const char * rlIniFile::filename ( )

Definition at line 203 of file rlinifile.cpp.

204 {
205  return fname.text();
206 }
char * text()
Definition: rlstring.cpp:126
rlString fname
Definition: rlinifile.h:110

◆ firstName()

const char * rlIniFile::firstName ( const char *  section)

Definition at line 436 of file rlinifile.cpp.

437 {
438  rlSection *s;
439  rlSectionName *n;
440 
441  s = _firstSection;
442  while(s != NULL)
443  {
444  if(strcmp(section,s->name) == 0)
445  {
446  n = s->firstName;
447  currentName = 0;
448  return n->name;
449  }
450  s = s->nextSection;
451  }
452  return NULL;
453 }
struct rlIniFile::_rlSectionName_ rlSectionName
int currentName
Definition: rlinifile.h:109
rlSectionName * firstName
Definition: rlinifile.h:100
rlSection * _firstSection
Definition: rlinifile.h:108
struct rlIniFile::_rlSection_ rlSection

◆ firstSection()

const char * rlIniFile::firstSection ( )

Definition at line 411 of file rlinifile.cpp.

412 {
413  currentSection = 0;
414  return _firstSection->name;
415 }
int currentSection
Definition: rlinifile.h:109
rlSection * _firstSection
Definition: rlinifile.h:108

◆ i18n()

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 493 of file rlinifile.cpp.

494 {
495  const char *cptr = text(default_section.text(), tag);
496  if(strcmp(cptr,null_string) == 0) return default_text;
497  else return cptr;
498 }
const char * text(const char *section, const char *name)
Definition: rlinifile.cpp:208
char * text()
Definition: rlstring.cpp:126
static const char null_string[]
Definition: rlinifile.cpp:27
rlString default_section
Definition: rlinifile.h:111

◆ nextName()

const char * rlIniFile::nextName ( const char *  section)

Definition at line 455 of file rlinifile.cpp.

456 {
457  rlSection *s;
458  rlSectionName *n;
459  int i;
460 
461  if(currentName < 0) return NULL;
462  currentName++;
463  i = 0;
464  s = _firstSection;
465  while(s != NULL)
466  {
467  if(strcmp(section,s->name) == 0)
468  {
469  n = s->firstName;
470  while(n != NULL)
471  {
472  if(i == currentName) return n->name;
473  i++;
474  n = n->nextName;
475  }
476  return NULL;
477  }
478  s = s->nextSection;
479  }
480  return NULL;
481 }
struct rlIniFile::_rlSectionName_ rlSectionName
int currentName
Definition: rlinifile.h:109
rlSectionName * firstName
Definition: rlinifile.h:100
rlSection * _firstSection
Definition: rlinifile.h:108
struct rlIniFile::_rlSection_ rlSection

◆ nextSection()

const char * rlIniFile::nextSection ( )

Definition at line 417 of file rlinifile.cpp.

418 {
419  rlSection *s;
420  int i;
421 
422  if(currentSection < 0) return NULL;
423  currentSection++;
424  i = 0;
425  s = _firstSection;
426  while(s != NULL)
427  {
428  if(i == currentSection) return s->name;
429  s = s->nextSection;
430  i++;
431  }
432  currentSection = -1;
433  return NULL;
434 }
int currentSection
Definition: rlinifile.h:109
rlSection * _firstSection
Definition: rlinifile.h:108
struct rlIniFile::_rlSection_ rlSection

◆ printf()

int rlIniFile::printf ( const char *  section,
const char *  name,
const char *  format,
  ... 
)

Definition at line 343 of file rlinifile.cpp.

344 {
345  int ret;
346  char buf[rl_PRINTF_LENGTH]; // should be big enough
347 
348  va_list ap;
349  va_start(ap,format);
350  ret = rlvsnprintf(buf, rl_PRINTF_LENGTH - 1, format, ap);
351  va_end(ap);
352  if(ret > 0) setText(section, name, buf);
353  return ret;
354 }
#define rl_PRINTF_LENGTH
Definition: rldefine.h:71
void setText(const char *section, const char *name, const char *text)
Definition: rlinifile.cpp:234
int rlvsnprintf(char *text, int len, const char *format, va_list ap)
Definition: rlcutil.cpp:197

◆ read()

int rlIniFile::read ( const char *  filename)

Definition at line 116 of file rlinifile.cpp.

117 {
118  FILE *fp;
119  char line[rl_PRINTF_LENGTH],
120  name_section[rl_PRINTF_LENGTH],
121  name_name[rl_PRINTF_LENGTH],
122  name_param[rl_PRINTF_LENGTH],
123  *cptr;
124  rlSection *s, *s_old;
125 
126  // delete old content
127  s = _firstSection;
128  while(s != NULL)
129  {
131  s_old = s;
132  s = s->nextSection;
133  delete s_old;
134  }
135 
136  // read the file
138  _firstSection = new rlSection; // first section holds names with section name = null_string
139  _firstSection->nextSection = NULL;
140  _firstSection->firstName = NULL;
141  _firstSection->name = new char[1];
142  _firstSection->name[0] = '\0';
143  fp = fopen(filename,"r");
144  if(fp == NULL) return -1;
145  name_section[0] = name_name[0] = name_param[0] = '\0';
146  while(fgets(line,sizeof(line)-1,fp) != NULL)
147  {
148  cptr = strchr(line,0x0d);
149  if(cptr != NULL) *cptr = '\0';
150  cptr = strchr(line,0x0a);
151  if(cptr != NULL) *cptr = '\0';
152  if(line[0] == '[') // section identifier
153  {
154  copyIdentifier(name_section,line);
155  setText(name_section, NULL, NULL);
156  }
157  else if(line[0] > ' ' && line[0] != '\t' && line[0] != '#') // name identifier
158  {
159  copyName(name_name,line);
160  copyParam(name_param,line);
161  setText(name_section, name_name, name_param);
162  }
163  else // it must be a comment line
164  {
165  setText(name_section, line, NULL);
166  }
167  }
168  fclose(fp);
169  return 0;
170 }
void copyName(char *buf, const char *line)
Definition: rlinifile.cpp:82
void copyIdentifier(char *buf, const char *line)
Definition: rlinifile.cpp:70
#define rl_PRINTF_LENGTH
Definition: rldefine.h:71
void deleteSectionNames(rlSection *section)
Definition: rlinifile.cpp:53
void setText(const char *section, const char *name, const char *text)
Definition: rlinifile.cpp:234
void copyParam(char *buf, const char *line)
Definition: rlinifile.cpp:99
int setText(const char *text)
Definition: rlstring.cpp:136
rlString fname
Definition: rlinifile.h:110
rlSectionName * firstName
Definition: rlinifile.h:100
const char * filename()
Definition: rlinifile.cpp:203
_rlSection_ * nextSection
Definition: rlinifile.h:99
rlSection * _firstSection
Definition: rlinifile.h:108
struct rlIniFile::_rlSection_ rlSection

◆ remove() [1/2]

void rlIniFile::remove ( const char *  section)

Definition at line 356 of file rlinifile.cpp.

357 {
358  rlSection *s, *last;
359 
360  last = NULL;
361  s = _firstSection;
362  while(s != NULL)
363  {
364  if(strcmp(section,s->name) == 0)
365  {
367  if(last != NULL) last->nextSection = s->nextSection;
368  if (s == _firstSection)
369  _firstSection = NULL;
370  delete s;
371  return;
372  }
373  last = s;
374  s = s->nextSection;
375  }
376 }
void deleteSectionNames(rlSection *section)
Definition: rlinifile.cpp:53
_rlSection_ * nextSection
Definition: rlinifile.h:99
rlSection * _firstSection
Definition: rlinifile.h:108
struct rlIniFile::_rlSection_ rlSection

◆ remove() [2/2]

void rlIniFile::remove ( const char *  section,
const char *  name 
)

Definition at line 378 of file rlinifile.cpp.

379 {
380  rlSection *s;
381  rlSectionName *n, *last;
382 
383  s = _firstSection;
384  while(s != NULL)
385  {
386  if(strcmp(section,s->name) == 0)
387  {
388  last = NULL;
389  n = s->firstName;
390  while(n != NULL)
391  {
392  if(strcmp(name,n->name) == 0)
393  {
394  if(n->name != NULL) delete [] n->name;
395  if(n->param != NULL) delete [] n->param;
396  if(last != NULL) last->nextName = n->nextName;
397  if (n == s->firstName)
398  s->firstName = NULL;
399  delete n;
400  return;
401  }
402  last = n;
403  n = n->nextName;
404  }
405  return;
406  }
407  s = s->nextSection;
408  }
409 }
struct rlIniFile::_rlSectionName_ rlSectionName
rlSectionName * firstName
Definition: rlinifile.h:100
rlSection * _firstSection
Definition: rlinifile.h:108
struct rlIniFile::_rlSection_ rlSection

◆ setDefaultSection()

void rlIniFile::setDefaultSection ( const char *  section)

Definition at line 483 of file rlinifile.cpp.

484 {
485  default_section.setText(section);
486 }
int setText(const char *text)
Definition: rlstring.cpp:136
rlString default_section
Definition: rlinifile.h:111

◆ setText()

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

Definition at line 234 of file rlinifile.cpp.

235 {
236  rlSection *s, *last_section;
237  rlSectionName *n, *last_name;
238 
239  if(section == NULL) return;
240  last_section = NULL;
241  last_name = NULL;
242  s = _firstSection;
243  while(s != NULL)
244  {
245  if(strcmp(section,s->name) == 0)
246  {
247  last_name = NULL;
248  n = s->firstName;
249  while(n != NULL)
250  {
251  if(name != NULL && name[0] != '#' && name[0] != '\0' && strcmp(name,n->name) == 0)
252  {
253  if(n->param != NULL) delete [] n->param;
254  if(text == NULL)
255  {
256  n->param = new char[1];
257  n->param[0] = '\0';
258  }
259  else
260  {
261  n->param = new char[strlen(text)+1];
262  strcpy(n->param,text);
263  }
264  return;
265  }
266  last_name = n;
267  n = n->nextName;
268  }
269  if(last_name == NULL)
270  {
271  s->firstName = new rlSectionName;
272  n = s->firstName;
273  }
274  else
275  {
276  last_name->nextName = new rlSectionName;
277  n = last_name->nextName;
278  }
279  if(name == NULL)
280  {
281  n->name = new char[1];
282  n->name[0] = '\0';
283  }
284  else
285  {
286  n->name = new char[strlen(name)+1];
287  strcpy(n->name,name);
288  }
289  if(text == NULL)
290  {
291  n->param = new char[1];
292  n->param[0] = '\0';
293  }
294  else
295  {
296  n->param = new char[strlen(text)+1];
297  strcpy(n->param,text);
298  }
299  n->nextName = NULL;
300  return;
301  }
302  last_section = s;
303  s = s->nextSection;
304  }
305  if(last_section == NULL)
306  {
307  _firstSection = new rlSection;
308  last_section = _firstSection;
309  }
310  else
311  {
312  last_section->nextSection = new rlSection;
313  last_section = last_section->nextSection;
314  }
315  last_section->name = new char[strlen(section)+1];
316  strcpy(last_section->name,section);
317  last_section->nextSection = NULL;
318  if(name == NULL)
319  {
320  last_section->firstName = NULL;
321  }
322  else
323  {
324  last_section->firstName = new rlSectionName;
325  n = last_section->firstName;
326  n->name = new char[strlen(name)+1];
327  strcpy(n->name,name);
328  if(text == NULL)
329  {
330  n->param = new char[1];
331  n->param[0] = '\0';
332  }
333  else
334  {
335  n->param = new char[strlen(text)+1];
336  strcpy(n->param,text);
337  }
338  n->nextName = NULL;
339  }
340  return;
341 }
const char * text(const char *section, const char *name)
Definition: rlinifile.cpp:208
struct rlIniFile::_rlSectionName_ rlSectionName
rlSectionName * firstName
Definition: rlinifile.h:100
_rlSection_ * nextSection
Definition: rlinifile.h:99
_rlSectionName_ * nextName
Definition: rlinifile.h:92
rlSection * _firstSection
Definition: rlinifile.h:108
struct rlIniFile::_rlSection_ rlSection

◆ text()

const char * rlIniFile::text ( const char *  section,
const char *  name 
)

Definition at line 208 of file rlinifile.cpp.

209 {
210  rlSection *s;
211  rlSectionName *n;
212 
213  s = _firstSection;
214  while(s != NULL)
215  {
216  if(strcmp(section,s->name) == 0)
217  {
218  n = s->firstName;
219  while(n != NULL)
220  {
221  if(n->name != NULL && strcmp(name,n->name) == 0)
222  {
223  return n->param;
224  }
225  n = n->nextName;
226  }
227  return null_string;
228  }
229  s = s->nextSection;
230  }
231  return null_string;
232 }
struct rlIniFile::_rlSectionName_ rlSectionName
rlSectionName * firstName
Definition: rlinifile.h:100
static const char null_string[]
Definition: rlinifile.cpp:27
rlSection * _firstSection
Definition: rlinifile.h:108
struct rlIniFile::_rlSection_ rlSection

◆ tr()

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 500 of file rlinifile.cpp.

501 {
502  if(txt == NULL) return "ERROR:txt=NULL";
503  char default_text[1024];
504  if(strlen(txt) < (sizeof(default_text) - 40)) sprintf(default_text,"tr_error:%s", txt);
505  else strcpy(default_text,"tr_error:text too long");
506  return i18n(txt,default_text);
507 }
const char * i18n(const char *tag, const char *default_text="")
Definition: rlinifile.cpp:493

◆ write()

int rlIniFile::write ( const char *  filename)

Definition at line 172 of file rlinifile.cpp.

173 {
174  FILE *fp;
175  rlSection *s;
176  rlSectionName *n;
177 
178  fp = fopen(filename,"w");
179  if(fp == NULL) return -1;
180 
181  s = _firstSection;
182  while(s != NULL)
183  {
184  if (s->name[0] == '#') fprintf(fp,"%s\n",s->name);
185  else if(s->name[0] == '\0') ;
186  else fprintf(fp,"[%s]\n",s->name);
187  n = s->firstName;
188  while(n != NULL)
189  {
190  if (n->name[0] == '#') fprintf(fp,"%s\n",n->name);
191  else if(n->name[0] == '\0') fprintf(fp,"\n");
192  else if(n->param[0] == '\0') fprintf(fp,"\n");
193  else fprintf(fp,"%s=%s\n",n->name,n->param);
194  n = n->nextName;
195  }
196  s = s->nextSection;
197  }
198 
199  fclose(fp);
200  return 0;
201 }
struct rlIniFile::_rlSectionName_ rlSectionName
const char * filename()
Definition: rlinifile.cpp:203
rlSection * _firstSection
Definition: rlinifile.h:108
struct rlIniFile::_rlSection_ rlSection

Member Data Documentation

◆ _firstSection

rlSection* rlIniFile::_firstSection
private

Definition at line 108 of file rlinifile.h.

◆ currentName

int rlIniFile::currentName
private

Definition at line 109 of file rlinifile.h.

◆ currentSection

int rlIniFile::currentSection
private

Definition at line 109 of file rlinifile.h.

◆ default_section

rlString rlIniFile::default_section
private

Definition at line 111 of file rlinifile.h.

◆ fname

rlString rlIniFile::fname
private

Definition at line 110 of file rlinifile.h.


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