root/library/bdm/base/libconfig/libconfig.h++ @ 652

Revision 248, 13.5 kB (checked in by smidl, 15 years ago)

doc

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