- Main Page
- Related Pages
- Modules
- Classes
- Files

# work/git/mixpp/bdm/libconfig/libconfig.h++

    00001 /* ----------------------------------------------------------------------------
    00002    libconfig - A library for processing structured configuration files
    00003    Copyright (C) 2005-2008  Mark A Lindner
    00004
    00005    This file is part of libconfig.
    00006
    00007    This library is free software; you can redistribute it and/or
    00008    modify it under the terms of the GNU Lesser General Public License
    00009    as published by the Free Software Foundation; either version 2.1 of
    00010    the License, or (at your option) any later version.
    00011
    00012    This library is distributed in the hope that it will be useful, but
    00013    WITHOUT ANY WARRANTY; without even the implied warranty of
    00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00015    Lesser General Public License for more details.
    00016
    00017    You should have received a copy of the GNU Library General Public
    00018    License along with this library; if not, see
    00019    <http://www.gnu.org/licenses/>.
    00020    ----------------------------------------------------------------------------
    00021 */
    00022
    00023 #ifndef __libconfig_hpp
    00024 #define __libconfig_hpp
    00025
    00026 #include <stdio.h>
    00027 #include <string>
    00028 #include <map>
    00029
    00030 namespace libconfig
    00031 {
    00032
    00033 #include "libconfig.h"
    00034
    00035   class LIBCONFIG_API ConfigException : public std::exception { };
    00036
    00037   class Setting; // fwd decl
    00038
    00039   class LIBCONFIG_API SettingException : public ConfigException
    00040   {
    00041     friend class Config;
    00042
    00043     public:
    00044
    00045     SettingException(const SettingException &other);
    00046     SettingException& operator=(const SettingException &other);
    00047
    00048     virtual ~SettingException() throw();
    00049
    00050     const char *getPath() const;
    00051
    00052     virtual const char *what() const throw();
    00053
    00054     protected:
    00055
    00056     SettingException(const Setting &setting);
    00057     SettingException(const Setting &setting, int idx);
    00058     SettingException(const Setting &setting, const char *name);
    00059     SettingException(const char *path);
    00060
    00061     private:
    00062
    00063     char *_path;
    00064   };
    00065
    00066   class LIBCONFIG_API SettingTypeException : public SettingException
    00067   {
    00068     friend class Config;
    00069     friend class Setting;
    00070
    00071     const char *what() const throw();
    00072
    00073     private:
    00074
    00075     SettingTypeException(const Setting &setting);
    00076     SettingTypeException(const Setting &setting, int idx);
    00077     SettingTypeException(const Setting &setting, const char *name);
    00078   };
    00079
    00080   class LIBCONFIG_API SettingNotFoundException : public SettingException
    00081   {
    00082     friend class Config;
    00083     friend class Setting;
    00084
    00085     const char *what() const throw();
    00086
    00087     private:
    00088
    00089     SettingNotFoundException(const Setting &setting, int idx);
    00090     SettingNotFoundException(const Setting &setting, const char *name);
    00091     SettingNotFoundException(const char *path);
    00092   };
    00093
    00094   class LIBCONFIG_API SettingNameException : public SettingException
    00095   {
    00096     friend class Config;
    00097     friend class Setting;
    00098
    00099     const char *what() const throw();
    00100
    00101     private:
    00102
    00103     SettingNameException(const Setting &setting, const char *name);
    00104   };
    00105
    00106   class LIBCONFIG_API FileIOException : public ConfigException
    00107   {
    00108     const char *what() const throw();
    00109   };
    00110
    00111   class LIBCONFIG_API ParseException : public ConfigException
    00112   {
    00113     friend class Config;
    00114
    00115     public:
    00116
    00117     virtual ~ParseException() throw();
    00118
    00119     inline int getLine() throw()
    00120     { return(_line); }
    00121
    00122     inline const char *getError() throw()
    00123     { return(_error); }
    00124
    00125     const char *what() const throw();
    00126
    00127     private:
    00128
    00129     ParseException(int line, const char *error);
    00130
    00131     int _line;
    00132     const char *_error;
    00133   };
    00134
    00135   class LIBCONFIG_API Setting
    00136   {
    00137     friend class Config;
    00138
    00139     public:
    00140
    00141     enum Type
    00142     {
    00143       TypeNone = 0,
    00144       // scalar types
    00145       TypeInt,
    00146       TypeInt64,
    00147       TypeFloat,
    00148       TypeString,
    00149       TypeBoolean,
    00150       // aggregate types
    00151       TypeGroup,
    00152       TypeArray,
    00153       TypeList
    00154     };
    00155
    00156     enum Format
    00157     {
    00158       FormatDefault = 0,
    00159       FormatHex = 1
    00160     };
    00161
    00162     private:
    00163
    00164     config_setting_t *_setting;
    00165     Type _type;
    00166     Format _format;
    00167
    00168     Setting(config_setting_t *setting);
    00169
    00170     void assertType(Type type) const
    00171       throw(SettingTypeException);
    00172     static Setting & wrapSetting(config_setting_t *setting);
    00173
    00174     Setting(const Setting& other); // not supported
    00175     Setting& operator=(const Setting& other); // not supported
    00176
    00177     public:
    00178
    00179     virtual ~Setting() throw();
    00180
    00181     inline Type getType() const throw() { return(_type); }
    00182
    00183     inline Format getFormat() const throw() { return(_format); }
    00184     void setFormat(Format format) throw();
    00185
    00186     operator bool() const throw(SettingTypeException);
    00187     operator long() const throw(SettingTypeException);
    00188     operator unsigned long() const throw(SettingTypeException);
    00189     operator int() const throw(SettingTypeException);
    00190     operator unsigned int() const throw(SettingTypeException);
    00191     operator long long() const throw(SettingTypeException);
    00192     operator unsigned long long() const throw(SettingTypeException);
    00193     operator double() const throw(SettingTypeException);
    00194     operator float() const throw(SettingTypeException);
    00195     operator const char *() const throw(SettingTypeException);
    00196     operator std::string() const throw(SettingTypeException);
    00197
    00198     Setting & operator=(bool value) throw(SettingTypeException);
    00199     Setting & operator=(long value) throw(SettingTypeException);
    00200     Setting & operator=(int value) throw(SettingTypeException);
    00201     Setting & operator=(const long long &value) throw(SettingTypeException);
    00202     Setting & operator=(const double &value) throw(SettingTypeException);
    00203     Setting & operator=(float value) throw(SettingTypeException);
    00204     Setting & operator=(const char *value) throw(SettingTypeException);
    00205     Setting & operator=(const std::string &value) throw(SettingTypeException);
    00206
    00207     Setting & operator[](const char * key) const
    00208       throw(SettingTypeException, SettingNotFoundException);
    00209
    00210     inline Setting & operator[](const std::string & key) const
    00211       throw(SettingTypeException, SettingNotFoundException)
    00212     { return(operator[](key.c_str())); }
    00213
    00214     Setting & operator[](int index) const
    00215       throw(SettingTypeException, SettingNotFoundException);
    00216
    00217     bool lookupValue(const char *name, bool &value) const throw();
    00218     bool lookupValue(const char *name, long &value) const throw();
    00219     bool lookupValue(const char *name, unsigned long &value) const throw();
    00220     bool lookupValue(const char *name, int &value) const throw();
    00221     bool lookupValue(const char *name, unsigned int &value) const throw();
    00222     bool lookupValue(const char *name, long long &value) const throw();
    00223     bool lookupValue(const char *name, unsigned long long &value)
    00224       const throw();
    00225     bool lookupValue(const char *name, double &value) const throw();
    00226     bool lookupValue(const char *name, float &value) const throw();
    00227     bool lookupValue(const char *name, const char *&value) const throw();
    00228     bool lookupValue(const char *name, std::string &value) const throw();
    00229
    00230     inline bool lookupValue(const std::string &name, bool &value)
    00231       const throw()
    00232     { return(lookupValue(name.c_str(), value)); }
    00233
    00234     inline bool lookupValue(const std::string &name, long &value)
    00235       const throw()
    00236     { return(lookupValue(name.c_str(), value)); }
    00237
    00238     inline bool lookupValue(const std::string &name, unsigned long &value)
    00239       const throw()
    00240     { return(lookupValue(name.c_str(), value)); }
    00241
    00242     inline bool lookupValue(const std::string &name, int &value) const throw()
    00243     { return(lookupValue(name.c_str(), value)); }
    00244
    00245     inline bool lookupValue(const std::string &name, unsigned int &value)
    00246       const throw()
    00247     { return(lookupValue(name.c_str(), value)); }
    00248
    00249     inline bool lookupValue(const std::string &name, long long &value)
    00250       const throw()
    00251     { return(lookupValue(name.c_str(), value)); }
    00252
    00253     inline bool lookupValue(const std::string &name,
    00254                             unsigned long long &value) const throw()
    00255     { return(lookupValue(name.c_str(), value)); }
    00256
    00257     inline bool lookupValue(const std::string &name, double &value) const
    00258       throw()
    00259     { return(lookupValue(name.c_str(), value)); }
    00260
    00261     inline bool lookupValue(const std::string &name, float &value) const
    00262       throw()
    00263     { return(lookupValue(name.c_str(), value)); }
    00264
    00265     inline bool lookupValue(const std::string &name, const char *&value) const
    00266       throw()
    00267     { return(lookupValue(name.c_str(), value)); }
    00268
    00269     inline bool lookupValue(const std::string &name, std::string &value) const
    00270       throw()
    00271     { return(lookupValue(name.c_str(), value)); }
    00272
    00273     void remove(const char *name)
    00274       throw(SettingTypeException, SettingNotFoundException);
    00275
    00276     inline void remove(const std::string & name)
    00277       throw(SettingTypeException, SettingNotFoundException)
    00278     { remove(name.c_str()); }
    00279
    00280     void remove(unsigned int idx)
    00281       throw(SettingTypeException, SettingNotFoundException);
    00282
    00283     inline Setting & add(const std::string & name, Type type)
    00284       throw(SettingNameException, SettingTypeException)
    00285     { return(add(name.c_str(), type)); }
    00286
    00287     Setting & add(const char *name, Type type)
    00288       throw(SettingNameException, SettingTypeException);
    00289
    00290     Setting & add(Type type)
    00291       throw(SettingTypeException);
    00292
    00293     inline bool exists(const std::string & name) const throw()
    00294     { return(exists(name.c_str())); }
    00295
    00296     bool exists(const char *name) const throw();
    00297
    00298     int getLength() const throw();
    00299     const char *getName() const throw();
    00300     std::string getPath() const;
    00301     int getIndex() const throw();
    00302
    00303     const Setting & getParent() const throw(SettingNotFoundException);
    00304     Setting & getParent() throw(SettingNotFoundException);
    00305
    00306     bool isRoot() const throw();
    00307
    00308     inline bool isGroup() const throw()
    00309     { return(_type == TypeGroup); }
    00310
    00311     inline bool isArray() const throw()
    00312     { return(_type == TypeArray); }
    00313
    00314     inline bool isList() const throw()
    00315     { return(_type == TypeList); }
    00316
    00317     inline bool isAggregate() const throw()
    00318     { return(_type >= TypeGroup); }
    00319
    00320     inline bool isScalar() const throw()
    00321     { return((_type > TypeNone) && (_type < TypeGroup)); }
    00322
    00323     inline bool isNumber() const throw()
    00324     { return((_type == TypeInt) || (_type == TypeInt64)
    00325              || (_type == TypeFloat)); }
    00326
    00327     inline unsigned int getSourceLine() const throw()
    00328     { return(config_setting_source_line(_setting)); }
    00329
    00330         inline Setting& lookup(const char *path) const
    00331   throw(SettingNotFoundException)
    00332 {
    00333   config_setting_t *s = config_lookup(_setting->config, path);
    00334   if(! s)
    00335     throw SettingNotFoundException(path);
    00336
    00337   return(Setting::wrapSetting(s));
    00338 }
    00339   };
    00340
    00341   class LIBCONFIG_API Config
    00342   {
    00343     private:
    00344
    00345     config_t _config;
    00346
    00347     static void ConfigDestructor(void *arg);
    00348     Config(const Config& other); // not supported
    00349     Config& operator=(const Config& other); // not supported
    00350
    00351     public:
    00352
    00353     Config();
    00354     virtual ~Config();
    00355
    00356     void setAutoConvert(bool flag);
    00357     bool getAutoConvert() const;
    00358
    00359     void read(FILE *stream) throw(ParseException);
    00360     void write(FILE *stream) const;
    00361
    00362     void readFile(const char *filename) throw(FileIOException, ParseException);
    00363     void writeFile(const char *filename) throw(FileIOException);
    00364
    00365     inline Setting & lookup(const std::string &path) const
    00366       throw(SettingNotFoundException)
    00367     { return(lookup(path.c_str())); }
    00368
    00369     Setting & lookup(const char *path) const
    00370       throw(SettingNotFoundException);
    00371
    00372     inline bool exists(const std::string & path) const throw()
    00373     { return(exists(path.c_str())); }
    00374
    00375     bool exists(const char *path) const throw();
    00376
    00377     bool lookupValue(const char *path, bool &value) const throw();
    00378     bool lookupValue(const char *path, long &value) const throw();
    00379     bool lookupValue(const char *path, unsigned long &value) const throw();
    00380     bool lookupValue(const char *path, int &value) const throw();
    00381     bool lookupValue(const char *path, unsigned int &value) const throw();
    00382     bool lookupValue(const char *path, long long &value) const throw();
    00383     bool lookupValue(const char *path, unsigned long long &value)
    00384       const throw();
    00385     bool lookupValue(const char *path, double &value) const throw();
    00386     bool lookupValue(const char *path, float &value) const throw();
    00387     bool lookupValue(const char *path, const char *&value) const throw();
    00388     bool lookupValue(const char *path, std::string &value) const throw();
    00389
    00390     inline bool lookupValue(const std::string &path, bool &value)
    00391       const throw()
    00392     { return(lookupValue(path.c_str(), value)); }
    00393
    00394     inline bool lookupValue(const std::string &path, long &value)
    00395       const throw()
    00396     { return(lookupValue(path.c_str(), value)); }
    00397
    00398     inline bool lookupValue(const std::string &path, unsigned long &value)
    00399       const throw()
    00400     { return(lookupValue(path.c_str(), value)); }
    00401
    00402     inline bool lookupValue(const std::string &path, int &value) const throw()
    00403     { return(lookupValue(path.c_str(), value)); }
    00404
    00405     inline bool lookupValue(const std::string &path, unsigned int &value)
    00406       const throw()
    00407     { return(lookupValue(path.c_str(), value)); }
    00408
    00409     inline bool lookupValue(const std::string &path, long long &value)
    00410       const throw()
    00411     { return(lookupValue(path.c_str(), value)); }
    00412
    00413     inline bool lookupValue(const std::string &path,
    00414                             unsigned long long &value) const throw()
    00415     { return(lookupValue(path.c_str(), value)); }
    00416
    00417     inline bool lookupValue(const std::string &path, double &value) const
    00418       throw()
    00419     { return(lookupValue(path.c_str(), value)); }
    00420
    00421     inline bool lookupValue(const std::string &path, float &value) const
    00422       throw()
    00423     { return(lookupValue(path.c_str(), value)); }
    00424
    00425     inline bool lookupValue(const std::string &path, const char *&value) const
    00426       throw()
    00427     { return(lookupValue(path.c_str(), value)); }
    00428
    00429     inline bool lookupValue(const std::string &path, std::string &value) const
    00430       throw()
    00431     { return(lookupValue(path.c_str(), value)); }
    00432
    00433     Setting & getRoot() const;
    00434   };
    00435
    00436 } // namespace libconfig
    00437
    00438 #endif // __libconfig_hpp