Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 7221 in orxonox.OLD for trunk/src/lib/parser


Ignore:
Timestamp:
Mar 15, 2006, 3:10:45 PM (19 years ago)
Author:
bensch
Message:

orxonox/trunk: merged the std-branche back, it runs on windows and Linux

svn merge https://svn.orxonox.net/orxonox/branches/std . -r7202:HEAD

Location:
trunk/src/lib/parser
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/lib/parser/ini_parser/ini_parser.cc

    r5953 r7221  
    4040 * @param fileName: the path and name of the file to parse
    4141 */
    42 IniParser::IniParser (const char* fileName)
    43 {
    44   this->fileName = NULL;
    45   this->comment = NULL;
    46 
    47   if (fileName != NULL)
     42IniParser::IniParser (const std::string& fileName)
     43{
     44  this->fileName = "";
     45  this->comment = "";
     46
     47  if (!fileName.empty())
    4848    this->readFile(fileName);
    4949}
     
    5656{
    5757  this->deleteSections();
    58   this->setFileName(NULL);
    59 }
     58  this->setFileName("");
     59}
     60
     61const std::string IniParser::emptyString = "";
    6062
    6163
     
    6668{
    6769  // 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
    8772  this->currentSection = this->sections.end();
    88   this->setFileName(NULL);
     73  this->setFileName("");
    8974}
    9075
     
    9580 * If fileName is NULL the new Name will be set to NULL too.
    9681 */
    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;
     82void IniParser::setFileName(const std::string& fileName)
     83{
     84  this->comment = "";
     85  this->fileName = fileName;
    11286}
    11387
     
    12195 * and the new one will be opened.
    12296 */
    123 bool IniParser::readFile(const char* fileName)
     97bool IniParser::readFile(const std::string& fileName)
    12498{
    12599  FILE*    stream;           //< The stream we use to read the file.
    126100  int      lineCount = 0;    //< The Count of lines.
    127101
    128   if (this->fileName != NULL)
     102  if (!this->fileName.empty())
    129103    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());
    136111    return false;
    137112  }
     
    143118    // READING IN THE INI-FILE //
    144119    /////////////////////////////
    145     char lineBuffer[PARSELINELENGHT];
    146     char buffer[PARSELINELENGHT];
     120    char lineBuffer[PARSELINELENGHT+1];
     121    char buffer[PARSELINELENGHT+1];
    147122    const char* lineBegin;
    148123    char* ptr;
     
    154129      if( (ptr = strchr( lineBuffer, '\n')) != NULL)
    155130        *ptr = 0;
     131      else
     132        lineBuffer[PARSELINELENGHT] = 0;
    156133      // cut up to the beginning of the line.
    157134      while((*lineBegin == ' ' || *lineBegin == '\t') && lineBegin < lineBuffer + strlen(lineBuffer))
    158135        ++lineBegin;
    159136
     137      if ( !strcmp( lineBegin, "" ) )
     138        continue;
     139
    160140      // check if we have a FileComment
    161141      if ( (*lineBegin == '#' || *lineBegin == ';'))
    162142      {
    163         char* newCommenLine = new char[strlen(lineBegin)+1];
    164         strcpy(newCommenLine, lineBegin);
     143        string newCommenLine = lineBegin;
    165144        this->commentList.push_back(newCommenLine);
    166145        continue;
     
    191170          continue;
    192171        }
    193         if( ptr == lineBegin) {
     172        if( ptr == lineBegin)
     173        {
    194174          lineCount++;
    195175          continue;
     
    228208 * @return true on success false otherwise
    229209 */
    230 bool IniParser::writeFile(const char* fileName) const
     210bool IniParser::writeFile(const std::string& fileName) const
    231211{
    232212  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());
    245225
    246226    std::list<IniSection>::const_iterator section;
    247227    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++)
    248235      {
    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());
    260239      }
     240    }
    261241  }
    262242  fclose(stream);
    263243}
    264244
    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 
     245void IniParser::setFileComment(const std::string& fileComment)
     246{
     247  this->comment = fileComment;
     248}
    279249
    280250/**
     
    284254 * @return true on success... there is only success or segfault :)
    285255 */
    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);
     256bool 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);
    294265
    295266  this->currentSection = --this->sections.end();
    296267  if (!this->sections.empty())
    297       this->currentEntry = (*this->currentSection).entries.begin();
    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());
    299270  return true;
    300271}
     
    306277 * @return true on success or false if the section could not be found
    307278 */
    308 bool IniParser::getSection(const char* sectionName)
     279bool IniParser::getSection(const std::string& sectionName)
    309280{
    310281  this->currentSection = this->getSectionIT(sectionName);
     
    321292 *
    322293 */
    323 void IniParser::setSectionComment(const char* comment, const char* sectionName)
     294void IniParser::setSectionComment(const std::string& comment, const std::string& sectionName)
    324295{
    325296  std::list<IniSection>::iterator section = this->getSectionIT(sectionName);
     
    327298    return;
    328299
    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;
    338301}
    339302
     
    342305 * @returns the Comment, or NULL on error.
    343306 */
    344 const char* IniParser::getSectionComment(const char* sectionName) const
     307const std::string& IniParser::getSectionComment(const std::string& sectionName) const
    345308{
    346309  std::list<IniSection>::const_iterator section = this->getSectionIT(sectionName);
     
    348311    return (*section).comment;
    349312  else
    350     return NULL;
     313    return IniParser::emptyString;
    351314}
    352315
     
    367330 * @returns the name of the section if found, NULL otherwise
    368331 */
    369 const char* IniParser::nextSection()
     332const std::string& IniParser::nextSection()
    370333{
    371334  if (this->currentSection == this->sections.end())
    372     return NULL;
     335    return IniParser::emptyString;
    373336
    374337  this->currentSection++;
    375338
    376339  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;
     340  {
     341    this->currentEntry = (*this->currentSection).entries.begin();
     342    return this->currentSection->name;
     343  }
     344  else
     345    return IniParser::emptyString;
    383346}
    384347
     
    393356 * @return true if everything is ok false on error
    394357 */
    395 bool IniParser::addVar(const char* entryName, const char* value, const char* sectionName)
     358bool IniParser::addVar(const std::string& entryName, const std::string& value, const std::string& sectionName)
    396359{
    397360  std::list<IniSection>::iterator section;
    398361
    399   if (sectionName != NULL)
     362  if (!sectionName.empty())
    400363  {
    401364    for (section = this->sections.begin(); section != this->sections.end(); section++)
    402       if (!strcmp((*section).name, sectionName))
     365      if ((*section).name == sectionName)
    403366        break;
    404367  }
     
    411374  if (section == this->sections.end())
    412375  {
    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());
    414377    return false;
    415378  }
     
    417380  {
    418381    (*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;
    424385    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());
    428389    this->currentEntry = --(*section).entries.end();
    429390    return true;
     
    442403 * lead to unwanted behaviour.
    443404*/
    444 const char* IniParser::getVar(const char* entryName, const char* sectionName, const char* defaultValue) const
    445 {
    446   if (this->fileName != NULL)
     405const std::string& IniParser::getVar(const std::string& entryName, const std::string& sectionName, const std::string& defaultValue) const
     406{
     407  if (!this->fileName.empty())
    447408  {
    448409    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)
    450411      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());
    453413  }
    454414  else
     
    456416
    457417  return defaultValue;
    458 
    459418}
    460419
     
    465424 * @param sectionName the Name of the Section
    466425 */
    467 void IniParser::setEntryComment(const char* comment, const char* entryName, const char* sectionName)
     426void IniParser::setEntryComment(const std::string& comment, const std::string& entryName, const std::string& sectionName)
    468427{
    469428  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;
    482430}
    483431
     
    487435 * @returns the queried Comment.
    488436 */
    489 const char* IniParser::getEntryComment(const char* entryName, const char* sectionName) const
     437const std::string& IniParser::getEntryComment(const std::string& entryName, const std::string& sectionName) const
    490438{
    491439  std::list<IniEntry>::const_iterator entry = this->getEntryIT(entryName, sectionName);
     
    501449{
    502450  if (!this->sections.empty() &&
    503        this->currentSection != this->sections.end())
     451      this->currentSection != this->sections.end())
    504452    this->currentEntry = (*this->currentSection).entries.begin();
    505453}
     
    530478 * @returns the name of the Current selected Section
    531479 */
    532 const char* IniParser::getCurrentSection() const
     480const std::string& IniParser::getCurrentSection() const
    533481{
    534482  if (!this->sections.empty() &&
     
    536484    return this->currentSection->name;
    537485  else
    538     return NULL;
    539  }
     486    return IniParser::emptyString ;
     487}
    540488
    541489
     
    543491 * @returns the current entries Name, or NULL if we havn't selected a Entry
    544492 */
    545 const char* IniParser::getCurrentName() const
    546 {
    547  if (!this->sections.empty() &&
    548      this->currentSection != this->sections.end() &&
    549      this->currentEntry != (*this->currentSection).entries.end())
    550    return (*this->currentEntry).name;
    551  else
    552    return NULL;
     493const 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;
    553501}
    554502
     
    556504 * @returns the current entries Value, or NULL if we havn't selected a Entry
    557505 */
    558 const char* IniParser::getCurrentValue() const
     506const std::string& IniParser::getCurrentValue() const
    559507{
    560508  if (!this->sections.empty() &&
     
    563511    return (*this->currentEntry).value;
    564512  else
    565     return NULL;
     513    return IniParser::emptyString;
    566514}
    567515
     
    571519 * @param sectionName the Name of the Section to get the Iterator from
    572520 */
    573 std::list<IniParser::IniSection>::const_iterator IniParser::getSectionIT(const char* sectionName) const
     521std::list<IniParser::IniSection>::const_iterator IniParser::getSectionIT(const std::string& sectionName) const
    574522{
    575523  std::list<IniSection>::const_iterator section = this->currentSection;
    576   if (sectionName == NULL)
     524  if (sectionName.empty())
    577525    return this->currentSection;
    578526  else
    579527    for (section = this->sections.begin(); section != this->sections.end(); section++)
    580       if (!strcmp((*section).name, sectionName))
     528      if ((*section).name == sectionName)
    581529        break;
    582530  return section;
     
    588536 * @param sectionName the Name of the Section to get the Iterator from
    589537 */
    590 std::list<IniParser::IniSection>::iterator IniParser::getSectionIT(const char* sectionName)
     538std::list<IniParser::IniSection>::iterator IniParser::getSectionIT(const std::string& sectionName)
    591539{
    592540  std::list<IniSection>::iterator section = this->currentSection;
    593   if (sectionName == NULL)
     541  if (sectionName.empty())
    594542    return this->currentSection;
    595543  else
    596544    for (section = this->sections.begin(); section != this->sections.end(); section++)
    597       if (!strcmp((*section).name, sectionName))
     545      if ((*section).name == sectionName)
    598546        break;
    599547  return section;
     
    606554 * @param sectionName the Name of the Section to get the Iterator from
    607555 */
    608 std::list<IniParser::IniEntry>::const_iterator IniParser::getEntryIT(const char* entryName, const char* sectionName) const
    609 {
    610   if (entryName == NULL)
     556std::list<IniParser::IniEntry>::const_iterator IniParser::getEntryIT(const std::string& entryName, const std::string& sectionName) const
     557{
     558  if (entryName.empty())
    611559    return this->currentEntry;
    612560  std::list<IniSection>::const_iterator section = this->getSectionIT(sectionName);
     
    615563  if (section != this->sections.end())
    616564    for (entry = (*section).entries.begin(); entry != (*section).entries.end(); entry++)
    617       if (!strcmp((*entry).name, entryName))
     565      if ((*entry).name == entryName)
    618566        break;
    619567  if (entry == (*section).entries.end())
     
    629577 * @param sectionName the Name of the Section to get the Iterator from
    630578 */
    631 std::list<IniParser::IniEntry>::iterator IniParser::getEntryIT(const char* entryName, const char* sectionName)
    632 {
    633   if (entryName == NULL)
     579std::list<IniParser::IniEntry>::iterator IniParser::getEntryIT(const std::string& entryName, const std::string& sectionName)
     580{
     581  if (entryName.empty())
    634582    return this->currentEntry;
    635583  std::list<IniSection>::iterator section = this->getSectionIT(sectionName);
     
    638586  if (section != this->sections.end())
    639587    for (entry = (*section).entries.begin(); entry != (*section).entries.end(); entry++)
    640       if (!strcmp((*entry).name, entryName))
     588      if ((*entry).name == entryName)
    641589        break;
    642590  if (entry == (*section).entries.end())
     
    652600void IniParser::setFileComment()
    653601{
    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 = "";
    659605    return;
    660606  }
    661607
    662   unsigned int stringLength = 1;
    663608  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
    669610  while (!this->commentList.empty())
    670611  {
    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();
    675615    this->commentList.pop_front();
    676616  }
     
    682622void IniParser::setSectionComment()
    683623{
    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())
    689627    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
    699629  while (!this->commentList.empty())
    700630  {
    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();
    705634    this->commentList.pop_front();
    706635  }
     
    712641void IniParser::setEntryComment()
    713642{
    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())
    719645    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
    729647  while (!this->commentList.empty())
    730648  {
    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();
    735652    this->commentList.pop_front();
    736653  }
    737 
    738654}
    739655
     
    744660void IniParser::debug() const
    745661{
    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())
    751667  {
    752668    std::list<IniSection>::const_iterator section;
    753669    for (section = this->sections.begin(); section != this->sections.end(); section++)
    754670    {
    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());
    758674
    759675      std::list<IniEntry>::const_iterator entry;
    760676      for (entry = (*section).entries.begin(); entry != (*section).entries.end(); entry++)
    761677      {
    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());
    765681      }
    766682    }
  • trunk/src/lib/parser/ini_parser/ini_parser.h

    r6981 r7221  
    1515
    1616#include <list>
     17#include <string>
    1718
    1819//! ini-file parser
     
    2728    struct IniEntry
    2829    {
    29       char*               comment;  //!< A Comment that is appendet to the Top of this Entry.
    30       char*               name;     //!< name of a given Entry
    31       char*               value;    //!< value of a given Entry
     30      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
    3233    };
    3334
     
    3536    struct IniSection
    3637    {
    37       char*               comment;  //!< A Comment that is appendet to the Top of this Section.
    38       char*               name;     //!< name of a given section
     38      std::string         comment;  //!< A Comment that is appendet to the Top of this Section.
     39      std::string         name;     //!< name of a given section
    3940      std::list<IniEntry> entries;  //!< a list of entries for this section
    4041    };
     
    4243
    4344  public:
    44     IniParser (const char* filename = NULL);
     45    IniParser (const std::string& filename = "");
    4546    virtual ~IniParser ();
    4647
    4748    /** @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; };
    4950    /** @returns the fileName we have opened. */
    50     const char* getFileName() const { return this->fileName; };
     51    const std::string& getFileName() const { return this->fileName; };
    5152
    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;
    5455
    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; };
    5758
    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;
    6263
    6364    // iterate through sections with these Functions
    6465    void firstSection();
    65     const char* nextSection();
     66    const std::string& nextSection();
    6667
    6768
    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;
    7273
    7374    // iterate Through Variables with these Functions.
     
    7778
    7879    // 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;
    8283
    8384
     
    8889  private:
    8990    void deleteSections();
    90     void setFileName(const char* fileName);
     91    void setFileName(const std::string& fileName);
    9192
    9293    void setFileComment();
     
    9495    void setEntryComment();
    9596
    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);
    9899
    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 = "");
    101102
    102103  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.
    105106    std::list<IniSection>            sections;        //!< a list of all stored Sections of the Parser
    106107    std::list<IniSection>::iterator  currentSection;  //!< the current selected Section
    107108    std::list<IniEntry>::iterator    currentEntry;    //!< the current selected entry (in currentSection)
    108109
    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;
    110114};
    111115
  • trunk/src/lib/parser/tinyxml/tinyxml.h

    r5819 r7221  
    2626#ifndef TINYXML_INCLUDED
    2727#define TINYXML_INCLUDED
     28
     29#define TIXML_USE_STL
    2830
    2931#ifdef _MSC_VER
     
    8183                #define TIXML_SNSCANF  snscanf
    8284        #endif
    83 #endif 
     85#endif
    8486
    8587class TiXmlDocument;
     
    9698const int TIXML_PATCH_VERSION = 2;
    9799
    98 /*      Internal structure for tracking location of items 
     100/*      Internal structure for tracking location of items
    99101        in the XML file.
    100102*/
     
    110112
    111113// Only used by Attribute::Query functions
    112 enum 
    113 { 
     114enum
     115{
    114116        TIXML_SUCCESS,
    115117        TIXML_NO_ATTRIBUTE,
     
    162164        /**     All TinyXml classes can print themselves to a filestream.
    163165                This is a formatted print, and will insert tabs and newlines.
    164                
     166
    165167                (For an unformatted stream, use the << operator.)
    166168        */
     
    206208        static const int utf8ByteTable[256];
    207209
    208         virtual const char* Parse(      const char* p, 
    209                                                                 TiXmlParsingData* data, 
     210        virtual const char* Parse(      const char* p,
     211                                                                TiXmlParsingData* data,
    210212                                                                TiXmlEncoding encoding /*= TIXML_ENCODING_UNKNOWN */ ) = 0;
    211213
     
    245247
    246248        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' );
    250252        }
    251253
     
    334336    /// Field containing a generic user pointer
    335337        void*                   userData;
    336        
     338
    337339        // None of these methods are reliable for any language except English.
    338340        // Good for approximation, not great for accuracy.
     
    386388
    387389public:
    388         #ifdef TIXML_USE_STL   
     390        #ifdef TIXML_USE_STL
    389391
    390392            /** An input stream operator, for every class. Tolerant of newlines and
     
    400402                    a node to a stream is very well defined. You'll get a nice stream
    401403                    of output, without any extra whitespace or newlines.
    402                    
     404
    403405                    But reading is not as well defined. (As it always is.) If you create
    404406                    a TiXmlElement (for example) and read that from an input stream,
     
    408410                    A TiXmlDocument will read nodes until it reads a root element, and
    409411                        all the children of that root element.
    410             */ 
     412            */
    411413            friend std::ostream& operator<< (std::ostream& out, const TiXmlNode& base);
    412414
     
    470472    #ifdef TIXML_USE_STL
    471473        /// STL std::string form.
    472         void SetValue( const std::string& _value )   
    473         {         
     474        void SetValue( const std::string& _value )
     475        {
    474476                StringToBuffer buf( _value );
    475                 SetValue( buf.buffer ? buf.buffer : "" );       
    476         }       
     477                SetValue( buf.buffer ? buf.buffer : "" );
     478        }
    477479        #endif
    478480
     
    492494        TiXmlNode* LastChild()  { return lastChild; }
    493495        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 );
    495497
    496498    #ifdef TIXML_USE_STL
     
    649651
    650652        /** Create an exact duplicate of this node and return it. The memory must be deleted
    651                 by the caller. 
     653                by the caller.
    652654        */
    653655        virtual TiXmlNode* Clone() const = 0;
     
    731733        /** QueryIntValue examines the value string. It is an alternative to the
    732734                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
    734736                the call returns TIXML_SUCCESS. If it is not
    735737                an integer, it returns TIXML_WRONG_TYPE.
     
    750752    #ifdef TIXML_USE_STL
    751753        /// STL std::string form.
    752         void SetName( const std::string& _name )       
    753         {       
     754        void SetName( const std::string& _name )
     755        {
    754756                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        {
    760762                StringToBuffer buf( _value );
    761                 SetValue( buf.buffer ? buf.buffer : "error" ); 
     763                SetValue( buf.buffer ? buf.buffer : "error" );
    762764        }
    763765        #endif
     
    801803/*      A class used to manage a group of attributes.
    802804        It is only used internally, both by the ELEMENT and the DECLARATION.
    803        
     805
    804806        The set can be changed transparent to the Element and Declaration
    805807        classes that use it, but NOT transparent to the Attribute
     
    882884        /** QueryIntAttribute examines the attribute - it is an alternative to the
    883885                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
    885887                the call returns TIXML_SUCCESS. If it is not
    886888                an integer, it returns TIXML_WRONG_TYPE. If the attribute
    887889                does not exist, then TIXML_NO_ATTRIBUTE is returned.
    888         */     
     890        */
    889891        int QueryIntAttribute( const char* name, int* _value ) const;
    890892        /// QueryDoubleAttribute examines the attribute - see QueryIntAttribute().
     
    913915
    914916        /// 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        {
    917919                StringToBuffer n( name );
    918920                StringToBuffer v( _value );
    919921                if ( n.buffer && v.buffer )
    920                         SetAttribute (n.buffer, v.buffer );     
    921         }       
     922                        SetAttribute (n.buffer, v.buffer );
     923        }
    922924        ///< STL std::string form.
    923         void SetAttribute( const std::string& name, int _value )       
    924         {       
     925        void SetAttribute( const std::string& name, int _value )
     926        {
    925927                StringToBuffer n( name );
    926928                if ( n.buffer )
    927                         SetAttribute (n.buffer, _value);       
    928         }       
     929                        SetAttribute (n.buffer, _value);
     930        }
    929931        #endif
    930932
     
    954956                and concise, GetText() is limited compared to getting the TiXmlText child
    955957                and accessing it directly.
    956        
     958
    957959                If the first child of 'this' is a TiXmlText, the GetText()
    958960                returns the character string of the Text node, else null is returned.
     
    964966                @endverbatim
    965967
    966                 'str' will be a pointer to "This is text". 
    967                
     968                'str' will be a pointer to "This is text".
     969
    968970                Note that this function can be misleading. If the element foo was created from
    969971                this XML:
    970972                @verbatim
    971                 <foo><b>This is text</b></foo> 
     973                <foo><b>This is text</b></foo>
    972974                @endverbatim
    973975
     
    975977                another element. From this XML:
    976978                @verbatim
    977                 <foo>This is <b>text</b></foo> 
     979                <foo>This is <b>text</b></foo>
    978980                @endverbatim
    979981                GetText() will return "This is ".
    980982
    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
    983985                                 safe type casts on the referenced node.
    984986        */
     
    10541056
    10551057
    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
    10571059        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
    10591061        SetCDATA() and query it with CDATA().
    10601062*/
     
    10631065        friend class TiXmlElement;
    10641066public:
    1065         /** Constructor for text element. By default, it is treated as 
     1067        /** Constructor for text element. By default, it is treated as
    10661068                normal, encoded text. If you want it be output as a CDATA text
    10671069                element, set the parameter _cdata to 'true'
     
    12791281                - The ErrorDesc() method will return the name of the error. (very useful)
    12801282                - The ErrorRow() and ErrorCol() will return the location of the error (if known)
    1281         */     
     1283        */
    12821284        bool Error() const                                              { return error; }
    12831285
     
    12901292        int ErrorId()   const                           { return errorId; }
    12911293
    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,
    12931295                and the first row is row 1. A value of 0 means the row and column wasn't applicable
    12941296                (memory errors, for example, have no row/column) or the parser lost the error. (An
     
    13031305                to report the correct values for row and column. It does not change the output
    13041306                or input in any way.
    1305                
     1307
    13061308                By calling this method, with a tab size
    13071309                greater than 0, the row and column of each node and attribute is stored
     
    13311333                state is automatically cleared if you Parse a new XML block.
    13321334        */
    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;
    13381340                                                                                        }
    13391341
     
    13811383        @endverbatim
    13821384
    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
    13841386        easy to write a *lot* of code that looks like:
    13851387
     
    14011403
    14021404        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
    14041406        and correct to use:
    14051407
     
    14221424
    14231425        @verbatim
    1424         int i=0; 
     1426        int i=0;
    14251427        while ( true )
    14261428        {
     
    14331435        @endverbatim
    14341436
    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
    14371439        to. Instead, prefer:
    14381440
     
    14641466        TiXmlHandle FirstChildElement( const char * value ) const;
    14651467
    1466         /** Return a handle to the "index" child with the given name. 
     1468        /** Return a handle to the "index" child with the given name.
    14671469                The first child is 0, the second 1, etc.
    14681470        */
    14691471        TiXmlHandle Child( const char* value, int index ) const;
    1470         /** Return a handle to the "index" child. 
     1472        /** Return a handle to the "index" child.
    14711473                The first child is 0, the second 1, etc.
    14721474        */
    14731475        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.
    14751477                The first child element is 0, the second 1, etc. Note that only TiXmlElements
    14761478                are indexed: other types are not counted.
    14771479        */
    14781480        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.
    14801482                The first child element is 0, the second 1, etc. Note that only TiXmlElements
    14811483                are indexed: other types are not counted.
     
    14921494
    14931495        /// Return the handle as a TiXmlNode. This may return null.
    1494         TiXmlNode* Node() const                 { return node; } 
     1496        TiXmlNode* Node() const                 { return node; }
    14951497        /// Return the handle as a TiXmlElement. This may return null.
    14961498        TiXmlElement* Element() const   { return ( ( node && node->ToElement() ) ? node->ToElement() : 0 ); }
Note: See TracChangeset for help on using the changeset viewer.