Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/core/ConfigFileManager.h @ 1062

Last change on this file since 1062 was 1062, checked in by rgrieder, 16 years ago
  • changed header file inclusion order
File size: 12.5 KB
Line 
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#define DEFAULT_CONFIG_FILE "default.ini"
42
43namespace orxonox
44{
45    enum _CoreExport ConfigFileType
46    {
47        CFT_Settings,
48        CFT_Keybindings
49    };
50
51
52    void reloadConfig();
53    void saveConfig();
54    void cleanConfig();
55    void loadSettings(const std::string& filename);
56    void loadKeybindings(const std::string& filename);
57
58
59    /////////////////////
60    // ConfigFileEntry //
61    /////////////////////
62    class _CoreExport ConfigFileEntry
63    {
64        public:
65            virtual void setValue(const std::string& value) = 0;
66            virtual std::string getValue() const = 0;
67            virtual const std::string& getName() const = 0;
68            virtual void setComment(const std::string& comment) = 0;
69            virtual unsigned int getIndex() const { return 0; }
70            virtual void setString(bool bString) = 0;
71            virtual std::string getFileEntry() const = 0;
72    };
73
74
75    //////////////////////////
76    // ConfigFileEntryValue //
77    //////////////////////////
78    class _CoreExport ConfigFileEntryValue : public ConfigFileEntry
79    {
80        public:
81            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) {}
82            inline virtual ~ConfigFileEntryValue() {}
83
84            inline virtual const std::string& getName() const
85                { return this->name_; }
86
87            inline virtual void setComment(const std::string& comment)
88                { this->additionalComment_ = comment; }
89
90            virtual void setValue(const std::string& value);
91            virtual std::string getValue() const;
92
93            inline bool isString() const
94                { return this->bString_; }
95            inline void setString(bool bString)
96                { this->bString_ = bString; }
97
98            virtual std::string getFileEntry() const;
99
100        protected:
101            std::string name_;
102            std::string value_;
103            bool bString_;
104            std::string additionalComment_;
105    };
106
107
108    ///////////////////////////////
109    // ConfigFileEntryVectorValue //
110    ///////////////////////////////
111    class _CoreExport ConfigFileEntryVectorValue : public ConfigFileEntryValue
112    {
113        public:
114            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) {}
115            inline virtual ~ConfigFileEntryVectorValue() {}
116
117            inline virtual unsigned int getIndex() const
118                { return this->index_; }
119
120            virtual std::string getFileEntry() const;
121
122        private:
123            unsigned int index_;
124    };
125
126
127    ////////////////////////////
128    // ConfigFileEntryComment //
129    ////////////////////////////
130    class _CoreExport ConfigFileEntryComment : public ConfigFileEntry
131    {
132        public:
133            inline ConfigFileEntryComment(const std::string& comment) : comment_(comment) {}
134            inline virtual ~ConfigFileEntryComment() {}
135
136            inline virtual const std::string& getName() const
137                { return this->comment_; }
138
139            inline virtual void setComment(const std::string& comment)
140                { this->comment_ = comment; }
141
142            inline virtual void setValue(const std::string& value)
143                {}
144            inline virtual std::string getValue() const
145                { return this->comment_; }
146
147            inline void setString(bool bString) {}
148
149            inline virtual std::string getFileEntry() const
150                { return this->comment_; }
151
152        private:
153            std::string comment_;
154    };
155
156
157    ///////////////////////
158    // ConfigFileSection //
159    ///////////////////////
160    class _CoreExport ConfigFileSection
161    {
162        friend class ConfigFile;
163
164        public:
165            inline ConfigFileSection(const std::string& name, const std::string& additionalComment = "") : name_(name), additionalComment_(additionalComment), bUpdated_(false) {}
166            ~ConfigFileSection();
167
168            inline const std::string& getName() const
169                { return this->name_; }
170
171            inline void setComment(const std::string& comment)
172                { this->additionalComment_ = comment; }
173
174            inline void setValue(const std::string& name, const std::string& value, bool bString)
175                { this->getEntry(name, value, bString)->setValue(value); }
176            inline std::string getValue(const std::string& name, const std::string& fallback, bool bString)
177                { return this->getEntry(name, fallback, bString)->getValue(); }
178
179            inline void setValue(const std::string& name, unsigned int index, const std::string& value, bool bString)
180                { this->getEntry(name, index, value, bString)->setValue(value); }
181            inline std::string getValue(const std::string& name, unsigned int index, const std::string& fallback, bool bString)
182                { return this->getEntry(name, index, fallback, bString)->getValue(); }
183
184            void deleteVectorEntries(const std::string& name, unsigned int startindex = 0);
185            unsigned int getVectorSize(const std::string& name);
186
187            std::string getFileEntry() const;
188
189        private:
190            std::list<ConfigFileEntry*>& getEntries()
191                { return this->entries_; }
192            std::list<ConfigFileEntry*>::const_iterator getEntriesBegin() const
193                { return this->entries_.begin(); }
194            std::list<ConfigFileEntry*>::const_iterator getEntriesEnd() const
195                { return this->entries_.end(); }
196
197            std::list<ConfigFileEntry*>::iterator getEntryIterator(const std::string& name, const std::string& fallback, bool bString);
198            std::list<ConfigFileEntry*>::iterator getEntryIterator(const std::string& name, unsigned int index, const std::string& fallback, bool bString);
199
200            inline ConfigFileEntry* getEntry(const std::string& name, const std::string& fallback, bool bString)
201                { return (*this->getEntryIterator(name, fallback, bString)); }
202            inline ConfigFileEntry* getEntry(const std::string& name, unsigned int index, const std::string& fallback, bool bString)
203                { return (*this->getEntryIterator(name, index, fallback, bString)); }
204
205            std::string name_;
206            std::string additionalComment_;
207            std::list<ConfigFileEntry*> entries_;
208            bool bUpdated_;
209    };
210
211
212    ////////////////
213    // ConfigFile //
214    ////////////////
215    class _CoreExport ConfigFile
216    {
217        public:
218            inline ConfigFile(const std::string& filename) : filename_(filename), bUpdated_(false) {}
219            ~ConfigFile();
220
221            void load(bool bCreateIfNotExisting = true);
222            void save() const;
223            void clean(bool bCleanComments = false);
224
225            inline void setValue(const std::string& section, const std::string& name, const std::string& value, bool bString)
226                { this->getSection(section)->setValue(name, value, bString); this->save(); }
227            inline std::string getValue(const std::string& section, const std::string& name, const std::string& fallback, bool bString)
228                { std::string output = this->getSection(section)->getValue(name, fallback, bString); this->saveIfUpdated(); return output; }
229
230            inline void setValue(const std::string& section, const std::string& name, unsigned int index, const std::string& value, bool bString)
231                { this->getSection(section)->setValue(name, index, value, bString); this->save(); }
232            inline std::string getValue(const std::string& section, const std::string& name, unsigned int index, const std::string& fallback, bool bString)
233                { std::string output = this->getSection(section)->getValue(name, index, fallback, bString); this->saveIfUpdated(); return output; }
234
235            inline void deleteVectorEntries(const std::string& section, const std::string& name, unsigned int startindex = 0)
236                { this->getSection(section)->deleteVectorEntries(name, startindex); }
237            inline unsigned int getVectorSize(const std::string& section, const std::string& name)
238                { return this->getSection(section)->getVectorSize(name); }
239
240        private:
241            ConfigFileSection* getSection(const std::string& section);
242            void saveIfUpdated();
243
244            std::string filename_;
245            std::list<ConfigFileSection*> sections_;
246            bool bUpdated_;
247    };
248
249
250    ///////////////////////
251    // ConfigFileManager //
252    ///////////////////////
253    class _CoreExport ConfigFileManager
254    {
255        public:
256            static ConfigFileManager* getSingleton();
257
258            void setFile(ConfigFileType type, const std::string& filename, bool bCreateIfNotExisting = true);
259
260            void load(bool bCreateIfNotExisting = true);
261            void save();
262            void clean(bool bCleanComments = false);
263
264            void load(ConfigFileType type, bool bCreateIfNotExisting = true);
265            void save(ConfigFileType type);
266            void clean(ConfigFileType type, bool bCleanComments = false);
267
268            inline void setValue(ConfigFileType type, const std::string& section, const std::string& name, const std::string& value, bool bString)
269                { this->getFile(type)->setValue(section, name, value, bString); }
270            inline std::string getValue(ConfigFileType type, const std::string& section, const std::string& name, const std::string& fallback, bool bString)
271                { return this->getFile(type)->getValue(section, name, fallback, bString); }
272
273            inline void setValue(ConfigFileType type, const std::string& section, const std::string& name, unsigned int index, const std::string& value, bool bString)
274                { this->getFile(type)->setValue(section, name, index, value, bString); }
275            inline std::string getValue(ConfigFileType type, const std::string& section, const std::string& name, unsigned int index, const std::string& fallback, bool bString)
276                { return this->getFile(type)->getValue(section, name, index, fallback, bString); }
277
278            inline void deleteVectorEntries(ConfigFileType type, const std::string& section, const std::string& name, unsigned int startindex = 0)
279                { this->getFile(type)->deleteVectorEntries(section, name, startindex); }
280            inline unsigned int getVectorSize(ConfigFileType type, const std::string& section, const std::string& name)
281                { return this->getFile(type)->getVectorSize(section, name); }
282
283            void updateConfigValues() const;
284            void updateConfigValues(ConfigFileType type) const;
285
286        private:
287            ConfigFileManager();
288            ConfigFileManager(const ConfigFileManager& other) {}
289            ~ConfigFileManager();
290
291            ConfigFile* getFile(ConfigFileType type);
292
293            std::string getFilePath(const std::string& name) const;
294
295            std::map<ConfigFileType, ConfigFile*> configFiles_;
296    };
297}
298
299#endif /* _ConfigFileManager_H__ */
Note: See TracBrowser for help on using the repository browser.