aboutsummaryrefslogtreecommitdiffstats
path: root/lib/libconfigcpp.c++
diff options
context:
space:
mode:
authorJonathan McCrohan <jmccrohan@gmail.com>2011-12-01 22:56:23 +0000
committerJonathan McCrohan <jmccrohan@gmail.com>2011-12-01 22:56:23 +0000
commit429e46051dba814e7d6c74368eb1bba550222cbe (patch)
treeed1dd43cd23c69f156aae2165006a16a66262cef /lib/libconfigcpp.c++
parent58bf1382be0cbcf3f9649286fd2719b789a1595f (diff)
downloadlibconfig-429e46051dba814e7d6c74368eb1bba550222cbe.tar.gz
Imported Upstream version 1.4.8upstream/1.4.8
Diffstat (limited to '')
-rw-r--r--lib/libconfigcpp.c++ (renamed from libconfigcpp.c++)239
1 files changed, 147 insertions, 92 deletions
diff --git a/libconfigcpp.c++ b/lib/libconfigcpp.c++
index 82ad56e..846c067 100644
--- a/libconfigcpp.c++
+++ b/lib/libconfigcpp.c++
@@ -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.
@@ -27,17 +27,27 @@
#endif
#include "wincompat.h"
-
-using namespace libconfig;
+#include "libconfig.h"
#include <cstring>
#include <cstdlib>
#include <sstream>
+namespace libconfig {
+
+// ---------------------------------------------------------------------------
+
+ParseException::ParseException(const char *file, int line, const char *error)
+ : _file(file ? ::strdup(file) : NULL), _line(line), _error(error)
+{
+}
+
// ---------------------------------------------------------------------------
-ParseException::ParseException(int line, const char *error)
- : _line(line), _error(error)
+ParseException::ParseException(const ParseException &other)
+ : _file(other._file ? ::strdup(other._file) : NULL),
+ _line(other._line),
+ _error(other._error)
{
}
@@ -45,6 +55,7 @@ ParseException::ParseException(int line, const char *error)
ParseException::~ParseException() throw()
{
+ ::free((void *)_file);
}
// ---------------------------------------------------------------------------
@@ -290,46 +301,117 @@ void Config::ConfigDestructor(void *arg)
// ---------------------------------------------------------------------------
Config::Config()
+ : _defaultFormat(Setting::FormatDefault)
{
- config_init(& _config);
- config_set_destructor(& _config, ConfigDestructor);
+ _config = new config_t;
+ config_init(_config);
+ config_set_destructor(_config, ConfigDestructor);
}
// ---------------------------------------------------------------------------
Config::~Config()
{
- config_destroy(& _config);
+ config_destroy(_config);
+ delete _config;
}
// ---------------------------------------------------------------------------
void Config::setAutoConvert(bool flag)
{
- config_set_auto_convert(& _config, (flag ? CONFIG_TRUE : CONFIG_FALSE));
+ config_set_auto_convert(_config, (flag ? CONFIG_TRUE : CONFIG_FALSE));
}
// ---------------------------------------------------------------------------
bool Config::getAutoConvert() const
{
- return(config_get_auto_convert(& _config) != CONFIG_FALSE);
+ return(config_get_auto_convert(_config) != CONFIG_FALSE);
+}
+
+// ---------------------------------------------------------------------------
+
+void Config::setDefaultFormat(Setting::Format format)
+{
+ if(format == Setting::FormatHex)
+ _defaultFormat = Setting::FormatHex;
+ else
+ _defaultFormat = Setting::FormatDefault;
+
+ config_set_default_format(_config, static_cast<short>(_defaultFormat));
+}
+
+// ---------------------------------------------------------------------------
+
+void Config::setTabWidth(unsigned short width) throw()
+{
+ config_set_tab_width(_config, width);
+}
+
+// ---------------------------------------------------------------------------
+
+unsigned short Config::getTabWidth() const throw()
+{
+ return(config_get_tab_width(_config));
+}
+
+// ---------------------------------------------------------------------------
+
+void Config::setIncludeDir(const char *includeDir) throw()
+{
+ config_set_include_dir(_config, includeDir);
+}
+
+// ---------------------------------------------------------------------------
+
+const char *Config::getIncludeDir() const throw()
+{
+ return(config_get_include_dir(_config));
+}
+
+// ---------------------------------------------------------------------------
+
+void Config::handleError() const
+{
+ switch(config_error_type(_config))
+ {
+ case CONFIG_ERR_NONE:
+ break;
+
+ case CONFIG_ERR_PARSE:
+ throw ParseException(config_error_file(_config),
+ config_error_line(_config),
+ config_error_text(_config));
+ break;
+
+ case CONFIG_ERR_FILE_IO:
+ default:
+ throw FileIOException();
+ }
}
// ---------------------------------------------------------------------------
void Config::read(FILE *stream) throw(ParseException)
{
- if(! config_read(& _config, stream))
- throw ParseException(config_error_line(& _config),
- config_error_text(& _config));
+ if(! config_read(_config, stream))
+ handleError();
+}
+
+// ---------------------------------------------------------------------------
+
+void Config::readString(const char *str) throw(ParseException)
+{
+ if(! config_read_string(_config, str))
+ handleError();
}
// ---------------------------------------------------------------------------
void Config::write(FILE *stream) const
{
- config_write(& _config, stream);
+ config_write(_config, stream);
}
// ---------------------------------------------------------------------------
@@ -337,27 +419,16 @@ void Config::write(FILE *stream) const
void Config::readFile(const char *filename) throw(FileIOException,
ParseException)
{
- FILE *f = fopen(filename, "rt");
- if(f == NULL)
- throw FileIOException();
- try
- {
- read(f);
- fclose(f);
- }
- catch(ParseException& p)
- {
- fclose(f);
- throw p;
- }
+ if(! config_read_file(_config, filename))
+ handleError();
}
// ---------------------------------------------------------------------------
void Config::writeFile(const char *filename) throw(FileIOException)
{
- if(! config_write_file(& _config, filename))
- throw FileIOException();
+ if(! config_write_file(_config, filename))
+ handleError();
}
// ---------------------------------------------------------------------------
@@ -365,7 +436,7 @@ void Config::writeFile(const char *filename) throw(FileIOException)
Setting & Config::lookup(const char *path) const
throw(SettingNotFoundException)
{
- config_setting_t *s = config_lookup(& _config, path);
+ config_setting_t *s = config_lookup(_config, path);
if(! s)
throw SettingNotFoundException(path);
@@ -376,7 +447,7 @@ Setting & Config::lookup(const char *path) const
bool Config::exists(const char *path) const throw()
{
- config_setting_t *s = config_lookup(& _config, path);
+ config_setting_t *s = config_lookup(_config, path);
return(s != NULL);
}
@@ -404,20 +475,6 @@ bool Config::lookupValue(const char *path, bool &value) const throw()
// ---------------------------------------------------------------------------
-bool Config::lookupValue(const char *path, long &value) const throw()
-{
- CONFIG_LOOKUP_NO_EXCEPTIONS(path, long, value);
-}
-
-// ---------------------------------------------------------------------------
-
-bool Config::lookupValue(const char *path, unsigned long &value) const throw()
-{
- CONFIG_LOOKUP_NO_EXCEPTIONS(path, unsigned long, value);
-}
-
-// ---------------------------------------------------------------------------
-
bool Config::lookupValue(const char *path, int &value) const throw()
{
CONFIG_LOOKUP_NO_EXCEPTIONS(path, int, value);
@@ -477,7 +534,7 @@ bool Config::lookupValue(const char *path, std::string &value) const throw()
Setting & Config::getRoot() const
{
- return(Setting::wrapSetting(config_root_setting(& _config)));
+ return(Setting::wrapSetting(config_root_setting(_config)));
}
// ---------------------------------------------------------------------------
@@ -558,6 +615,8 @@ void Setting::setFormat(Format format) throw()
}
else
_format = FormatDefault;
+
+ config_setting_set_format(_setting, static_cast<short>(_format));
}
// ---------------------------------------------------------------------------
@@ -571,7 +630,7 @@ Setting::operator bool() const throw(SettingTypeException)
// ---------------------------------------------------------------------------
-Setting::operator long() const throw(SettingTypeException)
+Setting::operator int() const throw(SettingTypeException)
{
assertType(TypeInt);
@@ -580,40 +639,36 @@ Setting::operator long() const throw(SettingTypeException)
// ---------------------------------------------------------------------------
-Setting::operator unsigned long() const throw(SettingTypeException)
+Setting::operator unsigned int() const throw(SettingTypeException)
{
assertType(TypeInt);
- long v = config_setting_get_int(_setting);
+ int v = config_setting_get_int(_setting);
if(v < 0)
v = 0;
- return(static_cast<unsigned long>(v));
+ return(static_cast<unsigned int>(v));
}
// ---------------------------------------------------------------------------
-Setting::operator int() const throw(SettingTypeException)
+Setting::operator long() const throw(SettingTypeException)
{
- assertType(TypeInt);
-
- // may cause loss of precision:
- return(static_cast<int>(config_setting_get_int(_setting)));
+ if(sizeof(long) == sizeof(long long))
+ return operator long long();
+ else
+ return operator int();
}
// ---------------------------------------------------------------------------
-Setting::operator unsigned int() const throw(SettingTypeException)
+Setting::operator unsigned long() const throw(SettingTypeException)
{
- assertType(TypeInt);
-
- long v = config_setting_get_int(_setting);
-
- if(v < 0)
- v = 0;
-
- return(static_cast<unsigned int>(v));
+ if(sizeof(long) == sizeof(long long))
+ return operator unsigned long long();
+ else
+ return operator unsigned int();
}
// ---------------------------------------------------------------------------
@@ -695,7 +750,7 @@ Setting & Setting::operator=(bool value) throw(SettingTypeException)
// ---------------------------------------------------------------------------
-Setting & Setting::operator=(long value) throw(SettingTypeException)
+Setting & Setting::operator=(int value) throw(SettingTypeException)
{
assertType(TypeInt);
@@ -706,15 +761,12 @@ Setting & Setting::operator=(long value) throw(SettingTypeException)
// ---------------------------------------------------------------------------
-Setting & Setting::operator=(int value) throw(SettingTypeException)
+Setting & Setting::operator=(long value) throw(SettingTypeException)
{
- assertType(TypeInt);
-
- long cvalue = static_cast<long>(value);
-
- config_setting_set_int(_setting, cvalue);
-
- return(*this);
+ if(sizeof(long) == sizeof(long long))
+ return(operator=(static_cast<long long>(value)));
+ else
+ return(operator=(static_cast<int>(value)));
}
// ---------------------------------------------------------------------------
@@ -830,21 +882,6 @@ bool Setting::lookupValue(const char *name, bool &value) const throw()
// ---------------------------------------------------------------------------
-bool Setting::lookupValue(const char *name, long &value) const throw()
-{
- SETTING_LOOKUP_NO_EXCEPTIONS(name, long, value);
-}
-
-// ---------------------------------------------------------------------------
-
-bool Setting::lookupValue(const char *name, unsigned long &value)
- const throw()
-{
- SETTING_LOOKUP_NO_EXCEPTIONS(name, unsigned long, value);
-}
-
-// ---------------------------------------------------------------------------
-
bool Setting::lookupValue(const char *name, int &value) const throw()
{
SETTING_LOOKUP_NO_EXCEPTIONS(name, int, value);
@@ -852,7 +889,8 @@ bool Setting::lookupValue(const char *name, int &value) const throw()
// ---------------------------------------------------------------------------
-bool Setting::lookupValue(const char *name, unsigned int &value) const throw()
+bool Setting::lookupValue(const char *name, unsigned int &value)
+ const throw()
{
SETTING_LOOKUP_NO_EXCEPTIONS(name, unsigned int, value);
}
@@ -963,6 +1001,20 @@ Setting & Setting::getParent() throw(SettingNotFoundException)
// ---------------------------------------------------------------------------
+unsigned int Setting::getSourceLine() const throw()
+{
+ return(config_setting_source_line(_setting));
+}
+
+// ---------------------------------------------------------------------------
+
+const char *Setting::getSourceFile() const throw()
+{
+ return(config_setting_source_file(_setting));
+}
+
+// ---------------------------------------------------------------------------
+
bool Setting::isRoot() const throw()
{
return(config_setting_is_root(_setting));
@@ -1037,8 +1089,8 @@ Setting & Setting::add(Setting::Type type) throw(SettingTypeException)
}
else
{
- if((type != TypeInt) && (type != TypeFloat) && (type != TypeString)
- && (type != TypeBoolean))
+ if((type != TypeInt) && (type != TypeInt64) && (type != TypeFloat)
+ && (type != TypeString) && (type != TypeBoolean))
throw SettingTypeException(*this, idx);
}
}
@@ -1085,7 +1137,7 @@ void Setting::assertType(Setting::Type type) const throw(SettingTypeException)
if(type != _type)
{
if(!(isNumber() && config_get_auto_convert(_setting->config)
- && ((type == TypeInt) || (type == TypeFloat))))
+ && ((type == TypeInt) || (type == TypeInt64) || (type == TypeFloat))))
throw SettingTypeException(*this);
}
}
@@ -1109,4 +1161,7 @@ Setting & Setting::wrapSetting(config_setting_t *s)
}
// ---------------------------------------------------------------------------
+
+}; // namespace libconfig
+
// eof