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 | |
---|
21 | |
---|
22 | /** |
---|
23 | * standard constructor |
---|
24 | */ |
---|
25 | Preferences::Preferences () |
---|
26 | { |
---|
27 | this->setClassID(CL_PREFERENCES, "Preferences"); |
---|
28 | this->setName("Preferences"); |
---|
29 | this->fileName = ""; |
---|
30 | } |
---|
31 | |
---|
32 | /** |
---|
33 | * the singleton reference to this class |
---|
34 | */ |
---|
35 | Preferences* Preferences::singletonRef = NULL; |
---|
36 | |
---|
37 | /** |
---|
38 | @brief standard deconstructor |
---|
39 | */ |
---|
40 | Preferences::~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 | */ |
---|
51 | bool Preferences::exists( const std::string& section, const std::string& name) |
---|
52 | { |
---|
53 | std::list<prefSection>::const_iterator it = data.begin(); |
---|
54 | |
---|
55 | for ( ; it!=data.end(); it++) |
---|
56 | { |
---|
57 | if ( it->sectionName == section ) |
---|
58 | { |
---|
59 | std::list<prefItem>::const_iterator it2 = it->items.begin(); |
---|
60 | |
---|
61 | for ( ; it2!=it->items.end(); it2++) |
---|
62 | { |
---|
63 | if ( it2->name == name ) |
---|
64 | return true; |
---|
65 | } |
---|
66 | |
---|
67 | break; |
---|
68 | } |
---|
69 | } |
---|
70 | |
---|
71 | return false; |
---|
72 | } |
---|
73 | |
---|
74 | /** |
---|
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 | */ |
---|
80 | bool 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 | /** |
---|
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 | */ |
---|
101 | void Preferences::setString(const std::string& section, const std::string& name, const std::string& value, bool dontSetModified) |
---|
102 | { |
---|
103 | MultiType t(value); |
---|
104 | setMultiType(section, name, t, dontSetModified); |
---|
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 | */ |
---|
113 | void Preferences::setInt(const std::string& section, const std::string& name, int value, bool dontSetModified) |
---|
114 | { |
---|
115 | MultiType t(value); |
---|
116 | setMultiType(section, name, t, dontSetModified); |
---|
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 | */ |
---|
125 | void Preferences::setFloat(const std::string& section, const std::string& name, float value, bool dontSetModified) |
---|
126 | { |
---|
127 | MultiType t(value); |
---|
128 | setMultiType(section, name, t, dontSetModified); |
---|
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 | */ |
---|
138 | std::string Preferences::getString(const std::string& section, const std::string& name, const std::string& defaultValue) |
---|
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 | */ |
---|
150 | int Preferences::getInt(const std::string& section, const std::string& name, int defaultValue) |
---|
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 | */ |
---|
162 | float Preferences::getFloat(const std::string& section, const std::string& name, float defaultValue) |
---|
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 | */ |
---|
173 | void Preferences::setMultiType(const std::string& section, const std::string& name, const MultiType& value, bool dontSetModified) |
---|
174 | { |
---|
175 | std::list<prefSection>::iterator it = data.begin(); |
---|
176 | |
---|
177 | for ( ; it!=data.end(); it++) |
---|
178 | { |
---|
179 | if ( it->sectionName == section ) |
---|
180 | { |
---|
181 | std::list<prefItem>::iterator it2 = it->items.begin(); |
---|
182 | |
---|
183 | for ( ; it2!=it->items.end(); it2++) |
---|
184 | { |
---|
185 | if ( it2->name == name ) |
---|
186 | { |
---|
187 | if (!dontSetModified) |
---|
188 | it2->modified = value.getString() != it2->value.getString(); |
---|
189 | |
---|
190 | it2->value = value; |
---|
191 | |
---|
192 | return; |
---|
193 | } |
---|
194 | } |
---|
195 | prefItem item; |
---|
196 | item.value = value; |
---|
197 | item.modified = !dontSetModified; |
---|
198 | item.name = name; |
---|
199 | it->items.push_back(item); |
---|
200 | return; |
---|
201 | } |
---|
202 | } |
---|
203 | |
---|
204 | prefItem item; |
---|
205 | item.value = value; |
---|
206 | item.modified = !dontSetModified; |
---|
207 | item.name = name; |
---|
208 | |
---|
209 | prefSection sec; |
---|
210 | sec.items.push_back(item); |
---|
211 | sec.sectionName = section; |
---|
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 | */ |
---|
222 | MultiType Preferences::getMultiType(const std::string& section, const std::string& name,const MultiType& defaultValue) |
---|
223 | { |
---|
224 | std::list<prefSection>::const_iterator it = data.begin(); |
---|
225 | |
---|
226 | for ( ; it!=data.end(); it++) |
---|
227 | { |
---|
228 | if ( it->sectionName == section ) |
---|
229 | { |
---|
230 | std::list<prefItem>::const_iterator it2 = it->items.begin(); |
---|
231 | |
---|
232 | for ( ; it2!=it->items.end(); it2++) |
---|
233 | { |
---|
234 | if ( it2->name == name ) |
---|
235 | { |
---|
236 | return it2->value; |
---|
237 | } |
---|
238 | } |
---|
239 | |
---|
240 | break; |
---|
241 | } |
---|
242 | } |
---|
243 | |
---|
244 | return defaultValue; |
---|
245 | } |
---|
246 | |
---|
247 | void Preferences::setUserIni(const std::string& fileName) |
---|
248 | { |
---|
249 | this->fileName = fileName; |
---|
250 | } |
---|
251 | |
---|
252 | bool Preferences::save() |
---|
253 | { |
---|
254 | if ( this->fileName == "" ) |
---|
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 | } |
---|
276 | /// HACK DO WE HAVE TO CHECK THIS?? |
---|
277 | //if ( didChanges ) |
---|
278 | { |
---|
279 | iniParser.writeFile( this->fileName ); |
---|
280 | } |
---|
281 | |
---|
282 | return true; |
---|
283 | } |
---|
284 | |
---|
285 | /** |
---|
286 | * prints out all section with its items and values |
---|
287 | */ |
---|
288 | void Preferences::debug() |
---|
289 | { |
---|
290 | std::list<prefSection>::iterator it = data.begin(); |
---|
291 | |
---|
292 | for ( ; it!=data.end(); it++) |
---|
293 | { |
---|
294 | PRINTF(0)("%s\n", it->sectionName.c_str()); |
---|
295 | std::list<prefItem>::iterator it2 = it->items.begin(); |
---|
296 | |
---|
297 | for ( ; it2!=it->items.end(); it2++) |
---|
298 | { |
---|
299 | PRINTF(0)("--> %s = '%s'%s\n", it2->name.c_str(), it2->value.getString().c_str(), ((!it2->modified)?"":" <modified>")); |
---|
300 | } |
---|
301 | } |
---|
302 | } |
---|
303 | |
---|
304 | /** |
---|
305 | * list all keys in section |
---|
306 | * @param section section |
---|
307 | * @return list of keys |
---|
308 | */ |
---|
309 | std::list< std::string > Preferences::listKeys( const std::string section ) |
---|
310 | { |
---|
311 | std::list<std::string> lst; |
---|
312 | |
---|
313 | std::list<prefSection>::const_iterator it = data.begin(); |
---|
314 | |
---|
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 | |
---|