Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/preferences/src/lib/util/preferences.cc @ 7247

Last change on this file since 7247 was 7244, checked in by rennerc, 19 years ago

preferences.cc compiles again

File size: 6.8 KB
RevLine 
[6380]1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11   ### File Specific:
12   main-programmer: Christoph Renner
13   co-programmer: ...
14*/
15
16//#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY
17
18#include "preferences.h"
19
20using namespace std;
21
22
23/**
24 * standard constructor
25 */
26Preferences::Preferences ()
27{
28   this->setClassID(CL_PREFERENCES, "Preferences");
29   this->setName("Preferences");
[7244]30   this->fileName = "";
[6380]31}
32
33/**
34 *  the singleton reference to this class
35 */
36Preferences* Preferences::singletonRef = NULL;
37
38/**
39   @brief standard deconstructor
40 */
41Preferences::~Preferences ()
42{
43  Preferences::singletonRef = NULL;
44}
45
46/**
47 * Check if this item exists
48 * @param section name of the section
49 * @param name name of the item to check
50 * @return true if the item exists
51 */
[7244]52bool Preferences::exists( const std::string& section, const std::string& name)
[6380]53{
54  std::list<prefSection>::const_iterator it = data.begin();
55
56  for ( ; it!=data.end(); it++)
57  {
[7244]58    if ( it->sectionName == section )
[6380]59    {
60      std::list<prefItem>::const_iterator it2 = it->items.begin();
61
[6383]62      for ( ; it2!=it->items.end(); it2++)
[6380]63      {
[7244]64        if ( it2->name == name )
[6380]65          return true;
66      }
67
68      break;
69    }
70  }
71
72  return false;
73}
74
75/**
76 * Sets the value of an item. Creates it if doesn't exits.
77 * @param section name of the section
78 * @param name name of the item
79 * @param value value
80 */
[7244]81void Preferences::setString(const std::string& section, const std::string& name, const std::string& value, bool dontSetModified)
[6380]82{
[6388]83  MultiType t(value);
84  setMultiType(section, name, t, dontSetModified);
[6380]85}
86
87/**
88 * Sets the value of an item. Creates it if doesn't exits.
89 * @param section name of the section
90 * @param name name of the item
91 * @param value value
92 */
[7244]93void Preferences::setInt(const std::string& section, const std::string& name, int value, bool dontSetModified)
[6380]94{
[6388]95  MultiType t(value);
96  setMultiType(section, name, t, dontSetModified);
[6380]97}
98
99/**
100 * Sets the value of an item. Creates it if doesn't exits.
101 * @param section name of the section
102 * @param name name of the item
103 * @param value value
104 */
[7244]105void Preferences::setFloat(const std::string& section, const std::string& name, float value, bool dontSetModified)
[6380]106{
[6388]107  MultiType t(value);
108  setMultiType(section, name, t, dontSetModified);
[6380]109}
110
111/**
112 * Get the value of an item
113 * @param section name of the section
114 * @param name name of the item to check
115 * @param defaultValue value to return if item doesn't exist
116 * @return value of the item if found. defaultValue else
117 */
[7244]118const std::string Preferences::getString(const std::string& section, const std::string& name, const std::string& defaultValue)
[6380]119{
120  return getMultiType(section, name, MultiType(defaultValue)).getString();
121}
122
123/**
124 * Get the value of an item
125 * @param section name of the section
126 * @param name name of the item to check
127 * @param defaultValue value to return if item doesn't exist
128 * @return value of the item if found. defaultValue else
129 */
[7244]130int Preferences::getInt(const std::string& section, const std::string& name, int defaultValue)
[6380]131{
132  return getMultiType(section, name, MultiType(defaultValue)).getInt();
133}
134
135/**
136 * Get the value of an item
137 * @param section name of the section
138 * @param name name of the item to check
139 * @param defaultValue value to return if item doesn't exist
140 * @return value of the item if found. defaultValue else
141 */
[7244]142float Preferences::getFloat(const std::string& section, const std::string& name, float defaultValue)
[6380]143{
144  return getMultiType(section, name, MultiType(defaultValue)).getFloat();
145}
146
147/**
148 * Sets the value of an item. Creates it if doesn't exits.
149 * @param section name of the section
150 * @param name name of the item
151 * @param value value
152 */
[7244]153void Preferences::setMultiType(const std::string& section, const std::string& name, const MultiType& value, bool dontSetModified)
[6380]154{
155  std::list<prefSection>::iterator it = data.begin();
156
157  for ( ; it!=data.end(); it++)
158  {
[7244]159    if ( it->sectionName == section )
[6380]160    {
161      std::list<prefItem>::iterator it2 = it->items.begin();
162
[6383]163      for ( ; it2!=it->items.end(); it2++)
[6380]164      {
[7244]165        if ( it2->name == name )
[6380]166        {
[6388]167          if (!dontSetModified)
[7244]168            it2->modified = value.getString() != it2->value.getString();
[6388]169
[6380]170          it2->value = value;
[6388]171
[6380]172          return;
173        }
174      }
175      prefItem item;
176      item.value = value;
[6388]177      item.modified = !dontSetModified;
[7244]178      item.name = name;
[6380]179      it->items.push_back(item);
180      return;
181    }
182  }
183
184  prefItem item;
185  item.value = value;
[6388]186  item.modified = !dontSetModified;
[7244]187  item.name = name;
[6380]188
189  prefSection sec;
190  sec.items.push_back(item);
[7244]191  sec.sectionName = section;
[6380]192  data.push_back( sec );
193}
194
195/**
196 * Get the value of an item
197 * @param section name of the section
198 * @param name name of the item to check
199 * @param defaultValue value to return if item doesn't exist
200 * @return value of the item if found. defaultValue else
201 */
[7244]202MultiType Preferences::getMultiType(const std::string& section, const std::string& name,const MultiType& defaultValue)
[6380]203{
204  std::list<prefSection>::const_iterator it = data.begin();
205
206  for ( ; it!=data.end(); it++)
207  {
[7244]208    if ( it->sectionName == section )
[6380]209    {
210      std::list<prefItem>::const_iterator it2 = it->items.begin();
211
[6383]212      for ( ; it2!=it->items.end(); it2++)
[6380]213      {
[7244]214        if ( it2->name == name )
[6380]215        {
216          return it2->value;
217        }
218      }
219
220      break;
221    }
222  }
223
224  return defaultValue;
225}
[6381]226
[7244]227void Preferences::setUserIni(const std::string& fileName)
[6388]228{
[7244]229  this->fileName = fileName;
[6388]230}
231
232bool Preferences::save()
233{
[7244]234  if ( this->fileName == "" )
[6388]235  {
236    PRINTF(1)("You must call setUserIni before you can call save()\n");
237    return false;
238  }
239  IniParser iniParser(this->fileName);
240
241  if ( !iniParser.isOpen() )
242    return false;
243
244  std::list<prefSection>::iterator it = data.begin();
245  bool didChanges = false;
246  for ( ; it!=data.end(); it++)
247  {
248    std::list<prefItem>::iterator it2 = it->items.begin();
249
250    for ( ; it2!=it->items.end(); it2++)
251    {
252      if ( it2->modified )
253      {
254        iniParser.editVar(it2->name, it2->value.getString(), it->sectionName);
255        didChanges = true;
256      }
257    }
258  }
259
260  if ( didChanges )
261  {
262    iniParser.writeFile( this->fileName );
263  }
264
265  return true;
266}
267
[6381]268/**
269 * prints out all section with its items and values
270 */
271void Preferences::debug()
272{
273  std::list<prefSection>::iterator it = data.begin();
274
275  for ( ; it!=data.end(); it++)
276  {
[7244]277    PRINTF(0)("%s\n", it->sectionName.c_str());
[6381]278    std::list<prefItem>::iterator it2 = it->items.begin();
279
[6383]280    for ( ; it2!=it->items.end(); it2++)
[6381]281    {
[7244]282      PRINTF(0)("--> %s = '%s'%s\n", it2->name.c_str(), it2->value.getString().c_str(), ((!it2->modified)?"":" <modified>"));
[6381]283    }
284  }
285}
286
287
Note: See TracBrowser for help on using the repository browser.