config/Parser: get_bool() throws on error
This commit is contained in:
parent
96a37da03d
commit
af7970337b
@ -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 &
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* 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);
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* 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
|
||||
|
Loading…
Reference in New Issue
Block a user