diff options
author | Max Kellermann <max@musicpd.org> | 2019-05-29 22:31:00 +0200 |
---|---|---|
committer | Max Kellermann <max@musicpd.org> | 2019-05-29 22:35:40 +0200 |
commit | af7970337bd910e3f3c922007faa8d755e2e4b4c (patch) | |
tree | 87e07107bf2eb430a1aab99723dd1f8a503eca28 /src | |
parent | 96a37da03d820e66c0378c336fe02a513bec224f (diff) |
config/Parser: get_bool() throws on error
Diffstat (limited to 'src')
-rw-r--r-- | src/config/Block.cxx | 8 | ||||
-rw-r--r-- | src/config/Data.cxx | 18 | ||||
-rw-r--r-- | src/config/Parser.cxx | 17 | ||||
-rw-r--r-- | src/config/Parser.hxx | 7 |
4 files changed, 18 insertions, 32 deletions
diff --git a/src/config/Block.cxx b/src/config/Block.cxx index 2671d7a11..a6205985a 100644 --- a/src/config/Block.cxx +++ b/src/config/Block.cxx @@ -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 * diff --git a/src/config/Data.cxx b/src/config/Data.cxx index c6dd2138a..0098ecbdf 100644 --- a/src/config/Data.cxx +++ b/src/config/Data.cxx @@ -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 & diff --git a/src/config/Parser.cxx b/src/config/Parser.cxx index bee4cbd3d..2f06b8952 100644 --- a/src/config/Parser.cxx +++ b/src/config/Parser.cxx @@ -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); } diff --git a/src/config/Parser.hxx b/src/config/Parser.hxx index 107ca0538..4e19f052c 100644 --- a/src/config/Parser.hxx +++ b/src/config/Parser.hxx @@ -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 |