Commit af797033 authored by Max Kellermann's avatar Max Kellermann

config/Parser: get_bool() throws on error

parent 96a37da0
......@@ -75,13 +75,7 @@ BlockParam::GetPositiveValue() const
bool
BlockParam::GetBoolValue() const
{
bool value2;
if (!get_bool(value.c_str(), &value2))
throw FormatRuntimeError("%s is not a boolean value (yes, true, 1) or "
"(no, false, 0) on line %i\n",
name.c_str(), line);
return value2;
return With(ParseBool);
}
const BlockParam *
......
......@@ -126,19 +126,11 @@ ConfigData::GetPositive(ConfigOption option, unsigned default_value) const
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;
return With(option, [default_value](const char *s){
return s != nullptr
? ParseBool(s)
: default_value;
});
}
ConfigBlock &
......
/*
* Copyright 2003-2018 The Music Player Daemon Project
* Copyright 2003-2019 The Music Player Daemon Project
* http://www.musicpd.org
*
* This program is free software; you can redistribute it and/or modify
......@@ -18,23 +18,20 @@
*/
#include "Parser.hxx"
#include "util/RuntimeError.hxx"
#include "util/StringUtil.hxx"
bool
get_bool(const char *value, bool *value_r)
ParseBool(const char *value)
{
static const char *const t[] = { "yes", "true", "1", nullptr };
static const char *const f[] = { "no", "false", "0", nullptr };
if (StringArrayContainsCase(t, value)) {
*value_r = true;
if (StringArrayContainsCase(t, value))
return true;
}
if (StringArrayContainsCase(f, value)) {
*value_r = false;
return true;
}
if (StringArrayContainsCase(f, value))
return false;
return false;
throw FormatRuntimeError("Not a valid boolean (\"yes\" or \"no\"): \"%s\"", value);
}
/*
* Copyright 2003-2018 The Music Player Daemon Project
* Copyright 2003-2019 The Music Player Daemon Project
* http://www.musicpd.org
*
* This program is free software; you can redistribute it and/or modify
......@@ -20,7 +20,10 @@
#ifndef MPD_CONFIG_PARSER_HXX
#define MPD_CONFIG_PARSER_HXX
/**
* Throws on error.
*/
bool
get_bool(const char *value, bool *value_r);
ParseBool(const char *value);
#endif
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