config/Block: add method GetPositiveValue()

Adds missing checks to several plugins.
This commit is contained in:
Max Kellermann
2018-01-02 17:22:25 +01:00
parent 7c5306a841
commit be65c7d5d0
10 changed files with 38 additions and 10 deletions

View File

@@ -52,6 +52,21 @@ BlockParam::GetUnsignedValue() const
return (unsigned)value2;
}
unsigned
BlockParam::GetPositiveValue() const
{
const char *const s = value.c_str();
char *endptr;
unsigned long value2 = strtoul(s, &endptr, 0);
if (endptr == s || *endptr != 0)
FormatFatalError("Not a valid number in line %i", line);
if (value2 <= 0)
FormatFatalError("Number in line %i must be positive", line);
return (unsigned)value2;
}
bool
BlockParam::GetBoolValue() const
{
@@ -131,6 +146,16 @@ ConfigBlock::GetBlockValue(const char *name, unsigned default_value) const
return bp->GetUnsignedValue();
}
unsigned
ConfigBlock::GetPositiveValue(const char *name, unsigned default_value) const
{
const auto *param = GetBlockParam(name);
if (param == nullptr)
return default_value;
return param->GetPositiveValue();
}
bool
ConfigBlock::GetBlockValue(const char *name, bool default_value) const
{