Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7244 was 7244, checked in by rennerc, 18 years ago

preferences.cc compiles again

File size: 6.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
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 * Sets the value of an item. Creates it if doesn't exits.
77 * @param section name of the section
78 * @param name name of the item
79 * @param value value
80 */
81void Preferences::setString(const std::string& section, const std::string& name, const std::string& value, bool dontSetModified)
82{
83  MultiType t(value);
84  setMultiType(section, name, t, dontSetModified);
85}
86
87/**
88 * Sets the value of an item. Creates it if doesn't exits.
89 * @param section name of the section
90 * @param name name of the item
91 * @param value value
92 */
93void Preferences::setInt(const std::string& section, const std::string& name, int value, bool dontSetModified)
94{
95  MultiType t(value);
96  setMultiType(section, name, t, dontSetModified);
97}
98
99/**
100 * Sets the value of an item. Creates it if doesn't exits.
101 * @param section name of the section
102 * @param name name of the item
103 * @param value value
104 */
105void Preferences::setFloat(const std::string& section, const std::string& name, float value, bool dontSetModified)
106{
107  MultiType t(value);
108  setMultiType(section, name, t, dontSetModified);
109}
110
111/**
112 * Get the value of an item
113 * @param section name of the section
114 * @param name name of the item to check
115 * @param defaultValue value to return if item doesn't exist
116 * @return value of the item if found. defaultValue else
117 */
118const std::string Preferences::getString(const std::string& section, const std::string& name, const std::string& defaultValue)
119{
120  return getMultiType(section, name, MultiType(defaultValue)).getString();
121}
122
123/**
124 * Get the value of an item
125 * @param section name of the section
126 * @param name name of the item to check
127 * @param defaultValue value to return if item doesn't exist
128 * @return value of the item if found. defaultValue else
129 */
130int Preferences::getInt(const std::string& section, const std::string& name, int defaultValue)
131{
132  return getMultiType(section, name, MultiType(defaultValue)).getInt();
133}
134
135/**
136 * Get the value of an item
137 * @param section name of the section
138 * @param name name of the item to check
139 * @param defaultValue value to return if item doesn't exist
140 * @return value of the item if found. defaultValue else
141 */
142float Preferences::getFloat(const std::string& section, const std::string& name, float defaultValue)
143{
144  return getMultiType(section, name, MultiType(defaultValue)).getFloat();
145}
146
147/**
148 * Sets the value of an item. Creates it if doesn't exits.
149 * @param section name of the section
150 * @param name name of the item
151 * @param value value
152 */
153void Preferences::setMultiType(const std::string& section, const std::string& name, const MultiType& value, bool dontSetModified)
154{
155  std::list<prefSection>::iterator it = data.begin();
156
157  for ( ; it!=data.end(); it++)
158  {
159    if ( it->sectionName == section )
160    {
161      std::list<prefItem>::iterator it2 = it->items.begin();
162
163      for ( ; it2!=it->items.end(); it2++)
164      {
165        if ( it2->name == name )
166        {
167          if (!dontSetModified)
168            it2->modified = value.getString() != it2->value.getString();
169
170          it2->value = value;
171
172          return;
173        }
174      }
175      prefItem item;
176      item.value = value;
177      item.modified = !dontSetModified;
178      item.name = name;
179      it->items.push_back(item);
180      return;
181    }
182  }
183
184  prefItem item;
185  item.value = value;
186  item.modified = !dontSetModified;
187  item.name = name;
188
189  prefSection sec;
190  sec.items.push_back(item);
191  sec.sectionName = section;
192  data.push_back( sec );
193}
194
195/**
196 * Get the value of an item
197 * @param section name of the section
198 * @param name name of the item to check
199 * @param defaultValue value to return if item doesn't exist
200 * @return value of the item if found. defaultValue else
201 */
202MultiType Preferences::getMultiType(const std::string& section, const std::string& name,const MultiType& defaultValue)
203{
204  std::list<prefSection>::const_iterator it = data.begin();
205
206  for ( ; it!=data.end(); it++)
207  {
208    if ( it->sectionName == section )
209    {
210      std::list<prefItem>::const_iterator it2 = it->items.begin();
211
212      for ( ; it2!=it->items.end(); it2++)
213      {
214        if ( it2->name == name )
215        {
216          return it2->value;
217        }
218      }
219
220      break;
221    }
222  }
223
224  return defaultValue;
225}
226
227void Preferences::setUserIni(const std::string& fileName)
228{
229  this->fileName = fileName;
230}
231
232bool Preferences::save()
233{
234  if ( this->fileName == "" )
235  {
236    PRINTF(1)("You must call setUserIni before you can call save()\n");
237    return false;
238  }
239  IniParser iniParser(this->fileName);
240
241  if ( !iniParser.isOpen() )
242    return false;
243
244  std::list<prefSection>::iterator it = data.begin();
245  bool didChanges = false;
246  for ( ; it!=data.end(); it++)
247  {
248    std::list<prefItem>::iterator it2 = it->items.begin();
249
250    for ( ; it2!=it->items.end(); it2++)
251    {
252      if ( it2->modified )
253      {
254        iniParser.editVar(it2->name, it2->value.getString(), it->sectionName);
255        didChanges = true;
256      }
257    }
258  }
259
260  if ( didChanges )
261  {
262    iniParser.writeFile( this->fileName );
263  }
264
265  return true;
266}
267
268/**
269 * prints out all section with its items and values
270 */
271void Preferences::debug()
272{
273  std::list<prefSection>::iterator it = data.begin();
274
275  for ( ; it!=data.end(); it++)
276  {
277    PRINTF(0)("%s\n", it->sectionName.c_str());
278    std::list<prefItem>::iterator it2 = it->items.begin();
279
280    for ( ; it2!=it->items.end(); it2++)
281    {
282      PRINTF(0)("--> %s = '%s'%s\n", it2->name.c_str(), it2->value.getString().c_str(), ((!it2->modified)?"":" <modified>"));
283    }
284  }
285}
286
287
Note: See TracBrowser for help on using the repository browser.