Orxonox  0.0.5 Codename: Arcturus
ConfigFile.h
Go to the documentation of this file.
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 
34 #ifndef _ConfigFile_H__
35 #define _ConfigFile_H__
36 
37 #include "core/CorePrereqs.h"
38 
39 #include "ConfigFileSection.h"
40 
41 namespace orxonox
42 {
44  // ConfigFile //
46 
52  {
53  public:
54  ConfigFile(const std::string& filename, bool bCopyFallbackFile = true);
55  virtual ~ConfigFile();
56 
57  virtual void load();
58  virtual void save() const;
59  virtual void saveAs(const std::string& filename) const;
60  virtual void clear();
61 
63  inline const std::string& getFilename()
64  { return this->filename_; }
65 
74  inline void setValue(const std::string& section, const std::string& name, const std::string& value, bool bString)
75  {
76  this->getOrCreateSection(section)->setValue(name, value, bString);
77  this->save();
78  }
86  inline const std::string& getValue(const std::string& section, const std::string& name, bool bString)
87  {
88  ConfigFileSection* sectionPtr = this->getSection(section);
89  return (sectionPtr ? sectionPtr->getValue(name, bString) : BLANKSTRING);
90  }
99  inline const std::string& getOrCreateValue(const std::string& section, const std::string& name, const std::string& fallback, bool bString)
100  {
101  const std::string& output = this->getOrCreateSection(section)->getOrCreateValue(name, fallback, bString);
102  this->saveIfUpdated();
103  return output;
104  }
105 
115  inline void setValue(const std::string& section, const std::string& name, unsigned int index, const std::string& value, bool bString)
116  {
117  this->getOrCreateSection(section)->setValue(name, index, value, bString);
118  this->save();
119  }
128  inline const std::string& getValue(const std::string& section, const std::string& name, unsigned int index, bool bString)
129  {
130  ConfigFileSection* sectionPtr = this->getSection(section);
131  return (sectionPtr ? sectionPtr->getValue(name, index, bString) : BLANKSTRING);
132  }
142  const std::string& getOrCreateValue(const std::string& section, const std::string& name, unsigned int index, const std::string& fallback, bool bString)
143  {
144  const std::string& output = this->getOrCreateSection(section)->getOrCreateValue(name, index, fallback, bString);
145  this->saveIfUpdated();
146  return output;
147  }
148 
149  void deleteVectorEntries(const std::string& section, const std::string& name, unsigned int startindex = 0);
155  inline unsigned int getVectorSize(const std::string& section, const std::string& name) const
156  {
157  ConfigFileSection* sectionPtr = this->getSection(section);
158  return (sectionPtr ? sectionPtr->getVectorSize(name) : 0);
159  }
160 
161  static const char* DEFAULT_CONFIG_FOLDER;
162 
163  protected:
164  ConfigFileSection* getSection(const std::string& section) const;
165  ConfigFileSection* getOrCreateSection(const std::string& section);
166 
167  std::list<ConfigFileSection*> sections_;
168 
169  private:
170  void saveIfUpdated();
171 
173  const bool bCopyFallbackFile_;
174  bool bUpdated_;
175  };
176 }
177 
178 #endif /* _ConfigFile_H__ */
std::string BLANKSTRING
A blank string (""). Used to return a blank string by reference.
Definition: StringUtils.cc:46
bool bUpdated_
Becomes true if a section is added.
Definition: ConfigFile.h:174
unsigned int getVectorSize(const std::string &section, const std::string &name) const
Returns the size of a config vector.
Definition: ConfigFile.h:155
unsigned int getVectorSize(const std::string &name) const
Returns the size of a config vector.
Definition: ConfigFileSection.cc:79
const std::string & getValue(const std::string &section, const std::string &name, unsigned int index, bool bString)
Returns the value of a given element of a vector in the config file.
Definition: ConfigFile.h:128
static const char * DEFAULT_CONFIG_FOLDER
The folder where the default config files will be stored.
Definition: ConfigFile.h:161
const std::string & getValue(const std::string &name, bool bString)
Returns the value of a given entry in the section.
Definition: ConfigFileSection.h:96
Shared library macros, enums, constants and forward declarations for the core library ...
::std::string string
Definition: gtest-port.h:756
const bool bCopyFallbackFile_
If true, the default config file is copied into the config-directory before loading the file...
Definition: ConfigFile.h:173
This class represents a config file, which is stored on the hard-disk and contains config values in d...
Definition: ConfigFile.h:51
void setValue(const std::string &section, const std::string &name, unsigned int index, const std::string &value, bool bString)
Stores the value of an element of a vector in the config file.
Definition: ConfigFile.h:115
Die Wagnis Klasse hat die folgenden Aufgaben:
Definition: ApplicationPaths.cc:66
const std::string & getOrCreateValue(const std::string &section, const std::string &name, unsigned int index, const std::string &fallback, bool bString)
Returns the value of a given element of a vector in the config file.
Definition: ConfigFile.h:142
#define _CoreExport
Definition: CorePrereqs.h:61
ARGUMENT_COMPLETION_FUNCTION_IMPLEMENTATION() fallback()
Fallback implementation, returns an empty list.
Definition: ArgumentCompletionFunctions.cc:67
const std::string & getOrCreateValue(const std::string &section, const std::string &name, const std::string &fallback, bool bString)
Returns the value of a given entry in the config file.
Definition: ConfigFile.h:99
Represents a section in a config file.
Definition: ConfigFileSection.h:54
void setValue(const std::string &section, const std::string &name, const std::string &value, bool bString)
Stores a value in the config file.
Definition: ConfigFile.h:74
const std::string filename_
The filename of this config file.
Definition: ConfigFile.h:172
std::list< ConfigFileSection * > sections_
A list of sections in this config file.
Definition: ConfigFile.h:167
const std::string & getValue(const std::string &section, const std::string &name, bool bString)
Returns the value of a given entry in the config file.
Definition: ConfigFile.h:86
const std::string & getFilename()
Returns the file-name of this config file.
Definition: ConfigFile.h:63