2bf418c84be8a3aea6d09a9bb1daaa68286d491e
[platform/upstream/libconfig.git] / lib / libconfig.h++
1 /* ----------------------------------------------------------------------------
2    libconfig - A library for processing structured configuration files
3    Copyright (C) 2005-2014  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 <exception>
28 #include <string>
29
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 */
38 #else /* ! WIN32 */
39 #define LIBCONFIGXX_API
40 #endif /* WIN32 */
41
42 #define LIBCONFIGXX_VER_MAJOR    1
43 #define LIBCONFIGXX_VER_MINOR    5
44 #define LIBCONFIGXX_VER_REVISION 0
45
46 struct config_t; // fwd decl
47 struct config_setting_t; // fwd decl
48
49 namespace libconfig {
50
51 class LIBCONFIGXX_API ConfigException : public std::exception { };
52
53 class Setting; // fwd decl
54 class SettingIterator;
55 class SettingConstIterator;
56
57 class LIBCONFIGXX_API SettingException : public ConfigException
58 {
59   public:
60
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);
65
66   SettingException(const SettingException &other);
67   SettingException& operator=(const SettingException &other);
68
69   virtual ~SettingException() throw();
70
71   const char *getPath() const;
72
73   virtual const char *what() const throw();
74
75   private:
76
77   char *_path;
78 };
79
80 class LIBCONFIGXX_API SettingTypeException : public SettingException
81 {
82   public:
83
84   SettingTypeException(const Setting &setting);
85   SettingTypeException(const Setting &setting, int idx);
86   SettingTypeException(const Setting &setting, const char *name);
87
88   virtual const char *what() const throw();
89 };
90
91 class LIBCONFIGXX_API SettingNotFoundException : public SettingException
92 {
93   public:
94
95   SettingNotFoundException(const char *path);
96   SettingNotFoundException(const Setting &setting, int idx);
97   SettingNotFoundException(const Setting &setting, const char *name);
98
99   virtual const char *what() const throw();
100 };
101
102 class LIBCONFIGXX_API SettingNameException : public SettingException
103 {
104   public:
105
106   SettingNameException(const Setting &setting, const char *name);
107
108   virtual const char *what() const throw();
109 };
110
111 class LIBCONFIGXX_API FileIOException : public ConfigException
112 {
113   public:
114
115   virtual const char *what() const throw();
116 };
117
118 class LIBCONFIGXX_API ParseException : public ConfigException
119 {
120   public:
121
122   ParseException(const char *file, int line, const char *error);
123
124   ParseException(const ParseException &other);
125
126   virtual ~ParseException() throw();
127
128   inline const char *getFile() const
129   { return(_file); }
130
131   inline int getLine() const
132   { return(_line); }
133
134   inline const char *getError() const
135   { return(_error); }
136
137   virtual const char *what() const throw();
138
139   private:
140
141   const char *_file;
142   int _line;
143   const char *_error;
144 };
145
146 class LIBCONFIGXX_API Setting
147 {
148   friend class Config;
149
150   public:
151
152   enum Type
153   {
154     TypeNone = 0,
155     // scalar types
156     TypeInt,
157     TypeInt64,
158     TypeFloat,
159     TypeString,
160     TypeBoolean,
161     // aggregate types
162     TypeGroup,
163     TypeArray,
164     TypeList
165   };
166
167   enum Format
168   {
169     FormatDefault = 0,
170     FormatHex = 1
171   };
172
173   enum Option
174   {
175     OptionNone = 0,
176     OptionAutoConvert = 0x01,
177     OptionSemicolonSeparators = 0x02,
178     OptionColonAssignmentForGroups = 0x04,
179     OptionColonAssignmentForNonGroups = 0x08,
180     OptionOpenBraceOnSeparateLine = 0x10
181   };
182
183   typedef SettingIterator iterator;
184   typedef SettingConstIterator const_iterator;
185
186   public:
187
188   virtual ~Setting();
189
190   inline Type getType() const { return(_type); }
191
192   inline Format getFormat() const { return(_format); }
193   void setFormat(Format format);
194
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;
206
207   inline const char *c_str() const
208   { return operator const char *(); }
209
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);
218
219   Setting & lookup(const char *path) const;
220   inline Setting & lookup(const std::string &path) const
221   { return(lookup(path.c_str())); }
222
223   Setting & operator[](const char *name) const;
224
225   inline Setting & operator[](const std::string &name) const
226   { return(operator[](name.c_str())); }
227
228   Setting & operator[](int index) const;
229
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;
239
240   inline bool lookupValue(const std::string &name, bool &value) const
241   { return(lookupValue(name.c_str(), value)); }
242
243   inline bool lookupValue(const std::string &name, int &value) const
244   { return(lookupValue(name.c_str(), value)); }
245
246   inline bool lookupValue(const std::string &name, unsigned int &value) const
247   { return(lookupValue(name.c_str(), value)); }
248
249   inline bool lookupValue(const std::string &name, long long &value) const
250   { return(lookupValue(name.c_str(), value)); }
251
252   inline bool lookupValue(const std::string &name,
253                           unsigned long long &value) const
254   { return(lookupValue(name.c_str(), value)); }
255
256   inline bool lookupValue(const std::string &name, double &value) const
257   { return(lookupValue(name.c_str(), value)); }
258
259   inline bool lookupValue(const std::string &name, float &value) const
260   { return(lookupValue(name.c_str(), value)); }
261
262   inline bool lookupValue(const std::string &name, const char *&value) const
263   { return(lookupValue(name.c_str(), value)); }
264
265   inline bool lookupValue(const std::string &name, std::string &value) const
266   { return(lookupValue(name.c_str(), value)); }
267
268   void remove(const char *name);
269
270   inline void remove(const std::string &name)
271   { remove(name.c_str()); }
272
273   void remove(unsigned int idx);
274
275   Setting & add(const char *name, Type type);
276
277   inline Setting & add(const std::string &name, Type type)
278   { return(add(name.c_str(), type)); }
279
280   Setting & add(Type type);
281
282   bool exists(const char *name) const;
283
284   inline bool exists(const std::string &name) const
285   { return(exists(name.c_str())); }
286
287   int getLength() const;
288   const char *getName() const;
289   std::string getPath() const;
290   int getIndex() const;
291
292   const Setting & getParent() const;
293   Setting & getParent();
294
295   bool isRoot() const;
296
297   inline bool isGroup() const
298   { return(_type == TypeGroup); }
299
300   inline bool isArray() const
301   { return(_type == TypeArray); }
302
303   inline bool isList() const
304   { return(_type == TypeList); }
305
306   inline bool isAggregate() const
307   { return(_type >= TypeGroup); }
308
309   inline bool isScalar() const
310   { return((_type > TypeNone) && (_type < TypeGroup)); }
311
312   inline bool isNumber() const
313   {
314     return((_type == TypeInt) || (_type == TypeInt64) || (_type == TypeFloat));
315   }
316
317   unsigned int getSourceLine() const;
318   const char *getSourceFile() const;
319
320   iterator begin();
321   iterator end();
322
323   const_iterator begin() const;
324   const_iterator end() const;
325
326   private:
327
328   config_setting_t *_setting;
329   Type _type;
330   Format _format;
331
332   Setting(config_setting_t *setting);
333
334   void assertType(Type type) const;
335   static Setting & wrapSetting(config_setting_t *setting);
336
337   Setting(const Setting& other); // not supported
338   Setting& operator=(const Setting& other); // not supported
339 };
340
341
342 class LIBCONFIGXX_API SettingIterator
343 {
344   public:
345
346   SettingIterator(Setting &setting, bool endIterator = false);
347   SettingIterator(const SettingIterator &other);
348   SettingIterator& operator=(const SettingIterator &other);
349
350   // Equality comparison.
351   inline bool operator==(SettingIterator const &other) const
352   { return((_setting == other._setting) && (_idx == other._idx)); }
353
354   inline bool operator!=(SettingIterator const &other) const
355   { return(!operator==(other)); }
356
357   bool operator<(SettingIterator const &other) const;
358
359   // Dereference operators.
360   inline Setting & operator*()
361   { return((*_setting)[_idx]); }
362
363   inline Setting * operator->()
364   { return(&(*_setting)[_idx]); }
365
366   inline const Setting & operator*() const
367   { return(*_setting)[_idx]; }
368   inline const Setting * operator->() const
369   { return(&(*_setting)[_idx]); }
370
371   // Increment and decrement operators.
372   SettingIterator & operator++();
373   SettingIterator operator++(int);
374
375   SettingIterator & operator--();
376   SettingIterator operator--(int);
377
378   // Arithmetic operators.
379   SettingIterator operator+(int offset) const;
380   SettingIterator & operator+=(int offset);
381
382   SettingIterator operator-(int offset) const;
383   SettingIterator & operator-=(int offset);
384
385   int operator-(const SettingIterator &other) const;
386
387   private:
388
389   Setting *_setting;
390
391   int _count;
392   int _idx;
393 };
394
395 SettingIterator operator+(int offset, const SettingIterator &si);
396
397 class LIBCONFIGXX_API SettingConstIterator
398 {
399   public:
400
401   SettingConstIterator(const Setting &setting, bool endIterator = false);
402   SettingConstIterator(const SettingConstIterator &rhs);
403   SettingConstIterator& operator=(const SettingConstIterator &rhs);
404
405   // Equality comparison.
406   bool operator==(SettingConstIterator const &other) const
407   { return((_setting == other._setting) && (_idx == other._idx)); }
408
409   inline bool operator!=(SettingConstIterator const &other) const
410   { return(!operator==(other)); }
411
412   // Dereference operators.
413   inline Setting const & operator*()
414   { return((*_setting)[_idx]); }
415   inline Setting const * operator->()
416   { return(&(*_setting)[_idx]); }
417
418   inline const Setting& operator*() const
419   { return((*_setting)[_idx]); }
420   inline const Setting * operator->() const
421   { return(&(*_setting)[_idx]); }
422
423   // Increment and decrement operators.
424   SettingConstIterator & operator++();
425   SettingConstIterator operator++(int);
426
427   SettingConstIterator & operator--();
428   SettingConstIterator operator--(int);
429
430   // Arithmetic operators.
431   SettingConstIterator operator+(int offset) const;
432   SettingConstIterator & operator+=(int offset);
433
434   SettingConstIterator operator-(int offset) const;
435   SettingConstIterator & operator-=(int offset);
436
437   int operator-(const SettingConstIterator &other) const;
438
439   private:
440
441   const Setting *_setting;
442
443   int _count;
444   int _idx;
445 };
446
447 SettingConstIterator operator+(int offset, const SettingConstIterator &si);
448
449 class LIBCONFIGXX_API Config
450 {
451   public:
452
453   Config();
454   virtual ~Config();
455
456   void setOptions(int options);
457   int getOptions() const;
458
459   void setAutoConvert(bool flag);
460   bool getAutoConvert() const;
461
462   void setDefaultFormat(Setting::Format format);
463   inline Setting::Format getDefaultFormat() const
464   { return(_defaultFormat); }
465
466   void setTabWidth(unsigned short width);
467   unsigned short getTabWidth() const;
468
469   void setIncludeDir(const char *includeDir);
470   const char *getIncludeDir() const;
471
472   void read(FILE *stream);
473   void write(FILE *stream) const;
474
475   void readString(const char *str);
476   inline void readString(const std::string &str)
477   { return(readString(str.c_str())); }
478
479   void readFile(const char *filename);
480   void writeFile(const char *filename);
481
482   Setting & lookup(const char *path) const;
483   inline Setting & lookup(const std::string &path) const
484   { return(lookup(path.c_str())); }
485
486   bool exists(const char *path) const;
487   inline bool exists(const std::string &path) const
488   { return(exists(path.c_str())); }
489
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;
499
500   inline bool lookupValue(const std::string &path, bool &value) const
501   { return(lookupValue(path.c_str(), value)); }
502
503   inline bool lookupValue(const std::string &path, int &value) const
504   { return(lookupValue(path.c_str(), value)); }
505
506   inline bool lookupValue(const std::string &path, unsigned int &value) const
507   { return(lookupValue(path.c_str(), value)); }
508
509   inline bool lookupValue(const std::string &path, long long &value) const
510   { return(lookupValue(path.c_str(), value)); }
511
512   inline bool lookupValue(const std::string &path,
513                           unsigned long long &value) const
514   { return(lookupValue(path.c_str(), value)); }
515
516   inline bool lookupValue(const std::string &path, double &value) const
517   { return(lookupValue(path.c_str(), value)); }
518
519   inline bool lookupValue(const std::string &path, float &value) const
520   { return(lookupValue(path.c_str(), value)); }
521
522   inline bool lookupValue(const std::string &path, const char *&value) const
523   { return(lookupValue(path.c_str(), value)); }
524
525   inline bool lookupValue(const std::string &path, std::string &value) const
526   { return(lookupValue(path.c_str(), value)); }
527
528   Setting & getRoot() const;
529
530   private:
531
532   static void ConfigDestructor(void *arg);
533   void handleError() const;
534
535   config_t *_config;
536   Setting::Format _defaultFormat;
537
538   Config(const Config& other); // not supported
539   Config& operator=(const Config& other); // not supported
540 };
541
542 } // namespace libconfig
543
544 #endif // __libconfig_hpp