Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 5933 in orxonox.OLD for trunk/src/lib/util


Ignore:
Timestamp:
Dec 6, 2005, 12:33:18 AM (19 years ago)
Author:
bensch
Message:

orxonox/trunk: sync (not running)

Location:
trunk/src/lib/util
Files:
2 edited

Legend:

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

    r5319 r5933  
    2121#include "ini_parser.h"
    2222
    23 #include "list.h"
    2423#include <stdlib.h>
    2524#include <string.h>
    26 #include "debug.h"
     25
     26#ifdef DEBUG
     27 #include "debug.h"
     28#else
     29 #define PRINTF(x) printf
     30#endif
    2731
    2832using namespace std;
     
    3135 *  constructs an IniParser using a file
    3236 * @param fileName: the path and name of the file to parse
    33 */
     37 */
    3438IniParser::IniParser (const char* fileName)
    3539{
    36   this->currentEntry = NULL;
    37   this->currentSection = NULL;
    38   this->sections = NULL;
    3940  this->fileName = NULL;
     41
    4042  if (fileName != NULL)
    4143    this->readFile(fileName);
    4244}
    4345
     46
    4447/**
    4548 *  removes the IniParser from memory
    46 */
     49 */
    4750IniParser::~IniParser ()
    4851{
    49   deleteSections();
    50 }
     52  this->deleteSections();
     53}
     54
    5155
    5256/**
     
    5559void IniParser::deleteSections()
    5660{
    57   if (this->sections)
    58   {
    59     tIterator<IniSection>* sectionIt = this->sections->getIterator();
    60     IniSection* sectionEnum = sectionIt->firstElement();
    61     while (sectionEnum)
    62     {
    63       tIterator<IniEntry>* entryIt = sectionEnum->entries->getIterator();
    64       IniEntry* entryEnum = entryIt->firstElement();
    65       while (entryEnum)
    66       {
    67         delete []entryEnum->name;
    68         delete []entryEnum->value;
    69         delete entryEnum;
    70         entryEnum = entryIt->nextElement();
    71       }
    72       delete entryIt;
    73 
    74       delete []sectionEnum->name;
    75       delete sectionEnum->entries;
    76       delete sectionEnum;
    77       sectionEnum = sectionIt->nextElement();
    78     }
    79     delete sectionIt;
    80   }
    81   delete this->sections;
    82   this->currentEntry = NULL;
    83   this->currentSection = NULL;
    84   this->sections = NULL;
     61  while(!this->sections.empty())
     62  {
     63    IniSection section = this->sections.front();
     64
     65    while(!section.entries.empty())
     66    {
     67      IniEntry entry = section.entries.front();
     68      delete []entry.name;
     69      delete []entry.value;
     70      section.entries.pop_front();
     71    }
     72
     73    delete []section.name;
     74    this->sections.pop_front();
     75  }
     76
     77//  this->currentEntry = NULL;
     78//  this->currentSection = NULL;
    8579  this->setFileName(NULL);
    8680}
     81
    8782
    8883/**
     
    9085 * @param fileName: path and name of the new file to parse
    9186 * @return true on success false otherwise;
    92 */
     87 */
    9388bool IniParser::readFile(const char* fileName)
    9489{
    9590  FILE*    stream;           //!< The stream we use to read the file.
    96   if (sections != NULL)
     91  if (!sections.empty())
    9792    deleteSections();
    9893  if( fileName == NULL)
     
    107102  else
    108103  {
    109     this->currentEntry = NULL;
    110     this->currentSection = NULL;
    111     this->sections = new tList<IniSection>;
     104    this->currentEntry = 0;//this->sections.begin();
     105    this->currentSection = this->sections.begin();
    112106
    113107    /////////////////////////////
     
    169163}
    170164
     165
    171166/**
    172167 * opens a file and writes to it
     
    187182  else
    188183  {
    189     if (this->sections)
    190     {
    191       tIterator<IniSection>* sectionIt = this->sections->getIterator();
    192       IniSection* sectionEnum = sectionIt->firstElement();
    193       while (sectionEnum)
     184    if (!this->sections.empty())
     185    {
     186      std::list<IniSection>::iterator section;
     187      for (section = this->sections.begin(); section != this->sections.end(); section++)
    194188      {
    195         fprintf(stream, "\n [%s]\n", sectionEnum->name);
    196 
    197         tIterator<IniEntry>* entryIt = sectionEnum->entries->getIterator();
    198         IniEntry* entryEnum = entryIt->firstElement();
    199         while (entryEnum)
    200         {
    201           fprintf(stream, "   %s = %s\n", entryEnum->name, entryEnum->value);
    202 
    203           entryEnum = entryIt->nextElement();
    204         }
    205         delete entryIt;
    206 
    207         sectionEnum = sectionIt->nextElement();
     189        fprintf(stream, "\n [%s]\n", (*section).name);
     190
     191        std::list<IniEntry>::iterator entry;
     192        for (entry = (*section).entries.begin(); entry != (*section).entries.end(); entry++)
     193          fprintf(stream, "   %s = %s\n", (*entry).name, (*entry).value);
    208194      }
    209       delete sectionIt;
    210195    }
    211196    else
     
    214199  fclose(stream);
    215200}
     201
    216202
    217203/**
     
    223209bool IniParser::addSection(const char* sectionName)
    224210{
    225   if (this->sections == NULL)
    226     this->sections = new tList<IniSection>;
    227 
    228   IniSection* newSection = new IniSection;
    229   newSection->name = new char[strlen(sectionName)+1];
    230   strcpy(newSection->name, sectionName);
    231   newSection->entries = new tList<IniEntry>;
    232   this->currentSection = newSection;
    233   this->sections->add(newSection);
     211  this->sections.push_back(IniSection());
     212
     213  this->sections.back().name = new char[strlen(sectionName)+1];
     214  strcpy(this->sections.back().name, sectionName);
     215
     216  this->currentSection = --this->sections.end();
    234217  PRINTF(5)("Added Section %s\n", sectionName);
    235218  return true;
    236219}
     220
    237221
    238222/**
     
    243227bool IniParser::getSection( const char* sectionName)
    244228{
    245   tIterator<IniSection>* sectionIt = this->sections->getIterator();
    246   IniSection* sectionEnum = sectionIt->firstElement();
    247   while (sectionEnum)
    248   {
    249     if (!strcmp(sectionEnum->name, sectionName))
    250     {
    251       this->currentSection = sectionEnum;
    252       this->currentEntry = NULL;
    253       delete sectionIt;
     229  std::list<IniSection>::iterator section;
     230  for (section = this->sections.begin(); section != this->sections.end(); section++)
     231  {
     232    if (!strcmp((*section).name, sectionName))
     233    {
     234      this->currentSection = section;
     235      this->currentEntry = (*section).entries.begin();
    254236      return true;
    255237    }
    256 
    257     sectionEnum = sectionIt->nextElement();
    258   }
    259   delete sectionIt;
     238  }
    260239  return false;
    261240}
    262241
     242
    263243/**
    264244 * moves to the first section
     
    266246void IniParser::getFirstSection()
    267247{
    268   if (this->sections)
    269     this->currentSection = this->sections->firstElement();
    270   else
    271     this->currentSection = NULL;
    272   this->currentEntry = NULL;
    273 }
     248  this->currentSection = this->sections.begin();
     249  this->currentEntry = (*this->currentSection).entries.begin();
     250}
     251
    274252
    275253/**
     
    279257const char* IniParser::nextSection()
    280258{
    281   if (this->currentSection == NULL)
     259  if (this->currentSection == this->sections.end())
    282260    return NULL;
    283261  else
    284262  {
    285     if (this->sections)
    286     {
    287       if (this->currentSection == this->sections->lastElement())
    288         this->currentSection = NULL;
    289       else
    290         this->currentSection = this->sections->nextElement(this->currentSection);
    291     }
     263    if (this->currentSection == this->sections.end())
     264      return NULL;
     265    else
     266      this->currentSection++;
    292267  }
    293268
     
    298273}
    299274
     275
    300276/**
    301277 * moves to the first Variable of the current Section
     
    303279void IniParser::getFirstVar()
    304280{
    305   if (this->currentSection)
    306     this->currentEntry = this->currentSection->entries->firstElement();
    307   else
    308     this->currentEntry = NULL;
    309 }
     281  if (this->currentSection != this->sections.end())
     282    this->currentEntry = (*this->currentSection).entries.begin();
     283//  else
     284//    this->currentEntry = NULL;
     285}
     286
    310287
    311288/**
     
    317294  if (this->currentSection == NULL
    318295      || this->currentEntry == NULL
    319       || this->currentEntry == this->currentSection->entries->lastElement())
     296      || this->currentEntry == (*this->currentSection).entries.end())
    320297  {
    321298    this->currentEntry = NULL;
    322299    return false;
    323300  }
    324   this->currentEntry = this->currentSection->entries->nextElement(this->currentEntry);
     301  this->currentEntry++;
    325302
    326303  if (this->currentEntry == NULL)
     
    329306    return true;
    330307}
     308
    331309
    332310/**
     
    341319bool IniParser::addVar(const char* entryName, const char* value, const char* sectionName)
    342320{
    343   IniSection* addSection = NULL;
     321  std::list<IniSection>::iterator section = this->sections.end();
     322
    344323  if (sectionName != NULL)
    345324  {
    346     tIterator<IniSection>* sectionIt = this->sections->getIterator();
    347     IniSection* sectionEnum = sectionIt->firstElement();
    348     while (sectionEnum)
    349     {
    350       if (!strcmp(sectionEnum->name, sectionName))
    351       {
    352         addSection = sectionEnum;
     325    for (section = this->sections.begin(); section != this->sections.end(); section++)
     326      if (!strcmp((*section).name, sectionName))
    353327        break;
    354       }
    355       sectionEnum = sectionIt->nextElement();
    356     }
    357     delete sectionIt;
    358   }
    359   else
    360     addSection = this->currentSection;
    361 
    362   if (addSection == NULL)
     328  }
     329  else
     330    section = this->currentSection;
     331
     332  if (section == this->sections.end())
    363333  {
    364334    PRINTF(2)("section not found for value %s\n", entryName);
     
    367337  else
    368338  {
    369     IniEntry* newEntry = new IniEntry;
    370     newEntry->name = new char[strlen (entryName)+1];
    371     strcpy(newEntry->name, entryName);
    372     newEntry->value = new char[strlen(value)+1];
    373     strcpy(newEntry->value, value);
    374     this->currentSection->entries->add(newEntry);
    375     PRINTF(5)("Added Entry %s with Value '%s' to Section %s\n", newEntry->name, newEntry->name, addSection->name);
     339    (*section).entries.push_back(IniEntry());
     340    (*section).entries.back().name = new char[strlen (entryName)+1];
     341    strcpy((*section).entries.back().name, entryName);
     342    (*section).entries.back().value = new char[strlen(value)+1];
     343    strcpy((*section).entries.back().value, value);
     344    PRINTF(5)("Added Entry %s with Value '%s' to Section %s\n", entryName, value, (*section).name);
    376345    return true;
    377346  }
    378347}
     348
    379349
    380350/**
     
    390360const char* IniParser::getVar(const char* entryName, const char* sectionName, const char* defaultValue) const
    391361{
    392   if (this->sections)
    393   {
    394     tIterator<IniSection>* sectionIt = this->sections->getIterator();
    395     IniSection* sectionEnum = sectionIt->firstElement();
    396     while (sectionEnum)
    397     {
    398       if (!strcmp(sectionEnum->name, sectionName))
     362  if (fileName != NULL)
     363  {
     364    std::list<IniSection>::const_iterator section;
     365    for (section = this->sections.begin(); section != this->sections.end(); section++)
     366    {
     367      if (!strcmp((*section).name, sectionName))
    399368      {
    400         tIterator<IniEntry>* entryIt = sectionEnum->entries->getIterator();
    401         IniEntry* entryEnum = entryIt->firstElement();
    402         while (entryEnum)
    403         {
    404           if (!strcmp(entryEnum->name, entryName))
    405           {
    406             delete entryIt;
    407             delete sectionIt;
    408             return entryEnum->value;
    409           }
    410           entryEnum = entryIt->nextElement();
    411         }
    412          delete entryIt;
    413          PRINTF(2)("Entry %s in section %s not found.\n", entryName, sectionName);
    414          break;
     369        std::list<IniEntry>::const_iterator entry;
     370        for (entry = (*section).entries.begin(); entry != (*section).entries.end(); entry++)
     371          if (!strcmp((*entry).name, entryName))
     372            return (*entry).value;
     373        PRINTF(2)("Entry %s in section %s not found.\n", entryName, sectionName);
     374        break;
    415375      }
    416       sectionEnum = sectionIt->nextElement();
    417     }
    418     delete sectionIt;
     376    }
    419377    PRINTF(2)("Section %s that should be containing %s not found.\n", sectionName, entryName);
    420378  }
     
    447405{
    448406  PRINTF(0)("Iniparser %s - debug\n", this->fileName);
    449   if (this->sections)
    450   {
    451     tIterator<IniSection>* sectionIt = this->sections->getIterator();
    452     IniSection* sectionEnum = sectionIt->firstElement();
    453     while (sectionEnum)
    454     {
    455       PRINTF(0)(" [%s]\n", sectionEnum->name);
    456 
    457       tIterator<IniEntry>* entryIt = sectionEnum->entries->getIterator();
    458       IniEntry* entryEnum = entryIt->firstElement();
    459       while (entryEnum)
    460       {
    461         PRINTF(0)("   :%s: -> '%s'\n", entryEnum->name, entryEnum->value);
    462 
    463         entryEnum = entryIt->nextElement();
    464       }
    465       delete entryIt;
    466 
    467       sectionEnum = sectionIt->nextElement();
    468     }
    469     delete sectionIt;
    470   }
    471   else
    472     PRINTF(1)("%s not opened\n", fileName);
    473 }
     407  if (this->fileName != NULL)
     408  {
     409    std::list<IniSection>::const_iterator section;
     410    for (section = this->sections.begin(); section != this->sections.end(); section++)
     411    {
     412      PRINTF(0)(" [%s]\n", (*section).name);
     413
     414      std::list<IniEntry>::const_iterator entry;
     415      for (entry = (*section).entries.begin(); entry != (*section).entries.end(); entry++)
     416        PRINTF(0)("   '%s' -> '%s'\n", (*entry).name, (*entry).value);
     417    }
     418  }
     419  else
     420    PRINTF(1)("no opened ini-file.\n");
     421}
  • trunk/src/lib/util/ini_parser.h

    r5405 r5933  
    1111#define PARSELINELENGHT     512       //!< how many chars to read at once
    1212#ifndef NULL
    13 #define NULL 0x0                      //!< NULL
     13 #define NULL 0x0                     //!< NULL
    1414#endif
    1515
    16 // FORWARD DECLARATION //
    17 template<class T> class tList;
     16#include <list>
    1817
    1918//! ini-file parser
     
    3534    {
    3635      char*               name;    //!< name of a given section
    37       tList<IniEntry>*    entries; //!< a list of entries for this section
     36      std::list<IniEntry> entries; //!< a list of entries for this section
    3837    };
    3938    ////////////////////////////////////
     
    5352
    5453    /** @returns true if the file is opened, false otherwise*/
    55     bool isOpen() const { return (sections != NULL)?true:false; };
     54    bool isOpen() const { return (this->fileName != NULL)? true : false; };
    5655    /** @returns the fileName we have opened. */
    5756    const char* getFileName() const { return this->fileName; };
     
    7776
    7877  private:
    79     char*                 fileName;        //!< The name of the File that was parsed.
    80     tList<IniSection>*    sections;        //!< a list of all stored Sections of the Parser
    81     IniSection*           currentSection;  //!< the current selected Section
    82     IniEntry*             currentEntry;    //!< the current selected entry (in currentSection)
     78    char*                            fileName;        //!< The name of the File that was parsed.
     79    std::list<IniSection>            sections;        //!< a list of all stored Sections of the Parser
     80    std::list<IniSection>::iterator  currentSection;  //!< the current selected Section
     81    std::list<IniEntry>::iterator    currentEntry;    //!< the current selected entry (in currentSection)
    8382};
    8483
Note: See TracChangeset for help on using the changeset viewer.