Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

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

File size: 7.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"
[7661]19#include "lib/parser/ini_parser/ini_parser.h"
[8362]20#include "debug.h"
[6380]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 */
[7661]139std::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  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  }
[7661]277  /// HACK DO WE HAVE TO CHECK THIS??
278  //if ( didChanges )
[6388]279  {
280    iniParser.writeFile( this->fileName );
281  }
282
283  return true;
284}
285
[6381]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  {
[7244]295    PRINTF(0)("%s\n", it->sectionName.c_str());
[6381]296    std::list<prefItem>::iterator it2 = it->items.begin();
297
[6383]298    for ( ; it2!=it->items.end(); it2++)
[6381]299    {
[7244]300      PRINTF(0)("--> %s = '%s'%s\n", it2->name.c_str(), it2->value.getString().c_str(), ((!it2->modified)?"":" <modified>"));
[6381]301    }
302  }
303}
304
[7248]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;
[7661]313
[7248]314  std::list<prefSection>::const_iterator it = data.begin();
[6381]315
[7248]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.