Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7629 was 7629, checked in by bensch, 18 years ago

minor ini-parser-stuff

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
21
22/**
23 * standard constructor
24 */
25Preferences::Preferences ()
26{
27   this->setClassID(CL_PREFERENCES, "Preferences");
28   this->setName("Preferences");
29   this->fileName = "";
30}
31
32/**
33 *  the singleton reference to this class
34 */
35Preferences* Preferences::singletonRef = NULL;
36
37/**
38   @brief standard deconstructor
39 */
40Preferences::~Preferences ()
41{
42  Preferences::singletonRef = NULL;
43}
44
45/**
46 * Check if this item exists
47 * @param section name of the section
48 * @param name name of the item to check
49 * @return true if the item exists
50 */
51bool Preferences::exists( const std::string& section, const std::string& name)
52{
53  std::list<prefSection>::const_iterator it = data.begin();
54
55  for ( ; it!=data.end(); it++)
56  {
57    if ( it->sectionName == section )
58    {
59      std::list<prefItem>::const_iterator it2 = it->items.begin();
60
61      for ( ; it2!=it->items.end(); it2++)
62      {
63        if ( it2->name == name )
64          return true;
65      }
66
67      break;
68    }
69  }
70
71  return false;
72}
73
74/**
75 * Check if this section exists
76 * @param section name of the section
77 * @param name name of the item to check
78 * @return true if the item exists
79 */
80bool Preferences::sectionExists( const std::string& section )
81{
82  std::list<prefSection>::const_iterator it = data.begin();
83
84  for ( ; it!=data.end(); it++)
85  {
86    if ( it->sectionName == section )
87    {
88      return true;
89    }
90  }
91
92  return false;
93}
94
95/**
96 * Sets the value of an item. Creates it if doesn't exits.
97 * @param section name of the section
98 * @param name name of the item
99 * @param value value
100 */
101void Preferences::setString(const std::string& section, const std::string& name, const std::string& value, bool dontSetModified)
102{
103  MultiType t(value);
104  setMultiType(section, name, t, dontSetModified);
105}
106
107/**
108 * Sets the value of an item. Creates it if doesn't exits.
109 * @param section name of the section
110 * @param name name of the item
111 * @param value value
112 */
113void Preferences::setInt(const std::string& section, const std::string& name, int value, bool dontSetModified)
114{
115  MultiType t(value);
116  setMultiType(section, name, t, dontSetModified);
117}
118
119/**
120 * Sets the value of an item. Creates it if doesn't exits.
121 * @param section name of the section
122 * @param name name of the item
123 * @param value value
124 */
125void Preferences::setFloat(const std::string& section, const std::string& name, float value, bool dontSetModified)
126{
127  MultiType t(value);
128  setMultiType(section, name, t, dontSetModified);
129}
130
131/**
132 * Get the value of an item
133 * @param section name of the section
134 * @param name name of the item to check
135 * @param defaultValue value to return if item doesn't exist
136 * @return value of the item if found. defaultValue else
137 */
138std::string Preferences::getString(const std::string& section, const std::string& name, const std::string& defaultValue)
139{
140  return getMultiType(section, name, MultiType(defaultValue)).getString();
141}
142
143/**
144 * Get the value of an item
145 * @param section name of the section
146 * @param name name of the item to check
147 * @param defaultValue value to return if item doesn't exist
148 * @return value of the item if found. defaultValue else
149 */
150int Preferences::getInt(const std::string& section, const std::string& name, int defaultValue)
151{
152  return getMultiType(section, name, MultiType(defaultValue)).getInt();
153}
154
155/**
156 * Get the value of an item
157 * @param section name of the section
158 * @param name name of the item to check
159 * @param defaultValue value to return if item doesn't exist
160 * @return value of the item if found. defaultValue else
161 */
162float Preferences::getFloat(const std::string& section, const std::string& name, float defaultValue)
163{
164  return getMultiType(section, name, MultiType(defaultValue)).getFloat();
165}
166
167/**
168 * Sets the value of an item. Creates it if doesn't exits.
169 * @param section name of the section
170 * @param name name of the item
171 * @param value value
172 */
173void Preferences::setMultiType(const std::string& section, const std::string& name, const MultiType& value, bool dontSetModified)
174{
175  std::list<prefSection>::iterator it = data.begin();
176
177  for ( ; it!=data.end(); it++)
178  {
179    if ( it->sectionName == section )
180    {
181      std::list<prefItem>::iterator it2 = it->items.begin();
182
183      for ( ; it2!=it->items.end(); it2++)
184      {
185        if ( it2->name == name )
186        {
187          if (!dontSetModified)
188            it2->modified = value.getString() != it2->value.getString();
189
190          it2->value = value;
191
192          return;
193        }
194      }
195      prefItem item;
196      item.value = value;
197      item.modified = !dontSetModified;
198      item.name = name;
199      it->items.push_back(item);
200      return;
201    }
202  }
203
204  prefItem item;
205  item.value = value;
206  item.modified = !dontSetModified;
207  item.name = name;
208
209  prefSection sec;
210  sec.items.push_back(item);
211  sec.sectionName = section;
212  data.push_back( sec );
213}
214
215/**
216 * Get the value of an item
217 * @param section name of the section
218 * @param name name of the item to check
219 * @param defaultValue value to return if item doesn't exist
220 * @return value of the item if found. defaultValue else
221 */
222MultiType Preferences::getMultiType(const std::string& section, const std::string& name,const MultiType& defaultValue)
223{
224  std::list<prefSection>::const_iterator it = data.begin();
225
226  for ( ; it!=data.end(); it++)
227  {
228    if ( it->sectionName == section )
229    {
230      std::list<prefItem>::const_iterator it2 = it->items.begin();
231
232      for ( ; it2!=it->items.end(); it2++)
233      {
234        if ( it2->name == name )
235        {
236          return it2->value;
237        }
238      }
239
240      break;
241    }
242  }
243
244  return defaultValue;
245}
246
247void Preferences::setUserIni(const std::string& fileName)
248{
249  this->fileName = fileName;
250}
251
252bool Preferences::save()
253{
254  if ( this->fileName == "" )
255  {
256    PRINTF(1)("You must call setUserIni before you can call save()\n");
257    return false;
258  }
259  IniParser iniParser(this->fileName);
260
261  if ( !iniParser.isOpen() )
262    return false;
263
264  std::list<prefSection>::iterator it = data.begin();
265  bool didChanges = false;
266  for ( ; it!=data.end(); it++)
267  {
268    std::list<prefItem>::iterator it2 = it->items.begin();
269
270    for ( ; it2!=it->items.end(); it2++)
271    {
272      if ( it2->modified )
273      {
274        iniParser.editVar(it2->name, it2->value.getString(), it->sectionName);
275        didChanges = true;
276      }
277    }
278  }
279
280  //if ( didChanges )
281  {
282    iniParser.debug();
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.