xoreos  0.0.5
configfile.h
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 // Inspired by ScummVM's config file and manager code
22 
27 #ifndef COMMON_CONFIGFILE_H
28 #define COMMON_CONFIGFILE_H
29 
30 #include <map>
31 
32 #include <boost/noncopyable.hpp>
33 
34 #include "src/common/ptrlist.h"
35 #include "src/common/ustring.h"
36 #include "src/common/stringmap.h"
37 
38 namespace Common {
39 
40 class SeekableReadStream;
41 class WriteStream;
42 
43 class ConfigFile;
44 
46 class ConfigDomain {
47 public:
48  ConfigDomain(const UString &name);
49  ~ConfigDomain();
50 
51  const UString &getName() const;
52 
53  bool hasKey(const UString &key) const;
54 
55  bool getKey(const UString &key, UString &value) const;
56 
57  // Specialized getters
58  UString getString(const UString &key, const UString &def = "" ) const;
59  bool getBool (const UString &key, bool def = false) const;
60  int getInt (const UString &key, int def = 0 ) const;
61  uint getUint (const UString &key, uint def = 0 ) const;
62  double getDouble(const UString &key, double def = 0.0 ) const;
63 
64  void setKey(const UString &key, const UString &value);
65 
66  // Specialized setters
67  void setString(const UString &key, const UString &value);
68  void setBool (const UString &key, bool value);
69  void setInt (const UString &key, int value);
70  void setUint (const UString &key, uint value);
71  void setDouble(const UString &key, double value);
72 
73  bool removeKey(const UString &key);
74 
75  bool renameKey(const UString &oldName, const UString &newName);
76 
82  void set(const ConfigDomain &domain, bool clobber = true);
83 
84 private:
86  struct Line {
87  StringIMap::const_iterator key;
89  };
90 
91  typedef std::list<Line> LineList;
92 
94 
97 
100 
101  friend class ConfigFile;
102 };
103 
113 class ConfigFile : boost::noncopyable {
114 public:
116 
117  ConfigFile();
118  ~ConfigFile();
119 
125  static bool isValidName(const UString &name);
126 
128  void clear();
129 
130  void load(SeekableReadStream &stream);
131  void save(WriteStream &stream) const;
132 
133  bool hasDomain(const UString &name) const;
134 
135  const DomainList &getDomains() const;
136 
137  ConfigDomain *getDomain(const UString &name);
138  const ConfigDomain *getDomain(const UString &name) const;
139 
140  ConfigDomain *addDomain(const UString &name);
141 
142  bool removeDomain(const UString &name);
143 
144  bool renameDomain(const UString &oldName, const UString &newName);
145 
146 private:
147  typedef std::map<UString, ConfigDomain *, UString::iless> DomainMap;
148 
151 
154 
155  // Loading helpers
156  void parseConfigLine(const UString &line, UString &domainName,
157  UString &key, UString &value, UString &comment, int lineNumber);
158  void addDomainKey(ConfigDomain &domain, const UString &key,
159  const UString &value, const UString &comment, int lineNumber);
160  void addDomain(ConfigDomain *domain, int lineNumber);
161 };
162 
163 } // End of namespace Common
164 
165 #endif // COMMON_CONFIGFILE_H
UString _comment
Comment on the same line as the domain definition.
Definition: configfile.h:98
bool removeDomain(const UString &name)
Definition: configfile.cpp:590
This class allows reading/writing INI style config files.
Definition: configfile.h:113
void setDouble(const UString &key, double value)
Definition: configfile.cpp:165
bool renameDomain(const UString &oldName, const UString &newName)
Definition: configfile.cpp:603
Definition: 2dafile.h:39
void save(WriteStream &stream) const
Definition: configfile.cpp:492
A class holding an UTF-8 string.
Definition: ustring.h:48
PtrList< ConfigDomain > DomainList
Definition: configfile.h:115
UString _prologue
Comment directly above the domain.
Definition: configfile.h:99
void setString(const UString &key, const UString &value)
Definition: configfile.cpp:149
void load(SeekableReadStream &stream)
Definition: configfile.cpp:283
std::map< UString, UString, UString::iless > StringIMap
Definition: stringmap.h:37
DomainList _domainList
List of domains in order.
Definition: configfile.h:149
void parseConfigLine(const UString &line, UString &domainName, UString &key, UString &value, UString &comment, int lineNumber)
Definition: configfile.cpp:421
UString getString(const UString &key, const UString &def="") const
Definition: configfile.cpp:63
ConfigDomain(const UString &name)
Definition: configfile.cpp:40
const DomainList & getDomains() const
Definition: configfile.cpp:555
StringIMap _keys
The key/value pairs of the config domain.
Definition: configfile.h:96
A list storing pointer to objects, with automatic deletion.
bool removeKey(const UString &key)
Definition: configfile.cpp:169
int getInt(const UString &key, int def=0) const
Definition: configfile.cpp:86
bool getKey(const UString &key, UString &value) const
Definition: configfile.cpp:54
DomainMap _domainMap
Domains indexed by name.
Definition: configfile.h:150
LineList _lines
The lines of the config domain.
Definition: configfile.h:95
bool renameKey(const UString &oldName, const UString &newName)
Definition: configfile.cpp:195
Generic interface for a writable data stream.
Definition: writestream.h:64
void setBool(const UString &key, bool value)
Definition: configfile.cpp:153
void setInt(const UString &key, int value)
Definition: configfile.cpp:157
double getDouble(const UString &key, double def=0.0) const
Definition: configfile.cpp:116
Unicode string handling.
std::map< UString, ConfigDomain *, UString::iless > DomainMap
Definition: configfile.h:147
UString _prologue
Comments on top of the file.
Definition: configfile.h:152
A map to quickly match strings from a list.
const UString & getName() const
Definition: configfile.cpp:46
Accessor for a domain (section) in a config file.
Definition: configfile.h:46
bool hasDomain(const UString &name) const
Definition: configfile.cpp:551
bool getBool(const UString &key, bool def=false) const
Definition: configfile.cpp:71
A line in the config domain.
Definition: configfile.h:86
void clear()
Reset everything stored in this config file.
Definition: configfile.cpp:275
UString _epilogue
Comments at the bottom of the file.
Definition: configfile.h:153
void addDomainKey(ConfigDomain &domain, const UString &key, const UString &value, const UString &comment, int lineNumber)
Definition: configfile.cpp:380
std::list< Line > LineList
Definition: configfile.h:91
ConfigDomain * getDomain(const UString &name)
Definition: configfile.cpp:559
void setKey(const UString &key, const UString &value)
Definition: configfile.cpp:131
uint getUint(const UString &key, uint def=0) const
Definition: configfile.cpp:101
static bool isValidName(const UString &name)
Check whether the given string is a valid section or key name.
Definition: configfile.cpp:263
bool hasKey(const UString &key) const
Definition: configfile.cpp:50
Interface for a seekable & readable data stream.
Definition: readstream.h:265
UString comment
Line comment.
Definition: configfile.h:88
ConfigDomain * addDomain(const UString &name)
Definition: configfile.cpp:575
unsigned int uint
Definition: types.h:211
StringIMap::const_iterator key
Pointer to the key/value pair.
Definition: configfile.h:87
void setUint(const UString &key, uint value)
Definition: configfile.cpp:161