Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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


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/util
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • 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.