Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/levelMichael/src/libraries/core/config/ConfigFileSection.h @ 9696

Last change on this file since 9696 was 9684, checked in by landauf, 11 years ago

added missing includes

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