[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" |
---|
[8362] | 20 | #include "debug.h" |
---|
[6380] | 21 | |
---|
[9715] | 22 | ObjectListDefinition(Preferences); |
---|
[6380] | 23 | |
---|
| 24 | /** |
---|
| 25 | * standard constructor |
---|
| 26 | */ |
---|
| 27 | Preferences::Preferences () |
---|
| 28 | { |
---|
[9684] | 29 | this->registerObject(this, Preferences::_objectList); |
---|
[6380] | 30 | this->setName("Preferences"); |
---|
[7244] | 31 | this->fileName = ""; |
---|
[6380] | 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 | */ |
---|
[7244] | 53 | bool Preferences::exists( const std::string& section, const std::string& name) |
---|
[6380] | 54 | { |
---|
| 55 | std::list<prefSection>::const_iterator it = data.begin(); |
---|
| 56 | |
---|
| 57 | for ( ; it!=data.end(); it++) |
---|
| 58 | { |
---|
[7244] | 59 | if ( it->sectionName == section ) |
---|
[6380] | 60 | { |
---|
| 61 | std::list<prefItem>::const_iterator it2 = it->items.begin(); |
---|
| 62 | |
---|
[6383] | 63 | for ( ; it2!=it->items.end(); it2++) |
---|
[6380] | 64 | { |
---|
[7244] | 65 | if ( it2->name == name ) |
---|
[6380] | 66 | return true; |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | break; |
---|
| 70 | } |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | return false; |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | /** |
---|
[7248] | 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 | /** |
---|
[6380] | 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 | */ |
---|
[7244] | 103 | void Preferences::setString(const std::string& section, const std::string& name, const std::string& value, bool dontSetModified) |
---|
[6380] | 104 | { |
---|
[6388] | 105 | MultiType t(value); |
---|
| 106 | setMultiType(section, name, t, dontSetModified); |
---|
[6380] | 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 | */ |
---|
[7244] | 115 | void Preferences::setInt(const std::string& section, const std::string& name, int value, bool dontSetModified) |
---|
[6380] | 116 | { |
---|
[6388] | 117 | MultiType t(value); |
---|
| 118 | setMultiType(section, name, t, dontSetModified); |
---|
[6380] | 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 | */ |
---|
[7244] | 127 | void Preferences::setFloat(const std::string& section, const std::string& name, float value, bool dontSetModified) |
---|
[6380] | 128 | { |
---|
[6388] | 129 | MultiType t(value); |
---|
| 130 | setMultiType(section, name, t, dontSetModified); |
---|
[6380] | 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 | */ |
---|
[7661] | 140 | std::string Preferences::getString(const std::string& section, const std::string& name, const std::string& defaultValue) |
---|
[6380] | 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 | */ |
---|
[7244] | 152 | int Preferences::getInt(const std::string& section, const std::string& name, int defaultValue) |
---|
[6380] | 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 | */ |
---|
[7244] | 164 | float Preferences::getFloat(const std::string& section, const std::string& name, float defaultValue) |
---|
[6380] | 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 | */ |
---|
[7244] | 175 | void Preferences::setMultiType(const std::string& section, const std::string& name, const MultiType& value, bool dontSetModified) |
---|
[6380] | 176 | { |
---|
| 177 | std::list<prefSection>::iterator it = data.begin(); |
---|
| 178 | |
---|
| 179 | for ( ; it!=data.end(); it++) |
---|
| 180 | { |
---|
[7244] | 181 | if ( it->sectionName == section ) |
---|
[6380] | 182 | { |
---|
| 183 | std::list<prefItem>::iterator it2 = it->items.begin(); |
---|
| 184 | |
---|
[6383] | 185 | for ( ; it2!=it->items.end(); it2++) |
---|
[6380] | 186 | { |
---|
[7244] | 187 | if ( it2->name == name ) |
---|
[6380] | 188 | { |
---|
[6388] | 189 | if (!dontSetModified) |
---|
[7244] | 190 | it2->modified = value.getString() != it2->value.getString(); |
---|
[6388] | 191 | |
---|
[6380] | 192 | it2->value = value; |
---|
[6388] | 193 | |
---|
[6380] | 194 | return; |
---|
| 195 | } |
---|
| 196 | } |
---|
| 197 | prefItem item; |
---|
| 198 | item.value = value; |
---|
[6388] | 199 | item.modified = !dontSetModified; |
---|
[7244] | 200 | item.name = name; |
---|
[6380] | 201 | it->items.push_back(item); |
---|
| 202 | return; |
---|
| 203 | } |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | prefItem item; |
---|
| 207 | item.value = value; |
---|
[6388] | 208 | item.modified = !dontSetModified; |
---|
[7244] | 209 | item.name = name; |
---|
[6380] | 210 | |
---|
| 211 | prefSection sec; |
---|
| 212 | sec.items.push_back(item); |
---|
[7244] | 213 | sec.sectionName = section; |
---|
[6380] | 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 | */ |
---|
[7244] | 224 | MultiType Preferences::getMultiType(const std::string& section, const std::string& name,const MultiType& defaultValue) |
---|
[6380] | 225 | { |
---|
| 226 | std::list<prefSection>::const_iterator it = data.begin(); |
---|
| 227 | |
---|
| 228 | for ( ; it!=data.end(); it++) |
---|
| 229 | { |
---|
[7244] | 230 | if ( it->sectionName == section ) |
---|
[6380] | 231 | { |
---|
| 232 | std::list<prefItem>::const_iterator it2 = it->items.begin(); |
---|
| 233 | |
---|
[6383] | 234 | for ( ; it2!=it->items.end(); it2++) |
---|
[6380] | 235 | { |
---|
[7244] | 236 | if ( it2->name == name ) |
---|
[6380] | 237 | { |
---|
| 238 | return it2->value; |
---|
| 239 | } |
---|
| 240 | } |
---|
| 241 | |
---|
| 242 | break; |
---|
| 243 | } |
---|
| 244 | } |
---|
| 245 | |
---|
| 246 | return defaultValue; |
---|
| 247 | } |
---|
[6381] | 248 | |
---|
[7244] | 249 | void Preferences::setUserIni(const std::string& fileName) |
---|
[6388] | 250 | { |
---|
[7244] | 251 | this->fileName = fileName; |
---|
[6388] | 252 | } |
---|
| 253 | |
---|
| 254 | bool Preferences::save() |
---|
| 255 | { |
---|
[7244] | 256 | if ( this->fileName == "" ) |
---|
[6388] | 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 | { |
---|
[9880] | 273 | iniParser.editEntry(it->sectionName, it2->name, it2->value.getString()); |
---|
[6388] | 274 | didChanges = true; |
---|
| 275 | } |
---|
| 276 | } |
---|
| 277 | } |
---|
[7661] | 278 | /// HACK DO WE HAVE TO CHECK THIS?? |
---|
| 279 | //if ( didChanges ) |
---|
[6388] | 280 | { |
---|
| 281 | iniParser.writeFile( this->fileName ); |
---|
| 282 | } |
---|
| 283 | |
---|
| 284 | return true; |
---|
| 285 | } |
---|
| 286 | |
---|
[6381] | 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 | { |
---|
[7244] | 296 | PRINTF(0)("%s\n", it->sectionName.c_str()); |
---|
[6381] | 297 | std::list<prefItem>::iterator it2 = it->items.begin(); |
---|
| 298 | |
---|
[6383] | 299 | for ( ; it2!=it->items.end(); it2++) |
---|
[6381] | 300 | { |
---|
[7244] | 301 | PRINTF(0)("--> %s = '%s'%s\n", it2->name.c_str(), it2->value.getString().c_str(), ((!it2->modified)?"":" <modified>")); |
---|
[6381] | 302 | } |
---|
| 303 | } |
---|
| 304 | } |
---|
| 305 | |
---|
[7248] | 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; |
---|
[7661] | 314 | |
---|
[7248] | 315 | std::list<prefSection>::const_iterator it = data.begin(); |
---|
[6381] | 316 | |
---|
[7248] | 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 | |
---|