1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: Benjamin Grauer |
---|
13 | co-programmer: Christian Meyer |
---|
14 | |
---|
15 | 2005-08-14: complete reimplementation: |
---|
16 | now the File is parsed at the initialisation, |
---|
17 | and informations is gathered there. |
---|
18 | */ |
---|
19 | |
---|
20 | |
---|
21 | #include "ini_parser.h" |
---|
22 | |
---|
23 | #include <stdlib.h> |
---|
24 | #include <string.h> |
---|
25 | |
---|
26 | #if HAVE_CONFIG_H |
---|
27 | #include <config.h> |
---|
28 | #endif |
---|
29 | |
---|
30 | #ifdef DEBUG |
---|
31 | #include "../../../defs/debug.h" |
---|
32 | #else |
---|
33 | #define PRINTF(x) printf |
---|
34 | #endif |
---|
35 | |
---|
36 | using namespace std; |
---|
37 | |
---|
38 | /** |
---|
39 | * @brief constructs an IniParser using a file |
---|
40 | * @param fileName: the path and name of the file to parse |
---|
41 | */ |
---|
42 | IniParser::IniParser (const char* fileName) |
---|
43 | { |
---|
44 | this->fileName = NULL; |
---|
45 | this->comment = NULL; |
---|
46 | |
---|
47 | if (fileName != NULL) |
---|
48 | this->readFile(fileName); |
---|
49 | } |
---|
50 | |
---|
51 | |
---|
52 | /** |
---|
53 | * @brief removes the IniParser from memory |
---|
54 | */ |
---|
55 | IniParser::~IniParser () |
---|
56 | { |
---|
57 | this->deleteSections(); |
---|
58 | this->setFileName(NULL); |
---|
59 | } |
---|
60 | |
---|
61 | |
---|
62 | /** |
---|
63 | * @brief removes all the sections. This is like delete, but even cooler :) |
---|
64 | */ |
---|
65 | void IniParser::deleteSections() |
---|
66 | { |
---|
67 | // in all sections |
---|
68 | while(!this->sections.empty()) |
---|
69 | { |
---|
70 | IniSection section = this->sections.front(); |
---|
71 | |
---|
72 | // in all entries of the sections |
---|
73 | while(!section.entries.empty()) |
---|
74 | { |
---|
75 | // delete all strings of entries. |
---|
76 | IniEntry entry = section.entries.front(); |
---|
77 | delete[] entry.name; |
---|
78 | delete[] entry.value; |
---|
79 | delete[] entry.comment; |
---|
80 | section.entries.pop_front(); |
---|
81 | } |
---|
82 | // delete all Sections |
---|
83 | delete[] section.name; |
---|
84 | delete[] section.comment; |
---|
85 | this->sections.pop_front(); |
---|
86 | } |
---|
87 | this->currentSection = this->sections.end(); |
---|
88 | this->setFileName(NULL); |
---|
89 | } |
---|
90 | |
---|
91 | |
---|
92 | /** |
---|
93 | * @brief sets the Name of the input-file |
---|
94 | * @param fileName The new FileName to set to the IniParser |
---|
95 | * If fileName is NULL the new Name will be set to NULL too. |
---|
96 | */ |
---|
97 | void IniParser::setFileName(const char* fileName) |
---|
98 | { |
---|
99 | if (this->fileName != NULL) |
---|
100 | delete[] this->fileName; |
---|
101 | if (this->comment != NULL) |
---|
102 | delete[] this->comment; |
---|
103 | this->comment = NULL; |
---|
104 | |
---|
105 | if (fileName != NULL) |
---|
106 | { |
---|
107 | this->fileName = new char[strlen(fileName)+1]; |
---|
108 | strcpy(this->fileName, fileName); |
---|
109 | } |
---|
110 | else |
---|
111 | this->fileName = NULL; |
---|
112 | } |
---|
113 | |
---|
114 | |
---|
115 | /** |
---|
116 | * @brief opens a file to parse |
---|
117 | * @param fileName: path and name of the new file to parse |
---|
118 | * @return true on success false otherwise; |
---|
119 | * |
---|
120 | * If there was already an opened file, the file will be closed, |
---|
121 | * and the new one will be opened. |
---|
122 | */ |
---|
123 | bool IniParser::readFile(const char* fileName) |
---|
124 | { |
---|
125 | FILE* stream; //< The stream we use to read the file. |
---|
126 | int lineCount = 0; //< The Count of lines. |
---|
127 | |
---|
128 | if (this->fileName != NULL) |
---|
129 | this->deleteSections(); |
---|
130 | if( fileName == NULL) |
---|
131 | return false; |
---|
132 | |
---|
133 | if( (stream = fopen (fileName, "r")) == NULL) |
---|
134 | { |
---|
135 | PRINTF(1)("IniParser could not open %s\n", fileName); |
---|
136 | return false; |
---|
137 | } |
---|
138 | else |
---|
139 | { |
---|
140 | this->setFileName(fileName); |
---|
141 | |
---|
142 | ///////////////////////////// |
---|
143 | // READING IN THE INI-FILE // |
---|
144 | ///////////////////////////// |
---|
145 | char lineBuffer[PARSELINELENGHT]; |
---|
146 | char buffer[PARSELINELENGHT]; |
---|
147 | const char* lineBegin; |
---|
148 | char* ptr; |
---|
149 | |
---|
150 | while( fgets (lineBuffer, PARSELINELENGHT, stream)) |
---|
151 | { |
---|
152 | lineBegin = lineBuffer; |
---|
153 | // remove newline char, and \0-terminate |
---|
154 | if( (ptr = strchr( lineBuffer, '\n')) != NULL) |
---|
155 | *ptr = 0; |
---|
156 | // cut up to the beginning of the line. |
---|
157 | while((*lineBegin == ' ' || *lineBegin == '\t') && lineBegin < lineBuffer + strlen(lineBuffer)) |
---|
158 | ++lineBegin; |
---|
159 | |
---|
160 | // check if we have a FileComment |
---|
161 | if ( (*lineBegin == '#' || *lineBegin == ';')) |
---|
162 | { |
---|
163 | char* newCommenLine = new char[strlen(lineBegin)+1]; |
---|
164 | strcpy(newCommenLine, lineBegin); |
---|
165 | this->commentList.push_back(newCommenLine); |
---|
166 | continue; |
---|
167 | } |
---|
168 | if (lineCount == 0 && !this->commentList.empty()) |
---|
169 | { |
---|
170 | this->setFileComment(); |
---|
171 | lineCount++; |
---|
172 | } |
---|
173 | |
---|
174 | // check for section identifyer |
---|
175 | else if( sscanf (lineBegin, "[%s", buffer) == 1) |
---|
176 | { |
---|
177 | if( (ptr = strchr( buffer, ']')) != NULL) |
---|
178 | { |
---|
179 | *ptr = 0; |
---|
180 | this->addSection(buffer); |
---|
181 | this->setSectionComment(); |
---|
182 | } |
---|
183 | } |
---|
184 | // check for Entry identifier (Entry = Value) |
---|
185 | else if( (ptr = strchr( lineBegin, '=')) != NULL) |
---|
186 | { |
---|
187 | if (currentSection == NULL) |
---|
188 | { |
---|
189 | PRINTF(2)("Not in a Section yet for %s\n", lineBegin); |
---|
190 | lineCount++; |
---|
191 | continue; |
---|
192 | } |
---|
193 | if( ptr == lineBegin) { |
---|
194 | lineCount++; |
---|
195 | continue; |
---|
196 | } |
---|
197 | char* valueBegin = ptr+1; |
---|
198 | while ((*valueBegin == ' ' || *valueBegin == '\t') && valueBegin <= lineBegin + strlen(lineBegin)) |
---|
199 | ++valueBegin; |
---|
200 | char* valueEnd = valueBegin + strlen(valueBegin)-1; |
---|
201 | while ((*valueEnd == ' ' || *valueEnd == '\t') && valueEnd >= valueBegin) |
---|
202 | --valueEnd; |
---|
203 | valueEnd[1] = '\0'; |
---|
204 | char* nameEnd = ptr-1; |
---|
205 | while ((*nameEnd == ' ' || *nameEnd == '\t' ) && nameEnd >= lineBegin) |
---|
206 | --nameEnd; |
---|
207 | nameEnd[1] = '\0'; |
---|
208 | |
---|
209 | this->addVar(lineBegin, valueBegin); |
---|
210 | this->setEntryComment(); |
---|
211 | |
---|
212 | lineCount++; |
---|
213 | } |
---|
214 | } |
---|
215 | } |
---|
216 | this->currentSection = this->sections.begin(); |
---|
217 | if (!this->sections.empty()) |
---|
218 | this->currentEntry = (*this->currentSection).entries.begin(); |
---|
219 | |
---|
220 | fclose(stream); |
---|
221 | return true; |
---|
222 | } |
---|
223 | |
---|
224 | |
---|
225 | /** |
---|
226 | * @brief opens a file and writes to it |
---|
227 | * @param fileName: path and name of the new file to write to |
---|
228 | * @return true on success false otherwise |
---|
229 | */ |
---|
230 | bool IniParser::writeFile(const char* fileName) const |
---|
231 | { |
---|
232 | FILE* stream; //!< The stream we use to read the file. |
---|
233 | if( fileName == NULL && (fileName = this->fileName) == NULL ) |
---|
234 | return false; |
---|
235 | |
---|
236 | if( (stream = fopen (fileName, "w")) == NULL) |
---|
237 | { |
---|
238 | PRINTF(1)("IniParser could not open %s\n", fileName); |
---|
239 | return false; |
---|
240 | } |
---|
241 | else |
---|
242 | { |
---|
243 | if (this->comment != NULL) |
---|
244 | fprintf(stream, "%s\n\n", this->comment); |
---|
245 | |
---|
246 | std::list<IniSection>::const_iterator section; |
---|
247 | for (section = this->sections.begin(); section != this->sections.end(); section++) |
---|
248 | { |
---|
249 | if ((*section).comment != NULL) |
---|
250 | fprintf(stream, "%s", (*section).comment); |
---|
251 | fprintf(stream, "\n [%s]\n", (*section).name); |
---|
252 | |
---|
253 | std::list<IniEntry>::const_iterator entry; |
---|
254 | for (entry = (*section).entries.begin(); entry != (*section).entries.end(); entry++) |
---|
255 | { |
---|
256 | if ((*entry).comment != NULL) |
---|
257 | fprintf(stream, "%s", (*entry).comment); |
---|
258 | fprintf(stream, " %s = %s\n", (*entry).name, (*entry).value); |
---|
259 | } |
---|
260 | } |
---|
261 | } |
---|
262 | fclose(stream); |
---|
263 | } |
---|
264 | |
---|
265 | void IniParser::setFileComment(const char* fileComment) |
---|
266 | { |
---|
267 | if (this->comment != NULL) |
---|
268 | delete this->comment; |
---|
269 | |
---|
270 | if (fileComment != NULL) |
---|
271 | { |
---|
272 | this->comment = new char[strlen(fileComment)+1]; |
---|
273 | strcpy(this->comment, fileComment); |
---|
274 | } |
---|
275 | else |
---|
276 | this->comment = NULL; |
---|
277 | } |
---|
278 | |
---|
279 | |
---|
280 | /** |
---|
281 | * @brief adds a section to the list of Sections, |
---|
282 | * if no Section list is availiable, it will create it |
---|
283 | * @param sectionName the Name of the section to add |
---|
284 | * @return true on success... there is only success or segfault :) |
---|
285 | */ |
---|
286 | bool IniParser::addSection(const char* sectionName) |
---|
287 | { |
---|
288 | if (sectionName == NULL) |
---|
289 | return false; |
---|
290 | this->sections.push_back(IniSection()); |
---|
291 | this->sections.back().comment = NULL; |
---|
292 | this->sections.back().name = new char[strlen(sectionName)+1]; |
---|
293 | strcpy(this->sections.back().name, sectionName); |
---|
294 | |
---|
295 | this->currentSection = --this->sections.end(); |
---|
296 | if (!this->sections.empty()) |
---|
297 | this->currentEntry = (*this->currentSection).entries.begin(); |
---|
298 | PRINTF(5)("Added Section %s\n", sectionName); |
---|
299 | return true; |
---|
300 | } |
---|
301 | |
---|
302 | |
---|
303 | /** |
---|
304 | * @brief Set the parsing cursor to the specified section |
---|
305 | * @param sectionName: the name of the section to set the cursor to |
---|
306 | * @return true on success or false if the section could not be found |
---|
307 | */ |
---|
308 | bool IniParser::getSection(const char* sectionName) |
---|
309 | { |
---|
310 | this->currentSection = this->getSectionIT(sectionName); |
---|
311 | if (this->currentSection != this->sections.end()) |
---|
312 | { |
---|
313 | this->currentEntry = (*this->currentSection).entries.begin(); |
---|
314 | return true; |
---|
315 | } |
---|
316 | else |
---|
317 | return false; |
---|
318 | } |
---|
319 | |
---|
320 | /** |
---|
321 | * |
---|
322 | */ |
---|
323 | void IniParser::setSectionComment(const char* comment, const char* sectionName) |
---|
324 | { |
---|
325 | std::list<IniSection>::iterator section = this->getSectionIT(sectionName); |
---|
326 | if (section == this->sections.end()) |
---|
327 | return; |
---|
328 | |
---|
329 | if ((*section).comment != NULL) |
---|
330 | delete[] (*section).comment; |
---|
331 | if (comment != NULL) |
---|
332 | { |
---|
333 | (*section).comment = new char[strlen (comment)+1]; |
---|
334 | strcpy((*section).comment, comment); |
---|
335 | } |
---|
336 | else |
---|
337 | (*section).comment = NULL; |
---|
338 | } |
---|
339 | |
---|
340 | /** |
---|
341 | * @param sectionName the Section to query for |
---|
342 | * @returns the Comment, or NULL on error. |
---|
343 | */ |
---|
344 | const char* IniParser::getSectionComment(const char* sectionName) const |
---|
345 | { |
---|
346 | std::list<IniSection>::const_iterator section = this->getSectionIT(sectionName); |
---|
347 | if (section != this->sections.end()) |
---|
348 | return (*section).comment; |
---|
349 | else |
---|
350 | return NULL; |
---|
351 | } |
---|
352 | |
---|
353 | |
---|
354 | /** |
---|
355 | * @brief moves to the first section |
---|
356 | */ |
---|
357 | void IniParser::firstSection() |
---|
358 | { |
---|
359 | this->currentSection = this->sections.begin(); |
---|
360 | if (!this->sections.empty()) |
---|
361 | this->currentEntry = (*this->currentSection).entries.begin(); |
---|
362 | } |
---|
363 | |
---|
364 | |
---|
365 | /** |
---|
366 | * @brief searches the next section |
---|
367 | * @returns the name of the section if found, NULL otherwise |
---|
368 | */ |
---|
369 | const char* IniParser::nextSection() |
---|
370 | { |
---|
371 | if (this->currentSection == this->sections.end()) |
---|
372 | return NULL; |
---|
373 | |
---|
374 | this->currentSection++; |
---|
375 | |
---|
376 | if (this->currentSection != this->sections.end()) |
---|
377 | { |
---|
378 | this->currentEntry = (*this->currentSection).entries.begin(); |
---|
379 | return this->currentSection->name; |
---|
380 | } |
---|
381 | else |
---|
382 | return NULL; |
---|
383 | } |
---|
384 | |
---|
385 | |
---|
386 | /** |
---|
387 | * @brief adds a new Entry to either the currentSection or the section called by sectionName |
---|
388 | * @param entryName the Name of the Entry to add |
---|
389 | * @param value the value to assign to this entry |
---|
390 | * @param sectionName if NULL then this entry will be set to the currentSection |
---|
391 | * otherwise to the section refered to by sectionName. |
---|
392 | * If both are NULL no entry will be added |
---|
393 | * @return true if everything is ok false on error |
---|
394 | */ |
---|
395 | bool IniParser::addVar(const char* entryName, const char* value, const char* sectionName) |
---|
396 | { |
---|
397 | std::list<IniSection>::iterator section; |
---|
398 | |
---|
399 | if (sectionName != NULL) |
---|
400 | { |
---|
401 | for (section = this->sections.begin(); section != this->sections.end(); section++) |
---|
402 | if (!strcmp((*section).name, sectionName)) |
---|
403 | break; |
---|
404 | } |
---|
405 | else |
---|
406 | section = this->currentSection; |
---|
407 | |
---|
408 | if (section == this->sections.end()) |
---|
409 | return false; |
---|
410 | |
---|
411 | if (section == this->sections.end()) |
---|
412 | { |
---|
413 | PRINTF(2)("section '%s' not found for value '%s'\n", sectionName, entryName); |
---|
414 | return false; |
---|
415 | } |
---|
416 | else |
---|
417 | { |
---|
418 | (*section).entries.push_back(IniEntry()); |
---|
419 | (*section).entries.back().comment = NULL; |
---|
420 | (*section).entries.back().name = new char[strlen(entryName)+1]; |
---|
421 | strcpy((*section).entries.back().name, entryName); |
---|
422 | (*section).entries.back().value = new char[strlen(value)+1]; |
---|
423 | strcpy((*section).entries.back().value, value); |
---|
424 | PRINTF(5)("Added Entry %s with Value '%s' to Section %s\n", |
---|
425 | (*section).entries.back().name, |
---|
426 | (*section).entries.back().value, |
---|
427 | (*section).name); |
---|
428 | this->currentEntry = --(*section).entries.end(); |
---|
429 | return true; |
---|
430 | } |
---|
431 | } |
---|
432 | |
---|
433 | /** |
---|
434 | * @brief edits the entry speciefied by entryName in sectionName/currentSection or creates it if it doesn't exist |
---|
435 | * @param entryName the Name of the Entry to add |
---|
436 | * @param value the value to assign to this entry |
---|
437 | * @param sectionName if NULL then this entry will be set to the currentSection |
---|
438 | * otherwise to the section refered to by sectionName. |
---|
439 | * If both are NULL no entry will be added |
---|
440 | * @return true if everything is ok false on error |
---|
441 | */ |
---|
442 | bool IniParser::editVar(const char* entryName, const char* value, const char* sectionName) |
---|
443 | { |
---|
444 | std::list<IniSection>::iterator section; |
---|
445 | |
---|
446 | if (sectionName != NULL) |
---|
447 | { |
---|
448 | for (section = this->sections.begin(); section != this->sections.end(); section++) |
---|
449 | if (!strcmp((*section).name, sectionName)) |
---|
450 | break; |
---|
451 | } |
---|
452 | else |
---|
453 | section = this->currentSection; |
---|
454 | |
---|
455 | if (section == this->sections.end()) |
---|
456 | { |
---|
457 | IniSection sec; |
---|
458 | sec.comment = NULL; |
---|
459 | sec.name = new char[strlen(sectionName)+1]; |
---|
460 | strcpy(sec.name, sectionName); |
---|
461 | section = this->sections.insert(this->sections.end(), sec); |
---|
462 | } |
---|
463 | |
---|
464 | if (section == this->sections.end()) |
---|
465 | { |
---|
466 | PRINTF(2)("section '%s' not found for value '%s'\n", sectionName, entryName); |
---|
467 | return false; |
---|
468 | } |
---|
469 | else |
---|
470 | { |
---|
471 | //try find item |
---|
472 | std::list<IniEntry>::iterator entry; |
---|
473 | for (entry = section->entries.begin(); entry!=section->entries.end(); entry++) |
---|
474 | if (!strcmp( entry->name, entryName )) |
---|
475 | break; |
---|
476 | |
---|
477 | //found it? |
---|
478 | if ( entry != section->entries.end() ) |
---|
479 | { |
---|
480 | if ( entry->value != NULL ) |
---|
481 | { |
---|
482 | delete[] entry->value; |
---|
483 | } |
---|
484 | entry->value = new char[strlen(value)+1]; |
---|
485 | strcpy(entry->value, value); |
---|
486 | |
---|
487 | return true; |
---|
488 | } |
---|
489 | |
---|
490 | //not found -> create it |
---|
491 | (*section).entries.push_back(IniEntry()); |
---|
492 | (*section).entries.back().comment = NULL; |
---|
493 | (*section).entries.back().name = new char[strlen(entryName)+1]; |
---|
494 | strcpy((*section).entries.back().name, entryName); |
---|
495 | (*section).entries.back().value = new char[strlen(value)+1]; |
---|
496 | strcpy((*section).entries.back().value, value); |
---|
497 | PRINTF(5)("Added Entry %s with Value '%s' to Section %s\n", |
---|
498 | (*section).entries.back().name, |
---|
499 | (*section).entries.back().value, |
---|
500 | (*section).name); |
---|
501 | this->currentEntry = --(*section).entries.end(); |
---|
502 | return true; |
---|
503 | } |
---|
504 | } |
---|
505 | |
---|
506 | |
---|
507 | /** |
---|
508 | * @brief directly acesses an entry in a section |
---|
509 | * @param entryName: the name of the entry to find |
---|
510 | * @param sectionName: the section where the entry is to be found |
---|
511 | * @param defaultValue: what should be returned in case the entry cannot be found |
---|
512 | * @return a pointer to a buffer conatining the value of the specified entry. This buffer will contain the data specified in defvalue in case the entry wasn't found |
---|
513 | * |
---|
514 | * The returned pointer points to an internal buffer, so do not free it on your own. Do not give a NULL pointer to defvalue, this will certainly |
---|
515 | * lead to unwanted behaviour. |
---|
516 | */ |
---|
517 | const char* IniParser::getVar(const char* entryName, const char* sectionName, const char* defaultValue) const |
---|
518 | { |
---|
519 | if (this->fileName != NULL) |
---|
520 | { |
---|
521 | std::list<IniEntry>::const_iterator entry = this->getEntryIT(entryName, sectionName); |
---|
522 | if (entry != NULL && !strcmp((*entry).name, entryName)) |
---|
523 | return (*entry).value; |
---|
524 | PRINTF(2)("Entry '%s' in section '%s' not found.\n", entryName, sectionName); |
---|
525 | |
---|
526 | } |
---|
527 | else |
---|
528 | PRINTF(2)("no File opened\n"); |
---|
529 | |
---|
530 | return defaultValue; |
---|
531 | |
---|
532 | } |
---|
533 | |
---|
534 | /** |
---|
535 | * Set the Comment of a specified Entry. |
---|
536 | * @param comment the Comment to set |
---|
537 | * @param entryName the Name of the Entry |
---|
538 | * @param sectionName the Name of the Section |
---|
539 | */ |
---|
540 | void IniParser::setEntryComment(const char* comment, const char* entryName, const char* sectionName) |
---|
541 | { |
---|
542 | std::list<IniEntry>::iterator entry = this->getEntryIT(entryName, sectionName); |
---|
543 | |
---|
544 | if ((*entry).comment != NULL) |
---|
545 | delete[] (*entry).comment; |
---|
546 | if (comment != NULL) |
---|
547 | { |
---|
548 | (*entry).comment = new char[strlen (comment)+1]; |
---|
549 | strcpy((*entry).comment, comment); |
---|
550 | } |
---|
551 | else |
---|
552 | (*entry).comment = NULL; |
---|
553 | |
---|
554 | |
---|
555 | } |
---|
556 | |
---|
557 | /** |
---|
558 | * @param entryName the Entry to query for |
---|
559 | * @param sectionName the Section to Query for |
---|
560 | * @returns the queried Comment. |
---|
561 | */ |
---|
562 | const char* IniParser::getEntryComment(const char* entryName, const char* sectionName) const |
---|
563 | { |
---|
564 | std::list<IniEntry>::const_iterator entry = this->getEntryIT(entryName, sectionName); |
---|
565 | |
---|
566 | return (*entry).comment; |
---|
567 | } |
---|
568 | |
---|
569 | |
---|
570 | /** |
---|
571 | * @brief moves to the first Variable of the current Section |
---|
572 | */ |
---|
573 | void IniParser::firstVar() |
---|
574 | { |
---|
575 | if (!this->sections.empty() && |
---|
576 | this->currentSection != this->sections.end()) |
---|
577 | this->currentEntry = (*this->currentSection).entries.begin(); |
---|
578 | } |
---|
579 | |
---|
580 | |
---|
581 | /** |
---|
582 | * @brief gets the next VarName = VarValue pair from the parsing stream |
---|
583 | * @return true on success, false otherwise (in the latter case name and value will be NULL) |
---|
584 | */ |
---|
585 | bool IniParser::nextVar() |
---|
586 | { |
---|
587 | if ( this->sections.empty() |
---|
588 | || this->currentSection == this->sections.end() |
---|
589 | || this->currentEntry == (*this->currentSection).entries.end()) |
---|
590 | return false; |
---|
591 | |
---|
592 | this->currentEntry++; |
---|
593 | |
---|
594 | if (this->currentEntry == (*this->currentSection).entries.end()) |
---|
595 | return false; |
---|
596 | else |
---|
597 | return true; |
---|
598 | } |
---|
599 | |
---|
600 | |
---|
601 | |
---|
602 | /** |
---|
603 | * @returns the name of the Current selected Section |
---|
604 | */ |
---|
605 | const char* IniParser::getCurrentSection() const |
---|
606 | { |
---|
607 | if (!this->sections.empty() && |
---|
608 | this->currentSection != this->sections.end()) |
---|
609 | return this->currentSection->name; |
---|
610 | else |
---|
611 | return NULL; |
---|
612 | } |
---|
613 | |
---|
614 | |
---|
615 | /** |
---|
616 | * @returns the current entries Name, or NULL if we havn't selected a Entry |
---|
617 | */ |
---|
618 | const char* IniParser::getCurrentName() const |
---|
619 | { |
---|
620 | if (!this->sections.empty() && |
---|
621 | this->currentSection != this->sections.end() && |
---|
622 | this->currentEntry != (*this->currentSection).entries.end()) |
---|
623 | return (*this->currentEntry).name; |
---|
624 | else |
---|
625 | return NULL; |
---|
626 | } |
---|
627 | |
---|
628 | /** |
---|
629 | * @returns the current entries Value, or NULL if we havn't selected a Entry |
---|
630 | */ |
---|
631 | const char* IniParser::getCurrentValue() const |
---|
632 | { |
---|
633 | if (!this->sections.empty() && |
---|
634 | this->currentSection != this->sections.end() && |
---|
635 | this->currentEntry != (*this->currentSection).entries.end()) |
---|
636 | return (*this->currentEntry).value; |
---|
637 | else |
---|
638 | return NULL; |
---|
639 | } |
---|
640 | |
---|
641 | |
---|
642 | /** |
---|
643 | * Finds the Section Iterator of the Section Called sectionName |
---|
644 | * @param sectionName the Name of the Section to get the Iterator from |
---|
645 | */ |
---|
646 | std::list<IniParser::IniSection>::const_iterator IniParser::getSectionIT(const char* sectionName) const |
---|
647 | { |
---|
648 | std::list<IniSection>::const_iterator section = this->currentSection; |
---|
649 | if (sectionName == NULL) |
---|
650 | return this->currentSection; |
---|
651 | else |
---|
652 | for (section = this->sections.begin(); section != this->sections.end(); section++) |
---|
653 | if (!strcmp((*section).name, sectionName)) |
---|
654 | break; |
---|
655 | return section; |
---|
656 | } |
---|
657 | |
---|
658 | |
---|
659 | /** |
---|
660 | * Finds the Section Iterator of the Section Called sectionName |
---|
661 | * @param sectionName the Name of the Section to get the Iterator from |
---|
662 | */ |
---|
663 | std::list<IniParser::IniSection>::iterator IniParser::getSectionIT(const char* sectionName) |
---|
664 | { |
---|
665 | std::list<IniSection>::iterator section = this->currentSection; |
---|
666 | if (sectionName == NULL) |
---|
667 | return this->currentSection; |
---|
668 | else |
---|
669 | for (section = this->sections.begin(); section != this->sections.end(); section++) |
---|
670 | if (!strcmp((*section).name, sectionName)) |
---|
671 | break; |
---|
672 | return section; |
---|
673 | } |
---|
674 | |
---|
675 | |
---|
676 | /** |
---|
677 | * Finds the Entry Iterator of the Section Called sectionName and entry called EntryName |
---|
678 | * @param entryName the Name of the Entry to get the Iterator from |
---|
679 | * @param sectionName the Name of the Section to get the Iterator from |
---|
680 | */ |
---|
681 | std::list<IniParser::IniEntry>::const_iterator IniParser::getEntryIT(const char* entryName, const char* sectionName) const |
---|
682 | { |
---|
683 | if (entryName == NULL) |
---|
684 | return this->currentEntry; |
---|
685 | std::list<IniSection>::const_iterator section = this->getSectionIT(sectionName); |
---|
686 | std::list<IniEntry>::const_iterator entry = this->currentEntry; |
---|
687 | |
---|
688 | if (section != this->sections.end()) |
---|
689 | for (entry = (*section).entries.begin(); entry != (*section).entries.end(); entry++) |
---|
690 | if (!strcmp((*entry).name, entryName)) |
---|
691 | break; |
---|
692 | if (entry == (*section).entries.end()) |
---|
693 | return NULL; |
---|
694 | else |
---|
695 | return entry; |
---|
696 | } |
---|
697 | |
---|
698 | |
---|
699 | /** |
---|
700 | * Finds the Entry Iterator of the Section Called sectionName and entry called EntryName |
---|
701 | * @param entryName the Name of the Entry to get the Iterator from |
---|
702 | * @param sectionName the Name of the Section to get the Iterator from |
---|
703 | */ |
---|
704 | std::list<IniParser::IniEntry>::iterator IniParser::getEntryIT(const char* entryName, const char* sectionName) |
---|
705 | { |
---|
706 | if (entryName == NULL) |
---|
707 | return this->currentEntry; |
---|
708 | std::list<IniSection>::iterator section = this->getSectionIT(sectionName); |
---|
709 | std::list<IniEntry>::iterator entry = this->currentEntry; |
---|
710 | |
---|
711 | if (section != this->sections.end()) |
---|
712 | for (entry = (*section).entries.begin(); entry != (*section).entries.end(); entry++) |
---|
713 | if (!strcmp((*entry).name, entryName)) |
---|
714 | break; |
---|
715 | if (entry == (*section).entries.end()) |
---|
716 | return NULL; |
---|
717 | else |
---|
718 | return entry; |
---|
719 | } |
---|
720 | |
---|
721 | |
---|
722 | /** |
---|
723 | * takes lines together to form one FileComment, ereasing the commentList |
---|
724 | */ |
---|
725 | void IniParser::setFileComment() |
---|
726 | { |
---|
727 | if (this->comment != NULL) |
---|
728 | delete[] this->comment; |
---|
729 | |
---|
730 | if (this->commentList.empty()) { |
---|
731 | this->comment = NULL; |
---|
732 | return; |
---|
733 | } |
---|
734 | |
---|
735 | unsigned int stringLength = 1; |
---|
736 | std::list<char*>::iterator comment; |
---|
737 | for (comment = this->commentList.begin(); comment != this->commentList.end(); comment++) |
---|
738 | stringLength += strlen((*comment)) +1; |
---|
739 | |
---|
740 | this->comment = new char[stringLength]; |
---|
741 | this->comment[0] = '\0'; |
---|
742 | while (!this->commentList.empty()) |
---|
743 | { |
---|
744 | if (*this->comment != '\0') |
---|
745 | strcat(this->comment, "\n"); |
---|
746 | strcat(this->comment, this->commentList.front()); |
---|
747 | delete[] this->commentList.front(); |
---|
748 | this->commentList.pop_front(); |
---|
749 | } |
---|
750 | } |
---|
751 | |
---|
752 | /** |
---|
753 | * takes lines together to form one SectionComment, ereasing the commentList |
---|
754 | */ |
---|
755 | void IniParser::setSectionComment() |
---|
756 | { |
---|
757 | if ((*this->currentSection).comment != NULL) |
---|
758 | delete[] (*this->currentSection).comment; |
---|
759 | |
---|
760 | if (this->commentList.empty()) { |
---|
761 | (*this->currentSection).comment = NULL; |
---|
762 | return; |
---|
763 | } |
---|
764 | |
---|
765 | unsigned int stringLength = 1; |
---|
766 | std::list<char*>::iterator comment; |
---|
767 | for (comment = this->commentList.begin(); comment != this->commentList.end(); comment++) |
---|
768 | stringLength += strlen((*comment)) +1; |
---|
769 | |
---|
770 | (*this->currentSection).comment = new char[stringLength]; |
---|
771 | (*this->currentSection).comment[0] = '\0'; |
---|
772 | while (!this->commentList.empty()) |
---|
773 | { |
---|
774 | if (*(*this->currentSection).comment != '\0') |
---|
775 | strcat((*this->currentSection).comment, "\n"); |
---|
776 | strcat((*this->currentSection).comment, this->commentList.front()); |
---|
777 | delete[] this->commentList.front(); |
---|
778 | this->commentList.pop_front(); |
---|
779 | } |
---|
780 | } |
---|
781 | |
---|
782 | /** |
---|
783 | * takes lines together to form one EntryComment, ereasing the commentList |
---|
784 | */ |
---|
785 | void IniParser::setEntryComment() |
---|
786 | { |
---|
787 | if ((*this->currentEntry).comment != NULL) |
---|
788 | delete[] (*this->currentEntry).comment; |
---|
789 | |
---|
790 | if (this->commentList.empty()) { |
---|
791 | (*this->currentEntry).comment = NULL; |
---|
792 | return; |
---|
793 | } |
---|
794 | |
---|
795 | unsigned int stringLength = 1; |
---|
796 | std::list<char*>::iterator comment; |
---|
797 | for (comment = this->commentList.begin(); comment != this->commentList.end(); comment++) |
---|
798 | stringLength += strlen((*comment)) +1; |
---|
799 | |
---|
800 | (*this->currentEntry).comment = new char[stringLength]; |
---|
801 | (*this->currentEntry).comment[0] = '\0'; |
---|
802 | while (!this->commentList.empty()) |
---|
803 | { |
---|
804 | if (*(*this->currentEntry).comment != '\0') |
---|
805 | strcat((*this->currentEntry).comment, "\n"); |
---|
806 | strcat((*this->currentEntry).comment, this->commentList.front()); |
---|
807 | delete[] this->commentList.front(); |
---|
808 | this->commentList.pop_front(); |
---|
809 | } |
---|
810 | |
---|
811 | } |
---|
812 | |
---|
813 | |
---|
814 | /** |
---|
815 | * @brief output the whole tree in a nice and easy way. |
---|
816 | */ |
---|
817 | void IniParser::debug() const |
---|
818 | { |
---|
819 | PRINTF(0)("Iniparser %s - debug\n", this->fileName); |
---|
820 | if (this->comment != NULL) |
---|
821 | PRINTF(0)("FileComment:\n %s\n\n", this->comment); |
---|
822 | |
---|
823 | if (this->fileName != NULL) |
---|
824 | { |
---|
825 | std::list<IniSection>::const_iterator section; |
---|
826 | for (section = this->sections.begin(); section != this->sections.end(); section++) |
---|
827 | { |
---|
828 | if ((*section).comment != NULL) |
---|
829 | PRINTF(0)(" %s\n", (*section).comment); |
---|
830 | PRINTF(0)(" [%s]\n", (*section).name); |
---|
831 | |
---|
832 | std::list<IniEntry>::const_iterator entry; |
---|
833 | for (entry = (*section).entries.begin(); entry != (*section).entries.end(); entry++) |
---|
834 | { |
---|
835 | if ((*entry).comment != NULL) |
---|
836 | PRINTF(0)(" %s\n", (*entry).comment); |
---|
837 | PRINTF(0)(" '%s' -> '%s'\n", (*entry).name, (*entry).value); |
---|
838 | } |
---|
839 | } |
---|
840 | } |
---|
841 | else |
---|
842 | PRINTF(1)("no opened ini-file.\n"); |
---|
843 | } |
---|
844 | |
---|