| 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 | |
|---|
| 20 | using namespace std; |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | /** |
|---|
| 24 | * standard constructor |
|---|
| 25 | */ |
|---|
| 26 | Preferences::Preferences () |
|---|
| 27 | { |
|---|
| 28 | this->setClassID(CL_PREFERENCES, "Preferences"); |
|---|
| 29 | this->setName("Preferences"); |
|---|
| 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 char* section, const char* name) |
|---|
| 52 | { |
|---|
| 53 | std::list<prefSection>::const_iterator it = data.begin(); |
|---|
| 54 | |
|---|
| 55 | for ( ; it!=data.end(); it++) |
|---|
| 56 | { |
|---|
| 57 | if ( strcmp(it->sectionName, section) == 0 ) |
|---|
| 58 | { |
|---|
| 59 | std::list<prefItem>::const_iterator it2 = it->items.begin(); |
|---|
| 60 | |
|---|
| 61 | for ( ; it2!=it->items.begin(); it2++) |
|---|
| 62 | { |
|---|
| 63 | if ( strcmp(it2->name, name) == 0 ) |
|---|
| 64 | return true; |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | break; |
|---|
| 68 | } |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | return false; |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | /** |
|---|
| 75 | * Sets the value of an item. Creates it if doesn't exits. |
|---|
| 76 | * @param section name of the section |
|---|
| 77 | * @param name name of the item |
|---|
| 78 | * @param value value |
|---|
| 79 | */ |
|---|
| 80 | void Preferences::setString(const char* section, const char* name, const char* value) |
|---|
| 81 | { |
|---|
| 82 | setMultiType(section, name, MultiType(value)); |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | /** |
|---|
| 86 | * Sets the value of an item. Creates it if doesn't exits. |
|---|
| 87 | * @param section name of the section |
|---|
| 88 | * @param name name of the item |
|---|
| 89 | * @param value value |
|---|
| 90 | */ |
|---|
| 91 | void Preferences::setInt(const char* section, const char* name, int value) |
|---|
| 92 | { |
|---|
| 93 | setMultiType(section, name, MultiType(value)); |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | /** |
|---|
| 97 | * Sets the value of an item. Creates it if doesn't exits. |
|---|
| 98 | * @param section name of the section |
|---|
| 99 | * @param name name of the item |
|---|
| 100 | * @param value value |
|---|
| 101 | */ |
|---|
| 102 | void Preferences::setFloat(const char* section, const char* name, float value) |
|---|
| 103 | { |
|---|
| 104 | setMultiType(section, name, MultiType(value)); |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | /** |
|---|
| 108 | * Get the value of an item |
|---|
| 109 | * @param section name of the section |
|---|
| 110 | * @param name name of the item to check |
|---|
| 111 | * @param defaultValue value to return if item doesn't exist |
|---|
| 112 | * @return value of the item if found. defaultValue else |
|---|
| 113 | */ |
|---|
| 114 | const char* Preferences::getString(const char* section, const char* name, const char* defaultValue) |
|---|
| 115 | { |
|---|
| 116 | return getMultiType(section, name, MultiType(defaultValue)).getString(); |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | /** |
|---|
| 120 | * Get the value of an item |
|---|
| 121 | * @param section name of the section |
|---|
| 122 | * @param name name of the item to check |
|---|
| 123 | * @param defaultValue value to return if item doesn't exist |
|---|
| 124 | * @return value of the item if found. defaultValue else |
|---|
| 125 | */ |
|---|
| 126 | int Preferences::getInt(const char* section, const char* name, int defaultValue) |
|---|
| 127 | { |
|---|
| 128 | return getMultiType(section, name, MultiType(defaultValue)).getInt(); |
|---|
| 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 | float Preferences::getFloat(const char* section, const char* name, float defaultValue) |
|---|
| 139 | { |
|---|
| 140 | return getMultiType(section, name, MultiType(defaultValue)).getFloat(); |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | /** |
|---|
| 144 | * Sets the value of an item. Creates it if doesn't exits. |
|---|
| 145 | * @param section name of the section |
|---|
| 146 | * @param name name of the item |
|---|
| 147 | * @param value value |
|---|
| 148 | */ |
|---|
| 149 | void Preferences::setMultiType(const char* section, const char* name, const MultiType& value) |
|---|
| 150 | { |
|---|
| 151 | std::list<prefSection>::iterator it = data.begin(); |
|---|
| 152 | |
|---|
| 153 | for ( ; it!=data.end(); it++) |
|---|
| 154 | { |
|---|
| 155 | if ( strcmp(it->sectionName, section) == 0 ) |
|---|
| 156 | { |
|---|
| 157 | std::list<prefItem>::iterator it2 = it->items.begin(); |
|---|
| 158 | |
|---|
| 159 | for ( ; it2!=it->items.begin(); it2++) |
|---|
| 160 | { |
|---|
| 161 | if ( strcmp(it2->name, name) == 0 ) |
|---|
| 162 | { |
|---|
| 163 | it2->value = value; |
|---|
| 164 | return; |
|---|
| 165 | } |
|---|
| 166 | } |
|---|
| 167 | prefItem item; |
|---|
| 168 | item.value = value; |
|---|
| 169 | item.name = new char[strlen(name)+1]; |
|---|
| 170 | strcpy( item.name, name ); |
|---|
| 171 | it->items.push_back(item); |
|---|
| 172 | return; |
|---|
| 173 | } |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | prefItem item; |
|---|
| 177 | item.value = value; |
|---|
| 178 | item.name = new char[strlen(name)+1]; |
|---|
| 179 | strcpy( item.name, name ); |
|---|
| 180 | |
|---|
| 181 | prefSection sec; |
|---|
| 182 | sec.items.push_back(item); |
|---|
| 183 | sec.sectionName = new char[strlen(section)+1]; |
|---|
| 184 | strcpy( sec.sectionName, section ); |
|---|
| 185 | data.push_back( sec ); |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | /** |
|---|
| 189 | * Get the value of an item |
|---|
| 190 | * @param section name of the section |
|---|
| 191 | * @param name name of the item to check |
|---|
| 192 | * @param defaultValue value to return if item doesn't exist |
|---|
| 193 | * @return value of the item if found. defaultValue else |
|---|
| 194 | */ |
|---|
| 195 | MultiType Preferences::getMultiType(const char* section, const char* name,const MultiType& defaultValue) |
|---|
| 196 | { |
|---|
| 197 | std::list<prefSection>::const_iterator it = data.begin(); |
|---|
| 198 | |
|---|
| 199 | for ( ; it!=data.end(); it++) |
|---|
| 200 | { |
|---|
| 201 | if ( strcmp(it->sectionName, section) == 0 ) |
|---|
| 202 | { |
|---|
| 203 | std::list<prefItem>::const_iterator it2 = it->items.begin(); |
|---|
| 204 | |
|---|
| 205 | for ( ; it2!=it->items.begin(); it2++) |
|---|
| 206 | { |
|---|
| 207 | if ( strcmp(it2->name, name) == 0 ) |
|---|
| 208 | { |
|---|
| 209 | return it2->value; |
|---|
| 210 | } |
|---|
| 211 | } |
|---|
| 212 | |
|---|
| 213 | break; |
|---|
| 214 | } |
|---|
| 215 | } |
|---|
| 216 | |
|---|
| 217 | return defaultValue; |
|---|
| 218 | } |
|---|
| 219 | |
|---|
| 220 | /** |
|---|
| 221 | * prints out all section with its items and values |
|---|
| 222 | */ |
|---|
| 223 | void Preferences::debug() |
|---|
| 224 | { |
|---|
| 225 | std::list<prefSection>::iterator it = data.begin(); |
|---|
| 226 | |
|---|
| 227 | for ( ; it!=data.end(); it++) |
|---|
| 228 | { |
|---|
| 229 | PRINTF(0)("%s\n", it->sectionName); |
|---|
| 230 | std::list<prefItem>::iterator it2 = it->items.begin(); |
|---|
| 231 | |
|---|
| 232 | for ( ; it2!=it->items.begin(); it2++) |
|---|
| 233 | { |
|---|
| 234 | PRINTF(0)("--> %s = '%s'\n", it2->name, it2->value.getString()); |
|---|
| 235 | } |
|---|
| 236 | } |
|---|
| 237 | } |
|---|
| 238 | |
|---|
| 239 | |
|---|