Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/util/preferences.cc @ 7261

Last change on this file since 7261 was 7256, checked in by bensch, 19 years ago

orxonox/trunk: merged the preferences back to the trunk
merged with command:
svn merge https://svn.orxonox.net/orxonox/branches/preferences . -r7233:HEAD
no conflicts… nice work

File size: 7.7 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
20using namespace std;
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 */
139const std::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  if ( !iniParser.isOpen() )
263    return false;
264
265  std::list<prefSection>::iterator it = data.begin();
266  bool didChanges = false;
267  for ( ; it!=data.end(); it++)
268  {
269    std::list<prefItem>::iterator it2 = it->items.begin();
270
271    for ( ; it2!=it->items.end(); it2++)
272    {
273      if ( it2->modified )
274      {
275        iniParser.editVar(it2->name, it2->value.getString(), it->sectionName);
276        didChanges = true;
277      }
278    }
279  }
280
281  if ( didChanges )
282  {
283    iniParser.writeFile( this->fileName );
284  }
285
286  return true;
287}
288
289/**
290 * prints out all section with its items and values
291 */
292void Preferences::debug()
293{
294  std::list<prefSection>::iterator it = data.begin();
295
296  for ( ; it!=data.end(); it++)
297  {
298    PRINTF(0)("%s\n", it->sectionName.c_str());
299    std::list<prefItem>::iterator it2 = it->items.begin();
300
301    for ( ; it2!=it->items.end(); it2++)
302    {
303      PRINTF(0)("--> %s = '%s'%s\n", it2->name.c_str(), it2->value.getString().c_str(), ((!it2->modified)?"":" <modified>"));
304    }
305  }
306}
307
308/**
309 * list all keys in section
310 * @param section section
311 * @return list of keys
312 */
313std::list< std::string > Preferences::listKeys( const std::string section )
314{
315  std::list<std::string> lst;
316 
317  std::list<prefSection>::const_iterator it = data.begin();
318
319  for ( ; it!=data.end(); it++)
320  {
321    if ( it->sectionName == section )
322    {
323      std::list<prefItem>::const_iterator it2 = it->items.begin();
324
325      for ( ; it2!=it->items.end(); it2++)
326      {
327        lst.push_back( it2->name );
328      }
329
330      break;
331    }
332  }
333
334  return lst;
335}
336
337
Note: See TracBrowser for help on using the repository browser.