#include <rlinifile.h>

Public Member Functions | |
| rlIniFile () | |
| virtual | ~rlIniFile () |
| int | read (const char *filename) |
| int | write (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) |
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 |
Classes | |
| struct | _rlSection_ |
| struct | _rlSectionName_ |
class for INI files as known from Windows.
Definition at line 24 of file rlinifile.h.
typedef struct rlIniFile::_rlSectionName_ rlIniFile::rlSectionName [private] |
typedef struct rlIniFile::_rlSection_ rlIniFile::rlSection [private] |
| rlIniFile::rlIniFile | ( | ) |
Definition at line 29 of file rlinifile.cpp.
00030 { 00031 _firstSection = new rlSection; // first section holds names with section name = null_string 00032 _firstSection->nextSection = NULL; 00033 _firstSection->firstName = NULL; 00034 _firstSection->name = new char[1]; 00035 _firstSection->name[0] = '\0'; 00036 currentSection = currentName = -1; 00037 }
| rlIniFile::~rlIniFile | ( | ) | [virtual] |
Definition at line 39 of file rlinifile.cpp.
00040 { 00041 rlSection *section, *last_section; 00042 00043 section = _firstSection; 00044 while(section != NULL) 00045 { 00046 deleteSectionNames(section); 00047 last_section = section; 00048 section = section->nextSection; 00049 delete last_section; 00050 } 00051 }
| int rlIniFile::read | ( | const char * | filename | ) |
Definition at line 112 of file rlinifile.cpp.
00113 { 00114 FILE *fp; 00115 int len; 00116 char line[rl_PRINTF_LENGTH], 00117 name_section[rl_PRINTF_LENGTH], 00118 name_name[rl_PRINTF_LENGTH], 00119 name_param[rl_PRINTF_LENGTH]; 00120 rlSection *s, *s_old; 00121 00122 // delete old content 00123 s = _firstSection; 00124 while(s != NULL) 00125 { 00126 deleteSectionNames(s); 00127 s_old = s; 00128 s = s->nextSection; 00129 delete s_old; 00130 } 00131 00132 // read the file 00133 _firstSection = new rlSection; // first section holds names with section name = null_string 00134 _firstSection->nextSection = NULL; 00135 _firstSection->firstName = NULL; 00136 _firstSection->name = new char[1]; 00137 _firstSection->name[0] = '\0'; 00138 fp = fopen(filename,"r"); 00139 if(fp == NULL) return -1; 00140 name_section[0] = name_name[0] = name_param[0] = '\0'; 00141 while(fgets(line,sizeof(line)-1,fp) != NULL) 00142 { 00143 if(line[0] == '[') // section identifier 00144 { 00145 copyIdentifier(name_section,line); 00146 setText(name_section, NULL, NULL); 00147 } 00148 else if(line[0] > ' ' && line[0] != '\t' && line[0] != '#') // name identifier 00149 { 00150 copyName(name_name,line); 00151 copyParam(name_param,line); 00152 setText(name_section, name_name, name_param); 00153 } 00154 else // it must be a comment line 00155 { 00156 len = strlen(line); 00157 if(len>0) line[len-1] = '\0'; 00158 setText(name_section, line, NULL); 00159 } 00160 } 00161 fclose(fp); 00162 return 0; 00163 }
| int rlIniFile::write | ( | const char * | filename | ) |
Definition at line 165 of file rlinifile.cpp.
00166 { 00167 FILE *fp; 00168 rlSection *s; 00169 rlSectionName *n; 00170 00171 fp = fopen(filename,"w"); 00172 if(fp == NULL) return -1; 00173 00174 s = _firstSection; 00175 while(s != NULL) 00176 { 00177 if (s->name[0] == '#') fprintf(fp,"%s\n",s->name); 00178 else if(s->name[0] == '\0') ; 00179 else fprintf(fp,"[%s]\n",s->name); 00180 n = s->firstName; 00181 while(n != NULL) 00182 { 00183 if (n->name[0] == '#') fprintf(fp,"%s\n",n->name); 00184 else if(n->name[0] == '\0') fprintf(fp,"\n"); 00185 else if(n->param[0] == '\0') fprintf(fp,"\n"); 00186 else fprintf(fp,"%s=%s\n",n->name,n->param); 00187 n = n->nextName; 00188 } 00189 s = s->nextSection; 00190 } 00191 00192 fclose(fp); 00193 return 0; 00194 }
| const char * rlIniFile::text | ( | const char * | section, | |
| const char * | name | |||
| ) |
Definition at line 196 of file rlinifile.cpp.
00197 { 00198 rlSection *s; 00199 rlSectionName *n; 00200 00201 s = _firstSection; 00202 while(s != NULL) 00203 { 00204 if(strcmp(section,s->name) == 0) 00205 { 00206 n = s->firstName; 00207 while(n != NULL) 00208 { 00209 if(n->name != NULL && strcmp(name,n->name) == 0) 00210 { 00211 return n->param; 00212 } 00213 n = n->nextName; 00214 } 00215 return null_string; 00216 } 00217 s = s->nextSection; 00218 } 00219 return null_string; 00220 }
| void rlIniFile::setText | ( | const char * | section, | |
| const char * | name, | |||
| const char * | text | |||
| ) |
Definition at line 222 of file rlinifile.cpp.
00223 { 00224 rlSection *s, *last_section; 00225 rlSectionName *n, *last_name; 00226 00227 if(section == NULL) return; 00228 last_section = NULL; 00229 last_name = NULL; 00230 s = _firstSection; 00231 while(s != NULL) 00232 { 00233 if(strcmp(section,s->name) == 0) 00234 { 00235 last_name = NULL; 00236 n = s->firstName; 00237 while(n != NULL) 00238 { 00239 if(name != NULL && name[0] != '#' && name[0] != '\0' && strcmp(name,n->name) == 0) 00240 { 00241 if(n->param != NULL) delete [] n->param; 00242 if(text == NULL) 00243 { 00244 n->param = new char[1]; 00245 n->param[0] = '\0'; 00246 } 00247 else 00248 { 00249 n->param = new char[strlen(text)+1]; 00250 strcpy(n->param,text); 00251 } 00252 return; 00253 } 00254 last_name = n; 00255 n = n->nextName; 00256 } 00257 if(last_name == NULL) 00258 { 00259 s->firstName = new rlSectionName; 00260 n = s->firstName; 00261 } 00262 else 00263 { 00264 last_name->nextName = new rlSectionName; 00265 n = last_name->nextName; 00266 } 00267 if(name == NULL) 00268 { 00269 n->name = new char[1]; 00270 n->name[0] = '\0'; 00271 } 00272 else 00273 { 00274 n->name = new char[strlen(name)+1]; 00275 strcpy(n->name,name); 00276 } 00277 if(text == NULL) 00278 { 00279 n->param = new char[1]; 00280 n->param[0] = '\0'; 00281 } 00282 else 00283 { 00284 n->param = new char[strlen(text)+1]; 00285 strcpy(n->param,text); 00286 } 00287 n->nextName = NULL; 00288 return; 00289 } 00290 last_section = s; 00291 s = s->nextSection; 00292 } 00293 if(last_section == NULL) 00294 { 00295 _firstSection = new rlSection; 00296 last_section = _firstSection; 00297 } 00298 else 00299 { 00300 last_section->nextSection = new rlSection; 00301 last_section = last_section->nextSection; 00302 } 00303 last_section->name = new char[strlen(section)+1]; 00304 strcpy(last_section->name,section); 00305 last_section->nextSection = NULL; 00306 if(name == NULL) 00307 { 00308 last_section->firstName = NULL; 00309 } 00310 else 00311 { 00312 last_section->firstName = new rlSectionName; 00313 n = last_section->firstName; 00314 n->name = new char[strlen(name)+1]; 00315 strcpy(n->name,name); 00316 if(text == NULL) 00317 { 00318 n->param = new char[1]; 00319 n->param[0] = '\0'; 00320 } 00321 else 00322 { 00323 n->param = new char[strlen(text)+1]; 00324 strcpy(n->param,text); 00325 } 00326 n->nextName = NULL; 00327 } 00328 return; 00329 }
| int rlIniFile::printf | ( | const char * | section, | |
| const char * | name, | |||
| const char * | format, | |||
| ... | ||||
| ) |
Definition at line 331 of file rlinifile.cpp.
00332 { 00333 int ret; 00334 char buf[rl_PRINTF_LENGTH]; // should be big enough 00335 00336 va_list ap; 00337 va_start(ap,format); 00338 ret = rlvsnprintf(buf, rl_PRINTF_LENGTH - 1, format, ap); 00339 va_end(ap); 00340 if(ret > 0) setText(section, name, buf); 00341 return ret; 00342 }
| void rlIniFile::remove | ( | const char * | section | ) |
Definition at line 344 of file rlinifile.cpp.
00345 { 00346 rlSection *s, *last; 00347 00348 last = NULL; 00349 s = _firstSection; 00350 while(s != NULL) 00351 { 00352 if(strcmp(section,s->name) == 0) 00353 { 00354 deleteSectionNames(s); 00355 if(last != NULL) last->nextSection = s->nextSection; 00356 delete s; 00357 return; 00358 } 00359 last = s; 00360 s = s->nextSection; 00361 } 00362 }
| void rlIniFile::remove | ( | const char * | section, | |
| const char * | name | |||
| ) |
Definition at line 364 of file rlinifile.cpp.
00365 { 00366 rlSection *s; 00367 rlSectionName *n, *last; 00368 00369 s = _firstSection; 00370 while(s != NULL) 00371 { 00372 if(strcmp(section,s->name) == 0) 00373 { 00374 last = NULL; 00375 n = s->firstName; 00376 while(n != NULL) 00377 { 00378 if(strcmp(name,n->name) == 0) 00379 { 00380 if(n->name != NULL) delete [] n->name; 00381 if(n->param != NULL) delete [] n->param; 00382 if(last != NULL) last->nextName = n->nextName; 00383 delete n; 00384 return; 00385 } 00386 last = n; 00387 n = n->nextName; 00388 } 00389 return; 00390 } 00391 s = s->nextSection; 00392 } 00393 }
| const char * rlIniFile::firstSection | ( | ) |
Definition at line 395 of file rlinifile.cpp.
00396 { 00397 currentSection = 0; 00398 return _firstSection->name; 00399 }
| const char * rlIniFile::nextSection | ( | ) |
Definition at line 401 of file rlinifile.cpp.
00402 { 00403 rlSection *s; 00404 int i; 00405 00406 if(currentSection < 0) return NULL; 00407 currentSection++; 00408 i = 0; 00409 s = _firstSection; 00410 while(s != NULL) 00411 { 00412 if(i == currentSection) return s->name; 00413 s = s->nextSection; 00414 i++; 00415 } 00416 currentSection = -1; 00417 return NULL; 00418 }
| const char * rlIniFile::firstName | ( | const char * | section | ) |
Definition at line 420 of file rlinifile.cpp.
00421 { 00422 rlSection *s; 00423 rlSectionName *n; 00424 00425 s = _firstSection; 00426 while(s != NULL) 00427 { 00428 if(strcmp(section,s->name) == 0) 00429 { 00430 n = s->firstName; 00431 currentName = 0; 00432 return n->name; 00433 } 00434 s = s->nextSection; 00435 } 00436 return NULL; 00437 }
| const char * rlIniFile::nextName | ( | const char * | section | ) |
Definition at line 439 of file rlinifile.cpp.
00440 { 00441 rlSection *s; 00442 rlSectionName *n; 00443 int i; 00444 00445 if(currentName < 0) return NULL; 00446 currentName++; 00447 i = 0; 00448 s = _firstSection; 00449 while(s != NULL) 00450 { 00451 if(strcmp(section,s->name) == 0) 00452 { 00453 n = s->firstName; 00454 while(n != NULL) 00455 { 00456 if(i == currentName) return n->name; 00457 i++; 00458 n = n->nextName; 00459 } 00460 return NULL; 00461 } 00462 s = s->nextSection; 00463 } 00464 return NULL; 00465 }
| void rlIniFile::copyIdentifier | ( | char * | buf, | |
| const char * | line | |||
| ) | [private] |
Definition at line 70 of file rlinifile.cpp.
00071 { 00072 int i = 1; 00073 buf[0] = '\0'; 00074 if(line[0] != '[') return; 00075 while(line[i] != ']' && line[i] != '\0') 00076 { 00077 *buf++ = line[i++]; 00078 } 00079 *buf = '\0'; 00080 }
| void rlIniFile::copyName | ( | char * | buf, | |
| const char * | line | |||
| ) | [private] |
Definition at line 82 of file rlinifile.cpp.
00083 { 00084 int i = 0; 00085 buf[0] = '\0'; 00086 //while(line[i] > ' ' && line[i] != '=') 00087 while(line[i] != '\0' && line[i] != '=') 00088 { 00089 *buf++ = line[i++]; 00090 } 00091 *buf = '\0'; 00092 i--; // eventually delete spaces 00093 while(i>=0 && (buf[i] == ' ' || buf[i] == '\t')) 00094 { 00095 buf[i--] = '\0'; 00096 } 00097 }
| void rlIniFile::copyParam | ( | char * | buf, | |
| const char * | line | |||
| ) | [private] |
Definition at line 99 of file rlinifile.cpp.
00100 { 00101 char *cptr; 00102 buf[0] = '\0'; 00103 cptr = strchr(line,'='); 00104 if(cptr == NULL) return; 00105 cptr++; 00106 while((*cptr == ' ' || *cptr == '\t') && *cptr != '\0') cptr++; 00107 if(*cptr == '\0') return; 00108 while(*cptr != '\0' && *cptr != '\n') *buf++ = *cptr++; 00109 *buf = '\0'; 00110 }
| void rlIniFile::deleteSectionNames | ( | rlSection * | section | ) | [private] |
Definition at line 53 of file rlinifile.cpp.
00054 { 00055 rlSectionName *name, *last_name; 00056 00057 if(section == NULL) return; 00058 name = section->firstName; 00059 while(name != NULL) 00060 { 00061 if(name->name != NULL) delete [] name->name; 00062 if(name->param != NULL) delete [] name->param; 00063 last_name = name; 00064 name = name->nextName; 00065 delete [] last_name; 00066 } 00067 if(section->name != NULL) delete [] section->name; 00068 }
rlSection* rlIniFile::_firstSection [private] |
Definition at line 59 of file rlinifile.h.
int rlIniFile::currentSection [private] |
Definition at line 60 of file rlinifile.h.
int rlIniFile::currentName [private] |
Definition at line 60 of file rlinifile.h.
1.5.5