Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/doc/src/libraries/core/ConfigFileManager.h @ 7363

Last change on this file since 7363 was 7363, checked in by landauf, 14 years ago

assigned a group to each header file in the core library

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