Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Fixed a problem in the Shell with the command history config file type.

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