Changeset 7203 in orxonox.OLD for branches/std/src/lib/parser
- Timestamp:
- Mar 9, 2006, 5:28:10 PM (19 years ago)
- Location:
- branches/std/src/lib/parser
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/std/src/lib/parser/ini_parser/ini_parser.cc
r5953 r7203 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 } … … 59 59 } 60 60 61 const std::string IniParser::emptyString = ""; 62 61 63 62 64 /** … … 68 70 while(!this->sections.empty()) 69 71 { 70 72 IniSection section = this->sections.front(); 71 73 72 74 // in all entries of the sections … … 75 77 // delete all strings of entries. 76 78 IniEntry entry = section.entries.front(); 77 delete[] entry.name;78 delete[] entry.value;79 delete[] entry.comment;80 79 section.entries.pop_front(); 81 80 } 82 81 // delete all Sections 83 delete[] section.name;84 delete[] section.comment;85 82 this->sections.pop_front(); 86 83 } … … 95 92 * If fileName is NULL the new Name will be set to NULL too. 96 93 */ 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; 94 void IniParser::setFileName(const std::string& fileName) 95 { 96 this->comment = ""; 97 98 if (!fileName.empty()) 99 this->fileName = fileName; 100 else 101 this->fileName = ""; 112 102 } 113 103 … … 121 111 * and the new one will be opened. 122 112 */ 123 bool IniParser::readFile(const char*fileName)113 bool IniParser::readFile(const std::string& fileName) 124 114 { 125 115 FILE* stream; //< The stream we use to read the file. 126 116 int lineCount = 0; //< The Count of lines. 127 117 128 if ( this->fileName != NULL)118 if (!this->fileName.empty()) 129 119 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 );120 if( fileName.empty()) 121 return false; 122 123 if( (stream = fopen (fileName.c_str(), "r")) == NULL) 124 { 125 PRINTF(1)("IniParser could not open %s\n", fileName.c_str()); 136 126 return false; 137 127 } … … 191 181 continue; 192 182 } 193 if( ptr == lineBegin) { 183 if( ptr == lineBegin) 184 { 194 185 lineCount++; 195 186 continue; … … 228 219 * @return true on success false otherwise 229 220 */ 230 bool IniParser::writeFile(const char*fileName) const221 bool IniParser::writeFile(const std::string& fileName) const 231 222 { 232 223 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 );224 if( fileName.empty()) 225 return false; 226 227 if( (stream = fopen (fileName.c_str(), "w")) == NULL) 228 { 229 PRINTF(1)("IniParser could not open %s\n", fileName.c_str()); 230 return false; 231 } 232 else 233 { 234 if (!this->comment.empty()) 235 fprintf(stream, "%s\n\n", this->comment.c_str()); 245 236 246 237 std::list<IniSection>::const_iterator section; 247 238 for (section = this->sections.begin(); section != this->sections.end(); section++) 239 { 240 if (!(*section).comment.empty()) 241 fprintf(stream, "%s", (*section).comment.c_str()); 242 fprintf(stream, "\n [%s]\n", (*section).name.c_str()); 243 244 std::list<IniEntry>::const_iterator entry; 245 for (entry = (*section).entries.begin(); entry != (*section).entries.end(); entry++) 248 246 { 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 } 247 if (!(*entry).comment.empty()) 248 fprintf(stream, "%s", (*entry).comment.c_str()); 249 fprintf(stream, " %s = %s\n", (*entry).name.c_str(), (*entry).value.c_str()); 260 250 } 251 } 261 252 } 262 253 fclose(stream); 263 254 } 264 255 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 256 void IniParser::setFileComment(const std::string& fileComment) 257 { 258 this->comment = fileComment; 259 } 279 260 280 261 /** … … 284 265 * @return true on success... there is only success or segfault :) 285 266 */ 286 bool IniParser::addSection(const char*sectionName)287 { 288 if (sectionName == NULL)267 bool IniParser::addSection(const std::string& sectionName) 268 { 269 if (sectionName.empty()) 289 270 return false; 290 271 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); 272 this->sections.back().comment = ""; 273 this->sections.back().name = sectionName; 294 274 295 275 this->currentSection = --this->sections.end(); 296 276 if (!this->sections.empty()) 297 277 this->currentEntry = (*this->currentSection).entries.begin(); 298 278 PRINTF(5)("Added Section %s\n", sectionName); 299 279 return true; … … 306 286 * @return true on success or false if the section could not be found 307 287 */ 308 bool IniParser::getSection(const char*sectionName)288 bool IniParser::getSection(const std::string& sectionName) 309 289 { 310 290 this->currentSection = this->getSectionIT(sectionName); … … 321 301 * 322 302 */ 323 void IniParser::setSectionComment(const char* comment, const char*sectionName)303 void IniParser::setSectionComment(const std::string& comment, const std::string& sectionName) 324 304 { 325 305 std::list<IniSection>::iterator section = this->getSectionIT(sectionName); … … 327 307 return; 328 308 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; 309 (*section).comment = comment; 338 310 } 339 311 … … 342 314 * @returns the Comment, or NULL on error. 343 315 */ 344 const char* IniParser::getSectionComment(const char*sectionName) const316 const std::string& IniParser::getSectionComment(const std::string& sectionName) const 345 317 { 346 318 std::list<IniSection>::const_iterator section = this->getSectionIT(sectionName); … … 348 320 return (*section).comment; 349 321 else 350 return NULL;322 return IniParser::emptyString; 351 323 } 352 324 … … 367 339 * @returns the name of the section if found, NULL otherwise 368 340 */ 369 const char*IniParser::nextSection()341 const std::string& IniParser::nextSection() 370 342 { 371 343 if (this->currentSection == this->sections.end()) 372 return NULL;344 return IniParser::emptyString; 373 345 374 346 this->currentSection++; 375 347 376 348 if (this->currentSection != this->sections.end()) 377 378 379 380 381 else 382 return NULL;349 { 350 this->currentEntry = (*this->currentSection).entries.begin(); 351 return this->currentSection->name; 352 } 353 else 354 return IniParser::emptyString; 383 355 } 384 356 … … 393 365 * @return true if everything is ok false on error 394 366 */ 395 bool IniParser::addVar(const char* entryName, const char* value, const char*sectionName)367 bool IniParser::addVar(const std::string& entryName, const std::string& value, const std::string& sectionName) 396 368 { 397 369 std::list<IniSection>::iterator section; 398 370 399 if ( sectionName != NULL)371 if (!sectionName.empty()) 400 372 { 401 373 for (section = this->sections.begin(); section != this->sections.end(); section++) 402 if ( !strcmp((*section).name, sectionName))374 if ((*section).name == sectionName) 403 375 break; 404 376 } … … 411 383 if (section == this->sections.end()) 412 384 { 413 PRINTF(2)("section '%s' not found for value '%s'\n", sectionName , entryName);385 PRINTF(2)("section '%s' not found for value '%s'\n", sectionName.c_str(), entryName.c_str()); 414 386 return false; 415 387 } … … 417 389 { 418 390 (*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); 391 (*section).entries.back().comment = ""; 392 (*section).entries.back().name = entryName; 393 (*section).entries.back().value = value; 424 394 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 );395 (*section).entries.back().name.c_str(), 396 (*section).entries.back().value.c_str(), 397 (*section).name.c_str()); 428 398 this->currentEntry = --(*section).entries.end(); 429 399 return true; … … 442 412 * lead to unwanted behaviour. 443 413 */ 444 const char* IniParser::getVar(const char* entryName, const char* sectionName, const char*defaultValue) const445 { 446 if ( this->fileName != NULL)414 const std::string& IniParser::getVar(const std::string& entryName, const std::string& sectionName, const std::string& defaultValue) const 415 { 416 if (!this->fileName.empty()) 447 417 { 448 418 std::list<IniEntry>::const_iterator entry = this->getEntryIT(entryName, sectionName); 449 if (entry != NULL && !strcmp((*entry).name, entryName))419 if (entry != NULL && (*entry).name == entryName) 450 420 return (*entry).value; 451 PRINTF(2)("Entry '%s' in section '%s' not found.\n", entryName , sectionName);421 PRINTF(2)("Entry '%s' in section '%s' not found.\n", entryName.c_str(), sectionName.c_str()); 452 422 453 423 } … … 456 426 457 427 return defaultValue; 458 459 428 } 460 429 … … 465 434 * @param sectionName the Name of the Section 466 435 */ 467 void IniParser::setEntryComment(const char* comment, const char* entryName, const char*sectionName)436 void IniParser::setEntryComment(const std::string& comment, const std::string& entryName, const std::string& sectionName) 468 437 { 469 438 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 439 (*entry).comment = comment; 482 440 } 483 441 … … 487 445 * @returns the queried Comment. 488 446 */ 489 const char* IniParser::getEntryComment(const char* entryName, const char*sectionName) const447 const std::string& IniParser::getEntryComment(const std::string& entryName, const std::string& sectionName) const 490 448 { 491 449 std::list<IniEntry>::const_iterator entry = this->getEntryIT(entryName, sectionName); … … 501 459 { 502 460 if (!this->sections.empty() && 503 461 this->currentSection != this->sections.end()) 504 462 this->currentEntry = (*this->currentSection).entries.begin(); 505 463 } … … 530 488 * @returns the name of the Current selected Section 531 489 */ 532 const char*IniParser::getCurrentSection() const490 const std::string& IniParser::getCurrentSection() const 533 491 { 534 492 if (!this->sections.empty() && … … 536 494 return this->currentSection->name; 537 495 else 538 return NULL;539 496 return IniParser::emptyString ; 497 } 540 498 541 499 … … 543 501 * @returns the current entries Name, or NULL if we havn't selected a Entry 544 502 */ 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;503 const std::string& IniParser::getCurrentName() const 504 { 505 if (!this->sections.empty() && 506 this->currentSection != this->sections.end() && 507 this->currentEntry != (*this->currentSection).entries.end()) 508 return (*this->currentEntry).name; 509 else 510 return emptyString; 553 511 } 554 512 … … 556 514 * @returns the current entries Value, or NULL if we havn't selected a Entry 557 515 */ 558 const char*IniParser::getCurrentValue() const516 const std::string& IniParser::getCurrentValue() const 559 517 { 560 518 if (!this->sections.empty() && … … 563 521 return (*this->currentEntry).value; 564 522 else 565 return NULL;523 return IniParser::emptyString; 566 524 } 567 525 … … 571 529 * @param sectionName the Name of the Section to get the Iterator from 572 530 */ 573 std::list<IniParser::IniSection>::const_iterator IniParser::getSectionIT(const char*sectionName) const531 std::list<IniParser::IniSection>::const_iterator IniParser::getSectionIT(const std::string& sectionName) const 574 532 { 575 533 std::list<IniSection>::const_iterator section = this->currentSection; 576 if (sectionName == NULL)534 if (sectionName.empty()) 577 535 return this->currentSection; 578 536 else 579 537 for (section = this->sections.begin(); section != this->sections.end(); section++) 580 if ( !strcmp((*section).name, sectionName))538 if ((*section).name == sectionName) 581 539 break; 582 540 return section; … … 588 546 * @param sectionName the Name of the Section to get the Iterator from 589 547 */ 590 std::list<IniParser::IniSection>::iterator IniParser::getSectionIT(const char*sectionName)548 std::list<IniParser::IniSection>::iterator IniParser::getSectionIT(const std::string& sectionName) 591 549 { 592 550 std::list<IniSection>::iterator section = this->currentSection; 593 if (sectionName == NULL)551 if (sectionName.empty()) 594 552 return this->currentSection; 595 553 else 596 554 for (section = this->sections.begin(); section != this->sections.end(); section++) 597 if ( !strcmp((*section).name, sectionName))555 if ((*section).name == sectionName) 598 556 break; 599 557 return section; … … 606 564 * @param sectionName the Name of the Section to get the Iterator from 607 565 */ 608 std::list<IniParser::IniEntry>::const_iterator IniParser::getEntryIT(const char* entryName, const char*sectionName) const609 { 610 if (entryName == NULL)566 std::list<IniParser::IniEntry>::const_iterator IniParser::getEntryIT(const std::string& entryName, const std::string& sectionName) const 567 { 568 if (entryName.empty()) 611 569 return this->currentEntry; 612 570 std::list<IniSection>::const_iterator section = this->getSectionIT(sectionName); … … 615 573 if (section != this->sections.end()) 616 574 for (entry = (*section).entries.begin(); entry != (*section).entries.end(); entry++) 617 if ( !strcmp((*entry).name, entryName))575 if ((*entry).name == entryName) 618 576 break; 619 577 if (entry == (*section).entries.end()) … … 629 587 * @param sectionName the Name of the Section to get the Iterator from 630 588 */ 631 std::list<IniParser::IniEntry>::iterator IniParser::getEntryIT(const char* entryName, const char*sectionName)632 { 633 if (entryName == NULL)589 std::list<IniParser::IniEntry>::iterator IniParser::getEntryIT(const std::string& entryName, const std::string& sectionName) 590 { 591 if (entryName.empty()) 634 592 return this->currentEntry; 635 593 std::list<IniSection>::iterator section = this->getSectionIT(sectionName); … … 638 596 if (section != this->sections.end()) 639 597 for (entry = (*section).entries.begin(); entry != (*section).entries.end(); entry++) 640 if ( !strcmp((*entry).name, entryName))598 if ((*entry).name == entryName) 641 599 break; 642 600 if (entry == (*section).entries.end()) … … 652 610 void IniParser::setFileComment() 653 611 { 654 if (this->comment != NULL) 655 delete[] this->comment; 656 657 if (this->commentList.empty()) { 658 this->comment = NULL; 612 if (this->commentList.empty()) 613 { 614 this->comment = ""; 659 615 return; 660 616 } 661 617 662 unsigned int stringLength = 1;663 618 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'; 619 669 620 while (!this->commentList.empty()) 670 621 { 671 if (*this->comment != '\0') 672 strcat(this->comment, "\n"); 673 strcat(this->comment, this->commentList.front()); 674 delete[] this->commentList.front(); 622 if (this->comment[0] != '\0') 623 this->comment += "\n"; 624 this->comment += this->commentList.front(); 675 625 this->commentList.pop_front(); 676 626 } … … 682 632 void IniParser::setSectionComment() 683 633 { 684 if ((*this->currentSection).comment != NULL) 685 delete[] (*this->currentSection).comment; 686 687 if (this->commentList.empty()) { 688 (*this->currentSection).comment = NULL; 634 (*this->currentSection).comment = ""; 635 636 if (this->commentList.empty()) 689 637 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'; 638 699 639 while (!this->commentList.empty()) 700 640 { 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(); 641 if ((*this->currentSection).comment[0] != '\0') 642 (*this->currentSection).comment += "\n"; 643 (*this->currentSection).comment += this->commentList.front(); 705 644 this->commentList.pop_front(); 706 645 } … … 712 651 void IniParser::setEntryComment() 713 652 { 714 if ((*this->currentEntry).comment != NULL) 715 delete[] (*this->currentEntry).comment; 716 717 if (this->commentList.empty()) { 718 (*this->currentEntry).comment = NULL; 653 (*this->currentEntry).comment = ""; 654 if (this->commentList.empty()) 719 655 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'; 656 729 657 while (!this->commentList.empty()) 730 658 { 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(); 659 if ((*this->currentEntry).comment[0] != '\0') 660 (*this->currentEntry).comment += "\n"; 661 (*this->currentEntry).comment += this->commentList.front(); 735 662 this->commentList.pop_front(); 736 663 } 737 738 664 } 739 665 … … 744 670 void IniParser::debug() const 745 671 { 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)672 PRINTF(0)("Iniparser %s - debug\n", this->fileName.c_str()); 673 if (!this->comment.empty()) 674 PRINTF(0)("FileComment:\n %s\n\n", this->comment.c_str()); 675 676 if (!this->fileName.empty()) 751 677 { 752 678 std::list<IniSection>::const_iterator section; 753 679 for (section = this->sections.begin(); section != this->sections.end(); section++) 754 680 { 755 if ( (*section).comment != NULL)756 PRINTF(0)(" %s\n", (*section).comment );757 PRINTF(0)(" [%s]\n", (*section).name );681 if (!(*section).comment.empty()) 682 PRINTF(0)(" %s\n", (*section).comment.c_str()); 683 PRINTF(0)(" [%s]\n", (*section).name.c_str()); 758 684 759 685 std::list<IniEntry>::const_iterator entry; 760 686 for (entry = (*section).entries.begin(); entry != (*section).entries.end(); entry++) 761 687 { 762 if ( (*entry).comment != NULL)763 PRINTF(0)(" %s\n", (*entry).comment );764 PRINTF(0)(" '%s' -> '%s'\n", (*entry).name , (*entry).value);688 if (!(*entry).comment.empty()) 689 PRINTF(0)(" %s\n", (*entry).comment.c_str()); 690 PRINTF(0)(" '%s' -> '%s'\n", (*entry).name.c_str(), (*entry).value.c_str()); 765 691 } 766 692 } -
branches/std/src/lib/parser/ini_parser/ini_parser.h
r6981 r7203 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 = NULL); 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 = NULL) const; 101 std::list<IniEntry>::iterator getEntryIT(const std::string& entryName, const std::string& sectionName = NULL); 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 -
branches/std/src/lib/parser/tinyxml/tinyxml.h
r5819 r7203 26 26 #ifndef TINYXML_INCLUDED 27 27 #define TINYXML_INCLUDED 28 29 #define TIXML_USE_STL 28 30 29 31 #ifdef _MSC_VER … … 81 83 #define TIXML_SNSCANF snscanf 82 84 #endif 83 #endif 85 #endif 84 86 85 87 class TiXmlDocument; … … 96 98 const int TIXML_PATCH_VERSION = 2; 97 99 98 /* Internal structure for tracking location of items 100 /* Internal structure for tracking location of items 99 101 in the XML file. 100 102 */ … … 110 112 111 113 // Only used by Attribute::Query functions 112 enum 113 { 114 enum 115 { 114 116 TIXML_SUCCESS, 115 117 TIXML_NO_ATTRIBUTE, … … 162 164 /** All TinyXml classes can print themselves to a filestream. 163 165 This is a formatted print, and will insert tabs and newlines. 164 166 165 167 (For an unformatted stream, use the << operator.) 166 168 */ … … 206 208 static const int utf8ByteTable[256]; 207 209 208 virtual const char* Parse( const char* p, 209 TiXmlParsingData* data, 210 virtual const char* Parse( const char* p, 211 TiXmlParsingData* data, 210 212 TiXmlEncoding encoding /*= TIXML_ENCODING_UNKNOWN */ ) = 0; 211 213 … … 245 247 246 248 static const char* SkipWhiteSpace( const char*, TiXmlEncoding encoding ); 247 inline static bool IsWhiteSpace( char c ) 248 { 249 return ( isspace( (unsigned char) c ) || c == '\n' || c == '\r' ); 249 inline static bool IsWhiteSpace( char c ) 250 { 251 return ( isspace( (unsigned char) c ) || c == '\n' || c == '\r' ); 250 252 } 251 253 … … 334 336 /// Field containing a generic user pointer 335 337 void* userData; 336 338 337 339 // None of these methods are reliable for any language except English. 338 340 // Good for approximation, not great for accuracy. … … 386 388 387 389 public: 388 #ifdef TIXML_USE_STL 390 #ifdef TIXML_USE_STL 389 391 390 392 /** An input stream operator, for every class. Tolerant of newlines and … … 400 402 a node to a stream is very well defined. You'll get a nice stream 401 403 of output, without any extra whitespace or newlines. 402 404 403 405 But reading is not as well defined. (As it always is.) If you create 404 406 a TiXmlElement (for example) and read that from an input stream, … … 408 410 A TiXmlDocument will read nodes until it reads a root element, and 409 411 all the children of that root element. 410 */ 412 */ 411 413 friend std::ostream& operator<< (std::ostream& out, const TiXmlNode& base); 412 414 … … 470 472 #ifdef TIXML_USE_STL 471 473 /// STL std::string form. 472 void SetValue( const std::string& _value ) 473 { 474 void SetValue( const std::string& _value ) 475 { 474 476 StringToBuffer buf( _value ); 475 SetValue( buf.buffer ? buf.buffer : "" ); 476 } 477 SetValue( buf.buffer ? buf.buffer : "" ); 478 } 477 479 #endif 478 480 … … 492 494 TiXmlNode* LastChild() { return lastChild; } 493 495 const TiXmlNode* LastChild( const char * value ) const; /// The last child of this node matching 'value'. Will be null if there are no children. 494 TiXmlNode* LastChild( const char * value ); 496 TiXmlNode* LastChild( const char * value ); 495 497 496 498 #ifdef TIXML_USE_STL … … 649 651 650 652 /** Create an exact duplicate of this node and return it. The memory must be deleted 651 by the caller. 653 by the caller. 652 654 */ 653 655 virtual TiXmlNode* Clone() const = 0; … … 731 733 /** QueryIntValue examines the value string. It is an alternative to the 732 734 IntValue() method with richer error checking. 733 If the value is an integer, it is stored in 'value' and 735 If the value is an integer, it is stored in 'value' and 734 736 the call returns TIXML_SUCCESS. If it is not 735 737 an integer, it returns TIXML_WRONG_TYPE. … … 750 752 #ifdef TIXML_USE_STL 751 753 /// STL std::string form. 752 void SetName( const std::string& _name ) 753 { 754 void SetName( const std::string& _name ) 755 { 754 756 StringToBuffer buf( _name ); 755 SetName ( buf.buffer ? buf.buffer : "error" ); 756 } 757 /// STL std::string form. 758 void SetValue( const std::string& _value ) 759 { 757 SetName ( buf.buffer ? buf.buffer : "error" ); 758 } 759 /// STL std::string form. 760 void SetValue( const std::string& _value ) 761 { 760 762 StringToBuffer buf( _value ); 761 SetValue( buf.buffer ? buf.buffer : "error" ); 763 SetValue( buf.buffer ? buf.buffer : "error" ); 762 764 } 763 765 #endif … … 801 803 /* A class used to manage a group of attributes. 802 804 It is only used internally, both by the ELEMENT and the DECLARATION. 803 805 804 806 The set can be changed transparent to the Element and Declaration 805 807 classes that use it, but NOT transparent to the Attribute … … 882 884 /** QueryIntAttribute examines the attribute - it is an alternative to the 883 885 Attribute() method with richer error checking. 884 If the attribute is an integer, it is stored in 'value' and 886 If the attribute is an integer, it is stored in 'value' and 885 887 the call returns TIXML_SUCCESS. If it is not 886 888 an integer, it returns TIXML_WRONG_TYPE. If the attribute 887 889 does not exist, then TIXML_NO_ATTRIBUTE is returned. 888 */ 890 */ 889 891 int QueryIntAttribute( const char* name, int* _value ) const; 890 892 /// QueryDoubleAttribute examines the attribute - see QueryIntAttribute(). … … 913 915 914 916 /// STL std::string form. 915 void SetAttribute( const std::string& name, const std::string& _value ) 916 { 917 void SetAttribute( const std::string& name, const std::string& _value ) 918 { 917 919 StringToBuffer n( name ); 918 920 StringToBuffer v( _value ); 919 921 if ( n.buffer && v.buffer ) 920 SetAttribute (n.buffer, v.buffer ); 921 } 922 SetAttribute (n.buffer, v.buffer ); 923 } 922 924 ///< STL std::string form. 923 void SetAttribute( const std::string& name, int _value ) 924 { 925 void SetAttribute( const std::string& name, int _value ) 926 { 925 927 StringToBuffer n( name ); 926 928 if ( n.buffer ) 927 SetAttribute (n.buffer, _value); 928 } 929 SetAttribute (n.buffer, _value); 930 } 929 931 #endif 930 932 … … 954 956 and concise, GetText() is limited compared to getting the TiXmlText child 955 957 and accessing it directly. 956 958 957 959 If the first child of 'this' is a TiXmlText, the GetText() 958 960 returns the character string of the Text node, else null is returned. … … 964 966 @endverbatim 965 967 966 'str' will be a pointer to "This is text". 967 968 'str' will be a pointer to "This is text". 969 968 970 Note that this function can be misleading. If the element foo was created from 969 971 this XML: 970 972 @verbatim 971 <foo><b>This is text</b></foo> 973 <foo><b>This is text</b></foo> 972 974 @endverbatim 973 975 … … 975 977 another element. From this XML: 976 978 @verbatim 977 <foo>This is <b>text</b></foo> 979 <foo>This is <b>text</b></foo> 978 980 @endverbatim 979 981 GetText() will return "This is ". 980 982 981 WARNING: GetText() accesses a child node - don't become confused with the 982 similarly named TiXmlHandle::Text() and TiXmlNode::ToText() which are 983 WARNING: GetText() accesses a child node - don't become confused with the 984 similarly named TiXmlHandle::Text() and TiXmlNode::ToText() which are 983 985 safe type casts on the referenced node. 984 986 */ … … 1054 1056 1055 1057 1056 /** XML text. A text node can have 2 ways to output the next. "normal" output 1058 /** XML text. A text node can have 2 ways to output the next. "normal" output 1057 1059 and CDATA. It will default to the mode it was parsed from the XML file and 1058 you generally want to leave it alone, but you can change the output mode with 1060 you generally want to leave it alone, but you can change the output mode with 1059 1061 SetCDATA() and query it with CDATA(). 1060 1062 */ … … 1063 1065 friend class TiXmlElement; 1064 1066 public: 1065 /** Constructor for text element. By default, it is treated as 1067 /** Constructor for text element. By default, it is treated as 1066 1068 normal, encoded text. If you want it be output as a CDATA text 1067 1069 element, set the parameter _cdata to 'true' … … 1279 1281 - The ErrorDesc() method will return the name of the error. (very useful) 1280 1282 - The ErrorRow() and ErrorCol() will return the location of the error (if known) 1281 */ 1283 */ 1282 1284 bool Error() const { return error; } 1283 1285 … … 1290 1292 int ErrorId() const { return errorId; } 1291 1293 1292 /** Returns the location (if known) of the error. The first column is column 1, 1294 /** Returns the location (if known) of the error. The first column is column 1, 1293 1295 and the first row is row 1. A value of 0 means the row and column wasn't applicable 1294 1296 (memory errors, for example, have no row/column) or the parser lost the error. (An … … 1303 1305 to report the correct values for row and column. It does not change the output 1304 1306 or input in any way. 1305 1307 1306 1308 By calling this method, with a tab size 1307 1309 greater than 0, the row and column of each node and attribute is stored … … 1331 1333 state is automatically cleared if you Parse a new XML block. 1332 1334 */ 1333 void ClearError() { error = false; 1334 errorId = 0; 1335 errorDesc = ""; 1336 errorLocation.row = errorLocation.col = 0; 1337 //errorLocation.last = 0; 1335 void ClearError() { error = false; 1336 errorId = 0; 1337 errorDesc = ""; 1338 errorLocation.row = errorLocation.col = 0; 1339 //errorLocation.last = 0; 1338 1340 } 1339 1341 … … 1381 1383 @endverbatim 1382 1384 1383 Assuming you want the value of "attributeB" in the 2nd "Child" element, it's very 1385 Assuming you want the value of "attributeB" in the 2nd "Child" element, it's very 1384 1386 easy to write a *lot* of code that looks like: 1385 1387 … … 1401 1403 1402 1404 And that doesn't even cover "else" cases. TiXmlHandle addresses the verbosity 1403 of such code. A TiXmlHandle checks for null pointers so it is perfectly safe 1405 of such code. A TiXmlHandle checks for null pointers so it is perfectly safe 1404 1406 and correct to use: 1405 1407 … … 1422 1424 1423 1425 @verbatim 1424 int i=0; 1426 int i=0; 1425 1427 while ( true ) 1426 1428 { … … 1433 1435 @endverbatim 1434 1436 1435 It seems reasonable, but it is in fact two embedded while loops. The Child method is 1436 a linear walk to find the element, so this code would iterate much more than it needs 1437 It seems reasonable, but it is in fact two embedded while loops. The Child method is 1438 a linear walk to find the element, so this code would iterate much more than it needs 1437 1439 to. Instead, prefer: 1438 1440 … … 1464 1466 TiXmlHandle FirstChildElement( const char * value ) const; 1465 1467 1466 /** Return a handle to the "index" child with the given name. 1468 /** Return a handle to the "index" child with the given name. 1467 1469 The first child is 0, the second 1, etc. 1468 1470 */ 1469 1471 TiXmlHandle Child( const char* value, int index ) const; 1470 /** Return a handle to the "index" child. 1472 /** Return a handle to the "index" child. 1471 1473 The first child is 0, the second 1, etc. 1472 1474 */ 1473 1475 TiXmlHandle Child( int index ) const; 1474 /** Return a handle to the "index" child element with the given name. 1476 /** Return a handle to the "index" child element with the given name. 1475 1477 The first child element is 0, the second 1, etc. Note that only TiXmlElements 1476 1478 are indexed: other types are not counted. 1477 1479 */ 1478 1480 TiXmlHandle ChildElement( const char* value, int index ) const; 1479 /** Return a handle to the "index" child element. 1481 /** Return a handle to the "index" child element. 1480 1482 The first child element is 0, the second 1, etc. Note that only TiXmlElements 1481 1483 are indexed: other types are not counted. … … 1492 1494 1493 1495 /// Return the handle as a TiXmlNode. This may return null. 1494 TiXmlNode* Node() const { return node; } 1496 TiXmlNode* Node() const { return node; } 1495 1497 /// Return the handle as a TiXmlElement. This may return null. 1496 1498 TiXmlElement* Element() const { return ( ( node && node->ToElement() ) ? node->ToElement() : 0 ); }
Note: See TracChangeset
for help on using the changeset viewer.