Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/libs/src/libraries/core/config/ConfigFileSection.h @ 10190

Last change on this file since 10190 was 9559, checked in by landauf, 12 years ago

split ConfigFileManager.h/cc into multiple files, one for each class. why did I ever put them all into the same file?

  • Property svn:eol-style set to native
File size: 9.7 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 _ConfigFileSection_H__
35#define _ConfigFileSection_H__
36
37#include "core/CorePrereqs.h"
38
39#include "ConfigFileEntry.h"
40
41namespace orxonox
42{
43    ///////////////////////
44    // ConfigFileSection //
45    ///////////////////////
46    /**
47        @brief Represents a section in a config file.
48
49        A section has a name and a list of config values.
50    */
51    class _CoreExport ConfigFileSection
52    {
53        friend class ConfigFile;
54        friend class SettingsConfigFile;
55
56        public:
57            /**
58                @brief Constructor: Initializes the section.
59
60                @param name The name of the section
61                @param additionalComment An additional comment placed after the title of the section in the config file
62            */
63            inline ConfigFileSection(const std::string& name, const std::string& additionalComment = "")
64                : name_(name)
65                , additionalComment_(additionalComment)
66                , bUpdated_(false)
67                {}
68            ~ConfigFileSection();
69
70            /// Returns the name of the section.
71            inline const std::string& getName() const
72                { return this->name_; }
73
74            /// Changes the comment which is placed after the title of the section in the config file.
75            inline void setComment(const std::string& comment)
76                { this->additionalComment_ = comment; }
77
78            /**
79                @brief Stores a value in the section. If the entry doesn't exist, it's created.
80
81                @param name     The name of the entry
82                @param value    The new value
83                @param bString  If true, the value is treated as string which means some special treatment of special characters.
84            */
85            inline void setValue(const std::string& name, const std::string& value, bool bString)
86                { this->getOrCreateEntry(name, value, bString)->setValue(value); }
87            /**
88                @brief Returns the value of a given entry in the section. Returns a blank string if the value doesn't exist.
89
90                @param name     The name of the entry
91                @param bString  If true, the value is treated as string which means some special treatment of special characters.
92            */
93            inline const std::string& getValue(const std::string& name, bool bString)
94            {
95                ConfigFileEntry* entry = this->getEntry(name);
96                if (entry)
97                {
98                    entry->setString(bString);  // if the entry was loaded from the config file, we have to tell it if it's a string
99                    return entry->getValue();
100                }
101                return BLANKSTRING;
102            }
103            /**
104                @brief Returns the value of a given entry in the section. If it doesn't exist, the entry is created using the fallback value.
105
106                @param name     The name of the entry
107                @param fallback The value that will be used if the entry doesn't exist
108                @param bString  If true, the value is treated as string which means some special treatment of special characters.
109            */
110            inline const std::string& getOrCreateValue(const std::string& name, const std::string& fallback, bool bString)
111                { return this->getOrCreateEntry(name, fallback, bString)->getValue(); }
112
113            /**
114                @brief Stores the value of an element of a vector in the section. If the entry doesn't exist, it's created.
115
116                @param name     The name of the vector
117                @param index    The index of the element in the vector
118                @param value    The new value
119                @param bString  If true, the value is treated as string which means some special treatment of special characters.
120            */
121            inline void setValue(const std::string& name, unsigned int index, const std::string& value, bool bString)
122                { this->getOrCreateEntry(name, index, value, bString)->setValue(value); }
123            /**
124                @brief Returns the value of a given element of a vector in the section. Returns a blank string if the value doesn't exist.
125
126                @param name     The name of the vector
127                @param index    The index of the element in the vector
128                @param bString  If true, the value is treated as string which means some special treatment of special characters.
129            */
130            inline const std::string& getValue(const std::string& name, unsigned int index, bool bString)
131            {
132                ConfigFileEntry* entry = this->getEntry(name, index);
133                if (entry)
134                {
135                    entry->setString(bString);  // if the entry was loaded from the config file, we have to tell it if it's a string
136                    return entry->getValue();
137                }
138                return BLANKSTRING;
139            }
140            /**
141                @brief Returns the value of a given element of a vector in the section. If it doesn't exist, the entry is created using the fallback value.
142
143                @param name     The name of the vector
144                @param index    The index of the element in the vector
145                @param fallback The value that will be used if the entry doesn't exist
146                @param bString  If true, the value is treated as string which means some special treatment of special characters.
147            */
148            inline const std::string& getOrCreateValue(const std::string& name, unsigned int index, const std::string& fallback, bool bString)
149                { return this->getOrCreateEntry(name, index, fallback, bString)->getValue(); }
150
151            void deleteVectorEntries(const std::string& name, unsigned int startindex = 0);
152            unsigned int getVectorSize(const std::string& name) const;
153
154            std::string getFileEntry() const;
155
156        private:
157            /// Returns the list of entries in this section.
158            std::list<ConfigFileEntry*>& getEntries()
159                { return this->entries_; }
160            /// Returns the begin-iterator of the list of entries in this section.
161            std::list<ConfigFileEntry*>::const_iterator getEntriesBegin() const
162                { return this->entries_.begin(); }
163            /// Returns the end-iterator of the list of entries in this section.
164            std::list<ConfigFileEntry*>::const_iterator getEntriesEnd() const
165                { return this->entries_.end(); }
166
167            std::list<ConfigFileEntry*>::iterator getOrCreateEntryIterator(const std::string& name, const std::string& fallback, bool bString);
168            std::list<ConfigFileEntry*>::iterator getOrCreateEntryIterator(const std::string& name, unsigned int index, const std::string& fallback, bool bString);
169
170            ConfigFileEntry* getEntry(const std::string& name) const;
171            /**
172                @brief Returns the entry with given name. If it doesn't exist, the entry is created using the fallback value.
173
174                @param name     The name of the entry
175                @param fallback The value that will be used if the entry doesn't exist
176                @param bString  If true, the value is treated as string which means some special treatment of special characters.
177            */
178            inline ConfigFileEntry* getOrCreateEntry(const std::string& name, const std::string& fallback, bool bString)
179                { return (*this->getOrCreateEntryIterator(name, fallback, bString)); }
180
181            ConfigFileEntry* getEntry(const std::string& name, unsigned int index) const;
182            /**
183                @brief Returns the entry that contains an element of a vector with given name. If it doesn't exist, the entry is created using the fallback value.
184
185                @param name     The name of the entry
186                @param index    The index of the element in the vector
187                @param fallback The value that will be used if the entry doesn't exist
188                @param bString  If true, the value is treated as string which means some special treatment of special characters.
189            */
190            inline ConfigFileEntry* getOrCreateEntry(const std::string& name, unsigned int index, const std::string& fallback, bool bString)
191                { return (*this->getOrCreateEntryIterator(name, index, fallback, bString)); }
192
193            std::string name_;                      ///< The name of the section
194            std::string additionalComment_;         ///< The additional comment which is placed after the title of the section in the config file
195            std::list<ConfigFileEntry*> entries_;   ///< The list of entries in this section
196            bool bUpdated_;                         ///< True if an entry is created
197    };
198}
199
200#endif /* _ConfigFileSection_H__ */
Note: See TracBrowser for help on using the repository browser.