Changeset 5933 in orxonox.OLD for trunk/src/lib
- Timestamp:
- Dec 6, 2005, 12:33:18 AM (19 years ago)
- Location:
- trunk/src/lib/util
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lib/util/ini_parser.cc
r5319 r5933 21 21 #include "ini_parser.h" 22 22 23 #include "list.h"24 23 #include <stdlib.h> 25 24 #include <string.h> 26 #include "debug.h" 25 26 #ifdef DEBUG 27 #include "debug.h" 28 #else 29 #define PRINTF(x) printf 30 #endif 27 31 28 32 using namespace std; … … 31 35 * constructs an IniParser using a file 32 36 * @param fileName: the path and name of the file to parse 33 */37 */ 34 38 IniParser::IniParser (const char* fileName) 35 39 { 36 this->currentEntry = NULL;37 this->currentSection = NULL;38 this->sections = NULL;39 40 this->fileName = NULL; 41 40 42 if (fileName != NULL) 41 43 this->readFile(fileName); 42 44 } 43 45 46 44 47 /** 45 48 * removes the IniParser from memory 46 */49 */ 47 50 IniParser::~IniParser () 48 51 { 49 deleteSections(); 50 } 52 this->deleteSections(); 53 } 54 51 55 52 56 /** … … 55 59 void IniParser::deleteSections() 56 60 { 57 if (this->sections) 58 { 59 tIterator<IniSection>* sectionIt = this->sections->getIterator(); 60 IniSection* sectionEnum = sectionIt->firstElement(); 61 while (sectionEnum) 62 { 63 tIterator<IniEntry>* entryIt = sectionEnum->entries->getIterator(); 64 IniEntry* entryEnum = entryIt->firstElement(); 65 while (entryEnum) 66 { 67 delete []entryEnum->name; 68 delete []entryEnum->value; 69 delete entryEnum; 70 entryEnum = entryIt->nextElement(); 71 } 72 delete entryIt; 73 74 delete []sectionEnum->name; 75 delete sectionEnum->entries; 76 delete sectionEnum; 77 sectionEnum = sectionIt->nextElement(); 78 } 79 delete sectionIt; 80 } 81 delete this->sections; 82 this->currentEntry = NULL; 83 this->currentSection = NULL; 84 this->sections = NULL; 61 while(!this->sections.empty()) 62 { 63 IniSection section = this->sections.front(); 64 65 while(!section.entries.empty()) 66 { 67 IniEntry entry = section.entries.front(); 68 delete []entry.name; 69 delete []entry.value; 70 section.entries.pop_front(); 71 } 72 73 delete []section.name; 74 this->sections.pop_front(); 75 } 76 77 // this->currentEntry = NULL; 78 // this->currentSection = NULL; 85 79 this->setFileName(NULL); 86 80 } 81 87 82 88 83 /** … … 90 85 * @param fileName: path and name of the new file to parse 91 86 * @return true on success false otherwise; 92 */87 */ 93 88 bool IniParser::readFile(const char* fileName) 94 89 { 95 90 FILE* stream; //!< The stream we use to read the file. 96 if ( sections != NULL)91 if (!sections.empty()) 97 92 deleteSections(); 98 93 if( fileName == NULL) … … 107 102 else 108 103 { 109 this->currentEntry = NULL; 110 this->currentSection = NULL; 111 this->sections = new tList<IniSection>; 104 this->currentEntry = 0;//this->sections.begin(); 105 this->currentSection = this->sections.begin(); 112 106 113 107 ///////////////////////////// … … 169 163 } 170 164 165 171 166 /** 172 167 * opens a file and writes to it … … 187 182 else 188 183 { 189 if (this->sections) 190 { 191 tIterator<IniSection>* sectionIt = this->sections->getIterator(); 192 IniSection* sectionEnum = sectionIt->firstElement(); 193 while (sectionEnum) 184 if (!this->sections.empty()) 185 { 186 std::list<IniSection>::iterator section; 187 for (section = this->sections.begin(); section != this->sections.end(); section++) 194 188 { 195 fprintf(stream, "\n [%s]\n", sectionEnum->name); 196 197 tIterator<IniEntry>* entryIt = sectionEnum->entries->getIterator(); 198 IniEntry* entryEnum = entryIt->firstElement(); 199 while (entryEnum) 200 { 201 fprintf(stream, " %s = %s\n", entryEnum->name, entryEnum->value); 202 203 entryEnum = entryIt->nextElement(); 204 } 205 delete entryIt; 206 207 sectionEnum = sectionIt->nextElement(); 189 fprintf(stream, "\n [%s]\n", (*section).name); 190 191 std::list<IniEntry>::iterator entry; 192 for (entry = (*section).entries.begin(); entry != (*section).entries.end(); entry++) 193 fprintf(stream, " %s = %s\n", (*entry).name, (*entry).value); 208 194 } 209 delete sectionIt;210 195 } 211 196 else … … 214 199 fclose(stream); 215 200 } 201 216 202 217 203 /** … … 223 209 bool IniParser::addSection(const char* sectionName) 224 210 { 225 if (this->sections == NULL) 226 this->sections = new tList<IniSection>; 227 228 IniSection* newSection = new IniSection; 229 newSection->name = new char[strlen(sectionName)+1]; 230 strcpy(newSection->name, sectionName); 231 newSection->entries = new tList<IniEntry>; 232 this->currentSection = newSection; 233 this->sections->add(newSection); 211 this->sections.push_back(IniSection()); 212 213 this->sections.back().name = new char[strlen(sectionName)+1]; 214 strcpy(this->sections.back().name, sectionName); 215 216 this->currentSection = --this->sections.end(); 234 217 PRINTF(5)("Added Section %s\n", sectionName); 235 218 return true; 236 219 } 220 237 221 238 222 /** … … 243 227 bool IniParser::getSection( const char* sectionName) 244 228 { 245 tIterator<IniSection>* sectionIt = this->sections->getIterator(); 246 IniSection* sectionEnum = sectionIt->firstElement(); 247 while (sectionEnum) 248 { 249 if (!strcmp(sectionEnum->name, sectionName)) 250 { 251 this->currentSection = sectionEnum; 252 this->currentEntry = NULL; 253 delete sectionIt; 229 std::list<IniSection>::iterator section; 230 for (section = this->sections.begin(); section != this->sections.end(); section++) 231 { 232 if (!strcmp((*section).name, sectionName)) 233 { 234 this->currentSection = section; 235 this->currentEntry = (*section).entries.begin(); 254 236 return true; 255 237 } 256 257 sectionEnum = sectionIt->nextElement(); 258 } 259 delete sectionIt; 238 } 260 239 return false; 261 240 } 262 241 242 263 243 /** 264 244 * moves to the first section … … 266 246 void IniParser::getFirstSection() 267 247 { 268 if (this->sections) 269 this->currentSection = this->sections->firstElement(); 270 else 271 this->currentSection = NULL; 272 this->currentEntry = NULL; 273 } 248 this->currentSection = this->sections.begin(); 249 this->currentEntry = (*this->currentSection).entries.begin(); 250 } 251 274 252 275 253 /** … … 279 257 const char* IniParser::nextSection() 280 258 { 281 if (this->currentSection == NULL)259 if (this->currentSection == this->sections.end()) 282 260 return NULL; 283 261 else 284 262 { 285 if (this->sections) 286 { 287 if (this->currentSection == this->sections->lastElement()) 288 this->currentSection = NULL; 289 else 290 this->currentSection = this->sections->nextElement(this->currentSection); 291 } 263 if (this->currentSection == this->sections.end()) 264 return NULL; 265 else 266 this->currentSection++; 292 267 } 293 268 … … 298 273 } 299 274 275 300 276 /** 301 277 * moves to the first Variable of the current Section … … 303 279 void IniParser::getFirstVar() 304 280 { 305 if (this->currentSection) 306 this->currentEntry = this->currentSection->entries->firstElement(); 307 else 308 this->currentEntry = NULL; 309 } 281 if (this->currentSection != this->sections.end()) 282 this->currentEntry = (*this->currentSection).entries.begin(); 283 // else 284 // this->currentEntry = NULL; 285 } 286 310 287 311 288 /** … … 317 294 if (this->currentSection == NULL 318 295 || this->currentEntry == NULL 319 || this->currentEntry == this->currentSection->entries->lastElement())296 || this->currentEntry == (*this->currentSection).entries.end()) 320 297 { 321 298 this->currentEntry = NULL; 322 299 return false; 323 300 } 324 this->currentEntry = this->currentSection->entries->nextElement(this->currentEntry);301 this->currentEntry++; 325 302 326 303 if (this->currentEntry == NULL) … … 329 306 return true; 330 307 } 308 331 309 332 310 /** … … 341 319 bool IniParser::addVar(const char* entryName, const char* value, const char* sectionName) 342 320 { 343 IniSection* addSection = NULL; 321 std::list<IniSection>::iterator section = this->sections.end(); 322 344 323 if (sectionName != NULL) 345 324 { 346 tIterator<IniSection>* sectionIt = this->sections->getIterator(); 347 IniSection* sectionEnum = sectionIt->firstElement(); 348 while (sectionEnum) 349 { 350 if (!strcmp(sectionEnum->name, sectionName)) 351 { 352 addSection = sectionEnum; 325 for (section = this->sections.begin(); section != this->sections.end(); section++) 326 if (!strcmp((*section).name, sectionName)) 353 327 break; 354 } 355 sectionEnum = sectionIt->nextElement(); 356 } 357 delete sectionIt; 358 } 359 else 360 addSection = this->currentSection; 361 362 if (addSection == NULL) 328 } 329 else 330 section = this->currentSection; 331 332 if (section == this->sections.end()) 363 333 { 364 334 PRINTF(2)("section not found for value %s\n", entryName); … … 367 337 else 368 338 { 369 IniEntry* newEntry = new IniEntry; 370 newEntry->name = new char[strlen (entryName)+1]; 371 strcpy(newEntry->name, entryName); 372 newEntry->value = new char[strlen(value)+1]; 373 strcpy(newEntry->value, value); 374 this->currentSection->entries->add(newEntry); 375 PRINTF(5)("Added Entry %s with Value '%s' to Section %s\n", newEntry->name, newEntry->name, addSection->name); 339 (*section).entries.push_back(IniEntry()); 340 (*section).entries.back().name = new char[strlen (entryName)+1]; 341 strcpy((*section).entries.back().name, entryName); 342 (*section).entries.back().value = new char[strlen(value)+1]; 343 strcpy((*section).entries.back().value, value); 344 PRINTF(5)("Added Entry %s with Value '%s' to Section %s\n", entryName, value, (*section).name); 376 345 return true; 377 346 } 378 347 } 348 379 349 380 350 /** … … 390 360 const char* IniParser::getVar(const char* entryName, const char* sectionName, const char* defaultValue) const 391 361 { 392 if (this->sections) 393 { 394 tIterator<IniSection>* sectionIt = this->sections->getIterator(); 395 IniSection* sectionEnum = sectionIt->firstElement(); 396 while (sectionEnum) 397 { 398 if (!strcmp(sectionEnum->name, sectionName)) 362 if (fileName != NULL) 363 { 364 std::list<IniSection>::const_iterator section; 365 for (section = this->sections.begin(); section != this->sections.end(); section++) 366 { 367 if (!strcmp((*section).name, sectionName)) 399 368 { 400 tIterator<IniEntry>* entryIt = sectionEnum->entries->getIterator(); 401 IniEntry* entryEnum = entryIt->firstElement(); 402 while (entryEnum) 403 { 404 if (!strcmp(entryEnum->name, entryName)) 405 { 406 delete entryIt; 407 delete sectionIt; 408 return entryEnum->value; 409 } 410 entryEnum = entryIt->nextElement(); 411 } 412 delete entryIt; 413 PRINTF(2)("Entry %s in section %s not found.\n", entryName, sectionName); 414 break; 369 std::list<IniEntry>::const_iterator entry; 370 for (entry = (*section).entries.begin(); entry != (*section).entries.end(); entry++) 371 if (!strcmp((*entry).name, entryName)) 372 return (*entry).value; 373 PRINTF(2)("Entry %s in section %s not found.\n", entryName, sectionName); 374 break; 415 375 } 416 sectionEnum = sectionIt->nextElement(); 417 } 418 delete sectionIt; 376 } 419 377 PRINTF(2)("Section %s that should be containing %s not found.\n", sectionName, entryName); 420 378 } … … 447 405 { 448 406 PRINTF(0)("Iniparser %s - debug\n", this->fileName); 449 if (this->sections) 450 { 451 tIterator<IniSection>* sectionIt = this->sections->getIterator(); 452 IniSection* sectionEnum = sectionIt->firstElement(); 453 while (sectionEnum) 454 { 455 PRINTF(0)(" [%s]\n", sectionEnum->name); 456 457 tIterator<IniEntry>* entryIt = sectionEnum->entries->getIterator(); 458 IniEntry* entryEnum = entryIt->firstElement(); 459 while (entryEnum) 460 { 461 PRINTF(0)(" :%s: -> '%s'\n", entryEnum->name, entryEnum->value); 462 463 entryEnum = entryIt->nextElement(); 464 } 465 delete entryIt; 466 467 sectionEnum = sectionIt->nextElement(); 468 } 469 delete sectionIt; 470 } 471 else 472 PRINTF(1)("%s not opened\n", fileName); 473 } 407 if (this->fileName != NULL) 408 { 409 std::list<IniSection>::const_iterator section; 410 for (section = this->sections.begin(); section != this->sections.end(); section++) 411 { 412 PRINTF(0)(" [%s]\n", (*section).name); 413 414 std::list<IniEntry>::const_iterator entry; 415 for (entry = (*section).entries.begin(); entry != (*section).entries.end(); entry++) 416 PRINTF(0)(" '%s' -> '%s'\n", (*entry).name, (*entry).value); 417 } 418 } 419 else 420 PRINTF(1)("no opened ini-file.\n"); 421 } -
trunk/src/lib/util/ini_parser.h
r5405 r5933 11 11 #define PARSELINELENGHT 512 //!< how many chars to read at once 12 12 #ifndef NULL 13 #define NULL 0x0//!< NULL13 #define NULL 0x0 //!< NULL 14 14 #endif 15 15 16 // FORWARD DECLARATION // 17 template<class T> class tList; 16 #include <list> 18 17 19 18 //! ini-file parser … … 35 34 { 36 35 char* name; //!< name of a given section 37 tList<IniEntry>*entries; //!< a list of entries for this section36 std::list<IniEntry> entries; //!< a list of entries for this section 38 37 }; 39 38 //////////////////////////////////// … … 53 52 54 53 /** @returns true if the file is opened, false otherwise*/ 55 bool isOpen() const { return ( sections != NULL)?true:false; };54 bool isOpen() const { return (this->fileName != NULL)? true : false; }; 56 55 /** @returns the fileName we have opened. */ 57 56 const char* getFileName() const { return this->fileName; }; … … 77 76 78 77 private: 79 char* fileName; //!< The name of the File that was parsed.80 tList<IniSection>*sections; //!< a list of all stored Sections of the Parser81 IniSection*currentSection; //!< the current selected Section82 IniEntry*currentEntry; //!< the current selected entry (in currentSection)78 char* fileName; //!< The name of the File that was parsed. 79 std::list<IniSection> sections; //!< a list of all stored Sections of the Parser 80 std::list<IniSection>::iterator currentSection; //!< the current selected Section 81 std::list<IniEntry>::iterator currentEntry; //!< the current selected entry (in currentSection) 83 82 }; 84 83
Note: See TracChangeset
for help on using the changeset viewer.