Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 6388 in orxonox.OLD for branches/preferences/src/lib/parser


Ignore:
Timestamp:
Jan 2, 2006, 11:09:42 PM (19 years ago)
Author:
rennerc
Message:

reading and writing inifiles now works

Location:
branches/preferences/src/lib/parser
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • branches/preferences/src/lib/parser/ini_parser/ini_parser.cc

    r5953 r6388  
    431431}
    432432
     433/**
     434 * @brief edits the entry speciefied by entryName in sectionName/currentSection or creates it if it doesn't exist
     435 * @param entryName the Name of the Entry to add
     436 * @param value the value to assign to this entry
     437 * @param sectionName if NULL then this entry will be set to the currentSection
     438 * otherwise to the section refered to by sectionName.
     439 * If both are NULL no entry will be added
     440 * @return true if everything is ok false on error
     441 */
     442bool IniParser::editVar(const char* entryName, const char* value, const char* sectionName)
     443{
     444  std::list<IniSection>::iterator section;
     445
     446  if (sectionName != NULL)
     447  {
     448    for (section = this->sections.begin(); section != this->sections.end(); section++)
     449      if (!strcmp((*section).name, sectionName))
     450        break;
     451  }
     452  else
     453    section = this->currentSection;
     454
     455  if (section == this->sections.end())
     456  {
     457    assert( sectionName != NULL );
     458
     459    IniSection sec;
     460    sec.comment = NULL;
     461    sec.name = new char[strlen(sectionName)+1];
     462    strcpy(sec.name, sectionName);
     463    section = this->sections.insert(this->sections.end(), sec);
     464  }
     465
     466  if (section == this->sections.end())
     467  {
     468    PRINTF(2)("section '%s' not found for value '%s'\n", sectionName, entryName);
     469    return false;
     470  }
     471  else
     472  {
     473    //try find item
     474    std::list<IniEntry>::iterator entry;
     475    for (entry = section->entries.begin(); entry!=section->entries.end(); entry++)
     476      if (!strcmp( entry->name, entryName ))
     477        break;
     478
     479    //found it?
     480    if ( entry != section->entries.end() )
     481    {
     482      if ( entry->value != NULL )
     483      {
     484        delete[] entry->value;
     485      }
     486      entry->value = new char[strlen(value)+1];
     487      strcpy(entry->value, value);
     488
     489      return true;
     490    }
     491
     492    //not found -> create it
     493    (*section).entries.push_back(IniEntry());
     494    (*section).entries.back().comment = NULL;
     495    (*section).entries.back().name = new char[strlen(entryName)+1];
     496    strcpy((*section).entries.back().name, entryName);
     497    (*section).entries.back().value = new char[strlen(value)+1];
     498    strcpy((*section).entries.back().value, value);
     499    PRINTF(5)("Added Entry %s with Value '%s' to Section %s\n",
     500    (*section).entries.back().name,
     501    (*section).entries.back().value,
     502    (*section).name);
     503    this->currentEntry = --(*section).entries.end();
     504    return true;
     505  }
     506}
     507
    433508
    434509/**
  • branches/preferences/src/lib/parser/ini_parser/ini_parser.h

    r5952 r6388  
    6767
    6868    bool addVar(const char* entryName, const char* value, const char* sectionName = NULL);
     69    bool editVar(const char* entryName, const char* value, const char* sectionName = NULL);
    6970    const char* getVar(const char* entryName, const char* sectionName, const char* defaultValue = "") const;
    7071    void setEntryComment(const char* comment, const char* entryName, const char* sectionName);
  • branches/preferences/src/lib/parser/preferences/cmd_line_prefs_reader.cc

    r6383 r6388  
    3636}
    3737
    38 /**
    39  * reads the commandline arguments and stores them in Preferences
    40  * @return true if commanline arguments have been interpreted correctly
    41  */
    42 bool CmdLinePrefsReader::readCmdLine()
    43 {
    44 }
     38
  • branches/preferences/src/lib/parser/preferences/cmd_line_prefs_reader.h

    r6383 r6388  
    1414   virtual ~CmdLinePrefsReader();
    1515
    16    bool readCmdLine();
    17 
    1816 private:
    1917
  • branches/preferences/src/lib/parser/preferences/ini_file_prefs_reader.cc

    r6383 r6388  
    2424 * standard constructor
    2525*/
    26 IniFilePrefsReader::IniFilePrefsReader ()
     26IniFilePrefsReader::IniFilePrefsReader ( const char * fileName )
    2727{
     28  IniParser iniParser;
     29
     30  if ( !iniParser.readFile( fileName ) )
     31    return;
     32
     33  Preferences* prefs = Preferences::getInstance();
     34
     35  iniParser.firstSection();
     36
     37  do
     38  {
     39    do
     40    {
     41      prefs->setString( iniParser.getCurrentSection(), iniParser.getCurrentName(), iniParser.getCurrentValue(), true );
     42    } while (iniParser.nextVar());
     43  } while (iniParser.nextSection()!=NULL);
     44
     45  /*iniParser.firstVar();
     46
     47  Preferences* prefs = Preferences::getInstance();
     48
     49  while ( iniParser.nextVar() )
     50  {
     51    prefs->setString( iniParser.getCurrentSection(), iniParser.getCurrentName(), iniParser.getCurrentValue(), true );
     52}*/
    2853}
    2954
     
    3661}
    3762
    38 /**
    39  * reads entries from inifile fileName into Preferences
    40  * @param fileName filename of inifile to read
    41  * @return true on success
    42  */
    43 bool IniFilePrefsReader::readIniFile( const char * fileName )
    44 {
    45   IniParser iniParser;
    4663
    47   if ( !iniParser.readFile( fileName ) )
    48     return false;
    49 
    50   iniParser.firstVar();
    51 
    52   Preferences* prefs = Preferences::getInstance();
    53 
    54   while ( iniParser.nextVar() )
    55   {
    56     prefs->setString( iniParser.getCurrentSection(), iniParser.getCurrentName(), iniParser.getCurrentValue() );
    57   }
    58 
    59   return true;
    60 }
  • branches/preferences/src/lib/parser/preferences/ini_file_prefs_reader.h

    r6383 r6388  
    1515
    1616 public:
    17    IniFilePrefsReader();
     17   IniFilePrefsReader(const char* fileName);
    1818   virtual ~IniFilePrefsReader();
    1919
    20    bool readIniFile(const char* fileName);
    21 
    22  private:
    2320
    2421};
Note: See TracChangeset for help on using the changeset viewer.