diff options
Diffstat (limited to '')
-rw-r--r-- | lib/libconfig.h (renamed from libconfig.h) | 87 | ||||
-rw-r--r-- | lib/libconfig.h++ | 459 | ||||
-rw-r--r-- | lib/libconfig.hh (renamed from libconfig.hh) | 2 |
3 files changed, 531 insertions, 17 deletions
diff --git a/libconfig.h b/lib/libconfig.h index cf6c67a..cc079db 100644 --- a/libconfig.h +++ b/lib/libconfig.h @@ -1,6 +1,6 @@ /* ---------------------------------------------------------------------------- libconfig - A library for processing structured configuration files - Copyright (C) 2005-2009 Mark A Lindner + Copyright (C) 2005-2010 Mark A Lindner This file is part of libconfig. @@ -39,6 +39,10 @@ extern "C" { #define LIBCONFIG_API #endif /* WIN32 */ +#define LIBCONFIG_VER_MAJOR 1 +#define LIBCONFIG_VER_MINOR 4 +#define LIBCONFIG_VER_REVISION 8 + #include <stdio.h> #define CONFIG_TYPE_NONE 0 @@ -61,7 +65,7 @@ extern "C" { typedef union config_value_t { - long ival; + int ival; long long llval; double fval; char *sval; @@ -78,12 +82,19 @@ typedef struct config_setting_t struct config_t *config; void *hook; unsigned int line; + const char *file; } config_setting_t; +typedef enum +{ + CONFIG_ERR_NONE = 0, + CONFIG_ERR_FILE_IO = 1, + CONFIG_ERR_PARSE = 2 +} config_error_t; + typedef struct config_list_t { unsigned int length; - unsigned int capacity; config_setting_t **elements; } config_list_t; @@ -91,17 +102,29 @@ typedef struct config_t { config_setting_t *root; void (*destructor)(void *); - int flags; + unsigned short flags; + unsigned short tab_width; + short default_format; + const char *include_dir; const char *error_text; + const char *error_file; int error_line; + config_error_t error_type; + const char **filenames; + unsigned int num_filenames; } config_t; extern LIBCONFIG_API int config_read(config_t *config, FILE *stream); extern LIBCONFIG_API void config_write(const config_t *config, FILE *stream); +extern LIBCONFIG_API void config_set_default_format(config_t *config, + short format); + extern LIBCONFIG_API void config_set_auto_convert(config_t *config, int flag); extern LIBCONFIG_API int config_get_auto_convert(const config_t *config); +extern LIBCONFIG_API int config_read_string(config_t *config, const char *str); + extern LIBCONFIG_API int config_read_file(config_t *config, const char *filename); extern LIBCONFIG_API int config_write_file(config_t *config, @@ -109,11 +132,13 @@ extern LIBCONFIG_API int config_write_file(config_t *config, extern LIBCONFIG_API void config_set_destructor(config_t *config, void (*destructor)(void *)); +extern LIBCONFIG_API void config_set_include_dir(config_t *config, + const char *include_dir); extern LIBCONFIG_API void config_init(config_t *config); extern LIBCONFIG_API void config_destroy(config_t *config); -extern LIBCONFIG_API long config_setting_get_int( +extern LIBCONFIG_API int config_setting_get_int( const config_setting_t *setting); extern LIBCONFIG_API long long config_setting_get_int64( const config_setting_t *setting); @@ -125,7 +150,7 @@ extern LIBCONFIG_API const char *config_setting_get_string( const config_setting_t *setting); extern LIBCONFIG_API int config_setting_lookup_int( - const config_setting_t *setting, const char *name, long *value); + const config_setting_t *setting, const char *name, int *value); extern LIBCONFIG_API int config_setting_lookup_int64( const config_setting_t *setting, const char *name, long long *value); extern LIBCONFIG_API int config_setting_lookup_float( @@ -136,7 +161,7 @@ extern LIBCONFIG_API int config_setting_lookup_string( const config_setting_t *setting, const char *name, const char **value); extern LIBCONFIG_API int config_setting_set_int(config_setting_t *setting, - long value); + int value); extern LIBCONFIG_API int config_setting_set_int64(config_setting_t *setting, long long value); extern LIBCONFIG_API int config_setting_set_float(config_setting_t *setting, @@ -148,9 +173,10 @@ extern LIBCONFIG_API int config_setting_set_string(config_setting_t *setting, extern LIBCONFIG_API int config_setting_set_format(config_setting_t *setting, short format); -extern LIBCONFIG_API short config_setting_get_format(config_setting_t *setting); +extern LIBCONFIG_API short config_setting_get_format( + const config_setting_t *setting); -extern LIBCONFIG_API long config_setting_get_int_elem( +extern LIBCONFIG_API int config_setting_get_int_elem( const config_setting_t *setting, int idx); extern LIBCONFIG_API long long config_setting_get_int64_elem( const config_setting_t *setting, int idx); @@ -162,7 +188,7 @@ extern LIBCONFIG_API const char *config_setting_get_string_elem( const config_setting_t *setting, int idx); extern LIBCONFIG_API config_setting_t *config_setting_set_int_elem( - config_setting_t *setting, int idx, long value); + config_setting_t *setting, int idx, int value); extern LIBCONFIG_API config_setting_t *config_setting_set_int64_elem( config_setting_t *setting, int idx, long long value); extern LIBCONFIG_API config_setting_t *config_setting_set_float_elem( @@ -172,6 +198,9 @@ extern LIBCONFIG_API config_setting_t *config_setting_set_bool_elem( extern LIBCONFIG_API config_setting_t *config_setting_set_string_elem( config_setting_t *setting, int idx, const char *value); +#define /* const char * */ config_get_include_dir(/* const config_t * */ C) \ + ((C)->include_dir) + #define /* int */ config_setting_type(/* const config_setting_t * */ S) \ ((S)->type) @@ -186,12 +215,12 @@ extern LIBCONFIG_API config_setting_t *config_setting_set_string_elem( /* const config_setting_t * */ S) \ (((S)->type == CONFIG_TYPE_GROUP) || ((S)->type == CONFIG_TYPE_LIST) \ || ((S)->type == CONFIG_TYPE_ARRAY)) - + #define /* int */ config_setting_is_number(/* const config_setting_t * */ S) \ (((S)->type == CONFIG_TYPE_INT) \ || ((S)->type == CONFIG_TYPE_INT64) \ || ((S)->type == CONFIG_TYPE_FLOAT)) - + #define /* int */ config_setting_is_scalar(/* const config_setting_t * */ S) \ (((S)->type == CONFIG_TYPE_BOOL) || ((S)->type == CONFIG_TYPE_STRING) \ || config_setting_is_number(S)) @@ -231,9 +260,11 @@ extern LIBCONFIG_API void config_setting_set_hook(config_setting_t *setting, extern LIBCONFIG_API config_setting_t *config_lookup(const config_t *config, const char *path); +extern LIBCONFIG_API config_setting_t *config_lookup_from( + config_setting_t *setting, const char *path); extern LIBCONFIG_API int config_lookup_int(const config_t *config, - const char *path, long *value); + const char *path, int *value); extern LIBCONFIG_API int config_lookup_int64(const config_t *config, const char *path, long long *value); @@ -249,16 +280,40 @@ extern LIBCONFIG_API int config_lookup_string(const config_t *config, /* const config_t * */ C) \ ((C)->root) -#define /* unsigned short */ config_setting_source_line( \ - /* const config_t */ C) \ - ((C)->line) +#define /* void */ config_set_default_format(/* config_t * */ C, \ + /* short */ F) \ + (C)->default_format = (F) + +#define /* short */ config_get_default_format(/* config_t * */ C) \ + ((C)->default_format) + +#define /* void */ config_set_tab_width(/* config_t * */ C, \ + /* unsigned short */ W) \ + (C)->tab_width = ((W) & 0x0F) + +#define /* unsigned char */ config_get_tab_width(/* const config_t * */ C) \ + ((C)->tab_width) + +#define /* unsigned short */ config_setting_source_line( \ + /* const config_setting_t * */ S) \ + ((S)->line) + +#define /* const char */ config_setting_source_file( \ + /* const config_setting_t * */ S) \ + ((S)->file) #define /* const char * */ config_error_text(/* const config_t * */ C) \ ((C)->error_text) +#define /* const char * */ config_error_file(/* const config_t * */ C) \ + ((C)->error_file) + #define /* int */ config_error_line(/* const config_t * */ C) \ ((C)->error_line) +#define /* config_error_t */ config_error_type(/* const config_t * */ C) \ + ((C)->error_type) + #ifdef __cplusplus } #endif /* __cplusplus */ diff --git a/lib/libconfig.h++ b/lib/libconfig.h++ new file mode 100644 index 0000000..dfc7939 --- /dev/null +++ b/lib/libconfig.h++ @@ -0,0 +1,459 @@ +/* ---------------------------------------------------------------------------- + libconfig - A library for processing structured configuration files + Copyright (C) 2005-2010 Mark A Lindner + + This file is part of libconfig. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public License + as published by the Free Software Foundation; either version 2.1 of + the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, see + <http://www.gnu.org/licenses/>. + ---------------------------------------------------------------------------- +*/ + +#ifndef __libconfig_hpp +#define __libconfig_hpp + +#include <stdio.h> +#include <exception> +#include <string> + +#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) +#if defined(LIBCONFIGXX_STATIC) +#define LIBCONFIGXX_API +#elif defined(LIBCONFIGXX_EXPORTS) +#define LIBCONFIGXX_API __declspec(dllexport) +#else /* ! LIBCONFIGXX_EXPORTS */ +#define LIBCONFIGXX_API __declspec(dllimport) +#endif /* LIBCONFIGXX_STATIC */ +#else /* ! WIN32 */ +#define LIBCONFIGXX_API +#endif /* WIN32 */ + +#define LIBCONFIGXX_VER_MAJOR 1 +#define LIBCONFIGXX_VER_MINOR 4 +#define LIBCONFIGXX_VER_REVISION 8 + +struct config_t; // fwd decl +struct config_setting_t; // fwd decl + +namespace libconfig { + +class LIBCONFIGXX_API ConfigException : public std::exception { }; + +class Setting; // fwd decl + +class LIBCONFIGXX_API SettingException : public ConfigException +{ + friend class Config; + + public: + + SettingException(const SettingException &other); + SettingException& operator=(const SettingException &other); + + virtual ~SettingException() throw(); + + const char *getPath() const; + + virtual const char *what() const throw(); + + protected: + + SettingException(const Setting &setting); + SettingException(const Setting &setting, int idx); + SettingException(const Setting &setting, const char *name); + SettingException(const char *path); + + private: + + char *_path; +}; + +class LIBCONFIGXX_API SettingTypeException : public SettingException +{ + friend class Config; + friend class Setting; + + public: + + const char *what() const throw(); + + private: + + SettingTypeException(const Setting &setting); + SettingTypeException(const Setting &setting, int idx); + SettingTypeException(const Setting &setting, const char *name); +}; + +class LIBCONFIGXX_API SettingNotFoundException : public SettingException +{ + friend class Config; + friend class Setting; + + public: + + const char *what() const throw(); + + private: + + SettingNotFoundException(const Setting &setting, int idx); + SettingNotFoundException(const Setting &setting, const char *name); + SettingNotFoundException(const char *path); +}; + +class LIBCONFIGXX_API SettingNameException : public SettingException +{ + friend class Config; + friend class Setting; + + public: + + const char *what() const throw(); + + private: + + SettingNameException(const Setting &setting, const char *name); +}; + +class LIBCONFIGXX_API FileIOException : public ConfigException +{ + public: + + const char *what() const throw(); +}; + +class LIBCONFIGXX_API ParseException : public ConfigException +{ + friend class Config; + + public: + + ParseException(const ParseException &other); + + virtual ~ParseException() throw(); + + inline const char *getFile() const throw() + { return(_file); } + + inline int getLine() const throw() + { return(_line); } + + inline const char *getError() const throw() + { return(_error); } + + const char *what() const throw(); + + private: + + ParseException(const char *file, int line, const char *error); + + const char *_file; + int _line; + const char *_error; +}; + +class LIBCONFIGXX_API Setting +{ + friend class Config; + + public: + + enum Type + { + TypeNone = 0, + // scalar types + TypeInt, + TypeInt64, + TypeFloat, + TypeString, + TypeBoolean, + // aggregate types + TypeGroup, + TypeArray, + TypeList + }; + + enum Format + { + FormatDefault = 0, + FormatHex = 1 + }; + + private: + + config_setting_t *_setting; + Type _type; + Format _format; + + Setting(config_setting_t *setting); + + void assertType(Type type) const + throw(SettingTypeException); + static Setting & wrapSetting(config_setting_t *setting); + + Setting(const Setting& other); // not supported + Setting& operator=(const Setting& other); // not supported + + public: + + virtual ~Setting() throw(); + + inline Type getType() const throw() { return(_type); } + + inline Format getFormat() const throw() { return(_format); } + void setFormat(Format format) throw(); + + operator bool() const throw(SettingTypeException); + operator int() const throw(SettingTypeException); + operator unsigned int() const throw(SettingTypeException); + operator long() const throw(SettingTypeException); + operator unsigned long() const throw(SettingTypeException); + operator long long() const throw(SettingTypeException); + operator unsigned long long() const throw(SettingTypeException); + operator double() const throw(SettingTypeException); + operator float() const throw(SettingTypeException); + operator const char *() const throw(SettingTypeException); + operator std::string() const throw(SettingTypeException); + + inline const char *c_str() const throw(SettingTypeException) + { return operator const char*(); } + + Setting & operator=(bool value) throw(SettingTypeException); + Setting & operator=(int value) throw(SettingTypeException); + Setting & operator=(long value) throw(SettingTypeException); + Setting & operator=(const long long &value) throw(SettingTypeException); + Setting & operator=(const double &value) throw(SettingTypeException); + Setting & operator=(float value) throw(SettingTypeException); + Setting & operator=(const char *value) throw(SettingTypeException); + Setting & operator=(const std::string &value) throw(SettingTypeException); + + Setting & operator[](const char *key) const + throw(SettingTypeException, SettingNotFoundException); + + inline Setting & operator[](const std::string &key) const + throw(SettingTypeException, SettingNotFoundException) + { return(operator[](key.c_str())); } + + Setting & operator[](int index) const + throw(SettingTypeException, SettingNotFoundException); + + bool lookupValue(const char *name, bool &value) const throw(); + bool lookupValue(const char *name, int &value) const throw(); + bool lookupValue(const char *name, unsigned int &value) const throw(); + bool lookupValue(const char *name, long long &value) const throw(); + bool lookupValue(const char *name, unsigned long long &value) + const throw(); + bool lookupValue(const char *name, double &value) const throw(); + bool lookupValue(const char *name, float &value) const throw(); + bool lookupValue(const char *name, const char *&value) const throw(); + bool lookupValue(const char *name, std::string &value) const throw(); + + inline bool lookupValue(const std::string &name, bool &value) + const throw() + { return(lookupValue(name.c_str(), value)); } + + inline bool lookupValue(const std::string &name, int &value) + const throw() + { return(lookupValue(name.c_str(), value)); } + + inline bool lookupValue(const std::string &name, unsigned int &value) + const throw() + { return(lookupValue(name.c_str(), value)); } + + inline bool lookupValue(const std::string &name, long long &value) + const throw() + { return(lookupValue(name.c_str(), value)); } + + inline bool lookupValue(const std::string &name, + unsigned long long &value) const throw() + { return(lookupValue(name.c_str(), value)); } + + inline bool lookupValue(const std::string &name, double &value) const + throw() + { return(lookupValue(name.c_str(), value)); } + + inline bool lookupValue(const std::string &name, float &value) const + throw() + { return(lookupValue(name.c_str(), value)); } + + inline bool lookupValue(const std::string &name, const char *&value) const + throw() + { return(lookupValue(name.c_str(), value)); } + + inline bool lookupValue(const std::string &name, std::string &value) const + throw() + { return(lookupValue(name.c_str(), value)); } + + void remove(const char *name) + throw(SettingTypeException, SettingNotFoundException); + + inline void remove(const std::string & name) + throw(SettingTypeException, SettingNotFoundException) + { remove(name.c_str()); } + + void remove(unsigned int idx) + throw(SettingTypeException, SettingNotFoundException); + + inline Setting & add(const std::string & name, Type type) + throw(SettingNameException, SettingTypeException) + { return(add(name.c_str(), type)); } + + Setting & add(const char *name, Type type) + throw(SettingNameException, SettingTypeException); + + Setting & add(Type type) throw(SettingTypeException); + + inline bool exists(const std::string &name) const throw() + { return(exists(name.c_str())); } + + bool exists(const char *name) const throw(); + + int getLength() const throw(); + const char *getName() const throw(); + std::string getPath() const; + int getIndex() const throw(); + + const Setting & getParent() const throw(SettingNotFoundException); + Setting & getParent() throw(SettingNotFoundException); + + bool isRoot() const throw(); + + inline bool isGroup() const throw() + { return(_type == TypeGroup); } + + inline bool isArray() const throw() + { return(_type == TypeArray); } + + inline bool isList() const throw() + { return(_type == TypeList); } + + inline bool isAggregate() const throw() + { return(_type >= TypeGroup); } + + inline bool isScalar() const throw() + { return((_type > TypeNone) && (_type < TypeGroup)); } + + inline bool isNumber() const throw() + { return((_type == TypeInt) || (_type == TypeInt64) + || (_type == TypeFloat)); } + + unsigned int getSourceLine() const throw(); + const char *getSourceFile() const throw(); +}; + +class LIBCONFIGXX_API Config +{ + private: + + config_t *_config; + + static void ConfigDestructor(void *arg); + Config(const Config& other); // not supported + Config& operator=(const Config& other); // not supported + + public: + + Config(); + virtual ~Config(); + + void setAutoConvert(bool flag); + bool getAutoConvert() const; + + void setDefaultFormat(Setting::Format format); + inline Setting::Format getDefaultFormat() const + { return(_defaultFormat); } + + void setTabWidth(unsigned short width) throw(); + unsigned short getTabWidth() const throw(); + + void setIncludeDir(const char *includeDir) throw(); + const char *getIncludeDir() const throw(); + + void read(FILE *stream) throw(ParseException); + void write(FILE *stream) const; + + void readString(const char *str) throw(ParseException); + inline void readString(const std::string &str) throw(ParseException) + { return(readString(str.c_str())); } + + void readFile(const char *filename) throw(FileIOException, ParseException); + void writeFile(const char *filename) throw(FileIOException); + + inline Setting & lookup(const std::string &path) const + throw(SettingNotFoundException) + { return(lookup(path.c_str())); } + + Setting & lookup(const char *path) const throw(SettingNotFoundException); + + inline bool exists(const std::string & path) const throw() + { return(exists(path.c_str())); } + + bool exists(const char *path) const throw(); + + bool lookupValue(const char *path, bool &value) const throw(); + bool lookupValue(const char *path, int &value) const throw(); + bool lookupValue(const char *path, unsigned int &value) const throw(); + bool lookupValue(const char *path, long long &value) const throw(); + bool lookupValue(const char *path, unsigned long long &value) + const throw(); + bool lookupValue(const char *path, double &value) const throw(); + bool lookupValue(const char *path, float &value) const throw(); + bool lookupValue(const char *path, const char *&value) const throw(); + bool lookupValue(const char *path, std::string &value) const throw(); + + inline bool lookupValue(const std::string &path, bool &value) const throw() + { return(lookupValue(path.c_str(), value)); } + + inline bool lookupValue(const std::string &path, int &value) const throw() + { return(lookupValue(path.c_str(), value)); } + + inline bool lookupValue(const std::string &path, unsigned int &value) + const throw() + { return(lookupValue(path.c_str(), value)); } + + inline bool lookupValue(const std::string &path, long long &value) + const throw() + { return(lookupValue(path.c_str(), value)); } + + inline bool lookupValue(const std::string &path, + unsigned long long &value) const throw() + { return(lookupValue(path.c_str(), value)); } + + inline bool lookupValue(const std::string &path, double &value) + const throw() + { return(lookupValue(path.c_str(), value)); } + + inline bool lookupValue(const std::string &path, float &value) + const throw() + { return(lookupValue(path.c_str(), value)); } + + inline bool lookupValue(const std::string &path, const char *&value) + const throw() + { return(lookupValue(path.c_str(), value)); } + + inline bool lookupValue(const std::string &path, std::string &value) + const throw() + { return(lookupValue(path.c_str(), value)); } + + Setting & getRoot() const; + + private: + + Setting::Format _defaultFormat; + + void handleError() const; +}; + +} // namespace libconfig + +#endif // __libconfig_hpp diff --git a/libconfig.hh b/lib/libconfig.hh index 8b3b2d5..6883a31 100644 --- a/libconfig.hh +++ b/lib/libconfig.hh @@ -1,6 +1,6 @@ /* ---------------------------------------------------------------------------- libconfig - A structured configuration file parsing library - Copyright (C) 2005-2008 Mark A Lindner + Copyright (C) 2005-2010 Mark A Lindner This file is part of libconfig. |