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 | #include "lib/parser/ini_parser/ini_parser.h" |
---|
20 | #include "debug.h" |
---|
21 | |
---|
22 | ObjectListDefinition(Preferences); |
---|
23 | |
---|
24 | /** |
---|
25 | * standard constructor |
---|
26 | */ |
---|
27 | Preferences::Preferences () |
---|
28 | { |
---|
29 | this->registerObject(this, Preferences::_objectList); |
---|
30 | this->setName("Preferences"); |
---|
31 | this->fileName = ""; |
---|
32 | } |
---|
33 | |
---|
34 | /** |
---|
35 | * the singleton reference to this class |
---|
36 | */ |
---|
37 | Preferences* Preferences::singletonRef = NULL; |
---|
38 | |
---|
39 | /** |
---|
40 | @brief standard deconstructor |
---|
41 | */ |
---|
42 | Preferences::~Preferences () |
---|
43 | { |
---|
44 | Preferences::singletonRef = NULL; |
---|
45 | } |
---|
46 | |
---|
47 | /** |
---|
48 | * Check if this item exists |
---|
49 | * @param section name of the section |
---|
50 | * @param name name of the item to check |
---|
51 | * @return true if the item exists |
---|
52 | */ |
---|
53 | bool Preferences::exists( const std::string& section, const std::string& name) |
---|
54 | { |
---|
55 | std::list<prefSection>::const_iterator it = data.begin(); |
---|
56 | |
---|
57 | for ( ; it!=data.end(); it++) |
---|
58 | { |
---|
59 | if ( it->sectionName == section ) |
---|
60 | { |
---|
61 | std::list<prefItem>::const_iterator it2 = it->items.begin(); |
---|
62 | |
---|
63 | for ( ; it2!=it->items.end(); it2++) |
---|
64 | { |
---|
65 | if ( it2->name == name ) |
---|
66 | return true; |
---|
67 | } |
---|
68 | |
---|
69 | break; |
---|
70 | } |
---|
71 | } |
---|
72 | |
---|
73 | return false; |
---|
74 | } |
---|
75 | |
---|
76 | /** |
---|
77 | * Check if this section exists |
---|
78 | * @param section name of the section |
---|
79 | * @param name name of the item to check |
---|
80 | * @return true if the item exists |
---|
81 | */ |
---|
82 | bool Preferences::sectionExists( const std::string& section ) |
---|
83 | { |
---|
84 | std::list<prefSection>::const_iterator it = data.begin(); |
---|
85 | |
---|
86 | for ( ; it!=data.end(); it++) |
---|
87 | { |
---|
88 | if ( it->sectionName == section ) |
---|
89 | { |
---|
90 | return true; |
---|
91 | } |
---|
92 | } |
---|
93 | |
---|
94 | return false; |
---|
95 | } |
---|
96 | |
---|
97 | /** |
---|
98 | * Sets the value of an item. Creates it if doesn't exits. |
---|
99 | * @param section name of the section |
---|
100 | * @param name name of the item |
---|
101 | * @param value value |
---|
102 | */ |
---|
103 | void Preferences::setString(const std::string& section, const std::string& name, const std::string& value, bool dontSetModified) |
---|
104 | { |
---|
105 | MultiType t(value); |
---|
106 | setMultiType(section, name, t, dontSetModified); |
---|
107 | } |
---|
108 | |
---|
109 | /** |
---|
110 | * Sets the value of an item. Creates it if doesn't exits. |
---|
111 | * @param section name of the section |
---|
112 | * @param name name of the item |
---|
113 | * @param value value |
---|
114 | */ |
---|
115 | void Preferences::setInt(const std::string& section, const std::string& name, int value, bool dontSetModified) |
---|
116 | { |
---|
117 | MultiType t(value); |
---|
118 | setMultiType(section, name, t, dontSetModified); |
---|
119 | } |
---|
120 | |
---|
121 | /** |
---|
122 | * Sets the value of an item. Creates it if doesn't exits. |
---|
123 | * @param section name of the section |
---|
124 | * @param name name of the item |
---|
125 | * @param value value |
---|
126 | */ |
---|
127 | void Preferences::setFloat(const std::string& section, const std::string& name, float value, bool dontSetModified) |
---|
128 | { |
---|
129 | MultiType t(value); |
---|
130 | setMultiType(section, name, t, dontSetModified); |
---|
131 | } |
---|
132 | |
---|
133 | /** |
---|
134 | * Get the value of an item |
---|
135 | * @param section name of the section |
---|
136 | * @param name name of the item to check |
---|
137 | * @param defaultValue value to return if item doesn't exist |
---|
138 | * @return value of the item if found. defaultValue else |
---|
139 | */ |
---|
140 | std::string Preferences::getString(const std::string& section, const std::string& name, const std::string& defaultValue) |
---|
141 | { |
---|
142 | return getMultiType(section, name, MultiType(defaultValue)).getString(); |
---|
143 | } |
---|
144 | |
---|
145 | /** |
---|
146 | * Get the value of an item |
---|
147 | * @param section name of the section |
---|
148 | * @param name name of the item to check |
---|
149 | * @param defaultValue value to return if item doesn't exist |
---|
150 | * @return value of the item if found. defaultValue else |
---|
151 | */ |
---|
152 | int Preferences::getInt(const std::string& section, const std::string& name, int defaultValue) |
---|
153 | { |
---|
154 | return getMultiType(section, name, MultiType(defaultValue)).getInt(); |
---|
155 | } |
---|
156 | |
---|
157 | /** |
---|
158 | * Get the value of an item |
---|
159 | * @param section name of the section |
---|
160 | * @param name name of the item to check |
---|
161 | * @param defaultValue value to return if item doesn't exist |
---|
162 | * @return value of the item if found. defaultValue else |
---|
163 | */ |
---|
164 | float Preferences::getFloat(const std::string& section, const std::string& name, float defaultValue) |
---|
165 | { |
---|
166 | return getMultiType(section, name, MultiType(defaultValue)).getFloat(); |
---|
167 | } |
---|
168 | |
---|
169 | /** |
---|
170 | * Sets the value of an item. Creates it if doesn't exits. |
---|
171 | * @param section name of the section |
---|
172 | * @param name name of the item |
---|
173 | * @param value value |
---|
174 | */ |
---|
175 | void Preferences::setMultiType(const std::string& section, const std::string& name, const MultiType& value, bool dontSetModified) |
---|
176 | { |
---|
177 | std::list<prefSection>::iterator it = data.begin(); |
---|
178 | |
---|
179 | for ( ; it!=data.end(); it++) |
---|
180 | { |
---|
181 | if ( it->sectionName == section ) |
---|
182 | { |
---|
183 | std::list<prefItem>::iterator it2 = it->items.begin(); |
---|
184 | |
---|
185 | for ( ; it2!=it->items.end(); it2++) |
---|
186 | { |
---|
187 | if ( it2->name == name ) |
---|
188 | { |
---|
189 | if (!dontSetModified) |
---|
190 | it2->modified = value.getString() != it2->value.getString(); |
---|
191 | |
---|
192 | it2->value = value; |
---|
193 | |
---|
194 | return; |
---|
195 | } |
---|
196 | } |
---|
197 | prefItem item; |
---|
198 | item.value = value; |
---|
199 | item.modified = !dontSetModified; |
---|
200 | item.name = name; |
---|
201 | it->items.push_back(item); |
---|
202 | return; |
---|
203 | } |
---|
204 | } |
---|
205 | |
---|
206 | prefItem item; |
---|
207 | item.value = value; |
---|
208 | item.modified = !dontSetModified; |
---|
209 | item.name = name; |
---|
210 | |
---|
211 | prefSection sec; |
---|
212 | sec.items.push_back(item); |
---|
213 | sec.sectionName = section; |
---|
214 | data.push_back( sec ); |
---|
215 | } |
---|
216 | |
---|
217 | /** |
---|
218 | * Get the value of an item |
---|
219 | * @param section name of the section |
---|
220 | * @param name name of the item to check |
---|
221 | * @param defaultValue value to return if item doesn't exist |
---|
222 | * @return value of the item if found. defaultValue else |
---|
223 | */ |
---|
224 | MultiType Preferences::getMultiType(const std::string& section, const std::string& name,const MultiType& defaultValue) |
---|
225 | { |
---|
226 | std::list<prefSection>::const_iterator it = data.begin(); |
---|
227 | |
---|
228 | for ( ; it!=data.end(); it++) |
---|
229 | { |
---|
230 | if ( it->sectionName == section ) |
---|
231 | { |
---|
232 | std::list<prefItem>::const_iterator it2 = it->items.begin(); |
---|
233 | |
---|
234 | for ( ; it2!=it->items.end(); it2++) |
---|
235 | { |
---|
236 | if ( it2->name == name ) |
---|
237 | { |
---|
238 | return it2->value; |
---|
239 | } |
---|
240 | } |
---|
241 | |
---|
242 | break; |
---|
243 | } |
---|
244 | } |
---|
245 | |
---|
246 | return defaultValue; |
---|
247 | } |
---|
248 | |
---|
249 | void Preferences::setUserIni(const std::string& fileName) |
---|
250 | { |
---|
251 | this->fileName = fileName; |
---|
252 | } |
---|
253 | |
---|
254 | bool Preferences::save() |
---|
255 | { |
---|
256 | if ( this->fileName == "" ) |
---|
257 | { |
---|
258 | PRINTF(1)("You must call setUserIni before you can call save()\n"); |
---|
259 | return false; |
---|
260 | } |
---|
261 | IniParser iniParser(this->fileName); |
---|
262 | |
---|
263 | std::list<prefSection>::iterator it = data.begin(); |
---|
264 | bool didChanges = false; |
---|
265 | for ( ; it!=data.end(); it++) |
---|
266 | { |
---|
267 | std::list<prefItem>::iterator it2 = it->items.begin(); |
---|
268 | |
---|
269 | for ( ; it2!=it->items.end(); it2++) |
---|
270 | { |
---|
271 | if ( it2->modified ) |
---|
272 | { |
---|
273 | iniParser.editVar(it2->name, it2->value.getString(), it->sectionName); |
---|
274 | didChanges = true; |
---|
275 | } |
---|
276 | } |
---|
277 | } |
---|
278 | /// HACK DO WE HAVE TO CHECK THIS?? |
---|
279 | //if ( didChanges ) |
---|
280 | { |
---|
281 | iniParser.writeFile( this->fileName ); |
---|
282 | } |
---|
283 | |
---|
284 | return true; |
---|
285 | } |
---|
286 | |
---|
287 | /** |
---|
288 | * prints out all section with its items and values |
---|
289 | */ |
---|
290 | void Preferences::debug() |
---|
291 | { |
---|
292 | std::list<prefSection>::iterator it = data.begin(); |
---|
293 | |
---|
294 | for ( ; it!=data.end(); it++) |
---|
295 | { |
---|
296 | PRINTF(0)("%s\n", it->sectionName.c_str()); |
---|
297 | std::list<prefItem>::iterator it2 = it->items.begin(); |
---|
298 | |
---|
299 | for ( ; it2!=it->items.end(); it2++) |
---|
300 | { |
---|
301 | PRINTF(0)("--> %s = '%s'%s\n", it2->name.c_str(), it2->value.getString().c_str(), ((!it2->modified)?"":" <modified>")); |
---|
302 | } |
---|
303 | } |
---|
304 | } |
---|
305 | |
---|
306 | /** |
---|
307 | * list all keys in section |
---|
308 | * @param section section |
---|
309 | * @return list of keys |
---|
310 | */ |
---|
311 | std::list< std::string > Preferences::listKeys( const std::string section ) |
---|
312 | { |
---|
313 | std::list<std::string> lst; |
---|
314 | |
---|
315 | std::list<prefSection>::const_iterator it = data.begin(); |
---|
316 | |
---|
317 | for ( ; it!=data.end(); it++) |
---|
318 | { |
---|
319 | if ( it->sectionName == section ) |
---|
320 | { |
---|
321 | std::list<prefItem>::const_iterator it2 = it->items.begin(); |
---|
322 | |
---|
323 | for ( ; it2!=it->items.end(); it2++) |
---|
324 | { |
---|
325 | lst.push_back( it2->name ); |
---|
326 | } |
---|
327 | |
---|
328 | break; |
---|
329 | } |
---|
330 | } |
---|
331 | |
---|
332 | return lst; |
---|
333 | } |
---|
334 | |
---|
335 | |
---|