Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation2/src/libraries/core/ConfigFileManager.h @ 6194

Last change on this file since 6194 was 6194, checked in by dafrick, 14 years ago

Some adjustments regarding tolua export and sound menu.

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