Commit ab318200 authored by Max Kellermann's avatar Max Kellermann

config/{Data,Block}: use With() in GetUnsigned(), GetPositive()

parent 947856ca
...@@ -47,28 +47,13 @@ BlockParam::GetIntValue() const ...@@ -47,28 +47,13 @@ BlockParam::GetIntValue() const
unsigned unsigned
BlockParam::GetUnsignedValue() const BlockParam::GetUnsignedValue() const
{ {
const char *const s = value.c_str(); return With(ParseUnsigned);
char *endptr;
unsigned long value2 = strtoul(s, &endptr, 0);
if (endptr == s || *endptr != 0)
throw FormatRuntimeError("Not a valid number in line %i", line);
return (unsigned)value2;
} }
unsigned unsigned
BlockParam::GetPositiveValue() const BlockParam::GetPositiveValue() const
{ {
const char *const s = value.c_str(); return With(ParsePositive);
char *endptr;
unsigned long value2 = strtoul(s, &endptr, 0);
if (endptr == s || *endptr != 0)
throw FormatRuntimeError("Not a valid number in line %i", line);
if (value2 <= 0)
throw FormatRuntimeError("Number in line %i must be positive", line);
return (unsigned)value2;
} }
bool bool
......
...@@ -84,43 +84,21 @@ ConfigData::GetPath(ConfigOption option) const ...@@ -84,43 +84,21 @@ ConfigData::GetPath(ConfigOption option) const
unsigned unsigned
ConfigData::GetUnsigned(ConfigOption option, unsigned default_value) const ConfigData::GetUnsigned(ConfigOption option, unsigned default_value) const
{ {
const auto *param = GetParam(option); return With(option, [default_value](const char *s){
long value; return s != nullptr
char *endptr; ? ParseUnsigned(s)
: default_value;
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 unsigned
ConfigData::GetPositive(ConfigOption option, unsigned default_value) const ConfigData::GetPositive(ConfigOption option, unsigned default_value) const
{ {
const auto *param = GetParam(option); return With(option, [default_value](const char *s){
long value; return s != nullptr
char *endptr; ? ParsePositive(s)
: default_value;
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 bool
......
...@@ -22,6 +22,8 @@ ...@@ -22,6 +22,8 @@
#include "util/StringStrip.hxx" #include "util/StringStrip.hxx"
#include "util/StringUtil.hxx" #include "util/StringUtil.hxx"
#include <cstdlib>
bool bool
ParseBool(const char *value) ParseBool(const char *value)
{ {
...@@ -37,6 +39,37 @@ ParseBool(const char *value) ...@@ -37,6 +39,37 @@ ParseBool(const char *value)
throw FormatRuntimeError(R"(Not a valid boolean ("yes" or "no"): "%s")", value); throw FormatRuntimeError(R"(Not a valid boolean ("yes" or "no"): "%s")", value);
} }
long
ParseLong(const char *s)
{
char *endptr;
long value = strtol(s, &endptr, 10);
if (endptr == s || *endptr != 0)
throw std::runtime_error("Failed to parse number");
return value;
}
unsigned
ParseUnsigned(const char *s)
{
auto value = ParseLong(s);
if (value < 0)
throw std::runtime_error("Value must not be negative");
return (unsigned)value;
}
unsigned
ParsePositive(const char *s)
{
auto value = ParseLong(s);
if (value <= 0)
throw std::runtime_error("Value must be positive");
return (unsigned)value;
}
template<size_t OPERAND> template<size_t OPERAND>
static size_t static size_t
Multiply(size_t value) Multiply(size_t value)
......
...@@ -29,6 +29,24 @@ bool ...@@ -29,6 +29,24 @@ bool
ParseBool(const char *value); ParseBool(const char *value);
/** /**
* Throws on error.
*/
long
ParseLong(const char *s);
/**
* Throws on error.
*/
unsigned
ParseUnsigned(const char *s);
/**
* Throws on error.
*/
unsigned
ParsePositive(const char *s);
/**
* Parse a string as a byte size. * Parse a string as a byte size.
* *
* Throws on error. * Throws on error.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment