Changeset 7221 in orxonox.OLD for trunk/src/lib/parser/ini_parser
- Timestamp:
- Mar 15, 2006, 3:10:45 PM (19 years ago)
- Location:
- trunk/src/lib/parser/ini_parser
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lib/parser/ini_parser/ini_parser.cc
r5953 r7221 40 40 * @param fileName: the path and name of the file to parse 41 41 */ 42 IniParser::IniParser (const char*fileName)43 { 44 this->fileName = NULL;45 this->comment = NULL;46 47 if ( fileName != NULL)42 IniParser::IniParser (const std::string& fileName) 43 { 44 this->fileName = ""; 45 this->comment = ""; 46 47 if (!fileName.empty()) 48 48 this->readFile(fileName); 49 49 } … … 56 56 { 57 57 this->deleteSections(); 58 this->setFileName(NULL); 59 } 58 this->setFileName(""); 59 } 60 61 const std::string IniParser::emptyString = ""; 60 62 61 63 … … 66 68 { 67 69 // 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 } 70 this->sections.clear(); 71 87 72 this->currentSection = this->sections.end(); 88 this->setFileName( NULL);73 this->setFileName(""); 89 74 } 90 75 … … 95 80 * If fileName is NULL the new Name will be set to NULL too. 96 81 */ 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; 82 void IniParser::setFileName(const std::string& fileName) 83 { 84 this->comment = ""; 85 this->fileName = fileName; 112 86 } 113 87 … … 121 95 * and the new one will be opened. 122 96 */ 123 bool IniParser::readFile(const char*fileName)97 bool IniParser::readFile(const std::string& fileName) 124 98 { 125 99 FILE* stream; //< The stream we use to read the file. 126 100 int lineCount = 0; //< The Count of lines. 127 101 128 if ( this->fileName != NULL)102 if (!this->fileName.empty()) 129 103 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); 104 105 if( fileName.empty()) 106 return false; 107 108 if( (stream = fopen (fileName.c_str(), "r")) == NULL) 109 { 110 PRINTF(1)("IniParser could not open %s\n", fileName.c_str()); 136 111 return false; 137 112 } … … 143 118 // READING IN THE INI-FILE // 144 119 ///////////////////////////// 145 char lineBuffer[PARSELINELENGHT ];146 char buffer[PARSELINELENGHT ];120 char lineBuffer[PARSELINELENGHT+1]; 121 char buffer[PARSELINELENGHT+1]; 147 122 const char* lineBegin; 148 123 char* ptr; … … 154 129 if( (ptr = strchr( lineBuffer, '\n')) != NULL) 155 130 *ptr = 0; 131 else 132 lineBuffer[PARSELINELENGHT] = 0; 156 133 // cut up to the beginning of the line. 157 134 while((*lineBegin == ' ' || *lineBegin == '\t') && lineBegin < lineBuffer + strlen(lineBuffer)) 158 135 ++lineBegin; 159 136 137 if ( !strcmp( lineBegin, "" ) ) 138 continue; 139 160 140 // check if we have a FileComment 161 141 if ( (*lineBegin == '#' || *lineBegin == ';')) 162 142 { 163 char* newCommenLine = new char[strlen(lineBegin)+1]; 164 strcpy(newCommenLine, lineBegin); 143 string newCommenLine = lineBegin; 165 144 this->commentList.push_back(newCommenLine); 166 145 continue; … … 191 170 continue; 192 171 } 193 if( ptr == lineBegin) { 172 if( ptr == lineBegin) 173 { 194 174 lineCount++; 195 175 continue; … … 228 208 * @return true on success false otherwise 229 209 */ 230 bool IniParser::writeFile(const char*fileName) const210 bool IniParser::writeFile(const std::string& fileName) const 231 211 { 232 212 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 );213 if( fileName.empty()) 214 return false; 215 216 if( (stream = fopen (fileName.c_str(), "w")) == NULL) 217 { 218 PRINTF(1)("IniParser could not open %s\n", fileName.c_str()); 219 return false; 220 } 221 else 222 { 223 if (!this->comment.empty()) 224 fprintf(stream, "%s\n\n", this->comment.c_str()); 245 225 246 226 std::list<IniSection>::const_iterator section; 247 227 for (section = this->sections.begin(); section != this->sections.end(); section++) 228 { 229 if (!(*section).comment.empty()) 230 fprintf(stream, "%s", (*section).comment.c_str()); 231 fprintf(stream, "\n [%s]\n", (*section).name.c_str()); 232 233 std::list<IniEntry>::const_iterator entry; 234 for (entry = (*section).entries.begin(); entry != (*section).entries.end(); entry++) 248 235 { 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 } 236 if (!(*entry).comment.empty()) 237 fprintf(stream, "%s", (*entry).comment.c_str()); 238 fprintf(stream, " %s = %s\n", (*entry).name.c_str(), (*entry).value.c_str()); 260 239 } 240 } 261 241 } 262 242 fclose(stream); 263 243 } 264 244 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 245 void IniParser::setFileComment(const std::string& fileComment) 246 { 247 this->comment = fileComment; 248 } 279 249 280 250 /** … … 284 254 * @return true on success... there is only success or segfault :) 285 255 */ 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); 256 bool IniParser::addSection(const std::string& sectionName) 257 { 258 if (sectionName.empty()) 259 return false; 260 IniSection newSection; 261 newSection.name = sectionName; 262 newSection.comment = ""; 263 264 this->sections.push_back(newSection); 294 265 295 266 this->currentSection = --this->sections.end(); 296 267 if (!this->sections.empty()) 297 298 PRINTF(5)("Added Section %s\n", sectionName );268 this->currentEntry = (*this->currentSection).entries.begin(); 269 PRINTF(5)("Added Section %s\n", sectionName.c_str()); 299 270 return true; 300 271 } … … 306 277 * @return true on success or false if the section could not be found 307 278 */ 308 bool IniParser::getSection(const char*sectionName)279 bool IniParser::getSection(const std::string& sectionName) 309 280 { 310 281 this->currentSection = this->getSectionIT(sectionName); … … 321 292 * 322 293 */ 323 void IniParser::setSectionComment(const char* comment, const char*sectionName)294 void IniParser::setSectionComment(const std::string& comment, const std::string& sectionName) 324 295 { 325 296 std::list<IniSection>::iterator section = this->getSectionIT(sectionName); … … 327 298 return; 328 299 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; 300 (*section).comment = comment; 338 301 } 339 302 … … 342 305 * @returns the Comment, or NULL on error. 343 306 */ 344 const char* IniParser::getSectionComment(const char*sectionName) const307 const std::string& IniParser::getSectionComment(const std::string& sectionName) const 345 308 { 346 309 std::list<IniSection>::const_iterator section = this->getSectionIT(sectionName); … … 348 311 return (*section).comment; 349 312 else 350 return NULL;313 return IniParser::emptyString; 351 314 } 352 315 … … 367 330 * @returns the name of the section if found, NULL otherwise 368 331 */ 369 const char*IniParser::nextSection()332 const std::string& IniParser::nextSection() 370 333 { 371 334 if (this->currentSection == this->sections.end()) 372 return NULL;335 return IniParser::emptyString; 373 336 374 337 this->currentSection++; 375 338 376 339 if (this->currentSection != this->sections.end()) 377 378 379 380 381 else 382 return NULL;340 { 341 this->currentEntry = (*this->currentSection).entries.begin(); 342 return this->currentSection->name; 343 } 344 else 345 return IniParser::emptyString; 383 346 } 384 347 … … 393 356 * @return true if everything is ok false on error 394 357 */ 395 bool IniParser::addVar(const char* entryName, const char* value, const char*sectionName)358 bool IniParser::addVar(const std::string& entryName, const std::string& value, const std::string& sectionName) 396 359 { 397 360 std::list<IniSection>::iterator section; 398 361 399 if ( sectionName != NULL)362 if (!sectionName.empty()) 400 363 { 401 364 for (section = this->sections.begin(); section != this->sections.end(); section++) 402 if ( !strcmp((*section).name, sectionName))365 if ((*section).name == sectionName) 403 366 break; 404 367 } … … 411 374 if (section == this->sections.end()) 412 375 { 413 PRINTF(2)("section '%s' not found for value '%s'\n", sectionName , entryName);376 PRINTF(2)("section '%s' not found for value '%s'\n", sectionName.c_str(), entryName.c_str()); 414 377 return false; 415 378 } … … 417 380 { 418 381 (*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); 382 (*section).entries.back().comment = ""; 383 (*section).entries.back().name = entryName; 384 (*section).entries.back().value = value; 424 385 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 );386 (*section).entries.back().name.c_str(), 387 (*section).entries.back().value.c_str(), 388 (*section).name.c_str()); 428 389 this->currentEntry = --(*section).entries.end(); 429 390 return true; … … 442 403 * lead to unwanted behaviour. 443 404 */ 444 const char* IniParser::getVar(const char* entryName, const char* sectionName, const char*defaultValue) const445 { 446 if ( this->fileName != NULL)405 const std::string& IniParser::getVar(const std::string& entryName, const std::string& sectionName, const std::string& defaultValue) const 406 { 407 if (!this->fileName.empty()) 447 408 { 448 409 std::list<IniEntry>::const_iterator entry = this->getEntryIT(entryName, sectionName); 449 if (entry != NULL && !strcmp((*entry).name, entryName))410 if (entry != NULL && (*entry).name == entryName) 450 411 return (*entry).value; 451 PRINTF(2)("Entry '%s' in section '%s' not found.\n", entryName, sectionName); 452 412 PRINTF(2)("Entry '%s' in section '%s' not found.\n", entryName.c_str(), sectionName.c_str()); 453 413 } 454 414 else … … 456 416 457 417 return defaultValue; 458 459 418 } 460 419 … … 465 424 * @param sectionName the Name of the Section 466 425 */ 467 void IniParser::setEntryComment(const char* comment, const char* entryName, const char*sectionName)426 void IniParser::setEntryComment(const std::string& comment, const std::string& entryName, const std::string& sectionName) 468 427 { 469 428 std::list<IniEntry>::iterator entry = this->getEntryIT(entryName, sectionName); 470 471 if ((*entry).comment != NULL) 472 delete[] (*entry).comment; 473 if (comment != NULL) 474 { 475 (*entry).comment = new char[strlen (comment)+1]; 476 strcpy((*entry).comment, comment); 477 } 478 else 479 (*entry).comment = NULL; 480 481 429 (*entry).comment = comment; 482 430 } 483 431 … … 487 435 * @returns the queried Comment. 488 436 */ 489 const char* IniParser::getEntryComment(const char* entryName, const char*sectionName) const437 const std::string& IniParser::getEntryComment(const std::string& entryName, const std::string& sectionName) const 490 438 { 491 439 std::list<IniEntry>::const_iterator entry = this->getEntryIT(entryName, sectionName); … … 501 449 { 502 450 if (!this->sections.empty() && 503 451 this->currentSection != this->sections.end()) 504 452 this->currentEntry = (*this->currentSection).entries.begin(); 505 453 } … … 530 478 * @returns the name of the Current selected Section 531 479 */ 532 const char*IniParser::getCurrentSection() const480 const std::string& IniParser::getCurrentSection() const 533 481 { 534 482 if (!this->sections.empty() && … … 536 484 return this->currentSection->name; 537 485 else 538 return NULL;539 486 return IniParser::emptyString ; 487 } 540 488 541 489 … … 543 491 * @returns the current entries Name, or NULL if we havn't selected a Entry 544 492 */ 545 const char*IniParser::getCurrentName() const546 { 547 if (!this->sections.empty() &&548 this->currentSection != this->sections.end() &&549 this->currentEntry != (*this->currentSection).entries.end())550 return (*this->currentEntry).name;551 else552 return NULL;493 const std::string& IniParser::getCurrentName() const 494 { 495 if (!this->sections.empty() && 496 this->currentSection != this->sections.end() && 497 this->currentEntry != (*this->currentSection).entries.end()) 498 return (*this->currentEntry).name; 499 else 500 return emptyString; 553 501 } 554 502 … … 556 504 * @returns the current entries Value, or NULL if we havn't selected a Entry 557 505 */ 558 const char*IniParser::getCurrentValue() const506 const std::string& IniParser::getCurrentValue() const 559 507 { 560 508 if (!this->sections.empty() && … … 563 511 return (*this->currentEntry).value; 564 512 else 565 return NULL;513 return IniParser::emptyString; 566 514 } 567 515 … … 571 519 * @param sectionName the Name of the Section to get the Iterator from 572 520 */ 573 std::list<IniParser::IniSection>::const_iterator IniParser::getSectionIT(const char*sectionName) const521 std::list<IniParser::IniSection>::const_iterator IniParser::getSectionIT(const std::string& sectionName) const 574 522 { 575 523 std::list<IniSection>::const_iterator section = this->currentSection; 576 if (sectionName == NULL)524 if (sectionName.empty()) 577 525 return this->currentSection; 578 526 else 579 527 for (section = this->sections.begin(); section != this->sections.end(); section++) 580 if ( !strcmp((*section).name, sectionName))528 if ((*section).name == sectionName) 581 529 break; 582 530 return section; … … 588 536 * @param sectionName the Name of the Section to get the Iterator from 589 537 */ 590 std::list<IniParser::IniSection>::iterator IniParser::getSectionIT(const char*sectionName)538 std::list<IniParser::IniSection>::iterator IniParser::getSectionIT(const std::string& sectionName) 591 539 { 592 540 std::list<IniSection>::iterator section = this->currentSection; 593 if (sectionName == NULL)541 if (sectionName.empty()) 594 542 return this->currentSection; 595 543 else 596 544 for (section = this->sections.begin(); section != this->sections.end(); section++) 597 if ( !strcmp((*section).name, sectionName))545 if ((*section).name == sectionName) 598 546 break; 599 547 return section; … … 606 554 * @param sectionName the Name of the Section to get the Iterator from 607 555 */ 608 std::list<IniParser::IniEntry>::const_iterator IniParser::getEntryIT(const char* entryName, const char*sectionName) const609 { 610 if (entryName == NULL)556 std::list<IniParser::IniEntry>::const_iterator IniParser::getEntryIT(const std::string& entryName, const std::string& sectionName) const 557 { 558 if (entryName.empty()) 611 559 return this->currentEntry; 612 560 std::list<IniSection>::const_iterator section = this->getSectionIT(sectionName); … … 615 563 if (section != this->sections.end()) 616 564 for (entry = (*section).entries.begin(); entry != (*section).entries.end(); entry++) 617 if ( !strcmp((*entry).name, entryName))565 if ((*entry).name == entryName) 618 566 break; 619 567 if (entry == (*section).entries.end()) … … 629 577 * @param sectionName the Name of the Section to get the Iterator from 630 578 */ 631 std::list<IniParser::IniEntry>::iterator IniParser::getEntryIT(const char* entryName, const char*sectionName)632 { 633 if (entryName == NULL)579 std::list<IniParser::IniEntry>::iterator IniParser::getEntryIT(const std::string& entryName, const std::string& sectionName) 580 { 581 if (entryName.empty()) 634 582 return this->currentEntry; 635 583 std::list<IniSection>::iterator section = this->getSectionIT(sectionName); … … 638 586 if (section != this->sections.end()) 639 587 for (entry = (*section).entries.begin(); entry != (*section).entries.end(); entry++) 640 if ( !strcmp((*entry).name, entryName))588 if ((*entry).name == entryName) 641 589 break; 642 590 if (entry == (*section).entries.end()) … … 652 600 void IniParser::setFileComment() 653 601 { 654 if (this->comment != NULL) 655 delete[] this->comment; 656 657 if (this->commentList.empty()) { 658 this->comment = NULL; 602 if (this->commentList.empty()) 603 { 604 this->comment = ""; 659 605 return; 660 606 } 661 607 662 unsigned int stringLength = 1;663 608 std::list<char*>::iterator comment; 664 for (comment = this->commentList.begin(); comment != this->commentList.end(); comment++) 665 stringLength += strlen((*comment)) +1; 666 667 this->comment = new char[stringLength]; 668 this->comment[0] = '\0'; 609 669 610 while (!this->commentList.empty()) 670 611 { 671 if (*this->comment != '\0') 672 strcat(this->comment, "\n"); 673 strcat(this->comment, this->commentList.front()); 674 delete[] this->commentList.front(); 612 if (this->comment[0] != '\0') 613 this->comment += "\n"; 614 this->comment += this->commentList.front(); 675 615 this->commentList.pop_front(); 676 616 } … … 682 622 void IniParser::setSectionComment() 683 623 { 684 if ((*this->currentSection).comment != NULL) 685 delete[] (*this->currentSection).comment; 686 687 if (this->commentList.empty()) { 688 (*this->currentSection).comment = NULL; 624 (*this->currentSection).comment = ""; 625 626 if (this->commentList.empty()) 689 627 return; 690 } 691 692 unsigned int stringLength = 1; 693 std::list<char*>::iterator comment; 694 for (comment = this->commentList.begin(); comment != this->commentList.end(); comment++) 695 stringLength += strlen((*comment)) +1; 696 697 (*this->currentSection).comment = new char[stringLength]; 698 (*this->currentSection).comment[0] = '\0'; 628 699 629 while (!this->commentList.empty()) 700 630 { 701 if (*(*this->currentSection).comment != '\0') 702 strcat((*this->currentSection).comment, "\n"); 703 strcat((*this->currentSection).comment, this->commentList.front()); 704 delete[] this->commentList.front(); 631 if ((*this->currentSection).comment[0] != '\0') 632 (*this->currentSection).comment += "\n"; 633 (*this->currentSection).comment += this->commentList.front(); 705 634 this->commentList.pop_front(); 706 635 } … … 712 641 void IniParser::setEntryComment() 713 642 { 714 if ((*this->currentEntry).comment != NULL) 715 delete[] (*this->currentEntry).comment; 716 717 if (this->commentList.empty()) { 718 (*this->currentEntry).comment = NULL; 643 (*this->currentEntry).comment = ""; 644 if (this->commentList.empty()) 719 645 return; 720 } 721 722 unsigned int stringLength = 1; 723 std::list<char*>::iterator comment; 724 for (comment = this->commentList.begin(); comment != this->commentList.end(); comment++) 725 stringLength += strlen((*comment)) +1; 726 727 (*this->currentEntry).comment = new char[stringLength]; 728 (*this->currentEntry).comment[0] = '\0'; 646 729 647 while (!this->commentList.empty()) 730 648 { 731 if (*(*this->currentEntry).comment != '\0') 732 strcat((*this->currentEntry).comment, "\n"); 733 strcat((*this->currentEntry).comment, this->commentList.front()); 734 delete[] this->commentList.front(); 649 if ((*this->currentEntry).comment[0] != '\0') 650 (*this->currentEntry).comment += "\n"; 651 (*this->currentEntry).comment += this->commentList.front(); 735 652 this->commentList.pop_front(); 736 653 } 737 738 654 } 739 655 … … 744 660 void IniParser::debug() const 745 661 { 746 PRINTF(0)("Iniparser %s - debug\n", this->fileName );747 if ( this->comment != NULL)748 PRINTF(0)("FileComment:\n %s\n\n", this->comment );749 750 if ( this->fileName != NULL)662 PRINTF(0)("Iniparser %s - debug\n", this->fileName.c_str()); 663 if (!this->comment.empty()) 664 PRINTF(0)("FileComment:\n %s\n\n", this->comment.c_str()); 665 666 if (!this->fileName.empty()) 751 667 { 752 668 std::list<IniSection>::const_iterator section; 753 669 for (section = this->sections.begin(); section != this->sections.end(); section++) 754 670 { 755 if ( (*section).comment != NULL)756 PRINTF(0)(" %s\n", (*section).comment );757 PRINTF(0)(" [%s]\n", (*section).name );671 if (!(*section).comment.empty()) 672 PRINTF(0)(" %s\n", (*section).comment.c_str()); 673 PRINTF(0)(" [%s]\n", (*section).name.c_str()); 758 674 759 675 std::list<IniEntry>::const_iterator entry; 760 676 for (entry = (*section).entries.begin(); entry != (*section).entries.end(); entry++) 761 677 { 762 if ( (*entry).comment != NULL)763 PRINTF(0)(" %s\n", (*entry).comment );764 PRINTF(0)(" '%s' -> '%s'\n", (*entry).name , (*entry).value);678 if (!(*entry).comment.empty()) 679 PRINTF(0)(" %s\n", (*entry).comment.c_str()); 680 PRINTF(0)(" '%s' -> '%s'\n", (*entry).name.c_str(), (*entry).value.c_str()); 765 681 } 766 682 } -
trunk/src/lib/parser/ini_parser/ini_parser.h
r6981 r7221 15 15 16 16 #include <list> 17 #include <string> 17 18 18 19 //! ini-file parser … … 27 28 struct IniEntry 28 29 { 29 char*comment; //!< A Comment that is appendet to the Top of this Entry.30 char*name; //!< name of a given Entry31 char*value; //!< value of a given Entry30 std::string comment; //!< A Comment that is appendet to the Top of this Entry. 31 std::string name; //!< name of a given Entry 32 std::string value; //!< value of a given Entry 32 33 }; 33 34 … … 35 36 struct IniSection 36 37 { 37 char*comment; //!< A Comment that is appendet to the Top of this Section.38 char*name; //!< name of a given section38 std::string comment; //!< A Comment that is appendet to the Top of this Section. 39 std::string name; //!< name of a given section 39 40 std::list<IniEntry> entries; //!< a list of entries for this section 40 41 }; … … 42 43 43 44 public: 44 IniParser (const char* filename = NULL);45 IniParser (const std::string& filename = ""); 45 46 virtual ~IniParser (); 46 47 47 48 /** @returns true if the file is opened, false otherwise*/ 48 bool isOpen() const { return ( this->fileName != NULL)? true : false; };49 bool isOpen() const { return (!this->fileName.empty())? true : false; }; 49 50 /** @returns the fileName we have opened. */ 50 const char*getFileName() const { return this->fileName; };51 const std::string& getFileName() const { return this->fileName; }; 51 52 52 bool readFile(const char*fileName);53 bool writeFile(const char*fileName) const;53 bool readFile(const std::string& fileName); 54 bool writeFile(const std::string& fileName) const; 54 55 55 void setFileComment(const char*fileComment);56 const char*getFileComment() const { return this->comment; };56 void setFileComment(const std::string& fileComment); 57 const std::string& getFileComment() const { return this->comment; }; 57 58 58 bool addSection(const char*sectionName);59 bool getSection(const char*sectionName);60 void setSectionComment(const char* comment, const char*sectionName);61 const char* getSectionComment(const char*sectionNane) const;59 bool addSection(const std::string& sectionName); 60 bool getSection(const std::string& sectionName); 61 void setSectionComment(const std::string& comment, const std::string& sectionName); 62 const std::string& getSectionComment(const std::string& sectionNane) const; 62 63 63 64 // iterate through sections with these Functions 64 65 void firstSection(); 65 const char*nextSection();66 const std::string& nextSection(); 66 67 67 68 68 bool addVar(const char* entryName, const char* value, const char* sectionName = NULL);69 const char* getVar(const char* entryName, const char* sectionName, const char*defaultValue = "") const;70 void setEntryComment(const char* comment, const char* entryName, const char*sectionName);71 const char* getEntryComment(const char* entryName, const char*sectionName) const;69 bool addVar(const std::string& entryName, const std::string& value, const std::string& sectionName = "" ); 70 const std::string& getVar(const std::string& entryName, const std::string& sectionName, const std::string& defaultValue = "") const; 71 void setEntryComment(const std::string& comment, const std::string& entryName, const std::string& sectionName); 72 const std::string& getEntryComment(const std::string& entryName, const std::string& sectionName) const; 72 73 73 74 // iterate Through Variables with these Functions. … … 77 78 78 79 // retrieving functions when iterating. 79 const char*getCurrentSection() const;80 const char*getCurrentName() const;81 const char*getCurrentValue() const;80 const std::string& getCurrentSection() const; 81 const std::string& getCurrentName() const; 82 const std::string& getCurrentValue() const; 82 83 83 84 … … 88 89 private: 89 90 void deleteSections(); 90 void setFileName(const char*fileName);91 void setFileName(const std::string& fileName); 91 92 92 93 void setFileComment(); … … 94 95 void setEntryComment(); 95 96 96 std::list<IniSection>::const_iterator getSectionIT(const char*sectionName) const;97 std::list<IniSection>::iterator getSectionIT(const char*sectionName);97 std::list<IniSection>::const_iterator getSectionIT(const std::string& sectionName) const; 98 std::list<IniSection>::iterator getSectionIT(const std::string& sectionName); 98 99 99 std::list<IniEntry>::const_iterator getEntryIT(const char* entryName, const char* sectionName = NULL) const;100 std::list<IniEntry>::iterator getEntryIT(const char* entryName, const char* sectionName = NULL);100 std::list<IniEntry>::const_iterator getEntryIT(const std::string& entryName, const std::string& sectionName = "") const; 101 std::list<IniEntry>::iterator getEntryIT(const std::string& entryName, const std::string& sectionName = ""); 101 102 102 103 private: 103 char*fileName; //!< The name of the File that was parsed.104 char*comment; //!< A Comment for the header of this File.104 std::string fileName; //!< The name of the File that was parsed. 105 std::string comment; //!< A Comment for the header of this File. 105 106 std::list<IniSection> sections; //!< a list of all stored Sections of the Parser 106 107 std::list<IniSection>::iterator currentSection; //!< the current selected Section 107 108 std::list<IniEntry>::iterator currentEntry; //!< the current selected entry (in currentSection) 108 109 109 std::list<char*> commentList; //!< A list of Comments. (this is for temporary saving of Comments, that are inserted in front of Sections/Entries.) 110 std::list<std::string> commentList; //!< A list of Comments. (this is for temporary saving of Comments, that are inserted in front of Sections/Entries.) 111 112 113 static const std::string emptyString; 110 114 }; 111 115
Note: See TracChangeset
for help on using the changeset viewer.