Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7687 was 7661, checked in by bensch, 19 years ago

orxonox/trunk: merged the QT_GUI back to the trunk
merged with command:
svn merge https://svn.orxonox.net/orxonox/branches/qt_gui . -r7607:HEAD

absolutely no conflicts :)

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"
[6380]20
21
22/**
23 * standard constructor
24 */
25Preferences::Preferences ()
26{
27   this->setClassID(CL_PREFERENCES, "Preferences");
28   this->setName("Preferences");
[7244]29   this->fileName = "";
[6380]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 */
[7244]51bool Preferences::exists( const std::string& section, const std::string& name)
[6380]52{
53  std::list<prefSection>::const_iterator it = data.begin();
54
55  for ( ; it!=data.end(); it++)
56  {
[7244]57    if ( it->sectionName == section )
[6380]58    {
59      std::list<prefItem>::const_iterator it2 = it->items.begin();
60
[6383]61      for ( ; it2!=it->items.end(); it2++)
[6380]62      {
[7244]63        if ( it2->name == name )
[6380]64          return true;
65      }
66
67      break;
68    }
69  }
70
71  return false;
72}
73
74/**
[7248]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/**
[6380]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 */
[7244]101void Preferences::setString(const std::string& section, const std::string& name, const std::string& value, bool dontSetModified)
[6380]102{
[6388]103  MultiType t(value);
104  setMultiType(section, name, t, dontSetModified);
[6380]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 */
[7244]113void Preferences::setInt(const std::string& section, const std::string& name, int value, bool dontSetModified)
[6380]114{
[6388]115  MultiType t(value);
116  setMultiType(section, name, t, dontSetModified);
[6380]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 */
[7244]125void Preferences::setFloat(const std::string& section, const std::string& name, float value, bool dontSetModified)
[6380]126{
[6388]127  MultiType t(value);
128  setMultiType(section, name, t, dontSetModified);
[6380]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 */
[7661]138std::string Preferences::getString(const std::string& section, const std::string& name, const std::string& defaultValue)
[6380]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 */
[7244]150int Preferences::getInt(const std::string& section, const std::string& name, int defaultValue)
[6380]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 */
[7244]162float Preferences::getFloat(const std::string& section, const std::string& name, float defaultValue)
[6380]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 */
[7244]173void Preferences::setMultiType(const std::string& section, const std::string& name, const MultiType& value, bool dontSetModified)
[6380]174{
175  std::list<prefSection>::iterator it = data.begin();
176
177  for ( ; it!=data.end(); it++)
178  {
[7244]179    if ( it->sectionName == section )
[6380]180    {
181      std::list<prefItem>::iterator it2 = it->items.begin();
182
[6383]183      for ( ; it2!=it->items.end(); it2++)
[6380]184      {
[7244]185        if ( it2->name == name )
[6380]186        {
[6388]187          if (!dontSetModified)
[7244]188            it2->modified = value.getString() != it2->value.getString();
[6388]189
[6380]190          it2->value = value;
[6388]191
[6380]192          return;
193        }
194      }
195      prefItem item;
196      item.value = value;
[6388]197      item.modified = !dontSetModified;
[7244]198      item.name = name;
[6380]199      it->items.push_back(item);
200      return;
201    }
202  }
203
204  prefItem item;
205  item.value = value;
[6388]206  item.modified = !dontSetModified;
[7244]207  item.name = name;
[6380]208
209  prefSection sec;
210  sec.items.push_back(item);
[7244]211  sec.sectionName = section;
[6380]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 */
[7244]222MultiType Preferences::getMultiType(const std::string& section, const std::string& name,const MultiType& defaultValue)
[6380]223{
224  std::list<prefSection>::const_iterator it = data.begin();
225
226  for ( ; it!=data.end(); it++)
227  {
[7244]228    if ( it->sectionName == section )
[6380]229    {
230      std::list<prefItem>::const_iterator it2 = it->items.begin();
231
[6383]232      for ( ; it2!=it->items.end(); it2++)
[6380]233      {
[7244]234        if ( it2->name == name )
[6380]235        {
236          return it2->value;
237        }
238      }
239
240      break;
241    }
242  }
243
244  return defaultValue;
245}
[6381]246
[7244]247void Preferences::setUserIni(const std::string& fileName)
[6388]248{
[7244]249  this->fileName = fileName;
[6388]250}
251
252bool Preferences::save()
253{
[7244]254  if ( this->fileName == "" )
[6388]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  std::list<prefSection>::iterator it = data.begin();
262  bool didChanges = false;
263  for ( ; it!=data.end(); it++)
264  {
265    std::list<prefItem>::iterator it2 = it->items.begin();
266
267    for ( ; it2!=it->items.end(); it2++)
268    {
269      if ( it2->modified )
270      {
271        iniParser.editVar(it2->name, it2->value.getString(), it->sectionName);
272        didChanges = true;
273      }
274    }
275  }
[7661]276  /// HACK DO WE HAVE TO CHECK THIS??
277  //if ( didChanges )
[6388]278  {
279    iniParser.writeFile( this->fileName );
280  }
281
282  return true;
283}
284
[6381]285/**
286 * prints out all section with its items and values
287 */
288void Preferences::debug()
289{
290  std::list<prefSection>::iterator it = data.begin();
291
292  for ( ; it!=data.end(); it++)
293  {
[7244]294    PRINTF(0)("%s\n", it->sectionName.c_str());
[6381]295    std::list<prefItem>::iterator it2 = it->items.begin();
296
[6383]297    for ( ; it2!=it->items.end(); it2++)
[6381]298    {
[7244]299      PRINTF(0)("--> %s = '%s'%s\n", it2->name.c_str(), it2->value.getString().c_str(), ((!it2->modified)?"":" <modified>"));
[6381]300    }
301  }
302}
303
[7248]304/**
305 * list all keys in section
306 * @param section section
307 * @return list of keys
308 */
309std::list< std::string > Preferences::listKeys( const std::string section )
310{
311  std::list<std::string> lst;
[7661]312
[7248]313  std::list<prefSection>::const_iterator it = data.begin();
[6381]314
[7248]315  for ( ; it!=data.end(); it++)
316  {
317    if ( it->sectionName == section )
318    {
319      std::list<prefItem>::const_iterator it2 = it->items.begin();
320
321      for ( ; it2!=it->items.end(); it2++)
322      {
323        lst.push_back( it2->name );
324      }
325
326      break;
327    }
328  }
329
330  return lst;
331}
332
333
Note: See TracBrowser for help on using the repository browser.