Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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


Ignore:
Timestamp:
Aug 14, 2005, 11:44:49 PM (19 years ago)
Author:
bensch
Message:

orxonox/trunk: IniParser: added the ability to write to files

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

Legend:

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

    r5019 r5020  
    3434{
    3535  this->setClassID(CL_INI_PARSER, "IniParser");
    36   this->setName(fileName);
    3736
    3837  this->currentEntry = NULL;
     
    8483  this->currentSection = NULL;
    8584  this->sections = NULL;
     85  this->setName(NULL);
    8686}
    8787
     
    9898  if( fileName == NULL)
    9999    return false;
    100 
    101   printf("1\n");
     100  this->setName(fileName);
     101
    102102  if( (stream = fopen (fileName, "r")) == NULL)
    103103  {
     
    107107  else
    108108  {
    109     printf("2\n");
    110109    this->currentEntry = NULL;
    111110    this->currentSection = NULL;
     
    134133        {
    135134          *ptr = 0;
    136           IniSection* newSection = new IniSection;
    137           newSection->name = new char[strlen(buffer)+1];
    138           strcpy(newSection->name, buffer);
    139           newSection->entries = new tList<IniEntry>;
    140           this->currentSection = newSection;
    141           this->sections->add(newSection);
     135          this->addSection(buffer);
    142136        }
    143137      }
     
    163157        nameEnd[1] = '\0';
    164158
    165         IniEntry* newEntry = new IniEntry;
    166         newEntry->value = new char[strlen(valueBegin)+1];
    167         strcpy(newEntry->value, valueBegin);
    168         newEntry->name = new char[strlen (nameBegin)+1];
    169         strcpy(newEntry->name, nameBegin);
    170 
    171         this->currentSection->entries->add(newEntry);
     159        this->addVar(nameBegin, valueBegin);
    172160      }
    173161    }
     
    175163  fclose(stream);
    176164  return true;
     165}
     166
     167/**
     168 * opens a file and writes to it
     169 * @param fileName: path and name of the new file to write to
     170 * @return true on success false otherwise
     171 */
     172bool IniParser::writeFile(const char* fileName)
     173{
     174  FILE*    stream;           //!< The stream we use to read the file.
     175  if (sections != NULL)
     176    deleteSections();
     177  if( fileName == NULL)
     178    return false;
     179
     180  if( (stream = fopen (fileName, "w")) == NULL)
     181  {
     182    PRINTF(1)("IniParser could not open %s\n", fileName);
     183    return false;
     184  }
     185  else
     186  {
     187    if (this->sections)
     188    {
     189      tIterator<IniSection>* sectionIt = this->sections->getIterator();
     190      IniSection* sectionEnum = sectionIt->nextElement();
     191      while (sectionEnum)
     192      {
     193        fprintf(stream, " [%s]\n", sectionEnum->name);
     194
     195        tIterator<IniEntry>* entryIt = sectionEnum->entries->getIterator();
     196        IniEntry* entryEnum = entryIt->nextElement();
     197        while (entryEnum)
     198        {
     199          fprintf(stream, " %s = %s\n", entryEnum->name, entryEnum->value);
     200
     201          entryEnum = entryIt->nextElement();
     202        }
     203        delete entryIt;
     204
     205        sectionEnum = sectionIt->nextElement();
     206      }
     207      delete sectionIt;
     208    }
     209    else
     210      PRINTF(1)("%s no sections defined yet\n", fileName);
     211  }
     212  fclose(stream);
     213}
     214
     215bool IniParser::addSection(const char* sectionName)
     216{
     217  if (this->sections == NULL)
     218    this->sections = new tList<IniSection>;
     219
     220  IniSection* newSection = new IniSection;
     221  newSection->name = new char[strlen(sectionName)+1];
     222  strcpy(newSection->name, sectionName);
     223  newSection->entries = new tList<IniEntry>;
     224  this->currentSection = newSection;
     225  this->sections->add(newSection);
    177226}
    178227
     
    269318  else
    270319    return true;
     320}
     321
     322/**
     323 * adds a new Entry to either the currentSection or the section called by sectionName
     324 * @param entryName the Name of the Entry to add
     325 * @param value the value to assign to this entry
     326 * @param sectionName if NULL then this entry will be set to the currentSection
     327 * otherwise to the section refered to by sectionName.
     328 * If both are NULL no entry will be added
     329 * @return true if everything is ok false on error
     330 */
     331bool IniParser::addVar(const char* entryName, const char* value, const char* sectionName)
     332{
     333  IniSection* addSection = NULL;
     334  if (sectionName != NULL)
     335  {
     336    tIterator<IniSection>* sectionIt = this->sections->getIterator();
     337    IniSection* sectionEnum = sectionIt->nextElement();
     338    while (sectionEnum)
     339    {
     340      if (!strcmp(sectionEnum->name, sectionName))
     341      {
     342        addSection = sectionEnum;
     343        break;
     344      }
     345      sectionEnum = sectionIt->nextElement();
     346    }
     347    delete sectionIt;
     348  }
     349  else
     350    addSection = this->currentSection;
     351
     352  if (addSection == NULL)
     353  {
     354    PRINTF(2)("section not found for value %s\n", entryName);
     355    return false;
     356  }
     357  else
     358  {
     359    IniEntry* newEntry = new IniEntry;
     360    newEntry->name = new char[strlen (entryName)+1];
     361    strcpy(newEntry->name, entryName);
     362    newEntry->value = new char[strlen(value)+1];
     363    strcpy(newEntry->value, value);
     364    this->currentSection->entries->add(newEntry);
     365    return true;
     366  }
    271367}
    272368
     
    277373 * @param defaultValue: what should be returned in case the entry cannot be found
    278374 * @return a pointer to a buffer conatining the value of the specified entry. This buffer will contain the data specified in defvalue in case the entry wasn't found
    279 
    280    The returned pointer points to an internal buffer, so do not free it on your own. Do not give a NULL pointer to defvalue, this will certainly
    281    lead to unwanted behaviour.
     375 *
     376 *  The returned pointer points to an internal buffer, so do not free it on your own. Do not give a NULL pointer to defvalue, this will certainly
     377 * lead to unwanted behaviour.
    282378*/
    283379const char* IniParser::getVar(const char* entryName, const char* sectionName, const char* defaultValue) const
  • orxonox/trunk/src/lib/util/ini_parser.h

    r5018 r5020  
    4242    ~IniParser ();
    4343
    44     bool readFile(const char* name);
     44    bool readFile(const char* fileName);
     45    bool writeFile(const char* fileName);
    4546
     47    bool addSection(const char* sectionName);
    4648    bool getSection(const char* sectionName);
    4749
     
    5254    bool isOpen() const { return (sections != NULL)?true:false; };
    5355
     56    bool addVar(const char* entryName, const char* value, const char* sectionName = NULL);
    5457    const char* getVar(const char* entryName, const char* sectionName, const char* defaultValue = "") const;
    5558
Note: See TracChangeset for help on using the changeset viewer.