Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/terrain.older/src/lib/util/preferences.cc @ 10661

Last change on this file since 10661 was 8362, checked in by bensch, 19 years ago

orxonox/trunk: removed stupid included in base_object.h
this should lead to faster compile-times

File size: 7.8 KB
Line 
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#include "lib/parser/ini_parser/ini_parser.h"
20#include "debug.h"
21
22
23/**
24 * standard constructor
25 */
26Preferences::Preferences ()
27{
28   this->setClassID(CL_PREFERENCES, "Preferences");
29   this->setName("Preferences");
30   this->fileName = "";
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 */
52bool Preferences::exists( const std::string& section, const std::string& name)
53{
54  std::list<prefSection>::const_iterator it = data.begin();
55
56  for ( ; it!=data.end(); it++)
57  {
58    if ( it->sectionName == section )
59    {
60      std::list<prefItem>::const_iterator it2 = it->items.begin();
61
62      for ( ; it2!=it->items.end(); it2++)
63      {
64        if ( it2->name == name )
65          return true;
66      }
67
68      break;
69    }
70  }
71
72  return false;
73}
74
75/**
76 * Check if this section exists
77 * @param section name of the section
78 * @param name name of the item to check
79 * @return true if the item exists
80 */
81bool Preferences::sectionExists( const std::string& section )
82{
83  std::list<prefSection>::const_iterator it = data.begin();
84
85  for ( ; it!=data.end(); it++)
86  {
87    if ( it->sectionName == section )
88    {
89      return true;
90    }
91  }
92
93  return false;
94}
95
96/**
97 * Sets the value of an item. Creates it if doesn't exits.
98 * @param section name of the section
99 * @param name name of the item
100 * @param value value
101 */
102void Preferences::setString(const std::string& section, const std::string& name, const std::string& value, bool dontSetModified)
103{
104  MultiType t(value);
105  setMultiType(section, name, t, dontSetModified);
106}
107
108/**
109 * Sets the value of an item. Creates it if doesn't exits.
110 * @param section name of the section
111 * @param name name of the item
112 * @param value value
113 */
114void Preferences::setInt(const std::string& section, const std::string& name, int value, bool dontSetModified)
115{
116  MultiType t(value);
117  setMultiType(section, name, t, dontSetModified);
118}
119
120/**
121 * Sets the value of an item. Creates it if doesn't exits.
122 * @param section name of the section
123 * @param name name of the item
124 * @param value value
125 */
126void Preferences::setFloat(const std::string& section, const std::string& name, float value, bool dontSetModified)
127{
128  MultiType t(value);
129  setMultiType(section, name, t, dontSetModified);
130}
131
132/**
133 * Get the value of an item
134 * @param section name of the section
135 * @param name name of the item to check
136 * @param defaultValue value to return if item doesn't exist
137 * @return value of the item if found. defaultValue else
138 */
139std::string Preferences::getString(const std::string& section, const std::string& name, const std::string& defaultValue)
140{
141  return getMultiType(section, name, MultiType(defaultValue)).getString();
142}
143
144/**
145 * Get the value of an item
146 * @param section name of the section
147 * @param name name of the item to check
148 * @param defaultValue value to return if item doesn't exist
149 * @return value of the item if found. defaultValue else
150 */
151int Preferences::getInt(const std::string& section, const std::string& name, int defaultValue)
152{
153  return getMultiType(section, name, MultiType(defaultValue)).getInt();
154}
155
156/**
157 * Get the value of an item
158 * @param section name of the section
159 * @param name name of the item to check
160 * @param defaultValue value to return if item doesn't exist
161 * @return value of the item if found. defaultValue else
162 */
163float Preferences::getFloat(const std::string& section, const std::string& name, float defaultValue)
164{
165  return getMultiType(section, name, MultiType(defaultValue)).getFloat();
166}
167
168/**
169 * Sets the value of an item. Creates it if doesn't exits.
170 * @param section name of the section
171 * @param name name of the item
172 * @param value value
173 */
174void Preferences::setMultiType(const std::string& section, const std::string& name, const MultiType& value, bool dontSetModified)
175{
176  std::list<prefSection>::iterator it = data.begin();
177
178  for ( ; it!=data.end(); it++)
179  {
180    if ( it->sectionName == section )
181    {
182      std::list<prefItem>::iterator it2 = it->items.begin();
183
184      for ( ; it2!=it->items.end(); it2++)
185      {
186        if ( it2->name == name )
187        {
188          if (!dontSetModified)
189            it2->modified = value.getString() != it2->value.getString();
190
191          it2->value = value;
192
193          return;
194        }
195      }
196      prefItem item;
197      item.value = value;
198      item.modified = !dontSetModified;
199      item.name = name;
200      it->items.push_back(item);
201      return;
202    }
203  }
204
205  prefItem item;
206  item.value = value;
207  item.modified = !dontSetModified;
208  item.name = name;
209
210  prefSection sec;
211  sec.items.push_back(item);
212  sec.sectionName = section;
213  data.push_back( sec );
214}
215
216/**
217 * Get the value of an item
218 * @param section name of the section
219 * @param name name of the item to check
220 * @param defaultValue value to return if item doesn't exist
221 * @return value of the item if found. defaultValue else
222 */
223MultiType Preferences::getMultiType(const std::string& section, const std::string& name,const MultiType& defaultValue)
224{
225  std::list<prefSection>::const_iterator it = data.begin();
226
227  for ( ; it!=data.end(); it++)
228  {
229    if ( it->sectionName == section )
230    {
231      std::list<prefItem>::const_iterator it2 = it->items.begin();
232
233      for ( ; it2!=it->items.end(); it2++)
234      {
235        if ( it2->name == name )
236        {
237          return it2->value;
238        }
239      }
240
241      break;
242    }
243  }
244
245  return defaultValue;
246}
247
248void Preferences::setUserIni(const std::string& fileName)
249{
250  this->fileName = fileName;
251}
252
253bool Preferences::save()
254{
255  if ( this->fileName == "" )
256  {
257    PRINTF(1)("You must call setUserIni before you can call save()\n");
258    return false;
259  }
260  IniParser iniParser(this->fileName);
261
262  std::list<prefSection>::iterator it = data.begin();
263  bool didChanges = false;
264  for ( ; it!=data.end(); it++)
265  {
266    std::list<prefItem>::iterator it2 = it->items.begin();
267
268    for ( ; it2!=it->items.end(); it2++)
269    {
270      if ( it2->modified )
271      {
272        iniParser.editVar(it2->name, it2->value.getString(), it->sectionName);
273        didChanges = true;
274      }
275    }
276  }
277  /// HACK DO WE HAVE TO CHECK THIS??
278  //if ( didChanges )
279  {
280    iniParser.writeFile( this->fileName );
281  }
282
283  return true;
284}
285
286/**
287 * prints out all section with its items and values
288 */
289void Preferences::debug()
290{
291  std::list<prefSection>::iterator it = data.begin();
292
293  for ( ; it!=data.end(); it++)
294  {
295    PRINTF(0)("%s\n", it->sectionName.c_str());
296    std::list<prefItem>::iterator it2 = it->items.begin();
297
298    for ( ; it2!=it->items.end(); it2++)
299    {
300      PRINTF(0)("--> %s = '%s'%s\n", it2->name.c_str(), it2->value.getString().c_str(), ((!it2->modified)?"":" <modified>"));
301    }
302  }
303}
304
305/**
306 * list all keys in section
307 * @param section section
308 * @return list of keys
309 */
310std::list< std::string > Preferences::listKeys( const std::string section )
311{
312  std::list<std::string> lst;
313
314  std::list<prefSection>::const_iterator it = data.begin();
315
316  for ( ; it!=data.end(); it++)
317  {
318    if ( it->sectionName == section )
319    {
320      std::list<prefItem>::const_iterator it2 = it->items.begin();
321
322      for ( ; it2!=it->items.end(); it2++)
323      {
324        lst.push_back( it2->name );
325      }
326
327      break;
328    }
329  }
330
331  return lst;
332}
333
334
Note: See TracBrowser for help on using the repository browser.