1 | /* ---------------------------------------------------------------------------- |
---|
2 | libconfig - A library for processing structured configuration files |
---|
3 | Copyright (C) 2005-2009 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 | |
---|
31 | #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) |
---|
32 | #if defined(LIBCONFIGXX_STATIC) |
---|
33 | #define LIBCONFIGXX_API |
---|
34 | #elif defined(LIBCONFIGXX_EXPORTS) |
---|
35 | #define LIBCONFIGXX_API __declspec(dllexport) |
---|
36 | #else /* ! LIBCONFIGXX_EXPORTS */ |
---|
37 | #define LIBCONFIGXX_API __declspec(dllimport) |
---|
38 | #endif /* LIBCONFIGXX_STATIC */ |
---|
39 | #else /* ! WIN32 */ |
---|
40 | #define LIBCONFIGXX_API |
---|
41 | #endif /* WIN32 */ |
---|
42 | |
---|
43 | |
---|
44 | #define LIBCONFIGXX_VER_MAJOR 1 |
---|
45 | #define LIBCONFIGXX_VER_MINOR 4 |
---|
46 | #define LIBCONFIGXX_VER_REVISION 0 |
---|
47 | |
---|
48 | struct config_t; // fwd decl |
---|
49 | struct config_setting_t; // fwd decl |
---|
50 | |
---|
51 | namespace libconfig { |
---|
52 | |
---|
53 | class LIBCONFIGXX_API ConfigException : public std::exception { }; |
---|
54 | |
---|
55 | class Setting; // fwd decl |
---|
56 | |
---|
57 | class LIBCONFIGXX_API SettingException : public ConfigException |
---|
58 | { |
---|
59 | friend class Config; |
---|
60 | |
---|
61 | public: |
---|
62 | |
---|
63 | SettingException(const SettingException &other); |
---|
64 | SettingException& operator=(const SettingException &other); |
---|
65 | |
---|
66 | virtual ~SettingException() throw(); |
---|
67 | |
---|
68 | const char *getPath() const; |
---|
69 | |
---|
70 | virtual const char *what() const throw(); |
---|
71 | |
---|
72 | protected: |
---|
73 | |
---|
74 | SettingException(const Setting &setting); |
---|
75 | SettingException(const Setting &setting, int idx); |
---|
76 | SettingException(const Setting &setting, const char *name); |
---|
77 | SettingException(const char *path); |
---|
78 | |
---|
79 | private: |
---|
80 | |
---|
81 | char *_path; |
---|
82 | }; |
---|
83 | |
---|
84 | class LIBCONFIGXX_API SettingTypeException : public SettingException |
---|
85 | { |
---|
86 | friend class Config; |
---|
87 | friend class Setting; |
---|
88 | |
---|
89 | public: |
---|
90 | |
---|
91 | const char *what() const throw(); |
---|
92 | |
---|
93 | private: |
---|
94 | |
---|
95 | SettingTypeException(const Setting &setting); |
---|
96 | SettingTypeException(const Setting &setting, int idx); |
---|
97 | SettingTypeException(const Setting &setting, const char *name); |
---|
98 | }; |
---|
99 | |
---|
100 | class LIBCONFIGXX_API SettingNotFoundException : public SettingException |
---|
101 | { |
---|
102 | friend class Config; |
---|
103 | friend class Setting; |
---|
104 | |
---|
105 | public: |
---|
106 | |
---|
107 | const char *what() const throw(); |
---|
108 | |
---|
109 | private: |
---|
110 | |
---|
111 | SettingNotFoundException(const Setting &setting, int idx); |
---|
112 | SettingNotFoundException(const Setting &setting, const char *name); |
---|
113 | SettingNotFoundException(const char *path); |
---|
114 | }; |
---|
115 | |
---|
116 | class LIBCONFIGXX_API SettingNameException : public SettingException |
---|
117 | { |
---|
118 | friend class Config; |
---|
119 | friend class Setting; |
---|
120 | |
---|
121 | public: |
---|
122 | |
---|
123 | const char *what() const throw(); |
---|
124 | |
---|
125 | private: |
---|
126 | |
---|
127 | SettingNameException(const Setting &setting, const char *name); |
---|
128 | }; |
---|
129 | |
---|
130 | class LIBCONFIGXX_API FileIOException : public ConfigException |
---|
131 | { |
---|
132 | public: |
---|
133 | |
---|
134 | const char *what() const throw(); |
---|
135 | }; |
---|
136 | |
---|
137 | class LIBCONFIGXX_API ParseException : public ConfigException |
---|
138 | { |
---|
139 | friend class Config; |
---|
140 | |
---|
141 | public: |
---|
142 | |
---|
143 | virtual ~ParseException() throw(); |
---|
144 | |
---|
145 | inline const char *getFile() const throw() |
---|
146 | { return(_file); } |
---|
147 | |
---|
148 | inline int getLine() const throw() |
---|
149 | { return(_line); } |
---|
150 | |
---|
151 | inline const char *getError() const throw() |
---|
152 | { return(_error); } |
---|
153 | |
---|
154 | const char *what() const throw(); |
---|
155 | |
---|
156 | private: |
---|
157 | |
---|
158 | ParseException(const char *file, int line, const char *error); |
---|
159 | |
---|
160 | const char *_file; |
---|
161 | int _line; |
---|
162 | const char *_error; |
---|
163 | }; |
---|
164 | |
---|
165 | class LIBCONFIGXX_API Setting |
---|
166 | { |
---|
167 | friend class Config; |
---|
168 | |
---|
169 | public: |
---|
170 | |
---|
171 | enum Type |
---|
172 | { |
---|
173 | TypeNone = 0, |
---|
174 | // scalar types |
---|
175 | TypeInt, |
---|
176 | TypeInt64, |
---|
177 | TypeFloat, |
---|
178 | TypeString, |
---|
179 | TypeBoolean, |
---|
180 | // aggregate types |
---|
181 | TypeGroup, |
---|
182 | TypeArray, |
---|
183 | TypeList |
---|
184 | }; |
---|
185 | |
---|
186 | enum Format |
---|
187 | { |
---|
188 | FormatDefault = 0, |
---|
189 | FormatHex = 1 |
---|
190 | }; |
---|
191 | |
---|
192 | private: |
---|
193 | |
---|
194 | config_setting_t *_setting; |
---|
195 | Type _type; |
---|
196 | Format _format; |
---|
197 | |
---|
198 | Setting(config_setting_t *setting); |
---|
199 | |
---|
200 | void assertType(Type type) const |
---|
201 | throw(SettingTypeException); |
---|
202 | static Setting & wrapSetting(config_setting_t *setting); |
---|
203 | |
---|
204 | Setting(const Setting& other); // not supported |
---|
205 | Setting& operator=(const Setting& other); // not supported |
---|
206 | |
---|
207 | public: |
---|
208 | |
---|
209 | virtual ~Setting() throw(); |
---|
210 | |
---|
211 | inline Type getType() const throw() { return(_type); } |
---|
212 | |
---|
213 | inline Format getFormat() const throw() { return(_format); } |
---|
214 | void setFormat(Format format) throw(); |
---|
215 | |
---|
216 | operator bool() const throw(SettingTypeException); |
---|
217 | operator int() const throw(SettingTypeException); |
---|
218 | operator unsigned int() const throw(SettingTypeException); |
---|
219 | operator long() const throw(SettingTypeException); |
---|
220 | operator unsigned long() const throw(SettingTypeException); |
---|
221 | operator long long() const throw(SettingTypeException); |
---|
222 | operator unsigned long long() const throw(SettingTypeException); |
---|
223 | operator double() const throw(SettingTypeException); |
---|
224 | operator float() const throw(SettingTypeException); |
---|
225 | operator const char *() const throw(SettingTypeException); |
---|
226 | operator std::string() const throw(SettingTypeException); |
---|
227 | |
---|
228 | Setting & operator=(bool value) throw(SettingTypeException); |
---|
229 | Setting & operator=(int value) throw(SettingTypeException); |
---|
230 | Setting & operator=(long value) throw(SettingTypeException); |
---|
231 | Setting & operator=(const long long &value) throw(SettingTypeException); |
---|
232 | Setting & operator=(const double &value) throw(SettingTypeException); |
---|
233 | Setting & operator=(float value) throw(SettingTypeException); |
---|
234 | Setting & operator=(const char *value) throw(SettingTypeException); |
---|
235 | Setting & operator=(const std::string &value) throw(SettingTypeException); |
---|
236 | |
---|
237 | Setting & operator[](const char * key) const |
---|
238 | throw(SettingTypeException, SettingNotFoundException); |
---|
239 | |
---|
240 | inline Setting & operator[](const std::string & key) const |
---|
241 | throw(SettingTypeException, SettingNotFoundException) |
---|
242 | { return(operator[](key.c_str())); } |
---|
243 | |
---|
244 | Setting & operator[](int index) const |
---|
245 | throw(SettingTypeException, SettingNotFoundException); |
---|
246 | |
---|
247 | bool lookupValue(const char *name, bool &value) const throw(); |
---|
248 | bool lookupValue(const char *name, int &value) const throw(); |
---|
249 | bool lookupValue(const char *name, unsigned int &value) const throw(); |
---|
250 | bool lookupValue(const char *name, long long &value) const throw(); |
---|
251 | bool lookupValue(const char *name, unsigned long long &value) |
---|
252 | const throw(); |
---|
253 | bool lookupValue(const char *name, double &value) const throw(); |
---|
254 | bool lookupValue(const char *name, float &value) const throw(); |
---|
255 | bool lookupValue(const char *name, const char *&value) const throw(); |
---|
256 | bool lookupValue(const char *name, std::string &value) const throw(); |
---|
257 | |
---|
258 | inline bool lookupValue(const std::string &name, bool &value) |
---|
259 | const throw() |
---|
260 | { return(lookupValue(name.c_str(), value)); } |
---|
261 | |
---|
262 | inline bool lookupValue(const std::string &name, int &value) |
---|
263 | const throw() |
---|
264 | { return(lookupValue(name.c_str(), value)); } |
---|
265 | |
---|
266 | inline bool lookupValue(const std::string &name, unsigned int &value) |
---|
267 | const throw() |
---|
268 | { return(lookupValue(name.c_str(), value)); } |
---|
269 | |
---|
270 | inline bool lookupValue(const std::string &name, long long &value) |
---|
271 | const throw() |
---|
272 | { return(lookupValue(name.c_str(), value)); } |
---|
273 | |
---|
274 | inline bool lookupValue(const std::string &name, |
---|
275 | unsigned long long &value) const throw() |
---|
276 | { return(lookupValue(name.c_str(), value)); } |
---|
277 | |
---|
278 | inline bool lookupValue(const std::string &name, double &value) const |
---|
279 | throw() |
---|
280 | { return(lookupValue(name.c_str(), value)); } |
---|
281 | |
---|
282 | inline bool lookupValue(const std::string &name, float &value) const |
---|
283 | throw() |
---|
284 | { return(lookupValue(name.c_str(), value)); } |
---|
285 | |
---|
286 | inline bool lookupValue(const std::string &name, const char *&value) const |
---|
287 | throw() |
---|
288 | { return(lookupValue(name.c_str(), value)); } |
---|
289 | |
---|
290 | inline bool lookupValue(const std::string &name, std::string &value) const |
---|
291 | throw() |
---|
292 | { return(lookupValue(name.c_str(), value)); } |
---|
293 | |
---|
294 | void remove(const char *name) |
---|
295 | throw(SettingTypeException, SettingNotFoundException); |
---|
296 | |
---|
297 | inline void remove(const std::string & name) |
---|
298 | throw(SettingTypeException, SettingNotFoundException) |
---|
299 | { remove(name.c_str()); } |
---|
300 | |
---|
301 | void remove(unsigned int idx) |
---|
302 | throw(SettingTypeException, SettingNotFoundException); |
---|
303 | |
---|
304 | inline Setting & add(const std::string & name, Type type) |
---|
305 | throw(SettingNameException, SettingTypeException) |
---|
306 | { return(add(name.c_str(), type)); } |
---|
307 | |
---|
308 | Setting & add(const char *name, Type type) |
---|
309 | throw(SettingNameException, SettingTypeException); |
---|
310 | |
---|
311 | Setting & add(Type type) throw(SettingTypeException); |
---|
312 | |
---|
313 | inline bool exists(const std::string & name) const throw() |
---|
314 | { return(exists(name.c_str())); } |
---|
315 | |
---|
316 | bool exists(const char *name) const throw(); |
---|
317 | |
---|
318 | int getLength() const throw(); |
---|
319 | const char *getName() const throw(); |
---|
320 | std::string getPath() const; |
---|
321 | int getIndex() const throw(); |
---|
322 | |
---|
323 | const Setting & getParent() const throw(SettingNotFoundException); |
---|
324 | Setting & getParent() throw(SettingNotFoundException); |
---|
325 | |
---|
326 | bool isRoot() const throw(); |
---|
327 | |
---|
328 | inline bool isGroup() const throw() |
---|
329 | { return(_type == TypeGroup); } |
---|
330 | |
---|
331 | inline bool isArray() const throw() |
---|
332 | { return(_type == TypeArray); } |
---|
333 | |
---|
334 | inline bool isList() const throw() |
---|
335 | { return(_type == TypeList); } |
---|
336 | |
---|
337 | inline bool isAggregate() const throw() |
---|
338 | { return(_type >= TypeGroup); } |
---|
339 | |
---|
340 | inline bool isScalar() const throw() |
---|
341 | { return((_type > TypeNone) && (_type < TypeGroup)); } |
---|
342 | |
---|
343 | inline bool isNumber() const throw() |
---|
344 | { return((_type == TypeInt) || (_type == TypeInt64) |
---|
345 | || (_type == TypeFloat)); } |
---|
346 | |
---|
347 | unsigned int getSourceLine() const throw(); |
---|
348 | const char *getSourceFile() const throw(); |
---|
349 | }; |
---|
350 | |
---|
351 | class LIBCONFIGXX_API Config |
---|
352 | { |
---|
353 | private: |
---|
354 | |
---|
355 | config_t *_config; |
---|
356 | |
---|
357 | static void ConfigDestructor(void *arg); |
---|
358 | Config(const Config& other); // not supported |
---|
359 | Config& operator=(const Config& other); // not supported |
---|
360 | |
---|
361 | public: |
---|
362 | |
---|
363 | Config(); |
---|
364 | virtual ~Config(); |
---|
365 | |
---|
366 | void setAutoConvert(bool flag); |
---|
367 | bool getAutoConvert() const; |
---|
368 | |
---|
369 | void setTabWidth(unsigned short width) throw(); |
---|
370 | unsigned short getTabWidth() const throw(); |
---|
371 | |
---|
372 | void read(FILE *stream) throw(ParseException); |
---|
373 | void write(FILE *stream) const; |
---|
374 | |
---|
375 | void readString(const char *str) throw(ParseException); |
---|
376 | inline void readString(const std::string &str) throw(ParseException) |
---|
377 | { return(readString(str.c_str())); } |
---|
378 | |
---|
379 | void readFile(const char *filename) throw(FileIOException, ParseException); |
---|
380 | void writeFile(const char *filename) throw(FileIOException); |
---|
381 | |
---|
382 | inline Setting & lookup(const std::string &path) const |
---|
383 | throw(SettingNotFoundException) |
---|
384 | { return(lookup(path.c_str())); } |
---|
385 | |
---|
386 | Setting & lookup(const char *path) const throw(SettingNotFoundException); |
---|
387 | |
---|
388 | inline bool exists(const std::string & path) const throw() |
---|
389 | { return(exists(path.c_str())); } |
---|
390 | |
---|
391 | bool exists(const char *path) const throw(); |
---|
392 | |
---|
393 | bool lookupValue(const char *path, bool &value) const throw(); |
---|
394 | bool lookupValue(const char *path, int &value) const throw(); |
---|
395 | bool lookupValue(const char *path, unsigned int &value) const throw(); |
---|
396 | bool lookupValue(const char *path, long long &value) const throw(); |
---|
397 | bool lookupValue(const char *path, unsigned long long &value) |
---|
398 | const throw(); |
---|
399 | bool lookupValue(const char *path, double &value) const throw(); |
---|
400 | bool lookupValue(const char *path, float &value) const throw(); |
---|
401 | bool lookupValue(const char *path, const char *&value) const throw(); |
---|
402 | bool lookupValue(const char *path, std::string &value) const throw(); |
---|
403 | |
---|
404 | inline bool lookupValue(const std::string &path, bool &value) const throw() |
---|
405 | { return(lookupValue(path.c_str(), value)); } |
---|
406 | |
---|
407 | inline bool lookupValue(const std::string &path, int &value) const throw() |
---|
408 | { return(lookupValue(path.c_str(), value)); } |
---|
409 | |
---|
410 | inline bool lookupValue(const std::string &path, unsigned int &value) |
---|
411 | const throw() |
---|
412 | { return(lookupValue(path.c_str(), value)); } |
---|
413 | |
---|
414 | inline bool lookupValue(const std::string &path, long long &value) |
---|
415 | const throw() |
---|
416 | { return(lookupValue(path.c_str(), value)); } |
---|
417 | |
---|
418 | inline bool lookupValue(const std::string &path, |
---|
419 | unsigned long long &value) const throw() |
---|
420 | { return(lookupValue(path.c_str(), value)); } |
---|
421 | |
---|
422 | inline bool lookupValue(const std::string &path, double &value) |
---|
423 | const throw() |
---|
424 | { return(lookupValue(path.c_str(), value)); } |
---|
425 | |
---|
426 | inline bool lookupValue(const std::string &path, float &value) |
---|
427 | const throw() |
---|
428 | { return(lookupValue(path.c_str(), value)); } |
---|
429 | |
---|
430 | inline bool lookupValue(const std::string &path, const char *&value) |
---|
431 | const throw() |
---|
432 | { return(lookupValue(path.c_str(), value)); } |
---|
433 | |
---|
434 | inline bool lookupValue(const std::string &path, std::string &value) |
---|
435 | const throw() |
---|
436 | { return(lookupValue(path.c_str(), value)); } |
---|
437 | |
---|
438 | Setting & getRoot() const; |
---|
439 | |
---|
440 | private: |
---|
441 | |
---|
442 | void handleError() const; |
---|
443 | }; |
---|
444 | |
---|
445 | }; // namespace libconfig |
---|
446 | |
---|
447 | #endif // __libconfig_hpp |
---|