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
176 OptionAutoConvert = 0x01,
177 OptionSemicolonSeparators = 0x02,
178 OptionColonAssignmentForGroups = 0x04,
179 OptionColonAssignmentForNonGroups = 0x08,
180 OptionOpenBraceOnSeparateLine = 0x10
183 typedef SettingIterator iterator;
184 typedef SettingConstIterator const_iterator;
190 inline Type getType() const { return(_type); }
192 inline Format getFormat() const { return(_format); }
193 void setFormat(Format format);
195 operator bool() const;
196 operator int() const;
197 operator unsigned int() const;
198 operator long() const;
199 operator unsigned long() const;
200 operator long long() const;
201 operator unsigned long long() const;
202 operator double() const;
203 operator float() const;
204 operator const char *() const;
205 operator std::string() const;
207 inline const char *c_str() const
208 { return operator const char *(); }
210 Setting & operator=(bool value);
211 Setting & operator=(int value);
212 Setting & operator=(long value);
213 Setting & operator=(const long long &value);
214 Setting & operator=(const double &value);
215 Setting & operator=(float value);
216 Setting & operator=(const char *value);
217 Setting & operator=(const std::string &value);
219 Setting & lookup(const char *path) const;
220 inline Setting & lookup(const std::string &path) const
221 { return(lookup(path.c_str())); }
223 Setting & operator[](const char *name) const;
225 inline Setting & operator[](const std::string &name) const
226 { return(operator[](name.c_str())); }
228 Setting & operator[](int index) const;
230 bool lookupValue(const char *name, bool &value) const;
231 bool lookupValue(const char *name, int &value) const;
232 bool lookupValue(const char *name, unsigned int &value) const;
233 bool lookupValue(const char *name, long long &value) const;
234 bool lookupValue(const char *name, unsigned long long &value) const;
235 bool lookupValue(const char *name, double &value) const;
236 bool lookupValue(const char *name, float &value) const;
237 bool lookupValue(const char *name, const char *&value) const;
238 bool lookupValue(const char *name, std::string &value) const;
240 inline bool lookupValue(const std::string &name, bool &value) const
241 { return(lookupValue(name.c_str(), value)); }
243 inline bool lookupValue(const std::string &name, int &value) const
244 { return(lookupValue(name.c_str(), value)); }
246 inline bool lookupValue(const std::string &name, unsigned int &value) const
247 { return(lookupValue(name.c_str(), value)); }
249 inline bool lookupValue(const std::string &name, long long &value) const
250 { return(lookupValue(name.c_str(), value)); }
252 inline bool lookupValue(const std::string &name,
253 unsigned long long &value) const
254 { return(lookupValue(name.c_str(), value)); }
256 inline bool lookupValue(const std::string &name, double &value) const
257 { return(lookupValue(name.c_str(), value)); }
259 inline bool lookupValue(const std::string &name, float &value) const
260 { return(lookupValue(name.c_str(), value)); }
262 inline bool lookupValue(const std::string &name, const char *&value) const
263 { return(lookupValue(name.c_str(), value)); }
265 inline bool lookupValue(const std::string &name, std::string &value) const
266 { return(lookupValue(name.c_str(), value)); }
268 void remove(const char *name);
270 inline void remove(const std::string &name)
271 { remove(name.c_str()); }
273 void remove(unsigned int idx);
275 Setting & add(const char *name, Type type);
277 inline Setting & add(const std::string &name, Type type)
278 { return(add(name.c_str(), type)); }
280 Setting & add(Type type);
282 bool exists(const char *name) const;
284 inline bool exists(const std::string &name) const
285 { return(exists(name.c_str())); }
287 int getLength() const;
288 const char *getName() const;
289 std::string getPath() const;
290 int getIndex() const;
292 const Setting & getParent() const;
293 Setting & getParent();
297 inline bool isGroup() const
298 { return(_type == TypeGroup); }
300 inline bool isArray() const
301 { return(_type == TypeArray); }
303 inline bool isList() const
304 { return(_type == TypeList); }
306 inline bool isAggregate() const
307 { return(_type >= TypeGroup); }
309 inline bool isScalar() const
310 { return((_type > TypeNone) && (_type < TypeGroup)); }
312 inline bool isNumber() const
314 return((_type == TypeInt) || (_type == TypeInt64) || (_type == TypeFloat));
317 unsigned int getSourceLine() const;
318 const char *getSourceFile() const;
323 const_iterator begin() const;
324 const_iterator end() const;
328 config_setting_t *_setting;
332 Setting(config_setting_t *setting);
334 void assertType(Type type) const;
335 static Setting & wrapSetting(config_setting_t *setting);
337 Setting(const Setting& other); // not supported
338 Setting& operator=(const Setting& other); // not supported
342 class LIBCONFIGXX_API SettingIterator
346 SettingIterator(Setting &setting, bool endIterator = false);
347 SettingIterator(const SettingIterator &other);
348 SettingIterator& operator=(const SettingIterator &other);
350 // Equality comparison.
351 inline bool operator==(SettingIterator const &other) const
352 { return((_setting == other._setting) && (_idx == other._idx)); }
354 inline bool operator!=(SettingIterator const &other) const
355 { return(!operator==(other)); }
357 bool operator<(SettingIterator const &other) const;
359 // Dereference operators.
360 inline Setting & operator*()
361 { return((*_setting)[_idx]); }
363 inline Setting * operator->()
364 { return(&(*_setting)[_idx]); }
366 inline const Setting & operator*() const
367 { return(*_setting)[_idx]; }
368 inline const Setting * operator->() const
369 { return(&(*_setting)[_idx]); }
371 // Increment and decrement operators.
372 SettingIterator & operator++();
373 SettingIterator operator++(int);
375 SettingIterator & operator--();
376 SettingIterator operator--(int);
378 // Arithmetic operators.
379 SettingIterator operator+(int offset) const;
380 SettingIterator & operator+=(int offset);
382 SettingIterator operator-(int offset) const;
383 SettingIterator & operator-=(int offset);
385 int operator-(const SettingIterator &other) const;
395 SettingIterator operator+(int offset, const SettingIterator &si);
397 class LIBCONFIGXX_API SettingConstIterator
401 SettingConstIterator(const Setting &setting, bool endIterator = false);
402 SettingConstIterator(const SettingConstIterator &rhs);
403 SettingConstIterator& operator=(const SettingConstIterator &rhs);
405 // Equality comparison.
406 bool operator==(SettingConstIterator const &other) const
407 { return((_setting == other._setting) && (_idx == other._idx)); }
409 inline bool operator!=(SettingConstIterator const &other) const
410 { return(!operator==(other)); }
412 // Dereference operators.
413 inline Setting const & operator*()
414 { return((*_setting)[_idx]); }
415 inline Setting const * operator->()
416 { return(&(*_setting)[_idx]); }
418 inline const Setting& operator*() const
419 { return((*_setting)[_idx]); }
420 inline const Setting * operator->() const
421 { return(&(*_setting)[_idx]); }
423 // Increment and decrement operators.
424 SettingConstIterator & operator++();
425 SettingConstIterator operator++(int);
427 SettingConstIterator & operator--();
428 SettingConstIterator operator--(int);
430 // Arithmetic operators.
431 SettingConstIterator operator+(int offset) const;
432 SettingConstIterator & operator+=(int offset);
434 SettingConstIterator operator-(int offset) const;
435 SettingConstIterator & operator-=(int offset);
437 int operator-(const SettingConstIterator &other) const;
441 const Setting *_setting;
447 SettingConstIterator operator+(int offset, const SettingConstIterator &si);
449 class LIBCONFIGXX_API Config
456 void setOptions(int options);
457 int getOptions() const;
459 void setAutoConvert(bool flag);
460 bool getAutoConvert() const;
462 void setDefaultFormat(Setting::Format format);
463 inline Setting::Format getDefaultFormat() const
464 { return(_defaultFormat); }
466 void setTabWidth(unsigned short width);
467 unsigned short getTabWidth() const;
469 void setIncludeDir(const char *includeDir);
470 const char *getIncludeDir() const;
472 void read(FILE *stream);
473 void write(FILE *stream) const;
475 void readString(const char *str);
476 inline void readString(const std::string &str)
477 { return(readString(str.c_str())); }
479 void readFile(const char *filename);
480 void writeFile(const char *filename);
482 Setting & lookup(const char *path) const;
483 inline Setting & lookup(const std::string &path) const
484 { return(lookup(path.c_str())); }
486 bool exists(const char *path) const;
487 inline bool exists(const std::string &path) const
488 { return(exists(path.c_str())); }
490 bool lookupValue(const char *path, bool &value) const;
491 bool lookupValue(const char *path, int &value) const;
492 bool lookupValue(const char *path, unsigned int &value) const;
493 bool lookupValue(const char *path, long long &value) const;
494 bool lookupValue(const char *path, unsigned long long &value) const;
495 bool lookupValue(const char *path, double &value) const;
496 bool lookupValue(const char *path, float &value) const;
497 bool lookupValue(const char *path, const char *&value) const;
498 bool lookupValue(const char *path, std::string &value) const;
500 inline bool lookupValue(const std::string &path, bool &value) const
501 { return(lookupValue(path.c_str(), value)); }
503 inline bool lookupValue(const std::string &path, int &value) const
504 { return(lookupValue(path.c_str(), value)); }
506 inline bool lookupValue(const std::string &path, unsigned int &value) const
507 { return(lookupValue(path.c_str(), value)); }
509 inline bool lookupValue(const std::string &path, long long &value) const
510 { return(lookupValue(path.c_str(), value)); }
512 inline bool lookupValue(const std::string &path,
513 unsigned long long &value) const
514 { return(lookupValue(path.c_str(), value)); }
516 inline bool lookupValue(const std::string &path, double &value) const
517 { return(lookupValue(path.c_str(), value)); }
519 inline bool lookupValue(const std::string &path, float &value) const
520 { return(lookupValue(path.c_str(), value)); }
522 inline bool lookupValue(const std::string &path, const char *&value) const
523 { return(lookupValue(path.c_str(), value)); }
525 inline bool lookupValue(const std::string &path, std::string &value) const
526 { return(lookupValue(path.c_str(), value)); }
528 Setting & getRoot() const;
532 static void ConfigDestructor(void *arg);
533 void handleError() const;
536 Setting::Format _defaultFormat;
538 Config(const Config& other); // not supported
539 Config& operator=(const Config& other); // not supported
542 } // namespace libconfig
544 #endif // __libconfig_hpp