diff options
author | Jonathan McCrohan <jmccrohan@gmail.com> | 2011-12-01 22:54:16 +0000 |
---|---|---|
committer | Jonathan McCrohan <jmccrohan@gmail.com> | 2011-12-01 22:54:16 +0000 |
commit | 58bf1382be0cbcf3f9649286fd2719b789a1595f (patch) | |
tree | b73665275a2d44879a8230c913b1ef21a42e57da /libconfig.h | |
download | libconfig-58bf1382be0cbcf3f9649286fd2719b789a1595f.tar.gz |
Imported Upstream version 1.3.2upstream/1.3.2
Diffstat (limited to '')
-rw-r--r-- | libconfig.h | 266 | ||||
-rw-r--r-- | libconfig.h++ | 424 | ||||
-rw-r--r-- | libconfig.hh | 23 |
3 files changed, 713 insertions, 0 deletions
diff --git a/libconfig.h b/libconfig.h new file mode 100644 index 0000000..cf6c67a --- /dev/null +++ b/libconfig.h @@ -0,0 +1,266 @@ +/* ---------------------------------------------------------------------------- + libconfig - A library for processing structured configuration files + Copyright (C) 2005-2009 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_h +#define __libconfig_h + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) +#if defined(LIBCONFIG_STATIC) +#define LIBCONFIG_API +#elif defined(LIBCONFIG_EXPORTS) +#define LIBCONFIG_API __declspec(dllexport) +#else /* ! LIBCONFIG_EXPORTS */ +#define LIBCONFIG_API __declspec(dllimport) +#endif /* LIBCONFIG_STATIC */ +#else /* ! WIN32 */ +#define LIBCONFIG_API +#endif /* WIN32 */ + +#include <stdio.h> + +#define CONFIG_TYPE_NONE 0 +#define CONFIG_TYPE_GROUP 1 +#define CONFIG_TYPE_INT 2 +#define CONFIG_TYPE_INT64 3 +#define CONFIG_TYPE_FLOAT 4 +#define CONFIG_TYPE_STRING 5 +#define CONFIG_TYPE_BOOL 6 +#define CONFIG_TYPE_ARRAY 7 +#define CONFIG_TYPE_LIST 8 + +#define CONFIG_FORMAT_DEFAULT 0 +#define CONFIG_FORMAT_HEX 1 + +#define CONFIG_OPTION_AUTOCONVERT 0x01 + +#define CONFIG_TRUE (1) +#define CONFIG_FALSE (0) + +typedef union config_value_t +{ + long ival; + long long llval; + double fval; + char *sval; + struct config_list_t *list; +} config_value_t; + +typedef struct config_setting_t +{ + char *name; + short type; + short format; + config_value_t value; + struct config_setting_t *parent; + struct config_t *config; + void *hook; + unsigned int line; +} config_setting_t; + +typedef struct config_list_t +{ + unsigned int length; + unsigned int capacity; + config_setting_t **elements; +} config_list_t; + +typedef struct config_t +{ + config_setting_t *root; + void (*destructor)(void *); + int flags; + const char *error_text; + int error_line; +} 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_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_file(config_t *config, + const char *filename); +extern LIBCONFIG_API int config_write_file(config_t *config, + const char *filename); + +extern LIBCONFIG_API void config_set_destructor(config_t *config, + void (*destructor)(void *)); + +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( + const config_setting_t *setting); +extern LIBCONFIG_API long long config_setting_get_int64( + const config_setting_t *setting); +extern LIBCONFIG_API double config_setting_get_float( + const config_setting_t *setting); +extern LIBCONFIG_API int config_setting_get_bool( + const config_setting_t *setting); +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); +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( + const config_setting_t *setting, const char *name, double *value); +extern LIBCONFIG_API int config_setting_lookup_bool( + const config_setting_t *setting, const char *name, int *value); +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); +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, + double value); +extern LIBCONFIG_API int config_setting_set_bool(config_setting_t *setting, + int value); +extern LIBCONFIG_API int config_setting_set_string(config_setting_t *setting, + const char *value); + +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 long 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); +extern LIBCONFIG_API double config_setting_get_float_elem( + const config_setting_t *setting, int idx); +extern LIBCONFIG_API int config_setting_get_bool_elem( + const config_setting_t *setting, int idx); +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); +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( + config_setting_t *setting, int idx, double value); +extern LIBCONFIG_API config_setting_t *config_setting_set_bool_elem( + config_setting_t *setting, int idx, int value); +extern LIBCONFIG_API config_setting_t *config_setting_set_string_elem( + config_setting_t *setting, int idx, const char *value); + +#define /* int */ config_setting_type(/* const config_setting_t * */ S) \ + ((S)->type) + +#define /* int */ config_setting_is_group(/* const config_setting_t * */ S) \ + ((S)->type == CONFIG_TYPE_GROUP) +#define /* int */ config_setting_is_array(/* const config_setting_t * */ S) \ + ((S)->type == CONFIG_TYPE_ARRAY) +#define /* int */ config_setting_is_list(/* const config_setting_t * */ S) \ + ((S)->type == CONFIG_TYPE_LIST) + +#define /* int */ config_setting_is_aggregate( \ + /* 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)) + +#define /* const char * */ config_setting_name( \ + /* const config_setting_t * */ S) \ + ((S)->name) + +#define /* config_setting_t * */ config_setting_parent( \ + /* const config_setting_t * */ S) \ + ((S)->parent) + +#define /* int */ config_setting_is_root( \ + /* const config_setting_t * */ S) \ + ((S)->parent ? CONFIG_FALSE : CONFIG_TRUE) + +extern LIBCONFIG_API int config_setting_index(const config_setting_t *setting); + +extern LIBCONFIG_API int config_setting_length( + const config_setting_t *setting); +extern LIBCONFIG_API config_setting_t *config_setting_get_elem( + const config_setting_t *setting, unsigned int idx); + +extern LIBCONFIG_API config_setting_t *config_setting_get_member( + const config_setting_t *setting, const char *name); + +extern LIBCONFIG_API config_setting_t *config_setting_add( + config_setting_t *parent, const char *name, int type); +extern LIBCONFIG_API int config_setting_remove(config_setting_t *parent, + const char *name); +extern LIBCONFIG_API int config_setting_remove_elem(config_setting_t *parent, + unsigned int idx); +extern LIBCONFIG_API void config_setting_set_hook(config_setting_t *setting, + void *hook); + +#define config_setting_get_hook(S) ((S)->hook) + +extern LIBCONFIG_API config_setting_t *config_lookup(const config_t *config, + const char *path); + +extern LIBCONFIG_API int config_lookup_int(const config_t *config, + const char *path, long *value); +extern LIBCONFIG_API int config_lookup_int64(const config_t *config, + const char *path, + long long *value); +extern LIBCONFIG_API int config_lookup_float(const config_t *config, + const char *path, double *value); +extern LIBCONFIG_API int config_lookup_bool(const config_t *config, + const char *path, int *value); +extern LIBCONFIG_API int config_lookup_string(const config_t *config, + const char *path, + const char **value); + +#define /* config_setting_t * */ config_root_setting( \ + /* const config_t * */ C) \ + ((C)->root) + +#define /* unsigned short */ config_setting_source_line( \ + /* const config_t */ C) \ + ((C)->line) + +#define /* const char * */ config_error_text(/* const config_t * */ C) \ + ((C)->error_text) + +#define /* int */ config_error_line(/* const config_t * */ C) \ + ((C)->error_line) + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* __libconfig_h */ diff --git a/libconfig.h++ b/libconfig.h++ new file mode 100644 index 0000000..0a3b4b2 --- /dev/null +++ b/libconfig.h++ @@ -0,0 +1,424 @@ +/* ---------------------------------------------------------------------------- + libconfig - A library for processing structured configuration files + Copyright (C) 2005-2009 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 <string> +#include <map> + +namespace libconfig +{ + +#include <libconfig.h> + + class LIBCONFIG_API ConfigException : public std::exception { }; + + class Setting; // fwd decl + + class LIBCONFIG_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 LIBCONFIG_API SettingTypeException : public SettingException + { + friend class Config; + friend class Setting; + + const char *what() const throw(); + + private: + + SettingTypeException(const Setting &setting); + SettingTypeException(const Setting &setting, int idx); + SettingTypeException(const Setting &setting, const char *name); + }; + + class LIBCONFIG_API SettingNotFoundException : public SettingException + { + friend class Config; + friend class Setting; + + const char *what() const throw(); + + private: + + SettingNotFoundException(const Setting &setting, int idx); + SettingNotFoundException(const Setting &setting, const char *name); + SettingNotFoundException(const char *path); + }; + + class LIBCONFIG_API SettingNameException : public SettingException + { + friend class Config; + friend class Setting; + + const char *what() const throw(); + + private: + + SettingNameException(const Setting &setting, const char *name); + }; + + class LIBCONFIG_API FileIOException : public ConfigException + { + const char *what() const throw(); + }; + + class LIBCONFIG_API ParseException : public ConfigException + { + friend class Config; + + public: + + virtual ~ParseException() throw(); + + inline int getLine() throw() + { return(_line); } + + inline const char *getError() throw() + { return(_error); } + + const char *what() const throw(); + + private: + + ParseException(int line, const char *error); + + int _line; + const char *_error; + }; + + class LIBCONFIG_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 long() const throw(SettingTypeException); + operator unsigned long() const throw(SettingTypeException); + operator int() const throw(SettingTypeException); + operator unsigned int() 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); + + Setting & operator=(bool value) throw(SettingTypeException); + Setting & operator=(long value) throw(SettingTypeException); + Setting & operator=(int 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, long &value) const throw(); + bool lookupValue(const char *name, unsigned long &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, long &value) + const throw() + { return(lookupValue(name.c_str(), value)); } + + inline bool lookupValue(const std::string &name, unsigned long &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)); } + + inline unsigned int getSourceLine() const throw() + { return(config_setting_source_line(_setting)); } + }; + + class LIBCONFIG_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 read(FILE *stream) throw(ParseException); + void write(FILE *stream) const; + + 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, long &value) const throw(); + bool lookupValue(const char *path, unsigned long &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, long &value) const throw() + { return(lookupValue(path.c_str(), value)); } + + inline bool lookupValue(const std::string &path, unsigned long &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; + }; + +} // namespace libconfig + +#endif // __libconfig_hpp diff --git a/libconfig.hh b/libconfig.hh new file mode 100644 index 0000000..8b3b2d5 --- /dev/null +++ b/libconfig.hh @@ -0,0 +1,23 @@ +/* ---------------------------------------------------------------------------- + libconfig - A structured configuration file parsing library + Copyright (C) 2005-2008 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 Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + ---------------------------------------------------------------------------- +*/ + +#include <libconfig.h++> |