| 1 | /* | 
|---|
| 2 | *   ORXONOX - the hottest 3D action shooter ever to exist | 
|---|
| 3 | *                    > www.orxonox.net < | 
|---|
| 4 | * | 
|---|
| 5 | * | 
|---|
| 6 | *   License notice: | 
|---|
| 7 | * | 
|---|
| 8 | *   This program is free software; you can redistribute it and/or | 
|---|
| 9 | *   modify it under the terms of the GNU General Public License | 
|---|
| 10 | *   as published by the Free Software Foundation; either version 2 | 
|---|
| 11 | *   of the License, or (at your option) any later version. | 
|---|
| 12 | * | 
|---|
| 13 | *   This program is distributed in the hope that it will be useful, | 
|---|
| 14 | *   but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 15 | *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 16 | *   GNU General Public License for more details. | 
|---|
| 17 | * | 
|---|
| 18 | *   You should have received a copy of the GNU General Public License | 
|---|
| 19 | *   along with this program; if not, write to the Free Software | 
|---|
| 20 | *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. | 
|---|
| 21 | * | 
|---|
| 22 | *   Author: | 
|---|
| 23 | *      Fabian 'x3n' Landau | 
|---|
| 24 | *   Co-authors: | 
|---|
| 25 | *      ... | 
|---|
| 26 | * | 
|---|
| 27 | */ | 
|---|
| 28 |  | 
|---|
| 29 | #ifndef _ConfigFileManager_H__ | 
|---|
| 30 | #define _ConfigFileManager_H__ | 
|---|
| 31 |  | 
|---|
| 32 | #include "CorePrereqs.h" | 
|---|
| 33 |  | 
|---|
| 34 | #include <iostream> | 
|---|
| 35 | #include <string> | 
|---|
| 36 | #include <list> | 
|---|
| 37 | #include <map> | 
|---|
| 38 |  | 
|---|
| 39 | #include "util/Math.h" | 
|---|
| 40 |  | 
|---|
| 41 | namespace orxonox | 
|---|
| 42 | { | 
|---|
| 43 | // Use unsigned int as config file type to have an arbitrary number of files | 
|---|
| 44 | class ConfigFileType | 
|---|
| 45 | { | 
|---|
| 46 | public: | 
|---|
| 47 | ConfigFileType() { } | 
|---|
| 48 | ConfigFileType(unsigned int type)              { type_ = type; } | 
|---|
| 49 | ConfigFileType(const ConfigFileType& instance) { type_ = instance.type_; } | 
|---|
| 50 |  | 
|---|
| 51 | operator unsigned int() { return type_; } | 
|---|
| 52 | ConfigFileType& operator =(unsigned int type) { type_ = type; return *this; } | 
|---|
| 53 | bool operator <(const ConfigFileType& right) const   { return (type_ < right.type_); } | 
|---|
| 54 |  | 
|---|
| 55 | /* *** Put the different config file types here *** */ | 
|---|
| 56 | static const unsigned int NoType              = 0; | 
|---|
| 57 | static const unsigned int Settings            = 1; | 
|---|
| 58 | static const unsigned int JoyStickCalibration = 2; | 
|---|
| 59 |  | 
|---|
| 60 | static const unsigned int numberOfReservedTypes = 1024; | 
|---|
| 61 |  | 
|---|
| 62 | private: | 
|---|
| 63 | unsigned int type_; | 
|---|
| 64 | }; | 
|---|
| 65 |  | 
|---|
| 66 | bool config(const std::string& classname, const std::string& varname, const std::string& value); | 
|---|
| 67 | bool tconfig(const std::string& classname, const std::string& varname, const std::string& value); | 
|---|
| 68 | void reloadConfig(); | 
|---|
| 69 | void saveConfig(); | 
|---|
| 70 | void cleanConfig(); | 
|---|
| 71 | void loadSettings(const std::string& filename); | 
|---|
| 72 |  | 
|---|
| 73 |  | 
|---|
| 74 | ///////////////////// | 
|---|
| 75 | // ConfigFileEntry // | 
|---|
| 76 | ///////////////////// | 
|---|
| 77 | class _CoreExport ConfigFileEntry | 
|---|
| 78 | { | 
|---|
| 79 | public: | 
|---|
| 80 | virtual ~ConfigFileEntry() {}; | 
|---|
| 81 | virtual void setValue(const std::string& value) = 0; | 
|---|
| 82 | virtual std::string getValue() const = 0; | 
|---|
| 83 | virtual const std::string& getName() const = 0; | 
|---|
| 84 | virtual void setComment(const std::string& comment) = 0; | 
|---|
| 85 | virtual unsigned int getIndex() const { return 0; } | 
|---|
| 86 | virtual void setString(bool bString) = 0; | 
|---|
| 87 | virtual std::string getFileEntry() const = 0; | 
|---|
| 88 | }; | 
|---|
| 89 |  | 
|---|
| 90 |  | 
|---|
| 91 | ////////////////////////// | 
|---|
| 92 | // ConfigFileEntryValue // | 
|---|
| 93 | ////////////////////////// | 
|---|
| 94 | class _CoreExport ConfigFileEntryValue : public ConfigFileEntry | 
|---|
| 95 | { | 
|---|
| 96 | public: | 
|---|
| 97 | inline ConfigFileEntryValue(const std::string& name, const std::string& value = "", bool bString = false, const std::string& additionalComment = "") : name_(name), value_(value), bString_(bString), additionalComment_(additionalComment) {} | 
|---|
| 98 | inline virtual ~ConfigFileEntryValue() {} | 
|---|
| 99 |  | 
|---|
| 100 | inline virtual const std::string& getName() const | 
|---|
| 101 | { return this->name_; } | 
|---|
| 102 |  | 
|---|
| 103 | inline virtual void setComment(const std::string& comment) | 
|---|
| 104 | { this->additionalComment_ = comment; } | 
|---|
| 105 |  | 
|---|
| 106 | virtual void setValue(const std::string& value); | 
|---|
| 107 | virtual std::string getValue() const; | 
|---|
| 108 |  | 
|---|
| 109 | inline bool isString() const | 
|---|
| 110 | { return this->bString_; } | 
|---|
| 111 | inline void setString(bool bString) | 
|---|
| 112 | { this->bString_ = bString; } | 
|---|
| 113 |  | 
|---|
| 114 | virtual std::string getFileEntry() const; | 
|---|
| 115 |  | 
|---|
| 116 | protected: | 
|---|
| 117 | std::string name_; | 
|---|
| 118 | std::string value_; | 
|---|
| 119 | bool bString_; | 
|---|
| 120 | std::string additionalComment_; | 
|---|
| 121 | }; | 
|---|
| 122 |  | 
|---|
| 123 |  | 
|---|
| 124 | /////////////////////////////// | 
|---|
| 125 | // ConfigFileEntryVectorValue // | 
|---|
| 126 | /////////////////////////////// | 
|---|
| 127 | class _CoreExport ConfigFileEntryVectorValue : public ConfigFileEntryValue | 
|---|
| 128 | { | 
|---|
| 129 | public: | 
|---|
| 130 | inline ConfigFileEntryVectorValue(const std::string& name, unsigned int index, const std::string& value = "", bool bString = false, const std::string& additionalComment = "") : ConfigFileEntryValue(name, value, bString, additionalComment), index_(index) {} | 
|---|
| 131 | inline virtual ~ConfigFileEntryVectorValue() {} | 
|---|
| 132 |  | 
|---|
| 133 | inline virtual unsigned int getIndex() const | 
|---|
| 134 | { return this->index_; } | 
|---|
| 135 |  | 
|---|
| 136 | virtual std::string getFileEntry() const; | 
|---|
| 137 |  | 
|---|
| 138 | private: | 
|---|
| 139 | unsigned int index_; | 
|---|
| 140 | }; | 
|---|
| 141 |  | 
|---|
| 142 |  | 
|---|
| 143 | //////////////////////////// | 
|---|
| 144 | // ConfigFileEntryComment // | 
|---|
| 145 | //////////////////////////// | 
|---|
| 146 | class _CoreExport ConfigFileEntryComment : public ConfigFileEntry | 
|---|
| 147 | { | 
|---|
| 148 | public: | 
|---|
| 149 | inline ConfigFileEntryComment(const std::string& comment) : comment_(comment) {} | 
|---|
| 150 | inline virtual ~ConfigFileEntryComment() {} | 
|---|
| 151 |  | 
|---|
| 152 | inline virtual const std::string& getName() const | 
|---|
| 153 | { return this->comment_; } | 
|---|
| 154 |  | 
|---|
| 155 | inline virtual void setComment(const std::string& comment) | 
|---|
| 156 | { this->comment_ = comment; } | 
|---|
| 157 |  | 
|---|
| 158 | inline virtual void setValue(const std::string& value) | 
|---|
| 159 | {} | 
|---|
| 160 | inline virtual std::string getValue() const | 
|---|
| 161 | { return this->comment_; } | 
|---|
| 162 |  | 
|---|
| 163 | inline void setString(bool bString) {} | 
|---|
| 164 |  | 
|---|
| 165 | inline virtual std::string getFileEntry() const | 
|---|
| 166 | { return this->comment_; } | 
|---|
| 167 |  | 
|---|
| 168 | private: | 
|---|
| 169 | std::string comment_; | 
|---|
| 170 | }; | 
|---|
| 171 |  | 
|---|
| 172 |  | 
|---|
| 173 | /////////////////////// | 
|---|
| 174 | // ConfigFileSection // | 
|---|
| 175 | /////////////////////// | 
|---|
| 176 | class _CoreExport ConfigFileSection | 
|---|
| 177 | { | 
|---|
| 178 | friend class ConfigFile; | 
|---|
| 179 |  | 
|---|
| 180 | public: | 
|---|
| 181 | inline ConfigFileSection(const std::string& name, const std::string& additionalComment = "") : name_(name), additionalComment_(additionalComment), bUpdated_(false) {} | 
|---|
| 182 | ~ConfigFileSection(); | 
|---|
| 183 |  | 
|---|
| 184 | inline const std::string& getName() const | 
|---|
| 185 | { return this->name_; } | 
|---|
| 186 |  | 
|---|
| 187 | inline void setComment(const std::string& comment) | 
|---|
| 188 | { this->additionalComment_ = comment; } | 
|---|
| 189 |  | 
|---|
| 190 | inline void setValue(const std::string& name, const std::string& value, bool bString) | 
|---|
| 191 | { this->getEntry(name, value, bString)->setValue(value); } | 
|---|
| 192 | inline std::string getValue(const std::string& name, const std::string& fallback, bool bString) | 
|---|
| 193 | { return this->getEntry(name, fallback, bString)->getValue(); } | 
|---|
| 194 |  | 
|---|
| 195 | inline void setValue(const std::string& name, unsigned int index, const std::string& value, bool bString) | 
|---|
| 196 | { this->getEntry(name, index, value, bString)->setValue(value); } | 
|---|
| 197 | inline std::string getValue(const std::string& name, unsigned int index, const std::string& fallback, bool bString) | 
|---|
| 198 | { return this->getEntry(name, index, fallback, bString)->getValue(); } | 
|---|
| 199 |  | 
|---|
| 200 | void deleteVectorEntries(const std::string& name, unsigned int startindex = 0); | 
|---|
| 201 | unsigned int getVectorSize(const std::string& name); | 
|---|
| 202 |  | 
|---|
| 203 | std::string getFileEntry() const; | 
|---|
| 204 |  | 
|---|
| 205 | private: | 
|---|
| 206 | std::list<ConfigFileEntry*>& getEntries() | 
|---|
| 207 | { return this->entries_; } | 
|---|
| 208 | std::list<ConfigFileEntry*>::const_iterator getEntriesBegin() const | 
|---|
| 209 | { return this->entries_.begin(); } | 
|---|
| 210 | std::list<ConfigFileEntry*>::const_iterator getEntriesEnd() const | 
|---|
| 211 | { return this->entries_.end(); } | 
|---|
| 212 |  | 
|---|
| 213 | std::list<ConfigFileEntry*>::iterator getEntryIterator(const std::string& name, const std::string& fallback, bool bString); | 
|---|
| 214 | std::list<ConfigFileEntry*>::iterator getEntryIterator(const std::string& name, unsigned int index, const std::string& fallback, bool bString); | 
|---|
| 215 |  | 
|---|
| 216 | inline ConfigFileEntry* getEntry(const std::string& name, const std::string& fallback, bool bString) | 
|---|
| 217 | { return (*this->getEntryIterator(name, fallback, bString)); } | 
|---|
| 218 | inline ConfigFileEntry* getEntry(const std::string& name, unsigned int index, const std::string& fallback, bool bString) | 
|---|
| 219 | { return (*this->getEntryIterator(name, index, fallback, bString)); } | 
|---|
| 220 |  | 
|---|
| 221 | std::string name_; | 
|---|
| 222 | std::string additionalComment_; | 
|---|
| 223 | std::list<ConfigFileEntry*> entries_; | 
|---|
| 224 | bool bUpdated_; | 
|---|
| 225 | }; | 
|---|
| 226 |  | 
|---|
| 227 |  | 
|---|
| 228 | //////////////// | 
|---|
| 229 | // ConfigFile // | 
|---|
| 230 | //////////////// | 
|---|
| 231 | class _CoreExport ConfigFile | 
|---|
| 232 | { | 
|---|
| 233 | public: | 
|---|
| 234 | inline ConfigFile(const std::string& filename, ConfigFileType type) | 
|---|
| 235 | : filename_(filename) | 
|---|
| 236 | , type_(type) | 
|---|
| 237 | , bUpdated_(false) | 
|---|
| 238 | { } | 
|---|
| 239 | ~ConfigFile(); | 
|---|
| 240 |  | 
|---|
| 241 | void load(bool bCreateIfNotExisting = true); | 
|---|
| 242 | void save() const; | 
|---|
| 243 | void saveAs(const std::string& filename); | 
|---|
| 244 | void clean(bool bCleanComments = false); | 
|---|
| 245 | void clear(); | 
|---|
| 246 |  | 
|---|
| 247 | const std::string& getFilename() { return this->filename_; } | 
|---|
| 248 |  | 
|---|
| 249 | inline void setValue(const std::string& section, const std::string& name, const std::string& value, bool bString) | 
|---|
| 250 | { this->getSection(section)->setValue(name, value, bString); this->save(); } | 
|---|
| 251 | inline std::string getValue(const std::string& section, const std::string& name, const std::string& fallback, bool bString) | 
|---|
| 252 | { std::string output = this->getSection(section)->getValue(name, fallback, bString); this->saveIfUpdated(); return output; } | 
|---|
| 253 |  | 
|---|
| 254 | inline void setValue(const std::string& section, const std::string& name, unsigned int index, const std::string& value, bool bString) | 
|---|
| 255 | { this->getSection(section)->setValue(name, index, value, bString); this->save(); } | 
|---|
| 256 | inline std::string getValue(const std::string& section, const std::string& name, unsigned int index, const std::string& fallback, bool bString) | 
|---|
| 257 | { std::string output = this->getSection(section)->getValue(name, index, fallback, bString); this->saveIfUpdated(); return output; } | 
|---|
| 258 |  | 
|---|
| 259 | inline void deleteVectorEntries(const std::string& section, const std::string& name, unsigned int startindex = 0) | 
|---|
| 260 | { this->getSection(section)->deleteVectorEntries(name, startindex); } | 
|---|
| 261 | inline unsigned int getVectorSize(const std::string& section, const std::string& name) | 
|---|
| 262 | { return this->getSection(section)->getVectorSize(name); } | 
|---|
| 263 |  | 
|---|
| 264 | void updateConfigValues(); | 
|---|
| 265 |  | 
|---|
| 266 | private: | 
|---|
| 267 | ConfigFileSection* getSection(const std::string& section); | 
|---|
| 268 | void saveIfUpdated(); | 
|---|
| 269 |  | 
|---|
| 270 | std::string filename_; | 
|---|
| 271 | ConfigFileType type_; | 
|---|
| 272 | std::list<ConfigFileSection*> sections_; | 
|---|
| 273 | bool bUpdated_; | 
|---|
| 274 | }; | 
|---|
| 275 |  | 
|---|
| 276 |  | 
|---|
| 277 | /////////////////////// | 
|---|
| 278 | // ConfigFileManager // | 
|---|
| 279 | /////////////////////// | 
|---|
| 280 | class _CoreExport ConfigFileManager | 
|---|
| 281 | { | 
|---|
| 282 | public: | 
|---|
| 283 | ConfigFileManager(); | 
|---|
| 284 | ~ConfigFileManager(); | 
|---|
| 285 |  | 
|---|
| 286 | void load(); | 
|---|
| 287 | void save(); | 
|---|
| 288 | void clean(bool bCleanComments = false); | 
|---|
| 289 |  | 
|---|
| 290 | void setFilename(ConfigFileType type, const std::string& filename); | 
|---|
| 291 | const std::string& getFilename(ConfigFileType type); | 
|---|
| 292 |  | 
|---|
| 293 | ConfigFileType getNewConfigFileType() { return mininmalFreeType_++; } | 
|---|
| 294 |  | 
|---|
| 295 | void load(ConfigFileType type); | 
|---|
| 296 | void save(ConfigFileType type); | 
|---|
| 297 | void saveAs(ConfigFileType type, const std::string& saveFilename); | 
|---|
| 298 | void clean(ConfigFileType type, bool bCleanComments = false); | 
|---|
| 299 |  | 
|---|
| 300 | inline void setValue(ConfigFileType type, const std::string& section, const std::string& name, const std::string& value, bool bString) | 
|---|
| 301 | { this->getFile(type)->setValue(section, name, value, bString); } | 
|---|
| 302 | inline std::string getValue(ConfigFileType type, const std::string& section, const std::string& name, const std::string& fallback, bool bString) | 
|---|
| 303 | { return this->getFile(type)->getValue(section, name, fallback, bString); } | 
|---|
| 304 |  | 
|---|
| 305 | inline void setValue(ConfigFileType type, const std::string& section, const std::string& name, unsigned int index, const std::string& value, bool bString) | 
|---|
| 306 | { this->getFile(type)->setValue(section, name, index, value, bString); } | 
|---|
| 307 | inline std::string getValue(ConfigFileType type, const std::string& section, const std::string& name, unsigned int index, const std::string& fallback, bool bString) | 
|---|
| 308 | { return this->getFile(type)->getValue(section, name, index, fallback, bString); } | 
|---|
| 309 |  | 
|---|
| 310 | inline void deleteVectorEntries(ConfigFileType type, const std::string& section, const std::string& name, unsigned int startindex = 0) | 
|---|
| 311 | { this->getFile(type)->deleteVectorEntries(section, name, startindex); } | 
|---|
| 312 | inline unsigned int getVectorSize(ConfigFileType type, const std::string& section, const std::string& name) | 
|---|
| 313 | { return this->getFile(type)->getVectorSize(section, name); } | 
|---|
| 314 |  | 
|---|
| 315 | void updateConfigValues(); | 
|---|
| 316 | void updateConfigValues(ConfigFileType type); | 
|---|
| 317 |  | 
|---|
| 318 | static ConfigFileManager& getInstance() { assert(singletonRef_s); return *singletonRef_s; } | 
|---|
| 319 |  | 
|---|
| 320 | private: | 
|---|
| 321 | ConfigFileManager(const ConfigFileManager&); | 
|---|
| 322 |  | 
|---|
| 323 | ConfigFile* getFile(ConfigFileType type); | 
|---|
| 324 |  | 
|---|
| 325 | std::map<ConfigFileType, ConfigFile*> configFiles_; | 
|---|
| 326 | unsigned int mininmalFreeType_; | 
|---|
| 327 |  | 
|---|
| 328 | static ConfigFileManager* singletonRef_s; | 
|---|
| 329 | }; | 
|---|
| 330 | } | 
|---|
| 331 |  | 
|---|
| 332 | #endif /* _ConfigFileManager_H__ */ | 
|---|