Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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


Ignore:
Timestamp:
Dec 6, 2005, 1:23:17 AM (19 years ago)
Author:
bensch
Message:

orxonox/trunk: cleanup in ini-parser

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

Legend:

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

    r5933 r5934  
    3333
    3434/**
    35  * constructs an IniParser using a file
     35 * @brief constructs an IniParser using a file
    3636 * @param fileName: the path and name of the file to parse
    3737 */
     
    3939{
    4040  this->fileName = NULL;
     41
    4142
    4243  if (fileName != NULL)
     
    4647
    4748/**
    48  * removes the IniParser from memory
     49 * @brief removes the IniParser from memory
    4950 */
    5051IniParser::~IniParser ()
     
    5556
    5657/**
    57  * removes all the sections. This is like delete, but even cooler :)
     58 * @brief removes all the sections. This is like delete, but even cooler :)
    5859 */
    5960void IniParser::deleteSections()
    6061{
     62  // in all sections
    6163  while(!this->sections.empty())
    6264  {
     65   
    6366    IniSection section = this->sections.front();
    64 
     67   
     68    // in all entries of the sections
    6569    while(!section.entries.empty())
    6670    {
     71      // delete all strings of entries.
    6772      IniEntry entry = section.entries.front();
    6873      delete []entry.name;
     
    7075      section.entries.pop_front();
    7176    }
    72 
     77    // delete all Sections
    7378    delete []section.name;
    7479    this->sections.pop_front();
    7580  }
    76 
    77 //  this->currentEntry = NULL;
    78 //  this->currentSection = NULL;
    7981  this->setFileName(NULL);
    8082}
     
    8284
    8385/**
    84  * opens another file to parse
     86 * @brief sets the Name of the input-file
     87 * @param fileName The new FileName to set to the IniParser
     88 * IF fileName is NULL the new Name will be set to NULL too.
     89 */
     90void IniParser::setFileName(const char* fileName)
     91{
     92  if (this->fileName)
     93    delete []this->fileName;
     94  if (fileName)
     95  {
     96    this->fileName = new char[strlen(fileName)+1];
     97    strcpy(this->fileName, fileName);
     98  }
     99  else
     100    this->fileName = NULL;
     101}
     102
     103
     104/**
     105 * @brief opens a file to parse
    85106 * @param fileName: path and name of the new file to parse
    86107 * @return true on success false otherwise;
     108 *
     109 * If there was already an opened file, the file will be closed,
     110 * and the new one will be opened.
    87111 */
    88112bool IniParser::readFile(const char* fileName)
    89113{
    90   FILE*    stream;           //!< The stream we use to read the file.
    91   if (!sections.empty())
    92     deleteSections();
     114  FILE*    stream;           //< The stream we use to read the file.
     115
     116  if (this->fileName != NULL)
     117    this->deleteSections();
    93118  if( fileName == NULL)
    94119    return false;
    95   this->setFileName(fileName);
    96120
    97121  if( (stream = fopen (fileName, "r")) == NULL)
     
    102126  else
    103127  {
    104     this->currentEntry = 0;//this->sections.begin();
    105     this->currentSection = this->sections.begin();
     128    this->setFileName(fileName);
    106129
    107130    /////////////////////////////
     
    159182    }
    160183  }
     184  this->currentSection = this->sections.begin();
     185  if (!this->sections.empty())
     186    this->currentEntry = (*this->currentSection).entries.begin();
     187
    161188  fclose(stream);
    162189  return true;
     
    165192
    166193/**
    167  * opens a file and writes to it
     194 * @brief opens a file and writes to it
    168195 * @param fileName: path and name of the new file to write to
    169196 * @return true on success false otherwise
     
    182209  else
    183210  {
    184     if (!this->sections.empty())
    185     {
    186       std::list<IniSection>::iterator section;
    187       for (section = this->sections.begin(); section != this->sections.end(); section++)
     211    std::list<IniSection>::iterator section;
     212    for (section = this->sections.begin(); section != this->sections.end(); section++)
    188213      {
    189214        fprintf(stream, "\n [%s]\n", (*section).name);
    190 
     215       
    191216        std::list<IniEntry>::iterator entry;
    192217        for (entry = (*section).entries.begin(); entry != (*section).entries.end(); entry++)
    193218          fprintf(stream, "   %s = %s\n", (*entry).name, (*entry).value);
    194219      }
    195     }
    196     else
    197       PRINTF(1)("%s no sections defined yet\n", fileName);
    198220  }
    199221  fclose(stream);
     
    202224
    203225/**
    204  * adds a section to the list of Sections,
     226 * @brief adds a section to the list of Sections,
    205227 * if no Section list is availiable, it will create it
    206228 * @param sectionName the Name of the section to add
     
    215237
    216238  this->currentSection = --this->sections.end();
     239  if (!this->sections.empty())
     240    this->currentEntry = (*this->currentSection).entries.begin();
    217241  PRINTF(5)("Added Section %s\n", sectionName);
    218242  return true;
     
    221245
    222246/**
    223  *  set the parsing cursor to the specified section
     247 * @brief Set the parsing cursor to the specified section
    224248 * @param sectionName: the name of the section to set the cursor to
    225249 * @return true on success or false if the section could not be found
     
    229253  std::list<IniSection>::iterator section;
    230254  for (section = this->sections.begin(); section != this->sections.end(); section++)
    231   {
    232255    if (!strcmp((*section).name, sectionName))
    233     {
    234       this->currentSection = section;
    235       this->currentEntry = (*section).entries.begin();
    236       return true;
    237     }
    238   }
     256      {
     257        this->currentSection = section;
     258        this->currentEntry = (*this->currentSection).entries.begin();
     259        return true;
     260      }
     261
    239262  return false;
    240263}
     
    242265
    243266/**
    244  * moves to the first section
     267 * @brief moves to the first section
    245268 */
    246269void IniParser::getFirstSection()
    247270{
    248271  this->currentSection = this->sections.begin();
    249   this->currentEntry = (*this->currentSection).entries.begin();
    250 }
    251 
    252 
    253 /**
    254  * searches the next section
     272  if (!this->sections.empty())
     273    this->currentEntry = (*this->currentSection).entries.begin();
     274}
     275
     276
     277/**
     278 * @brief searches the next section
    255279 * @returns the name of the section if found, NULL otherwise
    256280 */
     
    261285  else
    262286  {
     287    this->currentSection++;
     288  }
    263289    if (this->currentSection == this->sections.end())
    264290      return NULL;
    265     else
    266       this->currentSection++;
    267   }
    268 
    269   if (this->currentSection != NULL)
    270     return this->currentSection->name;
     291
     292  if (this->currentSection != this->sections.end())
     293    {
     294      this->currentEntry = (*this->currentSection).entries.begin();
     295      return this->currentSection->name;
     296    }
    271297  else
    272298    return NULL;
     
    275301
    276302/**
    277  * moves to the first Variable of the current Section
     303 * @brief moves to the first Variable of the current Section
    278304 */
    279305void IniParser::getFirstVar()
     
    281307  if (this->currentSection != this->sections.end())
    282308    this->currentEntry = (*this->currentSection).entries.begin();
    283 //  else
    284 //    this->currentEntry = NULL;
    285 }
    286 
    287 
    288 /**
    289  *  gets the next VarName=VarValue pair from the parsing stream
     309}
     310
     311
     312/**
     313 * @brief gets the next VarName = VarValue pair from the parsing stream
    290314 * @return true on success, false otherwise (in the latter case name and value will be NULL)
    291315 */
    292316bool IniParser::nextVar()
    293317{
    294   if (this->currentSection == NULL
    295       || this->currentEntry == NULL
    296       || this->currentEntry == (*this->currentSection).entries.end())
    297   {
    298     this->currentEntry = NULL;
    299     return false;
    300   }
     318  if ( this->sections.empty()
     319       || this->currentSection == this->sections.end()
     320       || this->currentEntry == (*this->currentSection).entries.end())
     321    return false;
     322
    301323  this->currentEntry++;
    302324
    303   if (this->currentEntry == NULL)
     325  if (this->currentEntry == (*this->currentSection).entries.end())
    304326    return false;
    305327  else
     
    309331
    310332/**
    311  * adds a new Entry to either the currentSection or the section called by sectionName
     333 * @brief adds a new Entry to either the currentSection or the section called by sectionName
    312334 * @param entryName the Name of the Entry to add
    313335 * @param value the value to assign to this entry
     
    319341bool IniParser::addVar(const char* entryName, const char* value, const char* sectionName)
    320342{
    321   std::list<IniSection>::iterator section = this->sections.end();
     343  std::list<IniSection>::iterator section;
    322344
    323345  if (sectionName != NULL)
     
    332354  if (section == this->sections.end())
    333355  {
    334     PRINTF(2)("section not found for value %s\n", entryName);
     356    PRINTF(2)("section '%s' not found for value '%s'\n", sectionName, entryName);
    335357    return false;
    336358  }
     
    349371
    350372/**
    351  * directly acesses an entry in a section
     373 * @brief directly acesses an entry in a section
    352374 * @param entryName: the name of the entry to find
    353375 * @param sectionName: the section where the entry is to be found
     
    363385  {
    364386    std::list<IniSection>::const_iterator section;
    365     for (section = this->sections.begin(); section != this->sections.end(); section++)
    366     {
    367       if (!strcmp((*section).name, sectionName))
     387    if (sectionName != NULL)
    368388      {
    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;
     389
     390        for (section = this->sections.begin(); section != this->sections.end(); section++)
     391          {
     392            if (!strcmp((*section).name, sectionName))
     393              {
     394                break;
     395              }
     396          }
     397        PRINTF(2)("Section %s that should be containing %s not found.\n", sectionName, entryName);
    375398      }
    376     }
    377     PRINTF(2)("Section %s that should be containing %s not found.\n", sectionName, entryName);
    378   }
    379   else
    380     PRINTF(1)("%s not opened\n", fileName);
     399    else
     400      section = this->currentSection;
     401
     402    std::list<IniEntry>::const_iterator entry;
     403    for (entry = (*section).entries.begin(); entry != (*section).entries.end(); entry++)
     404      if (!strcmp((*entry).name, entryName))
     405        return (*entry).value;
     406    PRINTF(2)("Entry '%s' in section '%s' not found.\n", entryName, sectionName);
     407   
     408  }
     409  else
     410    PRINTF(2)("%s not opened\n", fileName);
    381411
    382412  return defaultValue;
     
    384414}
    385415
    386 
    387 void IniParser::setFileName(const char* fileName)
    388 {
    389   if (this->fileName)
    390     delete []this->fileName;
    391   if (fileName)
    392   {
    393     this->fileName = new char[strlen(fileName)+1];
    394     strcpy(this->fileName, fileName);
    395   }
    396   else
    397     this->fileName = NULL;
    398 }
    399 
    400 
    401 /**
    402  * output the whole tree in a nice and easy way.
     416/**
     417 * @brief output the whole tree in a nice and easy way.
    403418 */
    404419void IniParser::debug() const
  • trunk/src/lib/util/ini_parser.h

    r5933 r5934  
    4848    bool getSection(const char* sectionName);
    4949
     50    /** @returns true if the file is opened, false otherwise*/
     51    bool isOpen() const { return (this->fileName != NULL)? true : false; };
     52
    5053    void getFirstSection();
    5154    const char* nextSection();
    5255
    53     /** @returns true if the file is opened, false otherwise*/
    54     bool isOpen() const { return (this->fileName != NULL)? true : false; };
    5556    /** @returns the fileName we have opened. */
    5657    const char* getFileName() const { return this->fileName; };
     
    7172    void debug() const;
    7273
     74
    7375  private:
    7476    void deleteSections();
Note: See TracChangeset for help on using the changeset viewer.