Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/core/ConfigFileManager.h @ 6417

Last change on this file since 6417 was 6417, checked in by rgrieder, 14 years ago

Merged presentation2 branch back to trunk.
Major new features:

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