Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7263 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
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/**
[7248]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/**
[6380]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 */
[7244]102void Preferences::setString(const std::string& section, const std::string& name, const std::string& value, bool dontSetModified)
[6380]103{
[6388]104  MultiType t(value);
105  setMultiType(section, name, t, dontSetModified);
[6380]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 */
[7244]114void Preferences::setInt(const std::string& section, const std::string& name, int value, bool dontSetModified)
[6380]115{
[6388]116  MultiType t(value);
117  setMultiType(section, name, t, dontSetModified);
[6380]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 */
[7244]126void Preferences::setFloat(const std::string& section, const std::string& name, float value, bool dontSetModified)
[6380]127{
[6388]128  MultiType t(value);
129  setMultiType(section, name, t, dontSetModified);
[6380]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 */
[7244]139const std::string Preferences::getString(const std::string& section, const std::string& name, const std::string& defaultValue)
[6380]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 */
[7244]151int Preferences::getInt(const std::string& section, const std::string& name, int defaultValue)
[6380]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 */
[7244]163float Preferences::getFloat(const std::string& section, const std::string& name, float defaultValue)
[6380]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 */
[7244]174void Preferences::setMultiType(const std::string& section, const std::string& name, const MultiType& value, bool dontSetModified)
[6380]175{
176  std::list<prefSection>::iterator it = data.begin();
177
178  for ( ; it!=data.end(); it++)
179  {
[7244]180    if ( it->sectionName == section )
[6380]181    {
182      std::list<prefItem>::iterator it2 = it->items.begin();
183
[6383]184      for ( ; it2!=it->items.end(); it2++)
[6380]185      {
[7244]186        if ( it2->name == name )
[6380]187        {
[6388]188          if (!dontSetModified)
[7244]189            it2->modified = value.getString() != it2->value.getString();
[6388]190
[6380]191          it2->value = value;
[6388]192
[6380]193          return;
194        }
195      }
196      prefItem item;
197      item.value = value;
[6388]198      item.modified = !dontSetModified;
[7244]199      item.name = name;
[6380]200      it->items.push_back(item);
201      return;
202    }
203  }
204
205  prefItem item;
206  item.value = value;
[6388]207  item.modified = !dontSetModified;
[7244]208  item.name = name;
[6380]209
210  prefSection sec;
211  sec.items.push_back(item);
[7244]212  sec.sectionName = section;
[6380]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 */
[7244]223MultiType Preferences::getMultiType(const std::string& section, const std::string& name,const MultiType& defaultValue)
[6380]224{
225  std::list<prefSection>::const_iterator it = data.begin();
226
227  for ( ; it!=data.end(); it++)
228  {
[7244]229    if ( it->sectionName == section )
[6380]230    {
231      std::list<prefItem>::const_iterator it2 = it->items.begin();
232
[6383]233      for ( ; it2!=it->items.end(); it2++)
[6380]234      {
[7244]235        if ( it2->name == name )
[6380]236        {
237          return it2->value;
238        }
239      }
240
241      break;
242    }
243  }
244
245  return defaultValue;
246}
[6381]247
[7244]248void Preferences::setUserIni(const std::string& fileName)
[6388]249{
[7244]250  this->fileName = fileName;
[6388]251}
252
253bool Preferences::save()
254{
[7244]255  if ( this->fileName == "" )
[6388]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
[6381]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  {
[7244]298    PRINTF(0)("%s\n", it->sectionName.c_str());
[6381]299    std::list<prefItem>::iterator it2 = it->items.begin();
300
[6383]301    for ( ; it2!=it->items.end(); it2++)
[6381]302    {
[7244]303      PRINTF(0)("--> %s = '%s'%s\n", it2->name.c_str(), it2->value.getString().c_str(), ((!it2->modified)?"":" <modified>"));
[6381]304    }
305  }
306}
307
[7248]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();
[6381]318
[7248]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.