Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

fixed another bug.
always remember: no matter how cool your feature is, it won't work as long as you execute it after a 'return' statement…

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