[9559] | 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 | /** |
---|
| 30 | @file |
---|
| 31 | @ingroup Config ConfigFile |
---|
| 32 | */ |
---|
| 33 | |
---|
| 34 | #ifndef _ConfigFile_H__ |
---|
| 35 | #define _ConfigFile_H__ |
---|
| 36 | |
---|
| 37 | #include "core/CorePrereqs.h" |
---|
| 38 | |
---|
| 39 | #include "ConfigFileSection.h" |
---|
| 40 | |
---|
| 41 | namespace orxonox |
---|
| 42 | { |
---|
| 43 | //////////////// |
---|
| 44 | // ConfigFile // |
---|
| 45 | //////////////// |
---|
| 46 | /** |
---|
| 47 | @brief This class represents a config file, which is stored on the hard-disk and contains config values in different sections. |
---|
| 48 | |
---|
| 49 | It provides an interface to manipulate the sections and values. |
---|
| 50 | */ |
---|
| 51 | class _CoreExport ConfigFile |
---|
| 52 | { |
---|
| 53 | public: |
---|
| 54 | ConfigFile(const std::string& filename, bool bCopyFallbackFile = true); |
---|
| 55 | virtual ~ConfigFile(); |
---|
| 56 | |
---|
| 57 | virtual void load(); |
---|
| 58 | virtual void save() const; |
---|
| 59 | virtual void saveAs(const std::string& filename) const; |
---|
| 60 | virtual void clear(); |
---|
| 61 | |
---|
| 62 | /// Returns the file-name of this config file |
---|
| 63 | inline const std::string& getFilename() |
---|
| 64 | { return this->filename_; } |
---|
| 65 | |
---|
| 66 | /** |
---|
| 67 | @brief Stores a value in the config file. If the entry or its section doesn't exist, it's created. |
---|
| 68 | |
---|
| 69 | @param section The name of the section |
---|
| 70 | @param name The name of the entry |
---|
| 71 | @param value The new value |
---|
| 72 | @param bString If true, the value is treated as string which means some special treatment of special characters. |
---|
| 73 | */ |
---|
| 74 | inline void setValue(const std::string& section, const std::string& name, const std::string& value, bool bString) |
---|
| 75 | { |
---|
| 76 | this->getOrCreateSection(section)->setValue(name, value, bString); |
---|
| 77 | this->save(); |
---|
| 78 | } |
---|
| 79 | /** |
---|
| 80 | @brief Returns the value of a given entry in the config file. Returns a blank string if the value doesn't exist. |
---|
| 81 | |
---|
| 82 | @param section The name of the section |
---|
| 83 | @param name The name of the entry |
---|
| 84 | @param bString If true, the value is treated as string which means some special treatment of special characters. |
---|
| 85 | */ |
---|
| 86 | inline const std::string& getValue(const std::string& section, const std::string& name, bool bString) |
---|
| 87 | { |
---|
| 88 | ConfigFileSection* sectionPtr = this->getSection(section); |
---|
| 89 | return (sectionPtr ? sectionPtr->getValue(name, bString) : BLANKSTRING); |
---|
| 90 | } |
---|
| 91 | /** |
---|
| 92 | @brief Returns the value of a given entry in the config file. If it doesn't exist, the entry is created using the fallback value. |
---|
| 93 | |
---|
| 94 | @param section The name of the section |
---|
| 95 | @param name The name of the entry |
---|
| 96 | @param fallback The value that will be used if the entry doesn't exist |
---|
| 97 | @param bString If true, the value is treated as string which means some special treatment of special characters. |
---|
| 98 | */ |
---|
| 99 | inline const std::string& getOrCreateValue(const std::string& section, const std::string& name, const std::string& fallback, bool bString) |
---|
| 100 | { |
---|
| 101 | const std::string& output = this->getOrCreateSection(section)->getOrCreateValue(name, fallback, bString); |
---|
| 102 | this->saveIfUpdated(); |
---|
| 103 | return output; |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | /** |
---|
| 107 | @brief Stores the value of an element of a vector in the config file. If the entry or its section doesn't exist, it's created. |
---|
| 108 | |
---|
| 109 | @param section The name of the section |
---|
| 110 | @param name The name of the vector |
---|
| 111 | @param index The index of the element in the vector |
---|
| 112 | @param value The new value |
---|
| 113 | @param bString If true, the value is treated as string which means some special treatment of special characters. |
---|
| 114 | */ |
---|
| 115 | inline void setValue(const std::string& section, const std::string& name, unsigned int index, const std::string& value, bool bString) |
---|
| 116 | { |
---|
| 117 | this->getOrCreateSection(section)->setValue(name, index, value, bString); |
---|
| 118 | this->save(); |
---|
| 119 | } |
---|
| 120 | /** |
---|
| 121 | @brief Returns the value of a given element of a vector in the config file. Returns a blank string if the value doesn't exist. |
---|
| 122 | |
---|
| 123 | @param section The name of the section |
---|
| 124 | @param name The name of the vector |
---|
| 125 | @param index The index of the element in the vector |
---|
| 126 | @param bString If true, the value is treated as string which means some special treatment of special characters. |
---|
| 127 | */ |
---|
| 128 | inline const std::string& getValue(const std::string& section, const std::string& name, unsigned int index, bool bString) |
---|
| 129 | { |
---|
| 130 | ConfigFileSection* sectionPtr = this->getSection(section); |
---|
| 131 | return (sectionPtr ? sectionPtr->getValue(name, index, bString) : BLANKSTRING); |
---|
| 132 | } |
---|
| 133 | /** |
---|
| 134 | @brief Returns the value of a given element of a vector in the config file. If it doesn't exist, the entry is created using the fallback value. |
---|
| 135 | |
---|
| 136 | @param section The name of the section |
---|
| 137 | @param name The name of the vector |
---|
| 138 | @param index The index of the element in the vector |
---|
| 139 | @param fallback The value that will be used if the entry doesn't exist |
---|
| 140 | @param bString If true, the value is treated as string which means some special treatment of special characters. |
---|
| 141 | */ |
---|
| 142 | const std::string& getOrCreateValue(const std::string& section, const std::string& name, unsigned int index, const std::string& fallback, bool bString) |
---|
| 143 | { |
---|
| 144 | const std::string& output = this->getOrCreateSection(section)->getOrCreateValue(name, index, fallback, bString); |
---|
| 145 | this->saveIfUpdated(); |
---|
| 146 | return output; |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | void deleteVectorEntries(const std::string& section, const std::string& name, unsigned int startindex = 0); |
---|
| 150 | /** |
---|
| 151 | @brief Returns the size of a config vector. |
---|
| 152 | @param section The section of the vector |
---|
| 153 | @param name The name of the vector |
---|
| 154 | */ |
---|
| 155 | inline unsigned int getVectorSize(const std::string& section, const std::string& name) const |
---|
| 156 | { |
---|
| 157 | ConfigFileSection* sectionPtr = this->getSection(section); |
---|
| 158 | return (sectionPtr ? sectionPtr->getVectorSize(name) : 0); |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | static const char* DEFAULT_CONFIG_FOLDER; ///< The folder where the default config files will be stored |
---|
| 162 | |
---|
| 163 | protected: |
---|
| 164 | ConfigFileSection* getSection(const std::string& section) const; |
---|
| 165 | ConfigFileSection* getOrCreateSection(const std::string& section); |
---|
| 166 | |
---|
| 167 | std::list<ConfigFileSection*> sections_; ///< A list of sections in this config file |
---|
| 168 | |
---|
| 169 | private: |
---|
| 170 | void saveIfUpdated(); |
---|
| 171 | |
---|
| 172 | const std::string filename_; ///< The filename of this config file |
---|
| 173 | const bool bCopyFallbackFile_; ///< If true, the default config file is copied into the config-directory before loading the file |
---|
| 174 | bool bUpdated_; ///< Becomes true if a section is added |
---|
| 175 | }; |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | #endif /* _ConfigFile_H__ */ |
---|