[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 | |
---|
| 34 | #include <list> |
---|
| 35 | #include <map> |
---|
[6536] | 36 | #include <set> |
---|
| 37 | #include <string> |
---|
| 38 | #include <boost/array.hpp> |
---|
[1505] | 39 | |
---|
[3370] | 40 | #include "util/Singleton.h" |
---|
[6536] | 41 | #include "util/StringUtils.h" |
---|
[1505] | 42 | |
---|
[6536] | 43 | namespace orxonox // tolua_export |
---|
| 44 | { // tolua_export |
---|
[2103] | 45 | |
---|
[1505] | 46 | ///////////////////// |
---|
| 47 | // ConfigFileEntry // |
---|
| 48 | ///////////////////// |
---|
| 49 | class _CoreExport ConfigFileEntry |
---|
| 50 | { |
---|
| 51 | public: |
---|
| 52 | virtual ~ConfigFileEntry() {}; |
---|
| 53 | virtual void setValue(const std::string& value) = 0; |
---|
[6425] | 54 | virtual const std::string& getValue() const = 0; |
---|
[1505] | 55 | virtual const std::string& getName() const = 0; |
---|
| 56 | virtual void setComment(const std::string& comment) = 0; |
---|
| 57 | virtual unsigned int getIndex() const { return 0; } |
---|
| 58 | virtual void setString(bool bString) = 0; |
---|
[6425] | 59 | virtual const std::string& getFileEntry() const = 0; |
---|
[1505] | 60 | }; |
---|
| 61 | |
---|
| 62 | |
---|
| 63 | ////////////////////////// |
---|
| 64 | // ConfigFileEntryValue // |
---|
| 65 | ////////////////////////// |
---|
| 66 | class _CoreExport ConfigFileEntryValue : public ConfigFileEntry |
---|
| 67 | { |
---|
| 68 | public: |
---|
[6417] | 69 | inline ConfigFileEntryValue(const std::string& name, const std::string& value = "", bool bString = false, const std::string& additionalComment = "") |
---|
| 70 | : name_(name) |
---|
| 71 | , value_(value) |
---|
[6427] | 72 | , additionalComment_(additionalComment) |
---|
[6417] | 73 | , bString_(bString) |
---|
[6425] | 74 | { this->update(); } |
---|
| 75 | |
---|
[1505] | 76 | inline virtual ~ConfigFileEntryValue() {} |
---|
| 77 | |
---|
| 78 | inline virtual const std::string& getName() const |
---|
| 79 | { return this->name_; } |
---|
| 80 | |
---|
| 81 | inline virtual void setComment(const std::string& comment) |
---|
[6425] | 82 | { this->additionalComment_ = comment; this->update(); } |
---|
[1505] | 83 | |
---|
[6425] | 84 | inline virtual void setValue(const std::string& value) |
---|
| 85 | { this->value_ = value; this->update(); } |
---|
| 86 | inline virtual const std::string& getValue() const |
---|
| 87 | { return this->value_; } |
---|
[1505] | 88 | |
---|
[6425] | 89 | inline void virtual setString(bool bString) |
---|
| 90 | { this->bString_ = bString; this->update(); } |
---|
[1505] | 91 | |
---|
[6425] | 92 | inline virtual const std::string& getFileEntry() const |
---|
| 93 | { return this->fileEntry_; } |
---|
[1505] | 94 | |
---|
[6425] | 95 | inline virtual const std::string& getKeyString() const |
---|
| 96 | { return this->name_; } |
---|
| 97 | |
---|
[1505] | 98 | protected: |
---|
[6425] | 99 | virtual void update(); |
---|
| 100 | |
---|
| 101 | const std::string name_; |
---|
[1505] | 102 | std::string value_; |
---|
[6425] | 103 | std::string additionalComment_; |
---|
| 104 | std::string fileEntry_; |
---|
[1505] | 105 | bool bString_; |
---|
| 106 | }; |
---|
| 107 | |
---|
| 108 | |
---|
[6425] | 109 | //////////////////////////////// |
---|
[1505] | 110 | // ConfigFileEntryVectorValue // |
---|
[6425] | 111 | //////////////////////////////// |
---|
[1505] | 112 | class _CoreExport ConfigFileEntryVectorValue : public ConfigFileEntryValue |
---|
| 113 | { |
---|
| 114 | public: |
---|
[6425] | 115 | inline ConfigFileEntryVectorValue(const std::string& name, unsigned int index, const std::string& value = "", bool bString = false, const std::string& additionalComment = "") |
---|
| 116 | : ConfigFileEntryValue(name, value, bString, additionalComment) |
---|
| 117 | , index_(index) |
---|
| 118 | { this->update(); /*No virtual calls in base class ctor*/ } |
---|
[1505] | 119 | |
---|
[6425] | 120 | inline ~ConfigFileEntryVectorValue() {} |
---|
| 121 | |
---|
| 122 | inline unsigned int getIndex() const |
---|
[1505] | 123 | { return this->index_; } |
---|
| 124 | |
---|
[6425] | 125 | inline const std::string& getKeyString() const |
---|
| 126 | { return this->keyString_; } |
---|
[1505] | 127 | |
---|
| 128 | private: |
---|
[6425] | 129 | void update(); |
---|
| 130 | |
---|
[1505] | 131 | unsigned int index_; |
---|
[6425] | 132 | std::string keyString_; |
---|
[1505] | 133 | }; |
---|
| 134 | |
---|
| 135 | |
---|
| 136 | //////////////////////////// |
---|
| 137 | // ConfigFileEntryComment // |
---|
| 138 | //////////////////////////// |
---|
| 139 | class _CoreExport ConfigFileEntryComment : public ConfigFileEntry |
---|
| 140 | { |
---|
| 141 | public: |
---|
| 142 | inline ConfigFileEntryComment(const std::string& comment) : comment_(comment) {} |
---|
| 143 | inline virtual ~ConfigFileEntryComment() {} |
---|
| 144 | |
---|
| 145 | inline virtual const std::string& getName() const |
---|
| 146 | { return this->comment_; } |
---|
| 147 | |
---|
| 148 | inline virtual void setComment(const std::string& comment) |
---|
| 149 | { this->comment_ = comment; } |
---|
| 150 | |
---|
| 151 | inline virtual void setValue(const std::string& value) |
---|
| 152 | {} |
---|
[6425] | 153 | inline virtual const std::string& getValue() const |
---|
| 154 | { return BLANKSTRING; } |
---|
[1505] | 155 | |
---|
[6425] | 156 | inline void setString(bool bString) |
---|
| 157 | {} |
---|
[1505] | 158 | |
---|
[6425] | 159 | inline virtual const std::string& getFileEntry() const |
---|
[1505] | 160 | { return this->comment_; } |
---|
| 161 | |
---|
[6425] | 162 | inline virtual const std::string& getKeyString() const |
---|
| 163 | { return BLANKSTRING; } |
---|
| 164 | |
---|
[1505] | 165 | private: |
---|
| 166 | std::string comment_; |
---|
| 167 | }; |
---|
| 168 | |
---|
| 169 | |
---|
| 170 | /////////////////////// |
---|
| 171 | // ConfigFileSection // |
---|
| 172 | /////////////////////// |
---|
| 173 | class _CoreExport ConfigFileSection |
---|
| 174 | { |
---|
| 175 | friend class ConfigFile; |
---|
[6536] | 176 | friend class SettingsConfigFile; |
---|
[1505] | 177 | |
---|
| 178 | public: |
---|
[6417] | 179 | inline ConfigFileSection(const std::string& name, const std::string& additionalComment = "") |
---|
| 180 | : name_(name) |
---|
| 181 | , additionalComment_(additionalComment) |
---|
| 182 | , bUpdated_(false) |
---|
| 183 | {} |
---|
[1505] | 184 | ~ConfigFileSection(); |
---|
| 185 | |
---|
| 186 | inline const std::string& getName() const |
---|
| 187 | { return this->name_; } |
---|
| 188 | |
---|
| 189 | inline void setComment(const std::string& comment) |
---|
| 190 | { this->additionalComment_ = comment; } |
---|
| 191 | |
---|
| 192 | inline void setValue(const std::string& name, const std::string& value, bool bString) |
---|
[6536] | 193 | { this->getOrCreateEntry(name, value, bString)->setValue(value); } |
---|
| 194 | inline const std::string& getValue(const std::string& name, bool bString) |
---|
| 195 | { |
---|
| 196 | ConfigFileEntry* entry = this->getEntry(name); |
---|
| 197 | if (entry) |
---|
| 198 | { |
---|
| 199 | entry->setString(bString); |
---|
| 200 | return entry->getValue(); |
---|
| 201 | } |
---|
| 202 | return BLANKSTRING; |
---|
| 203 | } |
---|
| 204 | inline const std::string& getOrCreateValue(const std::string& name, const std::string& fallback, bool bString) |
---|
| 205 | { return this->getOrCreateEntry(name, fallback, bString)->getValue(); } |
---|
[1505] | 206 | |
---|
| 207 | inline void setValue(const std::string& name, unsigned int index, const std::string& value, bool bString) |
---|
[6536] | 208 | { this->getOrCreateEntry(name, index, value, bString)->setValue(value); } |
---|
| 209 | inline const std::string& getValue(const std::string& name, unsigned int index, bool bString) |
---|
| 210 | { |
---|
| 211 | ConfigFileEntry* entry = this->getEntry(name, index); |
---|
| 212 | if (entry) |
---|
| 213 | { |
---|
| 214 | entry->setString(bString); |
---|
| 215 | return entry->getValue(); |
---|
| 216 | } |
---|
| 217 | return BLANKSTRING; |
---|
| 218 | } |
---|
| 219 | inline const std::string& getOrCreateValue(const std::string& name, unsigned int index, const std::string& fallback, bool bString) |
---|
| 220 | { return this->getOrCreateEntry(name, index, fallback, bString)->getValue(); } |
---|
[1505] | 221 | |
---|
| 222 | void deleteVectorEntries(const std::string& name, unsigned int startindex = 0); |
---|
[6536] | 223 | unsigned int getVectorSize(const std::string& name) const; |
---|
[1505] | 224 | |
---|
| 225 | std::string getFileEntry() const; |
---|
| 226 | |
---|
| 227 | private: |
---|
| 228 | std::list<ConfigFileEntry*>& getEntries() |
---|
| 229 | { return this->entries_; } |
---|
| 230 | std::list<ConfigFileEntry*>::const_iterator getEntriesBegin() const |
---|
| 231 | { return this->entries_.begin(); } |
---|
| 232 | std::list<ConfigFileEntry*>::const_iterator getEntriesEnd() const |
---|
| 233 | { return this->entries_.end(); } |
---|
| 234 | |
---|
[6536] | 235 | std::list<ConfigFileEntry*>::iterator getOrCreateEntryIterator(const std::string& name, const std::string& fallback, bool bString); |
---|
| 236 | std::list<ConfigFileEntry*>::iterator getOrCreateEntryIterator(const std::string& name, unsigned int index, const std::string& fallback, bool bString); |
---|
[1505] | 237 | |
---|
[6536] | 238 | ConfigFileEntry* getEntry(const std::string& name) const; |
---|
| 239 | inline ConfigFileEntry* getOrCreateEntry(const std::string& name, const std::string& fallback, bool bString) |
---|
| 240 | { return (*this->getOrCreateEntryIterator(name, fallback, bString)); } |
---|
| 241 | ConfigFileEntry* getEntry(const std::string& name, unsigned int index) const; |
---|
| 242 | inline ConfigFileEntry* getOrCreateEntry(const std::string& name, unsigned int index, const std::string& fallback, bool bString) |
---|
| 243 | { return (*this->getOrCreateEntryIterator(name, index, fallback, bString)); } |
---|
[1505] | 244 | |
---|
| 245 | std::string name_; |
---|
| 246 | std::string additionalComment_; |
---|
| 247 | std::list<ConfigFileEntry*> entries_; |
---|
| 248 | bool bUpdated_; |
---|
| 249 | }; |
---|
| 250 | |
---|
| 251 | |
---|
| 252 | //////////////// |
---|
| 253 | // ConfigFile // |
---|
| 254 | //////////////// |
---|
| 255 | class _CoreExport ConfigFile |
---|
| 256 | { |
---|
| 257 | public: |
---|
[6536] | 258 | ConfigFile(const std::string& filename, bool bCopyFallbackFile = true); |
---|
| 259 | virtual ~ConfigFile(); |
---|
[1505] | 260 | |
---|
[6536] | 261 | virtual void load(); |
---|
| 262 | virtual void save() const; |
---|
| 263 | virtual void saveAs(const std::string& filename) const; |
---|
| 264 | virtual void clear(); |
---|
[1505] | 265 | |
---|
[6536] | 266 | inline const std::string& getFilename() |
---|
| 267 | { return this->filename_; } |
---|
[2103] | 268 | |
---|
[1505] | 269 | inline void setValue(const std::string& section, const std::string& name, const std::string& value, bool bString) |
---|
[6536] | 270 | { |
---|
| 271 | this->getOrCreateSection(section)->setValue(name, value, bString); |
---|
| 272 | this->save(); |
---|
| 273 | } |
---|
| 274 | inline const std::string& getValue(const std::string& section, const std::string& name, bool bString) |
---|
| 275 | { |
---|
| 276 | ConfigFileSection* sectionPtr = this->getSection(section); |
---|
| 277 | return (sectionPtr ? sectionPtr->getValue(name, bString) : BLANKSTRING); |
---|
| 278 | } |
---|
| 279 | const std::string& getOrCreateValue(const std::string& section, const std::string& name, const std::string& fallback, bool bString); |
---|
[1505] | 280 | |
---|
| 281 | inline void setValue(const std::string& section, const std::string& name, unsigned int index, const std::string& value, bool bString) |
---|
[6536] | 282 | { |
---|
| 283 | this->getOrCreateSection(section)->setValue(name, index, value, bString); |
---|
| 284 | this->save(); |
---|
| 285 | } |
---|
| 286 | inline const std::string& getValue(const std::string& section, const std::string& name, unsigned int index, bool bString) |
---|
| 287 | { |
---|
| 288 | ConfigFileSection* sectionPtr = this->getSection(section); |
---|
| 289 | return (sectionPtr ? sectionPtr->getValue(name, index, bString) : BLANKSTRING); |
---|
| 290 | } |
---|
| 291 | const std::string& getOrCreateValue(const std::string& section, const std::string& name, unsigned int index, const std::string& fallback, bool bString); |
---|
[1505] | 292 | |
---|
[6536] | 293 | void deleteVectorEntries(const std::string& section, const std::string& name, unsigned int startindex = 0); |
---|
| 294 | inline unsigned int getVectorSize(const std::string& section, const std::string& name) const |
---|
| 295 | { |
---|
| 296 | ConfigFileSection* sectionPtr = this->getSection(section); |
---|
| 297 | return (sectionPtr ? sectionPtr->getVectorSize(name) : 0); |
---|
| 298 | } |
---|
[1505] | 299 | |
---|
[6536] | 300 | static const char* DEFAULT_CONFIG_FOLDER; |
---|
[2103] | 301 | |
---|
[6536] | 302 | protected: |
---|
| 303 | ConfigFileSection* getSection(const std::string& section) const; |
---|
| 304 | ConfigFileSection* getOrCreateSection(const std::string& section); |
---|
| 305 | |
---|
| 306 | std::list<ConfigFileSection*> sections_; |
---|
| 307 | |
---|
[1505] | 308 | private: |
---|
| 309 | void saveIfUpdated(); |
---|
[6536] | 310 | const std::string filename_; |
---|
| 311 | const bool bCopyFallbackFile_; |
---|
[1505] | 312 | bool bUpdated_; |
---|
| 313 | }; |
---|
| 314 | |
---|
| 315 | |
---|
[6536] | 316 | //////////////////////// |
---|
| 317 | // SettingsConfigFile // |
---|
| 318 | //////////////////////// |
---|
| 319 | class _CoreExport SettingsConfigFile // tolua_export |
---|
| 320 | : public ConfigFile, public Singleton<SettingsConfigFile> |
---|
| 321 | { // tolua_export |
---|
| 322 | friend class Singleton<SettingsConfigFile>; |
---|
| 323 | |
---|
| 324 | public: |
---|
| 325 | typedef std::multimap<std::string, std::pair<std::string, ConfigValueContainer*> > ContainerMap; |
---|
| 326 | |
---|
| 327 | SettingsConfigFile(const std::string& filename); |
---|
| 328 | ~SettingsConfigFile(); |
---|
| 329 | |
---|
| 330 | void load(); // tolua_export |
---|
| 331 | void setFilename(const std::string& filename); // tolua_export |
---|
| 332 | void clean(bool bCleanComments = false); // tolua_export |
---|
| 333 | |
---|
| 334 | bool config(const std::string& section, const std::string& entry, const std::string& value); // tolua_export |
---|
| 335 | bool tconfig(const std::string& section, const std::string& entry, const std::string& value); // tolua_export |
---|
| 336 | std::string getConfig(const std::string& section, const std::string& entry); // tolua_export |
---|
| 337 | |
---|
| 338 | void addConfigValueContainer(ConfigValueContainer* container); |
---|
| 339 | void removeConfigValueContainer(ConfigValueContainer* container); |
---|
| 340 | |
---|
| 341 | inline const std::set<std::string>& getSectionNames() |
---|
| 342 | { return this->sectionNames_; } |
---|
| 343 | inline ContainerMap::const_iterator getContainerLowerBound(const std::string section) |
---|
| 344 | { return this->containers_.lower_bound(section); } |
---|
| 345 | inline ContainerMap::const_iterator getContainerUpperBound(const std::string section) |
---|
| 346 | { return this->containers_.upper_bound(section); } |
---|
| 347 | |
---|
| 348 | static SettingsConfigFile& getInstance() { return Singleton<SettingsConfigFile>::getInstance(); } // tolua_export |
---|
| 349 | |
---|
| 350 | private: |
---|
| 351 | void updateConfigValues(); |
---|
| 352 | bool configImpl(const std::string& section, const std::string& entry, const std::string& value, bool (ConfigValueContainer::*function)(const MultiType&)); |
---|
| 353 | |
---|
| 354 | ContainerMap containers_; |
---|
| 355 | std::set<std::string> sectionNames_; |
---|
| 356 | static SettingsConfigFile* singletonPtr_s; |
---|
| 357 | }; // tolua_export |
---|
| 358 | |
---|
| 359 | |
---|
[1505] | 360 | /////////////////////// |
---|
| 361 | // ConfigFileManager // |
---|
| 362 | /////////////////////// |
---|
[3370] | 363 | class _CoreExport ConfigFileManager : public Singleton<ConfigFileManager> |
---|
[1505] | 364 | { |
---|
[3370] | 365 | friend class Singleton<ConfigFileManager>; |
---|
[1505] | 366 | public: |
---|
[2103] | 367 | ConfigFileManager(); |
---|
| 368 | ~ConfigFileManager(); |
---|
[1505] | 369 | |
---|
[6536] | 370 | void setFilename(ConfigFileType::Value type, const std::string& filename); |
---|
[1505] | 371 | |
---|
[6536] | 372 | inline ConfigFile* getConfigFile(ConfigFileType::Value type) |
---|
| 373 | { |
---|
| 374 | // Check array bounds |
---|
| 375 | return configFiles_.at(type); |
---|
| 376 | } |
---|
[2103] | 377 | |
---|
[1505] | 378 | private: |
---|
[2103] | 379 | ConfigFileManager(const ConfigFileManager&); |
---|
[1505] | 380 | |
---|
[6536] | 381 | boost::array<ConfigFile*, 3> configFiles_; |
---|
[3370] | 382 | static ConfigFileManager* singletonPtr_s; |
---|
[1505] | 383 | }; |
---|
[6417] | 384 | } // tolua_export |
---|
[1505] | 385 | |
---|
| 386 | #endif /* _ConfigFileManager_H__ */ |
---|