From 3933adef6a0832b8f140e344e42855c2e4dfb051 Mon Sep 17 00:00:00 2001 From: John Spray Date: Tue, 11 Jul 2017 07:00:54 -0400 Subject: [PATCH] common: reinstate config opt validators Signed-off-by: John Spray --- src/common/config.cc | 23 ++++---- src/common/options.cc | 103 +++++++++++++++++++++++++++++++----- src/common/options.h | 120 ++++++------------------------------------ 3 files changed, 116 insertions(+), 130 deletions(-) diff --git a/src/common/config.cc b/src/common/config.cc index 12a9c317e9f..0ab8e192f48 100644 --- a/src/common/config.cc +++ b/src/common/config.cc @@ -971,35 +971,31 @@ int md_config_t::_get_val_from_conf_file(const std::vector §io return -ENOENT; } -int md_config_t::set_val_impl(const std::string &val, const Option &opt, +int md_config_t::set_val_impl(const std::string &raw_val, const Option &opt, std::string *error_message) { assert(lock.is_locked()); - // TODO: hook validators back in -#if 0 + std::string val = raw_val; if (opt.validator) { - int r = opt.validator(&value, error_message); + int r = opt.validator(&val, error_message); if (r < 0) { return r; } } -#endif Option::value_t new_value; if (opt.type == Option::TYPE_INT) { - std::string err; - int64_t f = strict_si_cast(val.c_str(), &err); - if (!err.empty()) { + int64_t f = strict_si_cast(val.c_str(), error_message); + if (!error_message->empty()) { return -EINVAL; } new_value = f; } else if (opt.type == Option::TYPE_STR) { new_value = val; } else if (opt.type == Option::TYPE_FLOAT) { - std::string err; - double f = strict_strtod(val.c_str(), &err); - if (!err.empty()) { + double f = strict_strtod(val.c_str(), error_message); + if (!error_message->empty()) { return -EINVAL; } else { new_value = f; @@ -1010,9 +1006,8 @@ int md_config_t::set_val_impl(const std::string &val, const Option &opt, } else if (strcasecmp(val.c_str(), "true") == 0) { new_value = true; } else { - std::string err; - int b = strict_strtol(val.c_str(), 10, &err); - if (!err.empty()) { + int b = strict_strtol(val.c_str(), 10, error_message); + if (!error_message->empty()) { return -EINVAL; } new_value = !!b; diff --git a/src/common/options.cc b/src/common/options.cc index d0c5b2d3666..1a7f64e6d76 100644 --- a/src/common/options.cc +++ b/src/common/options.cc @@ -4,6 +4,11 @@ #include "acconfig.h" #include "options.h" +// Helpers for validators +#include "include/stringify.h" +#include +#include +#include const std::vector