summaryrefslogtreecommitdiff
path: root/src/Main.cxx
diff options
context:
space:
mode:
authorMax Kellermann <max@musicpd.org>2019-05-29 21:45:58 +0200
committerMax Kellermann <max@musicpd.org>2019-05-29 21:50:04 +0200
commitfdbec694c61f4680ee808460e0122517e4b3a568 (patch)
treed6a119e807ea44fc4dc048dd94178287a70dd691 /src/Main.cxx
parentb86d8d0cd83c0646fb31013442579c5d4c26ceb8 (diff)
config/Param: add method With()
Diffstat (limited to 'src/Main.cxx')
-rw-r--r--src/Main.cxx62
1 files changed, 28 insertions, 34 deletions
diff --git a/src/Main.cxx b/src/Main.cxx
index 7e3b1fcf4..d60018e8f 100644
--- a/src/Main.cxx
+++ b/src/Main.cxx
@@ -282,20 +282,23 @@ initialize_decoder_and_player(Instance &instance,
size_t buffer_size;
param = config.GetParam(ConfigOption::AUDIO_BUFFER_SIZE);
if (param != nullptr) {
- char *test;
- long tmp = strtol(param->value.c_str(), &test, 10);
- if (*test != '\0' || tmp <= 0 || tmp == LONG_MAX)
- throw FormatRuntimeError("buffer size \"%s\" is not a "
- "positive integer, line %i",
- param->value.c_str(), param->line);
- buffer_size = tmp * KILOBYTE;
-
- if (buffer_size < MIN_BUFFER_SIZE) {
- FormatWarning(config_domain, "buffer size %lu is too small, using %lu bytes instead",
- (unsigned long)buffer_size,
- (unsigned long)MIN_BUFFER_SIZE);
- buffer_size = MIN_BUFFER_SIZE;
- }
+ buffer_size = param->With([](const char *s){
+ char *test;
+ long tmp = strtol(s, &test, 10);
+ if (*test != '\0' || tmp <= 0 || tmp == LONG_MAX)
+ throw FormatRuntimeError("buffer size \"%s\" is not a "
+ "positive integer", s);
+ size_t result = tmp * KILOBYTE;
+
+ if (result < MIN_BUFFER_SIZE) {
+ FormatWarning(config_domain, "buffer size %lu is too small, using %lu bytes instead",
+ (unsigned long)result,
+ (unsigned long)MIN_BUFFER_SIZE);
+ result = MIN_BUFFER_SIZE;
+ }
+
+ return result;
+ });
} else
buffer_size = DEFAULT_BUFFER_SIZE;
@@ -309,17 +312,12 @@ initialize_decoder_and_player(Instance &instance,
config.GetPositive(ConfigOption::MAX_PLAYLIST_LENGTH,
DEFAULT_PLAYLIST_MAX_LENGTH);
- AudioFormat configured_audio_format = AudioFormat::Undefined();
- param = config.GetParam(ConfigOption::AUDIO_OUTPUT_FORMAT);
- if (param != nullptr) {
- try {
- configured_audio_format = ParseAudioFormat(param->value.c_str(),
- true);
- } catch (...) {
- std::throw_with_nested(FormatRuntimeError("error parsing line %i",
- param->line));
- }
- }
+ AudioFormat configured_audio_format = config.With(ConfigOption::AUDIO_OUTPUT_FORMAT, [](const char *s){
+ if (s == nullptr)
+ return AudioFormat::Undefined();
+
+ return ParseAudioFormat(s, true);
+ });
instance.partitions.emplace_back(instance,
"default",
@@ -329,15 +327,11 @@ initialize_decoder_and_player(Instance &instance,
replay_gain_config);
auto &partition = instance.partitions.back();
- try {
- param = config.GetParam(ConfigOption::REPLAYGAIN);
- if (param != nullptr)
- partition.replay_gain_mode =
- FromString(param->value.c_str());
- } catch (...) {
- std::throw_with_nested(FormatRuntimeError("Failed to parse line %i",
- param->line));
- }
+ partition.replay_gain_mode = config.With(ConfigOption::REPLAYGAIN, [](const char *s){
+ return s != nullptr
+ ? FromString(s)
+ : ReplayGainMode::OFF;
+ });
}
inline void