[1505] | 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 | |
---|
[3196] | 34 | #include <cassert> |
---|
[1505] | 35 | #include <string> |
---|
| 36 | #include <list> |
---|
| 37 | #include <map> |
---|
| 38 | |
---|
[2662] | 39 | #include "util/OrxEnum.h" |
---|
[1505] | 40 | |
---|
| 41 | namespace orxonox |
---|
| 42 | { |
---|
[2662] | 43 | // Use int as config file type to have an arbitrary number of files |
---|
| 44 | struct ConfigFileType : OrxEnum<ConfigFileType> |
---|
[1505] | 45 | { |
---|
[2662] | 46 | OrxEnumConstructors(ConfigFileType); |
---|
[2103] | 47 | |
---|
[2662] | 48 | static const int NoType = 0; |
---|
| 49 | static const int Settings = 1; |
---|
| 50 | static const int JoyStickCalibration = 2; |
---|
[2103] | 51 | |
---|
[2662] | 52 | static const int numberOfReservedTypes = 1024; |
---|
[1505] | 53 | }; |
---|
| 54 | |
---|
| 55 | bool config(const std::string& classname, const std::string& varname, const std::string& value); |
---|
| 56 | bool tconfig(const std::string& classname, const std::string& varname, const std::string& value); |
---|
| 57 | void reloadConfig(); |
---|
| 58 | void saveConfig(); |
---|
| 59 | void cleanConfig(); |
---|
| 60 | void loadSettings(const std::string& filename); |
---|
| 61 | |
---|
| 62 | |
---|
| 63 | ///////////////////// |
---|
| 64 | // ConfigFileEntry // |
---|
| 65 | ///////////////////// |
---|
| 66 | class _CoreExport ConfigFileEntry |
---|
| 67 | { |
---|
| 68 | public: |
---|
| 69 | virtual ~ConfigFileEntry() {}; |
---|
| 70 | virtual void setValue(const std::string& value) = 0; |
---|
| 71 | virtual std::string getValue() const = 0; |
---|
| 72 | virtual const std::string& getName() const = 0; |
---|
| 73 | virtual void setComment(const std::string& comment) = 0; |
---|
| 74 | virtual unsigned int getIndex() const { return 0; } |
---|
| 75 | virtual void setString(bool bString) = 0; |
---|
| 76 | virtual std::string getFileEntry() const = 0; |
---|
| 77 | }; |
---|
| 78 | |
---|
| 79 | |
---|
| 80 | ////////////////////////// |
---|
| 81 | // ConfigFileEntryValue // |
---|
| 82 | ////////////////////////// |
---|
| 83 | class _CoreExport ConfigFileEntryValue : public ConfigFileEntry |
---|
| 84 | { |
---|
| 85 | public: |
---|
| 86 | 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) {} |
---|
| 87 | inline virtual ~ConfigFileEntryValue() {} |
---|
| 88 | |
---|
| 89 | inline virtual const std::string& getName() const |
---|
| 90 | { return this->name_; } |
---|
| 91 | |
---|
| 92 | inline virtual void setComment(const std::string& comment) |
---|
| 93 | { this->additionalComment_ = comment; } |
---|
| 94 | |
---|
| 95 | virtual void setValue(const std::string& value); |
---|
| 96 | virtual std::string getValue() const; |
---|
| 97 | |
---|
| 98 | inline bool isString() const |
---|
| 99 | { return this->bString_; } |
---|
| 100 | inline void setString(bool bString) |
---|
| 101 | { this->bString_ = bString; } |
---|
| 102 | |
---|
| 103 | virtual std::string getFileEntry() const; |
---|
| 104 | |
---|
| 105 | protected: |
---|
| 106 | std::string name_; |
---|
| 107 | std::string value_; |
---|
| 108 | bool bString_; |
---|
| 109 | std::string additionalComment_; |
---|
| 110 | }; |
---|
| 111 | |
---|
| 112 | |
---|
| 113 | /////////////////////////////// |
---|
| 114 | // ConfigFileEntryVectorValue // |
---|
| 115 | /////////////////////////////// |
---|
| 116 | class _CoreExport ConfigFileEntryVectorValue : public ConfigFileEntryValue |
---|
| 117 | { |
---|
| 118 | public: |
---|
| 119 | 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) {} |
---|
| 120 | inline virtual ~ConfigFileEntryVectorValue() {} |
---|
| 121 | |
---|
| 122 | inline virtual unsigned int getIndex() const |
---|
| 123 | { return this->index_; } |
---|
| 124 | |
---|
| 125 | virtual std::string getFileEntry() const; |
---|
| 126 | |
---|
| 127 | private: |
---|
| 128 | unsigned int index_; |
---|
| 129 | }; |
---|
| 130 | |
---|
| 131 | |
---|
| 132 | //////////////////////////// |
---|
| 133 | // ConfigFileEntryComment // |
---|
| 134 | //////////////////////////// |
---|
| 135 | class _CoreExport ConfigFileEntryComment : public ConfigFileEntry |
---|
| 136 | { |
---|
| 137 | public: |
---|
| 138 | inline ConfigFileEntryComment(const std::string& comment) : comment_(comment) {} |
---|
| 139 | inline virtual ~ConfigFileEntryComment() {} |
---|
| 140 | |
---|
| 141 | inline virtual const std::string& getName() const |
---|
| 142 | { return this->comment_; } |
---|
| 143 | |
---|
| 144 | inline virtual void setComment(const std::string& comment) |
---|
| 145 | { this->comment_ = comment; } |
---|
| 146 | |
---|
| 147 | inline virtual void setValue(const std::string& value) |
---|
| 148 | {} |
---|
| 149 | inline virtual std::string getValue() const |
---|
| 150 | { return this->comment_; } |
---|
| 151 | |
---|
| 152 | inline void setString(bool bString) {} |
---|
| 153 | |
---|
| 154 | inline virtual std::string getFileEntry() const |
---|
| 155 | { return this->comment_; } |
---|
| 156 | |
---|
| 157 | private: |
---|
| 158 | std::string comment_; |
---|
| 159 | }; |
---|
| 160 | |
---|
| 161 | |
---|
| 162 | /////////////////////// |
---|
| 163 | // ConfigFileSection // |
---|
| 164 | /////////////////////// |
---|
| 165 | class _CoreExport ConfigFileSection |
---|
| 166 | { |
---|
| 167 | friend class ConfigFile; |
---|
| 168 | |
---|
| 169 | public: |
---|
| 170 | inline ConfigFileSection(const std::string& name, const std::string& additionalComment = "") : name_(name), additionalComment_(additionalComment), bUpdated_(false) {} |
---|
| 171 | ~ConfigFileSection(); |
---|
| 172 | |
---|
| 173 | inline const std::string& getName() const |
---|
| 174 | { return this->name_; } |
---|
| 175 | |
---|
| 176 | inline void setComment(const std::string& comment) |
---|
| 177 | { this->additionalComment_ = comment; } |
---|
| 178 | |
---|
| 179 | inline void setValue(const std::string& name, const std::string& value, bool bString) |
---|
| 180 | { this->getEntry(name, value, bString)->setValue(value); } |
---|
| 181 | inline std::string getValue(const std::string& name, const std::string& fallback, bool bString) |
---|
| 182 | { return this->getEntry(name, fallback, bString)->getValue(); } |
---|
| 183 | |
---|
| 184 | inline void setValue(const std::string& name, unsigned int index, const std::string& value, bool bString) |
---|
| 185 | { this->getEntry(name, index, value, bString)->setValue(value); } |
---|
| 186 | inline std::string getValue(const std::string& name, unsigned int index, const std::string& fallback, bool bString) |
---|
| 187 | { return this->getEntry(name, index, fallback, bString)->getValue(); } |
---|
| 188 | |
---|
| 189 | void deleteVectorEntries(const std::string& name, unsigned int startindex = 0); |
---|
| 190 | unsigned int getVectorSize(const std::string& name); |
---|
| 191 | |
---|
| 192 | std::string getFileEntry() const; |
---|
| 193 | |
---|
| 194 | private: |
---|
| 195 | std::list<ConfigFileEntry*>& getEntries() |
---|
| 196 | { return this->entries_; } |
---|
| 197 | std::list<ConfigFileEntry*>::const_iterator getEntriesBegin() const |
---|
| 198 | { return this->entries_.begin(); } |
---|
| 199 | std::list<ConfigFileEntry*>::const_iterator getEntriesEnd() const |
---|
| 200 | { return this->entries_.end(); } |
---|
| 201 | |
---|
| 202 | std::list<ConfigFileEntry*>::iterator getEntryIterator(const std::string& name, const std::string& fallback, bool bString); |
---|
| 203 | std::list<ConfigFileEntry*>::iterator getEntryIterator(const std::string& name, unsigned int index, const std::string& fallback, bool bString); |
---|
| 204 | |
---|
| 205 | inline ConfigFileEntry* getEntry(const std::string& name, const std::string& fallback, bool bString) |
---|
| 206 | { return (*this->getEntryIterator(name, fallback, bString)); } |
---|
| 207 | inline ConfigFileEntry* getEntry(const std::string& name, unsigned int index, const std::string& fallback, bool bString) |
---|
| 208 | { return (*this->getEntryIterator(name, index, fallback, bString)); } |
---|
| 209 | |
---|
| 210 | std::string name_; |
---|
| 211 | std::string additionalComment_; |
---|
| 212 | std::list<ConfigFileEntry*> entries_; |
---|
| 213 | bool bUpdated_; |
---|
| 214 | }; |
---|
| 215 | |
---|
| 216 | |
---|
| 217 | //////////////// |
---|
| 218 | // ConfigFile // |
---|
| 219 | //////////////// |
---|
| 220 | class _CoreExport ConfigFile |
---|
| 221 | { |
---|
| 222 | public: |
---|
[2103] | 223 | inline ConfigFile(const std::string& filename, ConfigFileType type) |
---|
| 224 | : filename_(filename) |
---|
| 225 | , type_(type) |
---|
| 226 | , bUpdated_(false) |
---|
| 227 | { } |
---|
[1505] | 228 | ~ConfigFile(); |
---|
| 229 | |
---|
| 230 | void load(bool bCreateIfNotExisting = true); |
---|
| 231 | void save() const; |
---|
[2103] | 232 | void saveAs(const std::string& filename); |
---|
[1505] | 233 | void clean(bool bCleanComments = false); |
---|
[2103] | 234 | void clear(); |
---|
[1505] | 235 | |
---|
[2103] | 236 | const std::string& getFilename() { return this->filename_; } |
---|
| 237 | |
---|
[1505] | 238 | inline void setValue(const std::string& section, const std::string& name, const std::string& value, bool bString) |
---|
| 239 | { this->getSection(section)->setValue(name, value, bString); this->save(); } |
---|
| 240 | inline std::string getValue(const std::string& section, const std::string& name, const std::string& fallback, bool bString) |
---|
| 241 | { std::string output = this->getSection(section)->getValue(name, fallback, bString); this->saveIfUpdated(); return output; } |
---|
| 242 | |
---|
| 243 | inline void setValue(const std::string& section, const std::string& name, unsigned int index, const std::string& value, bool bString) |
---|
| 244 | { this->getSection(section)->setValue(name, index, value, bString); this->save(); } |
---|
| 245 | inline std::string getValue(const std::string& section, const std::string& name, unsigned int index, const std::string& fallback, bool bString) |
---|
| 246 | { std::string output = this->getSection(section)->getValue(name, index, fallback, bString); this->saveIfUpdated(); return output; } |
---|
| 247 | |
---|
| 248 | inline void deleteVectorEntries(const std::string& section, const std::string& name, unsigned int startindex = 0) |
---|
| 249 | { this->getSection(section)->deleteVectorEntries(name, startindex); } |
---|
| 250 | inline unsigned int getVectorSize(const std::string& section, const std::string& name) |
---|
| 251 | { return this->getSection(section)->getVectorSize(name); } |
---|
| 252 | |
---|
[2103] | 253 | void updateConfigValues(); |
---|
| 254 | |
---|
[1505] | 255 | private: |
---|
| 256 | ConfigFileSection* getSection(const std::string& section); |
---|
| 257 | void saveIfUpdated(); |
---|
| 258 | |
---|
| 259 | std::string filename_; |
---|
[2103] | 260 | ConfigFileType type_; |
---|
[1505] | 261 | std::list<ConfigFileSection*> sections_; |
---|
| 262 | bool bUpdated_; |
---|
| 263 | }; |
---|
| 264 | |
---|
| 265 | |
---|
| 266 | /////////////////////// |
---|
| 267 | // ConfigFileManager // |
---|
| 268 | /////////////////////// |
---|
| 269 | class _CoreExport ConfigFileManager |
---|
| 270 | { |
---|
| 271 | public: |
---|
[2103] | 272 | ConfigFileManager(); |
---|
| 273 | ~ConfigFileManager(); |
---|
[1505] | 274 | |
---|
[2103] | 275 | void load(); |
---|
[1505] | 276 | void save(); |
---|
| 277 | void clean(bool bCleanComments = false); |
---|
| 278 | |
---|
[2103] | 279 | void setFilename(ConfigFileType type, const std::string& filename); |
---|
| 280 | const std::string& getFilename(ConfigFileType type); |
---|
| 281 | |
---|
| 282 | ConfigFileType getNewConfigFileType() { return mininmalFreeType_++; } |
---|
| 283 | |
---|
| 284 | void load(ConfigFileType type); |
---|
[1505] | 285 | void save(ConfigFileType type); |
---|
[2103] | 286 | void saveAs(ConfigFileType type, const std::string& saveFilename); |
---|
[1505] | 287 | void clean(ConfigFileType type, bool bCleanComments = false); |
---|
| 288 | |
---|
| 289 | inline void setValue(ConfigFileType type, const std::string& section, const std::string& name, const std::string& value, bool bString) |
---|
| 290 | { this->getFile(type)->setValue(section, name, value, bString); } |
---|
| 291 | inline std::string getValue(ConfigFileType type, const std::string& section, const std::string& name, const std::string& fallback, bool bString) |
---|
| 292 | { return this->getFile(type)->getValue(section, name, fallback, bString); } |
---|
| 293 | |
---|
| 294 | inline void setValue(ConfigFileType type, const std::string& section, const std::string& name, unsigned int index, const std::string& value, bool bString) |
---|
| 295 | { this->getFile(type)->setValue(section, name, index, value, bString); } |
---|
| 296 | inline std::string getValue(ConfigFileType type, const std::string& section, const std::string& name, unsigned int index, const std::string& fallback, bool bString) |
---|
| 297 | { return this->getFile(type)->getValue(section, name, index, fallback, bString); } |
---|
| 298 | |
---|
| 299 | inline void deleteVectorEntries(ConfigFileType type, const std::string& section, const std::string& name, unsigned int startindex = 0) |
---|
| 300 | { this->getFile(type)->deleteVectorEntries(section, name, startindex); } |
---|
| 301 | inline unsigned int getVectorSize(ConfigFileType type, const std::string& section, const std::string& name) |
---|
| 302 | { return this->getFile(type)->getVectorSize(section, name); } |
---|
| 303 | |
---|
[2103] | 304 | void updateConfigValues(); |
---|
| 305 | void updateConfigValues(ConfigFileType type); |
---|
[1505] | 306 | |
---|
[2103] | 307 | static ConfigFileManager& getInstance() { assert(singletonRef_s); return *singletonRef_s; } |
---|
| 308 | |
---|
[1505] | 309 | private: |
---|
[2103] | 310 | ConfigFileManager(const ConfigFileManager&); |
---|
[1505] | 311 | |
---|
| 312 | ConfigFile* getFile(ConfigFileType type); |
---|
| 313 | |
---|
[2103] | 314 | std::map<ConfigFileType, ConfigFile*> configFiles_; |
---|
| 315 | unsigned int mininmalFreeType_; |
---|
[1505] | 316 | |
---|
[2103] | 317 | static ConfigFileManager* singletonRef_s; |
---|
[1505] | 318 | }; |
---|
| 319 | } |
---|
| 320 | |
---|
| 321 | #endif /* _ConfigFileManager_H__ */ |
---|