Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6769 was 6388, checked in by rennerc, 19 years ago

reading and writing inifiles now works

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