diff options
-rw-r--r-- | NEWS | 1 | ||||
-rw-r--r-- | src/config/Data.cxx | 32 | ||||
-rw-r--r-- | src/config/Data.hxx | 12 | ||||
-rw-r--r-- | src/config/Parser.cxx | 8 | ||||
-rw-r--r-- | src/config/Parser.hxx | 7 |
5 files changed, 50 insertions, 10 deletions
@@ -4,6 +4,7 @@ ver 0.22.1 (not yet released) - jack, pulse: reduce the delay when stopping or pausing playback * playlist - cue: fix two crash bugs +* state_file: fix the state_file_interval setting ver 0.22 (2020/09/23) * protocol diff --git a/src/config/Data.cxx b/src/config/Data.cxx index 143ef5612..c11e3bdfc 100644 --- a/src/config/Data.cxx +++ b/src/config/Data.cxx @@ -101,6 +101,38 @@ ConfigData::GetPositive(ConfigOption option, unsigned default_value) const }); } +std::chrono::steady_clock::duration +ConfigData::GetUnsigned(ConfigOption option, + std::chrono::steady_clock::duration default_value) const +{ + return With(option, [default_value](const char *s){ + if (s == nullptr) + return default_value; + + auto value = ParseDuration(s); + if (value < std::chrono::steady_clock::duration{}) + throw std::runtime_error("Value must not be negative"); + + return value; + }); +} + +std::chrono::steady_clock::duration +ConfigData::GetPositive(ConfigOption option, + std::chrono::steady_clock::duration default_value) const +{ + return With(option, [default_value](const char *s){ + if (s == nullptr) + return default_value; + + auto value = ParseDuration(s); + if (value <= std::chrono::steady_clock::duration{}) + throw std::runtime_error("Value must be positive"); + + return value; + }); +} + bool ConfigData::GetBool(ConfigOption option, bool default_value) const { diff --git a/src/config/Data.hxx b/src/config/Data.hxx index 4ba918667..aab590cfc 100644 --- a/src/config/Data.hxx +++ b/src/config/Data.hxx @@ -78,22 +78,14 @@ struct ConfigData { std::chrono::steady_clock::duration GetUnsigned(ConfigOption option, - std::chrono::steady_clock::duration default_value) const { - // TODO: allow unit suffixes - auto u = GetUnsigned(option, default_value.count()); - return std::chrono::steady_clock::duration(u); - } + std::chrono::steady_clock::duration default_value) const; unsigned GetPositive(ConfigOption option, unsigned default_value) const; std::chrono::steady_clock::duration GetPositive(ConfigOption option, - std::chrono::steady_clock::duration default_value) const { - // TODO: allow unit suffixes - auto u = GetPositive(option, default_value.count()); - return std::chrono::steady_clock::duration(u); - } + std::chrono::steady_clock::duration default_value) const; bool GetBool(ConfigOption option, bool default_value) const; diff --git a/src/config/Parser.cxx b/src/config/Parser.cxx index 9bfb735c2..034832870 100644 --- a/src/config/Parser.cxx +++ b/src/config/Parser.cxx @@ -135,3 +135,11 @@ ParseSize(const char *s, std::size_t default_factor) return value; } + +std::chrono::steady_clock::duration +ParseDuration(const char *s) +{ + // TODO: allow unit suffixes + const std::chrono::seconds seconds(ParseLong(s)); + return std::chrono::duration_cast<std::chrono::steady_clock::duration>(seconds); +} diff --git a/src/config/Parser.hxx b/src/config/Parser.hxx index 71feec4ca..e67e99a1c 100644 --- a/src/config/Parser.hxx +++ b/src/config/Parser.hxx @@ -20,6 +20,7 @@ #ifndef MPD_CONFIG_PARSER_HXX #define MPD_CONFIG_PARSER_HXX +#include <chrono> #include <cstddef> /** @@ -54,4 +55,10 @@ ParsePositive(const char *s); std::size_t ParseSize(const char *s, std::size_t default_factor=1); +/** + * Throws on error. + */ +std::chrono::steady_clock::duration +ParseDuration(const char *s); + #endif |