Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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


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
Files:
8 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};
  • branches/preferences/src/lib/util/preferences.cc

    r6383 r6388  
    2828   this->setClassID(CL_PREFERENCES, "Preferences");
    2929   this->setName("Preferences");
     30   this->fileName = NULL;
    3031}
    3132
     
    4142{
    4243  Preferences::singletonRef = NULL;
     44
     45  if ( this->fileName )
     46  {
     47    delete this->fileName;
     48    this->fileName = NULL;
     49  }
    4350}
    4451
     
    7885 * @param value value
    7986 */
    80 void Preferences::setString(const char* section, const char* name, const char* value)
    81 {
    82   setMultiType(section, name, MultiType(value));
     87void Preferences::setString(const char* section, const char* name, const char* value, bool dontSetModified)
     88{
     89  MultiType t(value);
     90  setMultiType(section, name, t, dontSetModified);
    8391}
    8492
     
    8997 * @param value value
    9098 */
    91 void Preferences::setInt(const char* section, const char* name, int value)
    92 {
    93   setMultiType(section, name, MultiType(value));
     99void Preferences::setInt(const char* section, const char* name, int value, bool dontSetModified)
     100{
     101  MultiType t(value);
     102  setMultiType(section, name, t, dontSetModified);
    94103}
    95104
     
    100109 * @param value value
    101110 */
    102 void Preferences::setFloat(const char* section, const char* name, float value)
    103 {
    104   setMultiType(section, name, MultiType(value));
     111void Preferences::setFloat(const char* section, const char* name, float value, bool dontSetModified)
     112{
     113  MultiType t(value);
     114  setMultiType(section, name, t, dontSetModified);
    105115}
    106116
     
    147157 * @param value value
    148158 */
    149 void Preferences::setMultiType(const char* section, const char* name, const MultiType& value)
     159void Preferences::setMultiType(const char* section, const char* name, MultiType& value, bool dontSetModified)
    150160{
    151161  std::list<prefSection>::iterator it = data.begin();
     
    161171        if ( strcmp(it2->name, name) == 0 )
    162172        {
     173          if (!dontSetModified)
     174            it2->modified = strcmp(value.getString(), it2->value.getString())!=0;
     175
    163176          it2->value = value;
     177
    164178          return;
    165179        }
     
    167181      prefItem item;
    168182      item.value = value;
     183      item.modified = !dontSetModified;
    169184      item.name = new char[strlen(name)+1];
    170185      strcpy( item.name, name );
     
    176191  prefItem item;
    177192  item.value = value;
     193  item.modified = !dontSetModified;
    178194  item.name = new char[strlen(name)+1];
    179195  strcpy( item.name, name );
     
    218234}
    219235
     236void Preferences::setUserIni(const char* fileName)
     237{
     238  if ( this->fileName != NULL )
     239  {
     240    delete this->fileName;
     241  }
     242
     243  this->fileName = new char[strlen(fileName)+1];
     244  strcpy(this->fileName, fileName);
     245}
     246
     247bool Preferences::save()
     248{
     249  if ( this->fileName == NULL )
     250  {
     251    PRINTF(1)("You must call setUserIni before you can call save()\n");
     252    return false;
     253  }
     254  IniParser iniParser(this->fileName);
     255
     256  if ( !iniParser.isOpen() )
     257    return false;
     258
     259  std::list<prefSection>::iterator it = data.begin();
     260  bool didChanges = false;
     261  for ( ; it!=data.end(); it++)
     262  {
     263    std::list<prefItem>::iterator it2 = it->items.begin();
     264
     265    for ( ; it2!=it->items.end(); it2++)
     266    {
     267      if ( it2->modified )
     268      {
     269        iniParser.editVar(it2->name, it2->value.getString(), it->sectionName);
     270        didChanges = true;
     271      }
     272    }
     273  }
     274
     275  if ( didChanges )
     276  {
     277    iniParser.writeFile( this->fileName );
     278  }
     279
     280  return true;
     281}
     282
    220283/**
    221284 * prints out all section with its items and values
     
    232295    for ( ; it2!=it->items.end(); it2++)
    233296    {
    234       PRINTF(0)("--> %s = '%s'\n", it2->name, it2->value.getString());
    235     }
    236   }
    237 }
    238 
    239 
     297      PRINTF(0)("--> %s = '%s'%s\n", it2->name, it2->value.getString(), ((!it2->modified)?"":" <modified>"));
     298    }
     299  }
     300}
     301
     302
  • branches/preferences/src/lib/util/preferences.h

    r6381 r6388  
    99#include "base_object.h"
    1010#include "multi_type.h"
     11#include "lib/parser/ini_parser/ini_parser.h"
    1112
    1213// FORWARD DECLARATION
    1314
     15class IniFilePrefsReader;
    1416
    1517typedef struct {
    1618  char* name;
    1719  MultiType value;
     20  bool modified;
    1821} prefItem;
    1922
     
    3538   bool exists(const char* section, const char* name);
    3639
    37    void setString(const char* section, const char* name, const char* value);
    38    void setInt(const char* section, const char* name, int value);
    39    void setFloat(const char* section, const char* name, float value);
    40    void setMultiType(const char* section, const char* name, const MultiType& value);
     40   void setString(const char* section, const char* name, const char* value, bool dontSetModified = false);
     41   void setInt(const char* section, const char* name, int value, bool dontSetModified = false);
     42   void setFloat(const char* section, const char* name, float value, bool dontSetModified = false);
     43   void setMultiType(const char* section, const char* name, MultiType& value, bool dontSetModified = false);
    4144
    4245   const char* getString(const char* section, const char* name, const char* defaultValue);
     
    4447   float getFloat(const char* section, const char* name, float defaultValue);
    4548   MultiType getMultiType(const char* section, const char* name, const MultiType& defaultValue);
     49
     50   void setUserIni(const char* fileName);
     51
     52   bool save();
    4653
    4754
     
    5461
    5562   std::list<prefSection> data;
     63
     64   std::list<IniFilePrefsReader*> iniFilePrefsReaders;
     65
     66   char* fileName;
     67
    5668};
    5769
Note: See TracChangeset for help on using the changeset viewer.