e7dd8b9b8d0cff1e1c4358e7ec7f36767f8c482d
[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   typedef SettingIterator iterator;
174   typedef SettingConstIterator const_iterator;
175
176   public:
177
178   virtual ~Setting();
179
180   inline Type getType() const { return(_type); }
181
182   inline Format getFormat() const { return(_format); }
183   void setFormat(Format format);
184
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;
196
197   inline const char *c_str() const
198   { return operator const char *(); }
199
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);
208
209   Setting & lookup(const char *path) const;
210   inline Setting & lookup(const std::string &path) const
211   { return(lookup(path.c_str())); }
212
213   Setting & operator[](const char *name) const;
214   Setting & operator[](int index) const;
215
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;
225
226   inline bool lookupValue(const std::string &name, bool &value) const
227   { return(lookupValue(name.c_str(), value)); }
228
229   inline bool lookupValue(const std::string &name, int &value) const
230   { return(lookupValue(name.c_str(), value)); }
231
232   inline bool lookupValue(const std::string &name, unsigned int &value) const
233   { return(lookupValue(name.c_str(), value)); }
234
235   inline bool lookupValue(const std::string &name, long long &value) const
236   { return(lookupValue(name.c_str(), value)); }
237
238   inline bool lookupValue(const std::string &name,
239                           unsigned long long &value) const
240   { return(lookupValue(name.c_str(), value)); }
241
242   inline bool lookupValue(const std::string &name, double &value) const
243   { return(lookupValue(name.c_str(), value)); }
244
245   inline bool lookupValue(const std::string &name, float &value) const
246   { return(lookupValue(name.c_str(), value)); }
247
248   inline bool lookupValue(const std::string &name, const char *&value) const
249   { return(lookupValue(name.c_str(), value)); }
250
251   inline bool lookupValue(const std::string &name, std::string &value) const
252   { return(lookupValue(name.c_str(), value)); }
253
254   void remove(const char *name);
255
256   inline void remove(const std::string &name)
257   { remove(name.c_str()); }
258
259   void remove(unsigned int idx);
260
261   Setting & add(const char *name, Type type);
262
263   inline Setting & add(const std::string &name, Type type)
264   { return(add(name.c_str(), type)); }
265
266   Setting & add(Type type);
267
268   bool exists(const char *name) const;
269
270   inline bool exists(const std::string &name) const
271   { return(exists(name.c_str())); }
272
273   int getLength() const;
274   const char *getName() const;
275   std::string getPath() const;
276   int getIndex() const;
277
278   const Setting & getParent() const;
279   Setting & getParent();
280
281   bool isRoot() const;
282
283   inline bool isGroup() const
284   { return(_type == TypeGroup); }
285
286   inline bool isArray() const
287   { return(_type == TypeArray); }
288
289   inline bool isList() const
290   { return(_type == TypeList); }
291
292   inline bool isAggregate() const
293   { return(_type >= TypeGroup); }
294
295   inline bool isScalar() const
296   { return((_type > TypeNone) && (_type < TypeGroup)); }
297
298   inline bool isNumber() const
299   {
300     return((_type == TypeInt) || (_type == TypeInt64) || (_type == TypeFloat));
301   }
302
303   unsigned int getSourceLine() const;
304   const char *getSourceFile() const;
305
306   iterator begin();
307   iterator end();
308
309   const_iterator begin() const;
310   const_iterator end() const;
311
312   private:
313
314   config_setting_t *_setting;
315   Type _type;
316   Format _format;
317
318   Setting(config_setting_t *setting);
319
320   void assertType(Type type) const;
321   static Setting & wrapSetting(config_setting_t *setting);
322
323   Setting(const Setting& other); // not supported
324   Setting& operator=(const Setting& other); // not supported
325 };
326
327
328 class LIBCONFIGXX_API SettingIterator
329 {
330   public:
331
332   SettingIterator(Setting &setting, bool endIterator = false);
333   SettingIterator(const SettingIterator &other);
334   SettingIterator& operator=(const SettingIterator &other);
335
336   // Equality comparison.
337   inline bool operator==(SettingIterator const &other) const
338   { return((_setting == other._setting) && (_idx == other._idx)); }
339
340   inline bool operator!=(SettingIterator const &other) const
341   { return(!operator==(other)); }
342
343   bool operator<(SettingIterator const &other) const;
344
345   // Dereference operators.
346   inline Setting & operator*()
347   { return((*_setting)[_idx]); }
348
349   inline Setting * operator->()
350   { return(&(*_setting)[_idx]); }
351
352   inline const Setting & operator*() const
353   { return(*_setting)[_idx]; }
354   inline const Setting * operator->() const
355   { return(&(*_setting)[_idx]); }
356
357   // Increment and decrement operators.
358   SettingIterator & operator++();
359   SettingIterator operator++(int);
360
361   SettingIterator & operator--();
362   SettingIterator operator--(int);
363
364   // Arithmetic operators.
365   SettingIterator operator+(int offset) const;
366   SettingIterator & operator+=(int offset);
367
368   SettingIterator operator-(int offset) const;
369   SettingIterator & operator-=(int offset);
370
371   int operator-(const SettingIterator &other) const;
372
373   private:
374
375   Setting *_setting;
376
377   int _count;
378   int _idx;
379 };
380
381 SettingIterator operator+(int offset, const SettingIterator &si);
382
383 class LIBCONFIGXX_API SettingConstIterator
384 {
385   public:
386
387   SettingConstIterator(const Setting &setting, bool endIterator = false);
388   SettingConstIterator(const SettingConstIterator &rhs);
389   SettingConstIterator& operator=(const SettingConstIterator &rhs);
390
391   // Equality comparison.
392   bool operator==(SettingConstIterator const &other) const
393   { return((_setting == other._setting) && (_idx == other._idx)); }
394
395   inline bool operator!=(SettingConstIterator const &other) const
396   { return(!operator==(other)); }
397
398   // Dereference operators.
399   inline Setting const & operator*()
400   { return((*_setting)[_idx]); }
401   inline Setting const * operator->()
402   { return(&(*_setting)[_idx]); }
403
404   inline const Setting& operator*() const
405   { return((*_setting)[_idx]); }
406   inline const Setting * operator->() const
407   { return(&(*_setting)[_idx]); }
408
409   // Increment and decrement operators.
410   SettingConstIterator & operator++();
411   SettingConstIterator operator++(int);
412
413   SettingConstIterator & operator--();
414   SettingConstIterator operator--(int);
415
416   // Arithmetic operators.
417   SettingConstIterator operator+(int offset) const;
418   SettingConstIterator & operator+=(int offset);
419
420   SettingConstIterator operator-(int offset) const;
421   SettingConstIterator & operator-=(int offset);
422
423   int operator-(const SettingConstIterator &other) const;
424
425   private:
426
427   const Setting *_setting;
428
429   int _count;
430   int _idx;
431 };
432
433 SettingConstIterator operator+(int offset, const SettingConstIterator &si);
434
435 class LIBCONFIGXX_API Config
436 {
437   public:
438
439   Config();
440   virtual ~Config();
441
442   void setAutoConvert(bool flag);
443   bool getAutoConvert() const;
444
445   void setDefaultFormat(Setting::Format format);
446   inline Setting::Format getDefaultFormat() const
447   { return(_defaultFormat); }
448
449   void setTabWidth(unsigned short width);
450   unsigned short getTabWidth() const;
451
452   void setIncludeDir(const char *includeDir);
453   const char *getIncludeDir() const;
454
455   void read(FILE *stream);
456   void write(FILE *stream) const;
457
458   void readString(const char *str);
459   inline void readString(const std::string &str)
460   { return(readString(str.c_str())); }
461
462   void readFile(const char *filename);
463   void writeFile(const char *filename);
464
465   Setting & lookup(const char *path) const;
466   inline Setting & lookup(const std::string &path) const
467   { return(lookup(path.c_str())); }
468
469   bool exists(const char *path) const;
470   inline bool exists(const std::string &path) const
471   { return(exists(path.c_str())); }
472
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;
482
483   inline bool lookupValue(const std::string &path, bool &value) const
484   { return(lookupValue(path.c_str(), value)); }
485
486   inline bool lookupValue(const std::string &path, int &value) const
487   { return(lookupValue(path.c_str(), value)); }
488
489   inline bool lookupValue(const std::string &path, unsigned int &value) const
490   { return(lookupValue(path.c_str(), value)); }
491
492   inline bool lookupValue(const std::string &path, long long &value) const
493   { return(lookupValue(path.c_str(), value)); }
494
495   inline bool lookupValue(const std::string &path,
496                           unsigned long long &value) const
497   { return(lookupValue(path.c_str(), value)); }
498
499   inline bool lookupValue(const std::string &path, double &value) const
500   { return(lookupValue(path.c_str(), value)); }
501
502   inline bool lookupValue(const std::string &path, float &value) const
503   { return(lookupValue(path.c_str(), value)); }
504
505   inline bool lookupValue(const std::string &path, const char *&value) const
506   { return(lookupValue(path.c_str(), value)); }
507
508   inline bool lookupValue(const std::string &path, std::string &value) const
509   { return(lookupValue(path.c_str(), value)); }
510
511   Setting & getRoot() const;
512
513   private:
514
515   static void ConfigDestructor(void *arg);
516   void handleError() const;
517
518   config_t *_config;
519   Setting::Format _defaultFormat;
520
521   Config(const Config& other); // not supported
522   Config& operator=(const Config& other); // not supported
523 };
524
525 } // namespace libconfig
526
527 #endif // __libconfig_hpp