Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core2/src/orxonox/core/ConfigFileManager.h @ 1030

Last change on this file since 1030 was 1030, checked in by landauf, 16 years ago

extracted all config-value related macros from CoreIncludes.h and moved them to ConfigValueIncludes.h.

ConfigValueContainer can now handle std::vector<x> where 'x' is is any type supported by MultiTypeMath (all primitives, pointer, string, vector2, vector3, quaternion, colourvalue, radian, degree).

the vectors size is currently limited to 256 elements. this is just a practical limit, it can be raised if it's necessary. the reason for the limit is: you can add new elements to a vector by simply typing 'set classname varname index value' into the console or adding a new entry in the config-file. if 'index' is bigger than the vectors size, all elements up to 'index' are inserted. if the user accidentally enters a big number, he could end up with >4*109 elements in his config-file, resulting in 10-100gb on the hdd and a completely filled memory. and that's not exactly what i want ;)

File size: 12.1 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *
4 *
5 *   License notice:
6 *
7 *   This program is free software; you can redistribute it and/or
8 *   modify it under the terms of the GNU General Public License
9 *   as published by the Free Software Foundation; either version 2
10 *   of the License, or (at your option) any later version.
11 *
12 *   This program is distributed in the hope that it will be useful,
13 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 *   GNU General Public License for more details.
16 *
17 *   You should have received a copy of the GNU General Public License
18 *   along with this program; if not, write to the Free Software
19 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 *
21 *   Author:
22 *      Fabian 'x3n' Landau
23 *   Co-authors:
24 *      ...
25 *
26 */
27
28#ifndef _ConfigFileManager_H__
29#define _ConfigFileManager_H__
30
31#include <iostream>
32#include <string>
33#include <list>
34#include <map>
35
36#include "util/Math.h"
37
38#include "CorePrereqs.h"
39
40#define DEFAULT_CONFIG_FILE "default.ini"
41
42namespace orxonox
43{
44    enum _CoreExport ConfigFileType
45    {
46        CFT_Settings,
47        CFT_Keybindings
48    };
49
50
51    void reloadConfig();
52    void saveConfig();
53    void cleanConfig();
54    void loadSettings(const std::string& filename);
55    void loadKeybindings(const std::string& filename);
56
57
58    /////////////////////
59    // ConfigFileEntry //
60    /////////////////////
61    class _CoreExport ConfigFileEntry
62    {
63        public:
64            virtual void setValue(const std::string& value) = 0;
65            virtual const std::string& getValue() const = 0;
66            virtual const std::string& getName() const = 0;
67            virtual void setComment(const std::string& comment) = 0;
68            virtual unsigned int getIndex() const { return 0; }
69            virtual std::string getFileEntry() const = 0;
70    };
71
72
73    //////////////////////////
74    // ConfigFileEntryValue //
75    //////////////////////////
76    class _CoreExport ConfigFileEntryValue : public ConfigFileEntry
77    {
78        public:
79            inline ConfigFileEntryValue(const std::string& name, const std::string& value = "", const std::string& additionalComment = "") : name_(name), value_(value), additionalComment_(additionalComment), bString_(false) {}
80            inline virtual ~ConfigFileEntryValue() {}
81
82            inline virtual const std::string& getName() const
83                { return this->name_; }
84
85            inline virtual void setComment(const std::string& comment)
86                { this->additionalComment_ = comment; }
87
88            inline virtual void setValue(const std::string& value)
89                { this->value_ = value; }
90            inline virtual const std::string& getValue() const
91                { return this->value_; }
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            std::string additionalComment_;
104            bool bString_;
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 = "", const std::string& additionalComment = "") : ConfigFileEntryValue(name, value, 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 const std::string& getValue() const
145                { return this->comment_; }
146
147            inline virtual std::string getFileEntry() const
148                { return this->comment_; }
149
150        private:
151            std::string comment_;
152    };
153
154
155    ///////////////////////
156    // ConfigFileSection //
157    ///////////////////////
158    class _CoreExport ConfigFileSection
159    {
160        friend class ConfigFile;
161
162        public:
163            inline ConfigFileSection(const std::string& name, const std::string& additionalComment = "") : name_(name), additionalComment_(additionalComment), bUpdated_(false) {}
164            ~ConfigFileSection();
165
166            inline const std::string& getName() const
167                { return this->name_; }
168
169            inline void setComment(const std::string& comment)
170                { this->additionalComment_ = comment; }
171
172            inline void setValue(const std::string& name, const std::string& value)
173                { this->getEntry(name, value)->setValue(value); }
174            inline const std::string& getValue(const std::string& name, const std::string& fallback)
175                { return this->getEntry(name, fallback)->getValue(); }
176
177            inline void setValue(const std::string& name, unsigned int index, const std::string& value)
178                { this->getEntry(name, index, value)->setValue(value); }
179            inline const std::string& getValue(const std::string& name, unsigned int index, const std::string& fallback)
180                { return this->getEntry(name, index, fallback)->getValue(); }
181
182            void deleteVectorEntries(const std::string& name, unsigned int startindex = 0);
183            unsigned int getVectorSize(const std::string& name);
184
185            std::string getFileEntry() const;
186
187        private:
188            std::list<ConfigFileEntry*>& getEntries()
189                { return this->entries_; }
190            std::list<ConfigFileEntry*>::const_iterator getEntriesBegin() const
191                { return this->entries_.begin(); }
192            std::list<ConfigFileEntry*>::const_iterator getEntriesEnd() const
193                { return this->entries_.end(); }
194
195            std::list<ConfigFileEntry*>::iterator getEntryIterator(const std::string& name, const std::string& fallback = "");
196            std::list<ConfigFileEntry*>::iterator getEntryIterator(const std::string& name, unsigned int index, const std::string& fallback = "");
197
198            inline ConfigFileEntry* getEntry(const std::string& name, const std::string& fallback)
199                { return (*this->getEntryIterator(name, fallback)); }
200            inline ConfigFileEntry* getEntry(const std::string& name, unsigned int index, const std::string& fallback)
201                { return (*this->getEntryIterator(name, index, fallback)); }
202
203            std::string name_;
204            std::string additionalComment_;
205            std::list<ConfigFileEntry*> entries_;
206            bool bUpdated_;
207    };
208
209
210    ////////////////
211    // ConfigFile //
212    ////////////////
213    class _CoreExport ConfigFile
214    {
215        public:
216            inline ConfigFile(const std::string& filename) : filename_(filename), bUpdated_(false) {}
217            ~ConfigFile();
218
219            void load(bool bCreateIfNotExisting = true);
220            void save() const;
221            void clean(bool bCleanComments = false);
222
223            inline void setValue(const std::string& section, const std::string& name, const std::string& value)
224                { this->getSection(section)->setValue(name, value); this->save(); }
225            inline const std::string& getValue(const std::string& section, const std::string& name, const std::string& fallback)
226                { const std::string& output = this->getSection(section)->getValue(name, fallback); this->saveIfUpdated(); return output; }
227
228            inline void setValue(const std::string& section, const std::string& name, unsigned int index, const std::string& value)
229                { this->getSection(section)->setValue(name, index, value); this->save(); }
230            inline const std::string& getValue(const std::string& section, const std::string& name, unsigned int index, const std::string& fallback)
231                { const std::string& output = this->getSection(section)->getValue(name, index, fallback); this->saveIfUpdated(); return output; }
232
233            inline void deleteVectorEntries(const std::string& section, const std::string& name, unsigned int startindex = 0)
234                { this->getSection(section)->deleteVectorEntries(name, startindex); }
235            inline unsigned int getVectorSize(const std::string& section, const std::string& name)
236                { return this->getSection(section)->getVectorSize(name); }
237
238        private:
239            ConfigFileSection* getSection(const std::string& section);
240            void saveIfUpdated();
241
242            std::string filename_;
243            std::list<ConfigFileSection*> sections_;
244            bool bUpdated_;
245    };
246
247
248    ///////////////////////
249    // ConfigFileManager //
250    ///////////////////////
251    class _CoreExport ConfigFileManager
252    {
253        public:
254            static ConfigFileManager* getSingleton();
255
256            void setFile(ConfigFileType type, const std::string& filename, bool bCreateIfNotExisting = true);
257
258            void load(bool bCreateIfNotExisting = true);
259            void save();
260            void clean(bool bCleanComments = false);
261
262            void load(ConfigFileType type, bool bCreateIfNotExisting = true);
263            void save(ConfigFileType type);
264            void clean(ConfigFileType type, bool bCleanComments = false);
265
266            inline void setValue(ConfigFileType type, const std::string& section, const std::string& name, const std::string& value)
267                { this->getFile(type)->setValue(section, name, value); }
268            inline const std::string& getValue(ConfigFileType type, const std::string& section, const std::string& name, const std::string& fallback)
269                { return this->getFile(type)->getValue(section, name, fallback); }
270
271            inline void setValue(ConfigFileType type, const std::string& section, const std::string& name, unsigned int index, const std::string& value)
272                { this->getFile(type)->setValue(section, name, index, value); }
273            inline const std::string& getValue(ConfigFileType type, const std::string& section, const std::string& name, unsigned int index, const std::string& fallback)
274                { return this->getFile(type)->getValue(section, name, index, fallback); }
275
276            inline void deleteVectorEntries(ConfigFileType type, const std::string& section, const std::string& name, unsigned int startindex = 0)
277                { this->getFile(type)->deleteVectorEntries(section, name, startindex); }
278            inline unsigned int getVectorSize(ConfigFileType type, const std::string& section, const std::string& name)
279                { return this->getFile(type)->getVectorSize(section, name); }
280
281            void updateConfigValues() const;
282            void updateConfigValues(ConfigFileType type) const;
283
284        private:
285            ConfigFileManager();
286            ConfigFileManager(const ConfigFileManager& other) {}
287            ~ConfigFileManager();
288
289            ConfigFile* getFile(ConfigFileType type);
290
291            std::string getFilePath(const std::string& name) const;
292
293            std::map<ConfigFileType, ConfigFile*> configFiles_;
294    };
295}
296
297#endif /* _ConfigFileManager_H__ */
Note: See TracBrowser for help on using the repository browser.