Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/core/config/ConfigFileSection.h @ 12302

Last change on this file since 12302 was 11071, checked in by landauf, 9 years ago

merged branch cpp11_v3 back to trunk

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