config/Parser: get_bool() throws on error
This commit is contained in:
parent
96a37da03d
commit
af7970337b
@ -75,13 +75,7 @@ BlockParam::GetPositiveValue() const
|
|||||||
bool
|
bool
|
||||||
BlockParam::GetBoolValue() const
|
BlockParam::GetBoolValue() const
|
||||||
{
|
{
|
||||||
bool value2;
|
return With(ParseBool);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const BlockParam *
|
const BlockParam *
|
||||||
|
@ -126,19 +126,11 @@ ConfigData::GetPositive(ConfigOption option, unsigned default_value) const
|
|||||||
bool
|
bool
|
||||||
ConfigData::GetBool(ConfigOption option, bool default_value) const
|
ConfigData::GetBool(ConfigOption option, bool default_value) const
|
||||||
{
|
{
|
||||||
const auto *param = GetParam(option);
|
return With(option, [default_value](const char *s){
|
||||||
bool success, value;
|
return s != nullptr
|
||||||
|
? ParseBool(s)
|
||||||
if (param == nullptr)
|
: default_value;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ConfigBlock &
|
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
|
* http://www.musicpd.org
|
||||||
*
|
*
|
||||||
* This program is free software; you can redistribute it and/or modify
|
* This program is free software; you can redistribute it and/or modify
|
||||||
@ -18,23 +18,20 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "Parser.hxx"
|
#include "Parser.hxx"
|
||||||
|
#include "util/RuntimeError.hxx"
|
||||||
#include "util/StringUtil.hxx"
|
#include "util/StringUtil.hxx"
|
||||||
|
|
||||||
bool
|
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 t[] = { "yes", "true", "1", nullptr };
|
||||||
static const char *const f[] = { "no", "false", "0", nullptr };
|
static const char *const f[] = { "no", "false", "0", nullptr };
|
||||||
|
|
||||||
if (StringArrayContainsCase(t, value)) {
|
if (StringArrayContainsCase(t, value))
|
||||||
*value_r = true;
|
|
||||||
return true;
|
return true;
|
||||||
}
|
|
||||||
|
|
||||||
if (StringArrayContainsCase(f, value)) {
|
if (StringArrayContainsCase(f, value))
|
||||||
*value_r = false;
|
return false;
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
* http://www.musicpd.org
|
||||||
*
|
*
|
||||||
* This program is free software; you can redistribute it and/or modify
|
* This program is free software; you can redistribute it and/or modify
|
||||||
@ -20,7 +20,10 @@
|
|||||||
#ifndef MPD_CONFIG_PARSER_HXX
|
#ifndef MPD_CONFIG_PARSER_HXX
|
||||||
#define MPD_CONFIG_PARSER_HXX
|
#define MPD_CONFIG_PARSER_HXX
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Throws on error.
|
||||||
|
*/
|
||||||
bool
|
bool
|
||||||
get_bool(const char *value, bool *value_r);
|
ParseBool(const char *value);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user