From ffe6819966ea8adeb743e12324807983b22e2b9b Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 17 Jul 2018 22:18:15 +0200 Subject: [PATCH] config/Global: move value parser code to struct ConfigData --- src/config/Data.cxx | 85 +++++++++++++++++++++++++++++++++++++++++++ src/config/Data.hxx | 22 +++++++++++ src/config/Global.cxx | 64 +++----------------------------- 3 files changed, 112 insertions(+), 59 deletions(-) diff --git a/src/config/Data.cxx b/src/config/Data.cxx index 6ab5f20ba..2c1967f07 100644 --- a/src/config/Data.cxx +++ b/src/config/Data.cxx @@ -21,9 +21,13 @@ #include "Data.hxx" #include "Param.hxx" #include "Block.hxx" +#include "Parser.hxx" +#include "fs/AllocatedPath.hxx" #include "util/RuntimeError.hxx" #include "util/StringAPI.hxx" +#include + void ConfigData::Clear() { @@ -38,6 +42,87 @@ ConfigData::Clear() } } +const char * +ConfigData::GetString(ConfigOption option, + const char *default_value) const noexcept +{ + const auto *param = GetParam(option); + if (param == nullptr) + return default_value; + + return param->value.c_str(); +} + +AllocatedPath +ConfigData::GetPath(ConfigOption option) const +{ + const auto *param = GetParam(option); + if (param == nullptr) + return nullptr; + + return param->GetPath(); +} + +unsigned +ConfigData::GetUnsigned(ConfigOption option, unsigned default_value) const +{ + const auto *param = GetParam(option); + long value; + char *endptr; + + if (param == nullptr) + return default_value; + + const char *const s = param->value.c_str(); + value = strtol(s, &endptr, 0); + if (endptr == s || *endptr != 0 || value < 0) + throw FormatRuntimeError("Not a valid non-negative number in line %i", + param->line); + + return (unsigned)value; +} + +unsigned +ConfigData::GetPositive(ConfigOption option, unsigned default_value) const +{ + const auto *param = GetParam(option); + long value; + char *endptr; + + if (param == nullptr) + return default_value; + + const char *const s = param->value.c_str(); + value = strtol(s, &endptr, 0); + if (endptr == s || *endptr != 0) + throw FormatRuntimeError("Not a valid number in line %i", + param->line); + + if (value <= 0) + throw FormatRuntimeError("Not a positive number in line %i", + param->line); + + return (unsigned)value; +} + +bool +ConfigData::GetBool(ConfigOption option, bool default_value) const +{ + const auto *param = GetParam(option); + bool success, value; + + if (param == nullptr) + return default_value; + + success = get_bool(param->value.c_str(), &value); + if (!success) + throw FormatRuntimeError("Expected boolean value (yes, true, 1) or " + "(no, false, 0) on line %i\n", + param->line); + + return value; +} + const ConfigBlock * ConfigData::FindBlock(ConfigBlockOption option, const char *key, const char *value) const diff --git a/src/config/Data.hxx b/src/config/Data.hxx index cee34f24e..8c7a1e5fc 100644 --- a/src/config/Data.hxx +++ b/src/config/Data.hxx @@ -26,6 +26,7 @@ struct ConfigParam; struct ConfigBlock; +class AllocatedPath; struct ConfigData { std::array params{{nullptr}}; @@ -38,6 +39,27 @@ struct ConfigData { return params[size_t(option)]; } + gcc_pure + const char *GetString(ConfigOption option, + const char *default_value=nullptr) const noexcept; + + /** + * Returns an optional configuration variable which contains an + * absolute path. If there is a tilde prefix, it is expanded. + * Returns nullptr if the value is not present. + * + * Throws #std::runtime_error on error. + */ + AllocatedPath GetPath(ConfigOption option) const; + + unsigned GetUnsigned(ConfigOption option, + unsigned default_value) const; + + unsigned GetPositive(ConfigOption option, + unsigned default_value) const; + + bool GetBool(ConfigOption option, bool default_value) const; + gcc_pure const ConfigBlock *GetBlock(ConfigBlockOption option) const noexcept { return blocks[size_t(option)]; diff --git a/src/config/Global.cxx b/src/config/Global.cxx index 187c8837f..efea9e24e 100644 --- a/src/config/Global.cxx +++ b/src/config/Global.cxx @@ -19,7 +19,6 @@ #include "config.h" #include "Global.hxx" -#include "Parser.hxx" #include "Data.hxx" #include "Param.hxx" #include "Block.hxx" @@ -31,8 +30,6 @@ #include "util/RuntimeError.hxx" #include "Log.hxx" -#include - static ConfigData config_data; void config_global_finish(void) @@ -95,80 +92,29 @@ config_get_block(ConfigBlockOption option) noexcept const char * config_get_string(ConfigOption option, const char *default_value) noexcept { - const auto *param = config_get_param(option); - - if (param == nullptr) - return default_value; - - return param->value.c_str(); + return config_data.GetString(option, default_value); } AllocatedPath config_get_path(ConfigOption option) { - const auto *param = config_get_param(option); - if (param == nullptr) - return nullptr; - - return param->GetPath(); + return config_data.GetPath(option); } unsigned config_get_unsigned(ConfigOption option, unsigned default_value) { - const auto *param = config_get_param(option); - long value; - char *endptr; - - if (param == nullptr) - return default_value; - - const char *const s = param->value.c_str(); - value = strtol(s, &endptr, 0); - if (endptr == s || *endptr != 0 || value < 0) - throw FormatRuntimeError("Not a valid non-negative number in line %i", - param->line); - - return (unsigned)value; + return config_data.GetUnsigned(option, default_value); } unsigned config_get_positive(ConfigOption option, unsigned default_value) { - const auto *param = config_get_param(option); - long value; - char *endptr; - - if (param == nullptr) - return default_value; - - const char *const s = param->value.c_str(); - value = strtol(s, &endptr, 0); - if (endptr == s || *endptr != 0) - throw FormatRuntimeError("Not a valid number in line %i", - param->line); - - if (value <= 0) - throw FormatRuntimeError("Not a positive number in line %i", - param->line); - - return (unsigned)value; + return config_data.GetPositive(option, default_value); } bool config_get_bool(ConfigOption option, bool default_value) { - const auto *param = config_get_param(option); - bool success, value; - - if (param == nullptr) - return default_value; - - success = get_bool(param->value.c_str(), &value); - if (!success) - throw FormatRuntimeError("Expected boolean value (yes, true, 1) or " - "(no, false, 0) on line %i\n", - param->line); - - return value; + return config_data.GetBool(option, default_value); }