config/Global: move value parser code to struct ConfigData

This commit is contained in:
Max Kellermann 2018-07-17 22:18:15 +02:00
parent 8af75c78f8
commit ffe6819966
3 changed files with 112 additions and 59 deletions

View File

@ -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 <stdlib.h>
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

View File

@ -26,6 +26,7 @@
struct ConfigParam;
struct ConfigBlock;
class AllocatedPath;
struct ConfigData {
std::array<ConfigParam *, std::size_t(ConfigOption::MAX)> 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)];

View File

@ -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 <stdlib.h>
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);
}