1 /* ----------------------------------------------------------------------------
2 libconfig - A library for processing structured configuration files
3 Copyright (C) 2005-2014 Mark A Lindner
5 This file is part of libconfig.
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.
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.
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 ----------------------------------------------------------------------------
23 #ifndef __libconfig_hpp
24 #define __libconfig_hpp
30 #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
31 #if defined(LIBCONFIGXX_STATIC)
32 #define LIBCONFIGXX_API
33 #elif defined(LIBCONFIGXX_EXPORTS)
34 #define LIBCONFIGXX_API __declspec(dllexport)
35 #else /* ! LIBCONFIGXX_EXPORTS */
36 #define LIBCONFIGXX_API __declspec(dllimport)
37 #endif /* LIBCONFIGXX_STATIC */
39 #define LIBCONFIGXX_API
42 #define LIBCONFIGXX_VER_MAJOR 1
43 #define LIBCONFIGXX_VER_MINOR 5
44 #define LIBCONFIGXX_VER_REVISION 0
46 struct config_t; // fwd decl
47 struct config_setting_t; // fwd decl
51 class LIBCONFIGXX_API ConfigException : public std::exception { };
53 class Setting; // fwd decl
54 class SettingIterator;
55 class SettingConstIterator;
57 class LIBCONFIGXX_API SettingException : public ConfigException
61 SettingException(const Setting &setting);
62 SettingException(const Setting &setting, int idx);
63 SettingException(const Setting &setting, const char *name);
64 SettingException(const char *path);
66 SettingException(const SettingException &other);
67 SettingException& operator=(const SettingException &other);
69 virtual ~SettingException() throw();
71 const char *getPath() const;
73 virtual const char *what() const throw();
80 class LIBCONFIGXX_API SettingTypeException : public SettingException
84 SettingTypeException(const Setting &setting);
85 SettingTypeException(const Setting &setting, int idx);
86 SettingTypeException(const Setting &setting, const char *name);
88 virtual const char *what() const throw();
91 class LIBCONFIGXX_API SettingNotFoundException : public SettingException
95 SettingNotFoundException(const char *path);
96 SettingNotFoundException(const Setting &setting, int idx);
97 SettingNotFoundException(const Setting &setting, const char *name);
99 virtual const char *what() const throw();
102 class LIBCONFIGXX_API SettingNameException : public SettingException
106 SettingNameException(const Setting &setting, const char *name);
108 virtual const char *what() const throw();
111 class LIBCONFIGXX_API FileIOException : public ConfigException
115 virtual const char *what() const throw();
118 class LIBCONFIGXX_API ParseException : public ConfigException
122 ParseException(const char *file, int line, const char *error);
124 ParseException(const ParseException &other);
126 virtual ~ParseException() throw();
128 inline const char *getFile() const
131 inline int getLine() const
134 inline const char *getError() const
137 virtual const char *what() const throw();
146 class LIBCONFIGXX_API Setting
173 typedef SettingIterator iterator;
174 typedef SettingConstIterator const_iterator;
180 inline Type getType() const { return(_type); }
182 inline Format getFormat() const { return(_format); }
183 void setFormat(Format format);
185 operator bool() const;
186 operator int() const;
187 operator unsigned int() const;
188 operator long() const;
189 operator unsigned long() const;
190 operator long long() const;
191 operator unsigned long long() const;
192 operator double() const;
193 operator float() const;
194 operator const char *() const;
195 operator std::string() const;
197 inline const char *c_str() const
198 { return operator const char *(); }
200 Setting & operator=(bool value);
201 Setting & operator=(int value);
202 Setting & operator=(long value);
203 Setting & operator=(const long long &value);
204 Setting & operator=(const double &value);
205 Setting & operator=(float value);
206 Setting & operator=(const char *value);
207 Setting & operator=(const std::string &value);
209 Setting & lookup(const char *path) const;
210 inline Setting & lookup(const std::string &path) const
211 { return(lookup(path.c_str())); }
213 Setting & operator[](const char *name) const;
214 Setting & operator[](int index) const;
216 bool lookupValue(const char *name, bool &value) const;
217 bool lookupValue(const char *name, int &value) const;
218 bool lookupValue(const char *name, unsigned int &value) const;
219 bool lookupValue(const char *name, long long &value) const;
220 bool lookupValue(const char *name, unsigned long long &value) const;
221 bool lookupValue(const char *name, double &value) const;
222 bool lookupValue(const char *name, float &value) const;
223 bool lookupValue(const char *name, const char *&value) const;
224 bool lookupValue(const char *name, std::string &value) const;
226 inline bool lookupValue(const std::string &name, bool &value) const
227 { return(lookupValue(name.c_str(), value)); }
229 inline bool lookupValue(const std::string &name, int &value) const
230 { return(lookupValue(name.c_str(), value)); }
232 inline bool lookupValue(const std::string &name, unsigned int &value) const
233 { return(lookupValue(name.c_str(), value)); }
235 inline bool lookupValue(const std::string &name, long long &value) const
236 { return(lookupValue(name.c_str(), value)); }
238 inline bool lookupValue(const std::string &name,
239 unsigned long long &value) const
240 { return(lookupValue(name.c_str(), value)); }
242 inline bool lookupValue(const std::string &name, double &value) const
243 { return(lookupValue(name.c_str(), value)); }
245 inline bool lookupValue(const std::string &name, float &value) const
246 { return(lookupValue(name.c_str(), value)); }
248 inline bool lookupValue(const std::string &name, const char *&value) const
249 { return(lookupValue(name.c_str(), value)); }
251 inline bool lookupValue(const std::string &name, std::string &value) const
252 { return(lookupValue(name.c_str(), value)); }
254 void remove(const char *name);
256 inline void remove(const std::string &name)
257 { remove(name.c_str()); }
259 void remove(unsigned int idx);
261 Setting & add(const char *name, Type type);
263 inline Setting & add(const std::string &name, Type type)
264 { return(add(name.c_str(), type)); }
266 Setting & add(Type type);
268 bool exists(const char *name) const;
270 inline bool exists(const std::string &name) const
271 { return(exists(name.c_str())); }
273 int getLength() const;
274 const char *getName() const;
275 std::string getPath() const;
276 int getIndex() const;
278 const Setting & getParent() const;
279 Setting & getParent();
283 inline bool isGroup() const
284 { return(_type == TypeGroup); }
286 inline bool isArray() const
287 { return(_type == TypeArray); }
289 inline bool isList() const
290 { return(_type == TypeList); }
292 inline bool isAggregate() const
293 { return(_type >= TypeGroup); }
295 inline bool isScalar() const
296 { return((_type > TypeNone) && (_type < TypeGroup)); }
298 inline bool isNumber() const
300 return((_type == TypeInt) || (_type == TypeInt64) || (_type == TypeFloat));
303 unsigned int getSourceLine() const;
304 const char *getSourceFile() const;
309 const_iterator begin() const;
310 const_iterator end() const;
314 config_setting_t *_setting;
318 Setting(config_setting_t *setting);
320 void assertType(Type type) const;
321 static Setting & wrapSetting(config_setting_t *setting);
323 Setting(const Setting& other); // not supported
324 Setting& operator=(const Setting& other); // not supported
328 class LIBCONFIGXX_API SettingIterator
332 SettingIterator(Setting &setting, bool endIterator = false);
333 SettingIterator(const SettingIterator &other);
334 SettingIterator& operator=(const SettingIterator &other);
336 // Equality comparison.
337 inline bool operator==(SettingIterator const &other) const
338 { return((_setting == other._setting) && (_idx == other._idx)); }
340 inline bool operator!=(SettingIterator const &other) const
341 { return(!operator==(other)); }
343 bool operator<(SettingIterator const &other) const;
345 // Dereference operators.
346 inline Setting & operator*()
347 { return((*_setting)[_idx]); }
349 inline Setting * operator->()
350 { return(&(*_setting)[_idx]); }
352 inline const Setting & operator*() const
353 { return(*_setting)[_idx]; }
354 inline const Setting * operator->() const
355 { return(&(*_setting)[_idx]); }
357 // Increment and decrement operators.
358 SettingIterator & operator++();
359 SettingIterator operator++(int);
361 SettingIterator & operator--();
362 SettingIterator operator--(int);
364 // Arithmetic operators.
365 SettingIterator operator+(int offset) const;
366 SettingIterator & operator+=(int offset);
368 SettingIterator operator-(int offset) const;
369 SettingIterator & operator-=(int offset);
371 int operator-(const SettingIterator &other) const;
381 SettingIterator operator+(int offset, const SettingIterator &si);
383 class LIBCONFIGXX_API SettingConstIterator
387 SettingConstIterator(const Setting &setting, bool endIterator = false);
388 SettingConstIterator(const SettingConstIterator &rhs);
389 SettingConstIterator& operator=(const SettingConstIterator &rhs);
391 // Equality comparison.
392 bool operator==(SettingConstIterator const &other) const
393 { return((_setting == other._setting) && (_idx == other._idx)); }
395 inline bool operator!=(SettingConstIterator const &other) const
396 { return(!operator==(other)); }
398 // Dereference operators.
399 inline Setting const & operator*()
400 { return((*_setting)[_idx]); }
401 inline Setting const * operator->()
402 { return(&(*_setting)[_idx]); }
404 inline const Setting& operator*() const
405 { return((*_setting)[_idx]); }
406 inline const Setting * operator->() const
407 { return(&(*_setting)[_idx]); }
409 // Increment and decrement operators.
410 SettingConstIterator & operator++();
411 SettingConstIterator operator++(int);
413 SettingConstIterator & operator--();
414 SettingConstIterator operator--(int);
416 // Arithmetic operators.
417 SettingConstIterator operator+(int offset) const;
418 SettingConstIterator & operator+=(int offset);
420 SettingConstIterator operator-(int offset) const;
421 SettingConstIterator & operator-=(int offset);
423 int operator-(const SettingConstIterator &other) const;
427 const Setting *_setting;
433 SettingConstIterator operator+(int offset, const SettingConstIterator &si);
435 class LIBCONFIGXX_API Config
442 void setAutoConvert(bool flag);
443 bool getAutoConvert() const;
445 void setDefaultFormat(Setting::Format format);
446 inline Setting::Format getDefaultFormat() const
447 { return(_defaultFormat); }
449 void setTabWidth(unsigned short width);
450 unsigned short getTabWidth() const;
452 void setIncludeDir(const char *includeDir);
453 const char *getIncludeDir() const;
455 void read(FILE *stream);
456 void write(FILE *stream) const;
458 void readString(const char *str);
459 inline void readString(const std::string &str)
460 { return(readString(str.c_str())); }
462 void readFile(const char *filename);
463 void writeFile(const char *filename);
465 Setting & lookup(const char *path) const;
466 inline Setting & lookup(const std::string &path) const
467 { return(lookup(path.c_str())); }
469 bool exists(const char *path) const;
470 inline bool exists(const std::string &path) const
471 { return(exists(path.c_str())); }
473 bool lookupValue(const char *path, bool &value) const;
474 bool lookupValue(const char *path, int &value) const;
475 bool lookupValue(const char *path, unsigned int &value) const;
476 bool lookupValue(const char *path, long long &value) const;
477 bool lookupValue(const char *path, unsigned long long &value) const;
478 bool lookupValue(const char *path, double &value) const;
479 bool lookupValue(const char *path, float &value) const;
480 bool lookupValue(const char *path, const char *&value) const;
481 bool lookupValue(const char *path, std::string &value) const;
483 inline bool lookupValue(const std::string &path, bool &value) const
484 { return(lookupValue(path.c_str(), value)); }
486 inline bool lookupValue(const std::string &path, int &value) const
487 { return(lookupValue(path.c_str(), value)); }
489 inline bool lookupValue(const std::string &path, unsigned int &value) const
490 { return(lookupValue(path.c_str(), value)); }
492 inline bool lookupValue(const std::string &path, long long &value) const
493 { return(lookupValue(path.c_str(), value)); }
495 inline bool lookupValue(const std::string &path,
496 unsigned long long &value) const
497 { return(lookupValue(path.c_str(), value)); }
499 inline bool lookupValue(const std::string &path, double &value) const
500 { return(lookupValue(path.c_str(), value)); }
502 inline bool lookupValue(const std::string &path, float &value) const
503 { return(lookupValue(path.c_str(), value)); }
505 inline bool lookupValue(const std::string &path, const char *&value) const
506 { return(lookupValue(path.c_str(), value)); }
508 inline bool lookupValue(const std::string &path, std::string &value) const
509 { return(lookupValue(path.c_str(), value)); }
511 Setting & getRoot() const;
515 static void ConfigDestructor(void *arg);
516 void handleError() const;
519 Setting::Format _defaultFormat;
521 Config(const Config& other); // not supported
522 Config& operator=(const Config& other); // not supported
525 } // namespace libconfig
527 #endif // __libconfig_hpp