Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/gamestate/src/libraries/core/ConfigFileManager.h @ 6437

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

Added support for keybindings.ini merging:
When running development builds, the keybinder will merge the local file and the one from the data folder.
Catch: if you want to remove a binding, you'll have to write "NoBinding" (not case sensitive) to override the default command

The keybind command already does that for you though.

  • Property svn:eol-style set to native
File size: 15.1 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 <list>
35#include <map>
36#include <set>
37#include <string>
38#include <boost/array.hpp>
39
40#include "util/Singleton.h"
41#include "util/StringUtils.h"
42
43namespace orxonox // tolua_export
44{ // tolua_export
45
46    /////////////////////
47    // ConfigFileEntry //
48    /////////////////////
49    class _CoreExport ConfigFileEntry
50    {
51        public:
52            virtual ~ConfigFileEntry() {};
53            virtual void setValue(const std::string& value) = 0;
54            virtual const std::string& getValue() const = 0;
55            virtual const std::string& getName() const = 0;
56            virtual void setComment(const std::string& comment) = 0;
57            virtual unsigned int getIndex() const { return 0; }
58            virtual void setString(bool bString) = 0;
59            virtual const std::string& getFileEntry() const = 0;
60    };
61
62
63    //////////////////////////
64    // ConfigFileEntryValue //
65    //////////////////////////
66    class _CoreExport ConfigFileEntryValue : public ConfigFileEntry
67    {
68        public:
69            inline ConfigFileEntryValue(const std::string& name, const std::string& value = "", bool bString = false, const std::string& additionalComment = "")
70                : name_(name)
71                , value_(value)
72                , additionalComment_(additionalComment)
73                , bString_(bString)
74                { this->update(); }
75
76            inline virtual ~ConfigFileEntryValue() {}
77
78            inline virtual const std::string& getName() const
79                { return this->name_; }
80
81            inline virtual void setComment(const std::string& comment)
82                { this->additionalComment_ = comment; this->update(); }
83
84            inline virtual void setValue(const std::string& value)
85                { this->value_ = value; this->update(); }
86            inline virtual const std::string& getValue() const
87                { return this->value_; }
88
89            inline void virtual setString(bool bString)
90                { this->bString_ = bString; this->update(); }
91
92            inline virtual const std::string& getFileEntry() const
93                { return this->fileEntry_; }
94
95            inline virtual const std::string& getKeyString() const
96                { return this->name_; }
97
98        protected:
99            virtual void update();
100
101            const std::string name_;
102            std::string value_;
103            std::string additionalComment_;
104            std::string fileEntry_;
105            bool bString_;
106    };
107
108
109    ////////////////////////////////
110    // ConfigFileEntryVectorValue //
111    ////////////////////////////////
112    class _CoreExport ConfigFileEntryVectorValue : public ConfigFileEntryValue
113    {
114        public:
115            inline ConfigFileEntryVectorValue(const std::string& name, unsigned int index, const std::string& value = "", bool bString = false, const std::string& additionalComment = "")
116                : ConfigFileEntryValue(name, value, bString, additionalComment)
117                , index_(index)
118                { this->update(); /*No virtual calls in base class ctor*/ }
119
120            inline ~ConfigFileEntryVectorValue() {}
121
122            inline unsigned int getIndex() const
123                { return this->index_; }
124
125            inline const std::string& getKeyString() const
126                { return this->keyString_; }
127
128        private:
129            void update();
130
131            unsigned int index_;
132            std::string keyString_;
133    };
134
135
136    ////////////////////////////
137    // ConfigFileEntryComment //
138    ////////////////////////////
139    class _CoreExport ConfigFileEntryComment : public ConfigFileEntry
140    {
141        public:
142            inline ConfigFileEntryComment(const std::string& comment) : comment_(comment) {}
143            inline virtual ~ConfigFileEntryComment() {}
144
145            inline virtual const std::string& getName() const
146                { return this->comment_; }
147
148            inline virtual void setComment(const std::string& comment)
149                { this->comment_ = comment; }
150
151            inline virtual void setValue(const std::string& value)
152                {}
153            inline virtual const std::string& getValue() const
154                { return BLANKSTRING; }
155
156            inline void setString(bool bString)
157                {}
158
159            inline virtual const std::string& getFileEntry() const
160                { return this->comment_; }
161
162            inline virtual const std::string& getKeyString() const
163                { return BLANKSTRING; }
164
165        private:
166            std::string comment_;
167    };
168
169
170    ///////////////////////
171    // ConfigFileSection //
172    ///////////////////////
173    class _CoreExport ConfigFileSection
174    {
175        friend class ConfigFile;
176        friend class SettingsConfigFile;
177
178        public:
179            inline ConfigFileSection(const std::string& name, const std::string& additionalComment = "")
180                : name_(name)
181                , additionalComment_(additionalComment)
182                , bUpdated_(false)
183                {}
184            ~ConfigFileSection();
185
186            inline const std::string& getName() const
187                { return this->name_; }
188
189            inline void setComment(const std::string& comment)
190                { this->additionalComment_ = comment; }
191
192            inline void setValue(const std::string& name, const std::string& value, bool bString)
193                { this->getOrCreateEntry(name, value, bString)->setValue(value); }
194            inline const std::string& getValue(const std::string& name, bool bString)
195            {
196                ConfigFileEntry* entry = this->getEntry(name);
197                if (entry)
198                {
199                    entry->setString(bString);
200                    return entry->getValue();
201                }
202                return BLANKSTRING;
203            }
204            inline const std::string& getOrCreateValue(const std::string& name, const std::string& fallback, bool bString)
205                { return this->getOrCreateEntry(name, fallback, bString)->getValue(); }
206
207            inline void setValue(const std::string& name, unsigned int index, const std::string& value, bool bString)
208                { this->getOrCreateEntry(name, index, value, bString)->setValue(value); }
209            inline const std::string& getValue(const std::string& name, unsigned int index, bool bString)
210            {
211                ConfigFileEntry* entry = this->getEntry(name, index);
212                if (entry)
213                {
214                    entry->setString(bString);
215                    return entry->getValue();
216                }
217                return BLANKSTRING;
218            }
219            inline const std::string& getOrCreateValue(const std::string& name, unsigned int index, const std::string& fallback, bool bString)
220                { return this->getOrCreateEntry(name, index, fallback, bString)->getValue(); }
221
222            void deleteVectorEntries(const std::string& name, unsigned int startindex = 0);
223            unsigned int getVectorSize(const std::string& name) const;
224
225            std::string getFileEntry() const;
226
227        private:
228            std::list<ConfigFileEntry*>& getEntries()
229                { return this->entries_; }
230            std::list<ConfigFileEntry*>::const_iterator getEntriesBegin() const
231                { return this->entries_.begin(); }
232            std::list<ConfigFileEntry*>::const_iterator getEntriesEnd() const
233                { return this->entries_.end(); }
234
235            std::list<ConfigFileEntry*>::iterator getOrCreateEntryIterator(const std::string& name, const std::string& fallback, bool bString);
236            std::list<ConfigFileEntry*>::iterator getOrCreateEntryIterator(const std::string& name, unsigned int index, const std::string& fallback, bool bString);
237
238            ConfigFileEntry* getEntry(const std::string& name) const;
239            inline ConfigFileEntry* getOrCreateEntry(const std::string& name, const std::string& fallback, bool bString)
240                { return (*this->getOrCreateEntryIterator(name, fallback, bString)); }
241            ConfigFileEntry* getEntry(const std::string& name, unsigned int index) const;
242            inline ConfigFileEntry* getOrCreateEntry(const std::string& name, unsigned int index, const std::string& fallback, bool bString)
243                { return (*this->getOrCreateEntryIterator(name, index, fallback, bString)); }
244
245            std::string name_;
246            std::string additionalComment_;
247            std::list<ConfigFileEntry*> entries_;
248            bool bUpdated_;
249    };
250
251
252    ////////////////
253    // ConfigFile //
254    ////////////////
255    class _CoreExport ConfigFile
256    {
257        public:
258            ConfigFile(const std::string& filename, bool bCopyFallbackFile = true);
259            virtual ~ConfigFile();
260
261            virtual void load();
262            virtual void save() const;
263            virtual void saveAs(const std::string& filename) const;
264            virtual void clear();
265
266            inline const std::string& getFilename()
267                { return this->filename_; }
268
269            inline void setValue(const std::string& section, const std::string& name, const std::string& value, bool bString)
270            {
271                this->getOrCreateSection(section)->setValue(name, value, bString);
272                this->save();
273            }
274            inline const std::string& getValue(const std::string& section, const std::string& name, bool bString)
275            {
276                ConfigFileSection* sectionPtr = this->getSection(section);
277                return (sectionPtr ? sectionPtr->getValue(name, bString) : BLANKSTRING);
278            }
279            const std::string& getOrCreateValue(const std::string& section, const std::string& name, const std::string& fallback, bool bString);
280
281            inline void setValue(const std::string& section, const std::string& name, unsigned int index, const std::string& value, bool bString)
282            {
283                this->getOrCreateSection(section)->setValue(name, index, value, bString);
284                this->save();
285            }
286            inline const std::string& getValue(const std::string& section, const std::string& name, unsigned int index, bool bString)
287            {
288                ConfigFileSection* sectionPtr = this->getSection(section);
289                return (sectionPtr ? sectionPtr->getValue(name, index, bString) : BLANKSTRING);
290            }
291            const std::string& getOrCreateValue(const std::string& section, const std::string& name, unsigned int index, const std::string& fallback, bool bString);
292
293            void deleteVectorEntries(const std::string& section, const std::string& name, unsigned int startindex = 0);
294            inline unsigned int getVectorSize(const std::string& section, const std::string& name) const
295            {
296                ConfigFileSection* sectionPtr = this->getSection(section);
297                return (sectionPtr ? sectionPtr->getVectorSize(name) : 0);
298            }
299
300            static const char* DEFAULT_CONFIG_FOLDER;
301
302        protected:
303            ConfigFileSection* getSection(const std::string& section) const;
304            ConfigFileSection* getOrCreateSection(const std::string& section);
305
306            std::list<ConfigFileSection*> sections_;
307
308        private:
309            void saveIfUpdated();
310            const std::string filename_;
311            const bool bCopyFallbackFile_;
312            bool bUpdated_;
313    };
314
315
316    ////////////////////////
317    // SettingsConfigFile //
318    ////////////////////////
319    class _CoreExport SettingsConfigFile // tolua_export
320        : public ConfigFile, public Singleton<SettingsConfigFile>
321    { // tolua_export
322        friend class Singleton<SettingsConfigFile>;
323
324        public:
325            typedef std::multimap<std::string, std::pair<std::string, ConfigValueContainer*> > ContainerMap;
326
327            SettingsConfigFile(const std::string& filename);
328            ~SettingsConfigFile();
329
330            void load(); // tolua_export
331            void setFilename(const std::string& filename); // tolua_export
332            void clean(bool bCleanComments = false); // tolua_export
333
334            bool config(const std::string& section, const std::string& entry, const std::string& value); // tolua_export
335            bool tconfig(const std::string& section, const std::string& entry, const std::string& value); // tolua_export
336            std::string getConfig(const std::string& section, const std::string& entry); // tolua_export
337
338            void addConfigValueContainer(ConfigValueContainer* container);
339            void removeConfigValueContainer(ConfigValueContainer* container);
340
341            inline const std::set<std::string>& getSectionNames()
342                { return this->sectionNames_; }
343            inline ContainerMap::const_iterator getContainerLowerBound(const std::string section)
344                { return this->containers_.lower_bound(section); }
345            inline ContainerMap::const_iterator getContainerUpperBound(const std::string section)
346                { return this->containers_.upper_bound(section); }
347
348            static SettingsConfigFile& getInstance() { return Singleton<SettingsConfigFile>::getInstance(); } // tolua_export
349
350        private:
351            void updateConfigValues();
352            bool configImpl(const std::string& section, const std::string& entry, const std::string& value, bool (ConfigValueContainer::*function)(const MultiType&));
353
354            ContainerMap containers_;
355            std::set<std::string> sectionNames_;
356            static SettingsConfigFile* singletonPtr_s;
357    }; // tolua_export
358
359
360    ///////////////////////
361    // ConfigFileManager //
362    ///////////////////////
363    class _CoreExport ConfigFileManager : public Singleton<ConfigFileManager>
364    {
365        friend class Singleton<ConfigFileManager>;
366        public:
367            ConfigFileManager();
368            ~ConfigFileManager();
369
370            void setFilename(ConfigFileType::Value type, const std::string& filename);
371
372            inline ConfigFile* getConfigFile(ConfigFileType::Value type)
373            {
374                // Check array bounds
375                return configFiles_.at(type);
376            }
377
378        private:
379            ConfigFileManager(const ConfigFileManager&);
380
381            boost::array<ConfigFile*, 3> configFiles_;
382            static ConfigFileManager* singletonPtr_s;
383    };
384} // tolua_export
385
386#endif /* _ConfigFileManager_H__ */
Note: See TracBrowser for help on using the repository browser.