Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

config-value accepts now strings with special chars like \n or \t and hopefully every other. strings are enclosed by "…", but the quotes are only visible in the config-file. if you want something like " ← whitespace" you must add quotes, otherwise this would be stripped to "← whitespace".

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