This commit is contained in:
Jon Daniel 2012-05-01 19:04:33 +00:00
parent 62c312e4f3
commit 8eea6a8b9b
7 changed files with 184 additions and 5 deletions

View File

@ -34,7 +34,7 @@
static lg::log_domain log_config("config");
#define ERR_CF LOG_STREAM(err, log_config)
#define DBG_CF LOG_STREAM(debug, log_config)
config::attribute_value::attribute_value()
: value()
{
@ -63,10 +63,21 @@ config::attribute_value &config::attribute_value::operator=(bool v)
config::attribute_value &config::attribute_value::operator=(int v)
{
value = double(v);
value = v;
return *this;
}
config::attribute_value &config::attribute_value::operator=(size_t v)
{
value = v;
return *this;
}
config::attribute_value &config::attribute_value::operator=(time_t v)
{
value = v;
return *this;
}
config::attribute_value &config::attribute_value::operator=(double v)
{
value = v;
@ -103,13 +114,21 @@ bool config::attribute_value::to_bool(bool def) const
int config::attribute_value::to_int(int def) const
{
if (const double *p = boost::get<const double>(&value)) return int(*p);
const int* i = boost::get<const int>(&value);
if(i != NULL)
{
return *i;
}
return def;
}
double config::attribute_value::to_double(double def) const
{
if (const double *p = boost::get<const double>(&value)) return *p;
const double* d = boost::get<const double>(&value);;
if(d != NULL)
{
return *d;;
}
return def;
}
@ -124,6 +143,12 @@ struct config_attribute_str_visitor : boost::static_visitor<std::string>
}
std::string operator()(double d) const
{ return str_cast(d); }
std::string operator()(size_t s) const
{ return str_cast(s); }
std::string operator()(int i) const
{ return str_cast(i); }
std::string operator()(time_t t) const
{ return str_cast(t); }
std::string operator()(std::string const &s) const
{ return s; }
std::string operator()(t_string const &s) const

View File

@ -172,7 +172,7 @@ public:
*/
class attribute_value
{
typedef boost::variant<boost::blank, bool, double, std::string, t_string> value_type;
typedef boost::variant<boost::blank, bool, int, time_t, size_t,double, std::string, t_string> value_type;
value_type value;
public:
@ -188,6 +188,8 @@ public:
attribute_value &operator=(bool v);
attribute_value &operator=(int v);
attribute_value &operator=(double v);
attribute_value &operator=(size_t v);
attribute_value &operator=(time_t v);
attribute_value &operator=(const char *v)
{ return operator=(std::string(v)); }

View File

@ -886,6 +886,9 @@ file_tree_checksum::file_tree_checksum(const config& cfg) :
sum_size(lexical_cast_default<size_t>(cfg["size"])),
modified(lexical_cast_default<time_t>(cfg["modified"]))
{
ERR_FS << "nfiles: " << nfiles << std::endl;
ERR_FS << "sum_ize: " << sum_size << std::endl;
ERR_FS << "modified: " << modified << std::endl;
}
void file_tree_checksum::write(config& cfg) const

View File

@ -177,6 +177,12 @@ struct luaW_pushscalar_visitor : boost::static_visitor<>
{ lua_pushboolean(L, b); }
void operator()(double d) const
{ lua_pushnumber(L, d); }
void operator()(size_t s) const
{ lua_pushnumber(L,s); }
void operator()(time_t t) const
{ lua_pushnumber(L,t); }
void operator()(int i) const
{ lua_pushnumber(L,i); }
void operator()(std::string const &s) const
{ lua_pushstring(L, s.c_str()); }
void operator()(t_string const &s) const

View File

@ -424,6 +424,12 @@ struct write_key_val_visitor : boost::static_visitor<void>
{ out_ << (b ? "yes" : "no"); }
void operator()(double d) const
{ int i = d; if (d == i) out_ << i; else out_ << d; }
void operator()(size_t s) const
{ out_ << s; }
void operator()(time_t t) const
{ out_ << t; }
void operator()(int i) const
{ out_ << i; }
void operator()(std::string const &s) const
{ out_ << '"' << escaped_string(s) << '"'; }
void operator()(t_string const &s) const;

View File

@ -21,7 +21,120 @@
#include "util.hpp"
#include <cstdlib>
template<>
size_t lexical_cast<size_t, const std::string&>(const std::string& a)
{
char* endptr;
size_t res = strtoul(a.c_str(), &endptr, 10);
if (a.empty() || *endptr != '\0') {
throw bad_lexical_cast();
} else {
return res;
}
}
template<>
size_t lexical_cast<size_t, const char*>(const char* a)
{
char* endptr;
size_t res = strtoul(a, &endptr, 10);
if (*a == '\0' || *endptr != '\0') {
throw bad_lexical_cast();
} else {
return res;
}
}
template<>
size_t lexical_cast_default<size_t, const std::string&>(const std::string& a, size_t def)
{
if(a.empty()) {
return def;
}
char* endptr;
size_t res = strtoul(a.c_str(), &endptr, 10);
if (*endptr != '\0') {
return def;
} else {
return res;
}
}
template<>
size_t lexical_cast_default<size_t, const char*>(const char* a, size_t def)
{
if(*a == '\0') {
return def;
}
char* endptr;
size_t res = strtoul(a, &endptr, 10);
if (*endptr != '\0') {
return def;
} else {
return res;
}
}
template<>
time_t lexical_cast<time_t, const std::string&>(const std::string& a)
{
char* endptr;
time_t res = strtol(a.c_str(), &endptr, 10);
if (a.empty() || *endptr != '\0') {
throw bad_lexical_cast();
} else {
return res;
}
}
template<>
time_t lexical_cast<time_t, const char*>(const char* a)
{
char* endptr;
time_t res = strtol(a, &endptr, 10);
if (*a == '\0' || *endptr != '\0') {
throw bad_lexical_cast();
} else {
return res;
}
}
template<>
time_t lexical_cast_default<time_t, const std::string&>(const std::string& a, time_t def)
{
if(a.empty()) {
return def;
}
char* endptr;
time_t res = strtol(a.c_str(), &endptr, 10);
if (*endptr != '\0') {
return def;
} else {
return res;
}
}
template<>
time_t lexical_cast_default<time_t, const char*>(const char* a, time_t def)
{
if(*a == '\0') {
return def;
}
char* endptr;
time_t res = strtol(a, &endptr, 10);
if (*endptr != '\0') {
return def;
} else {
return res;
}
}
template<>
int lexical_cast<int, const std::string&>(const std::string& a)
{

View File

@ -93,6 +93,30 @@ To lexical_cast_default(From a, To def=To())
}
}
template<>
size_t lexical_cast<size_t, const std::string&>(const std::string& a);
template<>
size_t lexical_cast<size_t, const char*>(const char* a);
template<>
size_t lexical_cast_default<size_t, const std::string&>(const std::string& a, size_t def);
template<>
size_t lexical_cast_default<size_t, const char*>(const char* a, size_t def);
template<>
time_t lexical_cast<time_t, const std::string&>(const std::string& a);
template<>
time_t lexical_cast<time_t, const char*>(const char* a);
template<>
time_t lexical_cast_default<time_t, const std::string&>(const std::string& a, time_t def);
template<>
time_t lexical_cast_default<time_t, const char*>(const char* a, time_t def);
template<>
int lexical_cast<int, const std::string&>(const std::string& a);