1 |
/* ---------------------------------------------------------------------------- |
---|
2 |
libconfig - A library for processing structured configuration files |
---|
3 |
Copyright (C) 2005-2008 Mark A Lindner |
---|
4 |
|
---|
5 |
This file is part of libconfig. |
---|
6 |
|
---|
7 |
This library is free software; you can redistribute it and/or |
---|
8 |
modify it under the terms of the GNU Lesser General Public License |
---|
9 |
as published by the Free Software Foundation; either version 2.1 of |
---|
10 |
the License, or (at your option) any later version. |
---|
11 |
|
---|
12 |
This library is distributed in the hope that it will be useful, but |
---|
13 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
15 |
Lesser General Public License for more details. |
---|
16 |
|
---|
17 |
You should have received a copy of the GNU Library General Public |
---|
18 |
License along with this library; if not, see |
---|
19 |
<http://www.gnu.org/licenses/>. |
---|
20 |
---------------------------------------------------------------------------- |
---|
21 |
*/ |
---|
22 |
|
---|
23 |
#ifndef __libconfig_hpp |
---|
24 |
#define __libconfig_hpp |
---|
25 |
|
---|
26 |
#include <stdio.h> |
---|
27 |
#include <string> |
---|
28 |
#include <map> |
---|
29 |
|
---|
30 |
namespace libconfig |
---|
31 |
{ |
---|
32 |
|
---|
33 |
#include "libconfig.h" |
---|
34 |
|
---|
35 |
class LIBCONFIG_API ConfigException { }; |
---|
36 |
|
---|
37 |
class LIBCONFIG_API SettingException : public ConfigException |
---|
38 |
{ |
---|
39 |
public: |
---|
40 |
|
---|
41 |
SettingException(); |
---|
42 |
virtual ~SettingException(); |
---|
43 |
}; |
---|
44 |
|
---|
45 |
class LIBCONFIG_API SettingTypeException : public SettingException |
---|
46 |
{ |
---|
47 |
public: |
---|
48 |
|
---|
49 |
SettingTypeException(); |
---|
50 |
}; |
---|
51 |
|
---|
52 |
class LIBCONFIG_API SettingNotFoundException : public SettingException |
---|
53 |
{ |
---|
54 |
public: |
---|
55 |
|
---|
56 |
SettingNotFoundException(); |
---|
57 |
}; |
---|
58 |
|
---|
59 |
class LIBCONFIG_API SettingNameException : public SettingException |
---|
60 |
{ |
---|
61 |
public: |
---|
62 |
|
---|
63 |
SettingNameException(); |
---|
64 |
}; |
---|
65 |
|
---|
66 |
class LIBCONFIG_API FileIOException : public ConfigException { }; |
---|
67 |
|
---|
68 |
class LIBCONFIG_API ParseException : public ConfigException |
---|
69 |
{ |
---|
70 |
friend class Config; |
---|
71 |
|
---|
72 |
public: |
---|
73 |
|
---|
74 |
virtual ~ParseException(); |
---|
75 |
|
---|
76 |
inline int getLine() throw() |
---|
77 |
{ return(_line); } |
---|
78 |
|
---|
79 |
inline const char *getError() throw() |
---|
80 |
{ return(_error); } |
---|
81 |
|
---|
82 |
private: |
---|
83 |
|
---|
84 |
ParseException(int line, const char *error); |
---|
85 |
|
---|
86 |
int _line; |
---|
87 |
const char *_error; |
---|
88 |
}; |
---|
89 |
|
---|
90 |
class LIBCONFIG_API Setting |
---|
91 |
{ |
---|
92 |
friend class Config; |
---|
93 |
|
---|
94 |
public: |
---|
95 |
|
---|
96 |
enum Type |
---|
97 |
{ |
---|
98 |
TypeNone = 0, |
---|
99 |
// scalar types |
---|
100 |
TypeInt, |
---|
101 |
TypeInt64, |
---|
102 |
TypeFloat, |
---|
103 |
TypeString, |
---|
104 |
TypeBoolean, |
---|
105 |
// aggregate types |
---|
106 |
TypeGroup, |
---|
107 |
TypeArray, |
---|
108 |
TypeList |
---|
109 |
}; |
---|
110 |
|
---|
111 |
enum Format |
---|
112 |
{ |
---|
113 |
FormatDefault = 0, |
---|
114 |
FormatHex = 1 |
---|
115 |
}; |
---|
116 |
|
---|
117 |
private: |
---|
118 |
|
---|
119 |
config_setting_t *_setting; |
---|
120 |
Type _type; |
---|
121 |
Format _format; |
---|
122 |
|
---|
123 |
Setting(config_setting_t *setting); |
---|
124 |
|
---|
125 |
void assertType(Type type) const |
---|
126 |
throw(SettingTypeException); |
---|
127 |
static Setting & wrapSetting(config_setting_t *setting); |
---|
128 |
|
---|
129 |
Setting(const Setting& other); // not supported |
---|
130 |
Setting& operator=(const Setting& other); // not supported |
---|
131 |
|
---|
132 |
public: |
---|
133 |
|
---|
134 |
virtual ~Setting() throw(); |
---|
135 |
|
---|
136 |
inline Type getType() const throw() { return(_type); } |
---|
137 |
|
---|
138 |
inline Format getFormat() const throw() { return(_format); } |
---|
139 |
void setFormat(Format format) throw(); |
---|
140 |
|
---|
141 |
operator bool() const throw(SettingTypeException); |
---|
142 |
operator long() const throw(SettingTypeException); |
---|
143 |
operator unsigned long() const throw(SettingTypeException); |
---|
144 |
operator int() const throw(SettingTypeException); |
---|
145 |
operator unsigned int() const throw(SettingTypeException); |
---|
146 |
operator long long() const throw(SettingTypeException); |
---|
147 |
operator unsigned long long() const throw(SettingTypeException); |
---|
148 |
operator double() const throw(SettingTypeException); |
---|
149 |
operator float() const throw(SettingTypeException); |
---|
150 |
operator const char *() const throw(SettingTypeException); |
---|
151 |
operator std::string() const throw(SettingTypeException); |
---|
152 |
|
---|
153 |
Setting & operator=(bool value) throw(SettingTypeException); |
---|
154 |
Setting & operator=(long value) throw(SettingTypeException); |
---|
155 |
Setting & operator=(int value) throw(SettingTypeException); |
---|
156 |
Setting & operator=(const long long &value) throw(SettingTypeException); |
---|
157 |
Setting & operator=(const double &value) throw(SettingTypeException); |
---|
158 |
Setting & operator=(float value) throw(SettingTypeException); |
---|
159 |
Setting & operator=(const char *value) throw(SettingTypeException); |
---|
160 |
Setting & operator=(const std::string &value) throw(SettingTypeException); |
---|
161 |
|
---|
162 |
Setting & operator[](const char * key) const |
---|
163 |
throw(SettingTypeException, SettingNotFoundException); |
---|
164 |
|
---|
165 |
inline Setting & operator[](const std::string & key) const |
---|
166 |
throw(SettingTypeException, SettingNotFoundException) |
---|
167 |
{ return(operator[](key.c_str())); } |
---|
168 |
|
---|
169 |
Setting & operator[](int index) const |
---|
170 |
throw(SettingTypeException, SettingNotFoundException); |
---|
171 |
|
---|
172 |
bool lookupValue(const char *name, bool &value) const throw(); |
---|
173 |
bool lookupValue(const char *name, long &value) const throw(); |
---|
174 |
bool lookupValue(const char *name, unsigned long &value) const throw(); |
---|
175 |
bool lookupValue(const char *name, int &value) const throw(); |
---|
176 |
bool lookupValue(const char *name, unsigned int &value) const throw(); |
---|
177 |
bool lookupValue(const char *name, long long &value) const throw(); |
---|
178 |
bool lookupValue(const char *name, unsigned long long &value) |
---|
179 |
const throw(); |
---|
180 |
bool lookupValue(const char *name, double &value) const throw(); |
---|
181 |
bool lookupValue(const char *name, float &value) const throw(); |
---|
182 |
bool lookupValue(const char *name, const char *&value) const throw(); |
---|
183 |
bool lookupValue(const char *name, std::string &value) const throw(); |
---|
184 |
|
---|
185 |
inline bool lookupValue(const std::string &name, bool &value) |
---|
186 |
const throw() |
---|
187 |
{ return(lookupValue(name.c_str(), value)); } |
---|
188 |
|
---|
189 |
inline bool lookupValue(const std::string &name, long &value) |
---|
190 |
const throw() |
---|
191 |
{ return(lookupValue(name.c_str(), value)); } |
---|
192 |
|
---|
193 |
inline bool lookupValue(const std::string &name, unsigned long &value) |
---|
194 |
const throw() |
---|
195 |
{ return(lookupValue(name.c_str(), value)); } |
---|
196 |
|
---|
197 |
inline bool lookupValue(const std::string &name, int &value) const throw() |
---|
198 |
{ return(lookupValue(name.c_str(), value)); } |
---|
199 |
|
---|
200 |
inline bool lookupValue(const std::string &name, unsigned int &value) |
---|
201 |
const throw() |
---|
202 |
{ return(lookupValue(name.c_str(), value)); } |
---|
203 |
|
---|
204 |
inline bool lookupValue(const std::string &name, long long &value) |
---|
205 |
const throw() |
---|
206 |
{ return(lookupValue(name.c_str(), value)); } |
---|
207 |
|
---|
208 |
inline bool lookupValue(const std::string &name, |
---|
209 |
unsigned long long &value) const throw() |
---|
210 |
{ return(lookupValue(name.c_str(), value)); } |
---|
211 |
|
---|
212 |
inline bool lookupValue(const std::string &name, double &value) const |
---|
213 |
throw() |
---|
214 |
{ return(lookupValue(name.c_str(), value)); } |
---|
215 |
|
---|
216 |
inline bool lookupValue(const std::string &name, float &value) const |
---|
217 |
throw() |
---|
218 |
{ return(lookupValue(name.c_str(), value)); } |
---|
219 |
|
---|
220 |
inline bool lookupValue(const std::string &name, const char *&value) const |
---|
221 |
throw() |
---|
222 |
{ return(lookupValue(name.c_str(), value)); } |
---|
223 |
|
---|
224 |
inline bool lookupValue(const std::string &name, std::string &value) const |
---|
225 |
throw() |
---|
226 |
{ return(lookupValue(name.c_str(), value)); } |
---|
227 |
|
---|
228 |
void remove(const char *name) |
---|
229 |
throw(SettingTypeException, SettingNotFoundException); |
---|
230 |
|
---|
231 |
inline void remove(const std::string & name) |
---|
232 |
throw(SettingTypeException, SettingNotFoundException) |
---|
233 |
{ remove(name.c_str()); } |
---|
234 |
|
---|
235 |
void remove(unsigned int idx) |
---|
236 |
throw(SettingTypeException, SettingNotFoundException); |
---|
237 |
|
---|
238 |
inline Setting & add(const std::string & name, Type type) |
---|
239 |
throw(SettingNameException, SettingTypeException) |
---|
240 |
{ return(add(name.c_str(), type)); } |
---|
241 |
|
---|
242 |
Setting & add(const char *name, Type type) |
---|
243 |
throw(SettingNameException, SettingTypeException); |
---|
244 |
|
---|
245 |
Setting & add(Type type) |
---|
246 |
throw(SettingTypeException); |
---|
247 |
|
---|
248 |
inline bool exists(const std::string & name) const throw() |
---|
249 |
{ return(exists(name.c_str())); } |
---|
250 |
|
---|
251 |
bool exists(const char *name) const throw(); |
---|
252 |
|
---|
253 |
int getLength() const throw(); |
---|
254 |
const char *getName() const throw(); |
---|
255 |
std::string getPath() const; |
---|
256 |
int getIndex() const throw(); |
---|
257 |
|
---|
258 |
const Setting & getParent() const throw(SettingNotFoundException); |
---|
259 |
Setting & getParent() throw(SettingNotFoundException); |
---|
260 |
|
---|
261 |
bool isRoot() const throw(); |
---|
262 |
|
---|
263 |
inline bool isGroup() const throw() |
---|
264 |
{ return(_type == TypeGroup); } |
---|
265 |
|
---|
266 |
inline bool isArray() const throw() |
---|
267 |
{ return(_type == TypeArray); } |
---|
268 |
|
---|
269 |
inline bool isList() const throw() |
---|
270 |
{ return(_type == TypeList); } |
---|
271 |
|
---|
272 |
inline bool isAggregate() const throw() |
---|
273 |
{ return(_type >= TypeGroup); } |
---|
274 |
|
---|
275 |
inline bool isScalar() const throw() |
---|
276 |
{ return((_type > TypeNone) && (_type < TypeGroup)); } |
---|
277 |
|
---|
278 |
inline bool isNumber() const throw() |
---|
279 |
{ return((_type == TypeInt) || (_type == TypeInt64) |
---|
280 |
|| (_type == TypeFloat)); } |
---|
281 |
|
---|
282 |
inline unsigned int getSourceLine() const throw() |
---|
283 |
{ return(config_setting_source_line(_setting)); } |
---|
284 |
}; |
---|
285 |
|
---|
286 |
class LIBCONFIG_API Config |
---|
287 |
{ |
---|
288 |
private: |
---|
289 |
|
---|
290 |
config_t _config; |
---|
291 |
|
---|
292 |
static void ConfigDestructor(void *arg); |
---|
293 |
Config(const Config& other); // not supported |
---|
294 |
Config& operator=(const Config& other); // not supported |
---|
295 |
|
---|
296 |
public: |
---|
297 |
|
---|
298 |
Config(); |
---|
299 |
virtual ~Config(); |
---|
300 |
|
---|
301 |
void setAutoConvert(bool flag); |
---|
302 |
bool getAutoConvert() const; |
---|
303 |
|
---|
304 |
void read(FILE *stream) throw(ParseException); |
---|
305 |
void write(FILE *stream) const; |
---|
306 |
|
---|
307 |
void readFile(const char *filename) throw(FileIOException, ParseException); |
---|
308 |
void writeFile(const char *filename) throw(FileIOException); |
---|
309 |
|
---|
310 |
inline Setting & lookup(const std::string &path) const |
---|
311 |
throw(SettingNotFoundException) |
---|
312 |
{ return(lookup(path.c_str())); } |
---|
313 |
|
---|
314 |
Setting & lookup(const char *path) const |
---|
315 |
throw(SettingNotFoundException); |
---|
316 |
|
---|
317 |
inline bool exists(const std::string & path) const throw() |
---|
318 |
{ return(exists(path.c_str())); } |
---|
319 |
|
---|
320 |
bool exists(const char *path) const throw(); |
---|
321 |
|
---|
322 |
bool lookupValue(const char *path, bool &value) const throw(); |
---|
323 |
bool lookupValue(const char *path, long &value) const throw(); |
---|
324 |
bool lookupValue(const char *path, unsigned long &value) const throw(); |
---|
325 |
bool lookupValue(const char *path, int &value) const throw(); |
---|
326 |
bool lookupValue(const char *path, unsigned int &value) const throw(); |
---|
327 |
bool lookupValue(const char *path, long long &value) const throw(); |
---|
328 |
bool lookupValue(const char *path, unsigned long long &value) |
---|
329 |
const throw(); |
---|
330 |
bool lookupValue(const char *path, double &value) const throw(); |
---|
331 |
bool lookupValue(const char *path, float &value) const throw(); |
---|
332 |
bool lookupValue(const char *path, const char *&value) const throw(); |
---|
333 |
bool lookupValue(const char *path, std::string &value) const throw(); |
---|
334 |
|
---|
335 |
inline bool lookupValue(const std::string &path, bool &value) |
---|
336 |
const throw() |
---|
337 |
{ return(lookupValue(path.c_str(), value)); } |
---|
338 |
|
---|
339 |
inline bool lookupValue(const std::string &path, long &value) |
---|
340 |
const throw() |
---|
341 |
{ return(lookupValue(path.c_str(), value)); } |
---|
342 |
|
---|
343 |
inline bool lookupValue(const std::string &path, unsigned long &value) |
---|
344 |
const throw() |
---|
345 |
{ return(lookupValue(path.c_str(), value)); } |
---|
346 |
|
---|
347 |
inline bool lookupValue(const std::string &path, int &value) const throw() |
---|
348 |
{ return(lookupValue(path.c_str(), value)); } |
---|
349 |
|
---|
350 |
inline bool lookupValue(const std::string &path, unsigned int &value) |
---|
351 |
const throw() |
---|
352 |
{ return(lookupValue(path.c_str(), value)); } |
---|
353 |
|
---|
354 |
inline bool lookupValue(const std::string &path, long long &value) |
---|
355 |
const throw() |
---|
356 |
{ return(lookupValue(path.c_str(), value)); } |
---|
357 |
|
---|
358 |
inline bool lookupValue(const std::string &path, |
---|
359 |
unsigned long long &value) const throw() |
---|
360 |
{ return(lookupValue(path.c_str(), value)); } |
---|
361 |
|
---|
362 |
inline bool lookupValue(const std::string &path, double &value) const |
---|
363 |
throw() |
---|
364 |
{ return(lookupValue(path.c_str(), value)); } |
---|
365 |
|
---|
366 |
inline bool lookupValue(const std::string &path, float &value) const |
---|
367 |
throw() |
---|
368 |
{ return(lookupValue(path.c_str(), value)); } |
---|
369 |
|
---|
370 |
inline bool lookupValue(const std::string &path, const char *&value) const |
---|
371 |
throw() |
---|
372 |
{ return(lookupValue(path.c_str(), value)); } |
---|
373 |
|
---|
374 |
inline bool lookupValue(const std::string &path, std::string &value) const |
---|
375 |
throw() |
---|
376 |
{ return(lookupValue(path.c_str(), value)); } |
---|
377 |
|
---|
378 |
Setting & getRoot() const; |
---|
379 |
}; |
---|
380 |
|
---|
381 |
} // namespace libconfig |
---|
382 |
|
---|
383 |
#endif // __libconfig_hpp |
---|