Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/core/ConfigFileManager.h @ 6425

Last change on this file since 6425 was 6425, checked in by rgrieder, 15 years ago

Replaced "=" with " = " in our ini files for the config value.
Also made some changes to have const std::string& forwarding with getValue().

  • Property svn:eol-style set to native
File size: 14.4 KB
RevLine 
[1505]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
[3196]34#include <cassert>
[1505]35#include <string>
36#include <list>
37#include <map>
38
[2662]39#include "util/OrxEnum.h"
[3370]40#include "util/Singleton.h"
[1505]41
[6417]42// tolua_begin
[1505]43namespace orxonox
44{
[6417]45    // tolua_end
[2662]46    // Use int as config file type to have an arbitrary number of files
47    struct ConfigFileType : OrxEnum<ConfigFileType>
[1505]48    {
[2662]49        OrxEnumConstructors(ConfigFileType);
[2103]50
[2662]51        static const int NoType              = 0;
52        static const int Settings            = 1;
53        static const int JoyStickCalibration = 2;
[6417]54        static const int CommandHistory      = 3;
[2103]55
[2662]56        static const int numberOfReservedTypes = 1024;
[1505]57    };
58
[6417]59    _CoreExport bool config(const std::string& classname, const std::string& varname, const std::string& value); // tolua_export
[6425]60    _CoreExport const std::string& getConfig(const std::string& classname, const std::string& varname); // tolua_export
[6105]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);
[1505]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;
[6425]76            virtual const std::string& getValue() const = 0;
[1505]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;
[6425]81            virtual const std::string& getFileEntry() const = 0;
[1505]82    };
83
84
85    //////////////////////////
86    // ConfigFileEntryValue //
87    //////////////////////////
88    class _CoreExport ConfigFileEntryValue : public ConfigFileEntry
89    {
90        public:
[6417]91            inline ConfigFileEntryValue(const std::string& name, const std::string& value = "", bool bString = false, const std::string& additionalComment = "")
92                : name_(name)
93                , value_(value)
94                , bString_(bString)
95                , additionalComment_(additionalComment)
[6425]96                { this->update(); }
97
[1505]98            inline virtual ~ConfigFileEntryValue() {}
99
100            inline virtual const std::string& getName() const
101                { return this->name_; }
102
103            inline virtual void setComment(const std::string& comment)
[6425]104                { this->additionalComment_ = comment; this->update(); }
[1505]105
[6425]106            inline virtual void setValue(const std::string& value)
107                { this->value_ = value; this->update(); }
108            inline virtual const std::string& getValue() const
109                { return this->value_; }
[1505]110
[6425]111            inline void virtual setString(bool bString)
112                { this->bString_ = bString; this->update(); }
[1505]113
[6425]114            inline virtual const std::string& getFileEntry() const
115                { return this->fileEntry_; }
[1505]116
[6425]117            inline virtual const std::string& getKeyString() const
118                { return this->name_; }
119
[1505]120        protected:
[6425]121            virtual void update();
122
123            const std::string name_;
[1505]124            std::string value_;
[6425]125            std::string additionalComment_;
126            std::string fileEntry_;
[1505]127            bool bString_;
128    };
129
130
[6425]131    ////////////////////////////////
[1505]132    // ConfigFileEntryVectorValue //
[6425]133    ////////////////////////////////
[1505]134    class _CoreExport ConfigFileEntryVectorValue : public ConfigFileEntryValue
135    {
136        public:
[6425]137            inline ConfigFileEntryVectorValue(const std::string& name, unsigned int index, const std::string& value = "", bool bString = false, const std::string& additionalComment = "")
138                : ConfigFileEntryValue(name, value, bString, additionalComment)
139                , index_(index)
140                { this->update(); /*No virtual calls in base class ctor*/ }
[1505]141
[6425]142            inline ~ConfigFileEntryVectorValue() {}
143
144            inline unsigned int getIndex() const
[1505]145                { return this->index_; }
146
[6425]147            inline const std::string& getKeyString() const
148                { return this->keyString_; }
[1505]149
150        private:
[6425]151            void update();
152
[1505]153            unsigned int index_;
[6425]154            std::string keyString_;
[1505]155    };
156
157
158    ////////////////////////////
159    // ConfigFileEntryComment //
160    ////////////////////////////
161    class _CoreExport ConfigFileEntryComment : public ConfigFileEntry
162    {
163        public:
164            inline ConfigFileEntryComment(const std::string& comment) : comment_(comment) {}
165            inline virtual ~ConfigFileEntryComment() {}
166
167            inline virtual const std::string& getName() const
168                { return this->comment_; }
169
170            inline virtual void setComment(const std::string& comment)
171                { this->comment_ = comment; }
172
173            inline virtual void setValue(const std::string& value)
174                {}
[6425]175            inline virtual const std::string& getValue() const
176                { return BLANKSTRING; }
[1505]177
[6425]178            inline void setString(bool bString)
179                {}
[1505]180
[6425]181            inline virtual const std::string& getFileEntry() const
[1505]182                { return this->comment_; }
183
[6425]184            inline virtual const std::string& getKeyString() const
185                { return BLANKSTRING; }
186
[1505]187        private:
188            std::string comment_;
189    };
190
191
192    ///////////////////////
193    // ConfigFileSection //
194    ///////////////////////
195    class _CoreExport ConfigFileSection
196    {
197        friend class ConfigFile;
198
199        public:
[6417]200            inline ConfigFileSection(const std::string& name, const std::string& additionalComment = "")
201                : name_(name)
202                , additionalComment_(additionalComment)
203                , bUpdated_(false)
204                {}
[1505]205            ~ConfigFileSection();
206
207            inline const std::string& getName() const
208                { return this->name_; }
209
210            inline void setComment(const std::string& comment)
211                { this->additionalComment_ = comment; }
212
213            inline void setValue(const std::string& name, const std::string& value, bool bString)
214                { this->getEntry(name, value, bString)->setValue(value); }
[6425]215            inline const std::string& getValue(const std::string& name, const std::string& fallback, bool bString)
[1505]216                { return this->getEntry(name, fallback, bString)->getValue(); }
217
218            inline void setValue(const std::string& name, unsigned int index, const std::string& value, bool bString)
219                { this->getEntry(name, index, value, bString)->setValue(value); }
[6425]220            inline const std::string& getValue(const std::string& name, unsigned int index, const std::string& fallback, bool bString)
[1505]221                { return this->getEntry(name, index, fallback, bString)->getValue(); }
222
223            void deleteVectorEntries(const std::string& name, unsigned int startindex = 0);
224            unsigned int getVectorSize(const std::string& name);
225
226            std::string getFileEntry() const;
227
228        private:
229            std::list<ConfigFileEntry*>& getEntries()
230                { return this->entries_; }
231            std::list<ConfigFileEntry*>::const_iterator getEntriesBegin() const
232                { return this->entries_.begin(); }
233            std::list<ConfigFileEntry*>::const_iterator getEntriesEnd() const
234                { return this->entries_.end(); }
235
236            std::list<ConfigFileEntry*>::iterator getEntryIterator(const std::string& name, const std::string& fallback, bool bString);
237            std::list<ConfigFileEntry*>::iterator getEntryIterator(const std::string& name, unsigned int index, const std::string& fallback, bool bString);
238
239            inline ConfigFileEntry* getEntry(const std::string& name, const std::string& fallback, bool bString)
240                { return (*this->getEntryIterator(name, fallback, bString)); }
241            inline ConfigFileEntry* getEntry(const std::string& name, unsigned int index, const std::string& fallback, bool bString)
242                { return (*this->getEntryIterator(name, index, fallback, bString)); }
243
244            std::string name_;
245            std::string additionalComment_;
246            std::list<ConfigFileEntry*> entries_;
247            bool bUpdated_;
248    };
249
250
251    ////////////////
252    // ConfigFile //
253    ////////////////
254    class _CoreExport ConfigFile
255    {
256        public:
[2103]257            inline ConfigFile(const std::string& filename, ConfigFileType type)
258                : filename_(filename)
259                , type_(type)
260                , bUpdated_(false)
261            { }
[1505]262            ~ConfigFile();
263
264            void load(bool bCreateIfNotExisting = true);
265            void save() const;
[2103]266            void saveAs(const std::string& filename);
[1505]267            void clean(bool bCleanComments = false);
[2103]268            void clear();
[1505]269
[2103]270            const std::string& getFilename() { return this->filename_; }
271
[1505]272            inline void setValue(const std::string& section, const std::string& name, const std::string& value, bool bString)
273                { this->getSection(section)->setValue(name, value, bString); this->save(); }
[6425]274            inline const std::string& getValue(const std::string& section, const std::string& name, const std::string& fallback, bool bString)
[6417]275                { const std::string& output = this->getSection(section)->getValue(name, fallback, bString); this->saveIfUpdated(); return output; }
[1505]276
277            inline void setValue(const std::string& section, const std::string& name, unsigned int index, const std::string& value, bool bString)
278                { this->getSection(section)->setValue(name, index, value, bString); this->save(); }
[6425]279            inline const std::string& getValue(const std::string& section, const std::string& name, unsigned int index, const std::string& fallback, bool bString)
[6417]280                { const std::string& output = this->getSection(section)->getValue(name, index, fallback, bString); this->saveIfUpdated(); return output; }
[1505]281
282            inline void deleteVectorEntries(const std::string& section, const std::string& name, unsigned int startindex = 0)
283                { this->getSection(section)->deleteVectorEntries(name, startindex); }
284            inline unsigned int getVectorSize(const std::string& section, const std::string& name)
285                { return this->getSection(section)->getVectorSize(name); }
286
[2103]287            void updateConfigValues();
288
[1505]289        private:
290            ConfigFileSection* getSection(const std::string& section);
291            void saveIfUpdated();
292
293            std::string filename_;
[2103]294            ConfigFileType type_;
[1505]295            std::list<ConfigFileSection*> sections_;
296            bool bUpdated_;
297    };
298
299
300    ///////////////////////
301    // ConfigFileManager //
302    ///////////////////////
[3370]303    class _CoreExport ConfigFileManager : public Singleton<ConfigFileManager>
[1505]304    {
[3370]305        friend class Singleton<ConfigFileManager>;
[1505]306        public:
[2103]307            ConfigFileManager();
308            ~ConfigFileManager();
[1505]309
[2103]310            void load();
[1505]311            void save();
312            void clean(bool bCleanComments = false);
313
[2103]314            void setFilename(ConfigFileType type, const std::string& filename);
315            const std::string& getFilename(ConfigFileType type);
316
317            ConfigFileType getNewConfigFileType() { return mininmalFreeType_++; }
318
319            void load(ConfigFileType type);
[1505]320            void save(ConfigFileType type);
[2103]321            void saveAs(ConfigFileType type, const std::string& saveFilename);
[1505]322            void clean(ConfigFileType type, bool bCleanComments = false);
323
324            inline void setValue(ConfigFileType type, const std::string& section, const std::string& name, const std::string& value, bool bString)
325                { this->getFile(type)->setValue(section, name, value, bString); }
[6425]326            inline const std::string& getValue(ConfigFileType type, const std::string& section, const std::string& name, const std::string& fallback, bool bString)
[1505]327                { return this->getFile(type)->getValue(section, name, fallback, bString); }
328
329            inline void setValue(ConfigFileType type, const std::string& section, const std::string& name, unsigned int index, const std::string& value, bool bString)
330                { this->getFile(type)->setValue(section, name, index, value, bString); }
[6425]331            inline const std::string& getValue(ConfigFileType type, const std::string& section, const std::string& name, unsigned int index, const std::string& fallback, bool bString)
[1505]332                { return this->getFile(type)->getValue(section, name, index, fallback, bString); }
333
334            inline void deleteVectorEntries(ConfigFileType type, const std::string& section, const std::string& name, unsigned int startindex = 0)
335                { this->getFile(type)->deleteVectorEntries(section, name, startindex); }
336            inline unsigned int getVectorSize(ConfigFileType type, const std::string& section, const std::string& name)
337                { return this->getFile(type)->getVectorSize(section, name); }
338
[2103]339            void updateConfigValues();
340            void updateConfigValues(ConfigFileType type);
[1505]341
[5695]342            static std::string DEFAULT_CONFIG_FILE;
343
[1505]344        private:
[2103]345            ConfigFileManager(const ConfigFileManager&);
[1505]346
347            ConfigFile* getFile(ConfigFileType type);
348
[2103]349            std::map<ConfigFileType, ConfigFile*> configFiles_;
350            unsigned int mininmalFreeType_;
[1505]351
[3370]352            static ConfigFileManager* singletonPtr_s;
[1505]353    };
[6417]354} // tolua_export
[1505]355
356#endif /* _ConfigFileManager_H__ */
Note: See TracBrowser for help on using the repository browser.