Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/consolecommands3/src/libraries/core/ConfigFileManager.h @ 7196

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

removed unnecessary return values from console commands "log", "error", "warning", "info", and "debug"
removed return value from console command Chat (the non-static chat functions still return a bool)
config and tconfig don't return a bool anymore but instead print an error if the config value doesn't exist.

fixed console command "printRTT" - it shouldn't crash on a standalone system.

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