diff --git a/Makefile.am b/Makefile.am index 08540966f..978ea1053 100644 --- a/Makefile.am +++ b/Makefile.am @@ -853,7 +853,7 @@ libbasic_a_SOURCES = \ src/CheckAudioFormat.cxx src/CheckAudioFormat.hxx \ src/AudioFormat.cxx src/AudioFormat.hxx \ src/AudioParser.cxx src/AudioParser.hxx \ - src/ReplayGainMode.hxx \ + src/ReplayGainMode.cxx src/ReplayGainMode.hxx \ src/ReplayGainInfo.cxx src/ReplayGainInfo.hxx # configuration library diff --git a/src/ReplayGainConfig.cxx b/src/ReplayGainConfig.cxx index 68b8c4b77..04b639f45 100644 --- a/src/ReplayGainConfig.cxx +++ b/src/ReplayGainConfig.cxx @@ -22,10 +22,10 @@ #include "config/Param.hxx" #include "config/ConfigGlobal.hxx" #include "system/FatalError.hxx" +#include "util/RuntimeError.hxx" #include #include -#include #include ReplayGainMode replay_gain_mode = ReplayGainMode::OFF; @@ -36,54 +36,16 @@ float replay_gain_preamp = 1.0; float replay_gain_missing_preamp = 1.0; bool replay_gain_limit = DEFAULT_REPLAYGAIN_LIMIT; -const char * -replay_gain_get_mode_string(void) -{ - switch (replay_gain_mode) { - case ReplayGainMode::AUTO: - return "auto"; - - case ReplayGainMode::OFF: - return "off"; - - case ReplayGainMode::TRACK: - return "track"; - - case ReplayGainMode::ALBUM: - return "album"; - } - - assert(false); - gcc_unreachable(); -} - -bool -replay_gain_set_mode_string(const char *p) -{ - assert(p != nullptr); - - if (strcmp(p, "off") == 0) - replay_gain_mode = ReplayGainMode::OFF; - else if (strcmp(p, "track") == 0) - replay_gain_mode = ReplayGainMode::TRACK; - else if (strcmp(p, "album") == 0) - replay_gain_mode = ReplayGainMode::ALBUM; - else if (strcmp(p, "auto") == 0) - replay_gain_mode = ReplayGainMode::AUTO; - else - return false; - - return true; -} - void replay_gain_global_init(void) { const auto *param = config_get_param(ConfigOption::REPLAYGAIN); - if (param != nullptr && - !replay_gain_set_mode_string(param->value.c_str())) { - FormatFatalError("replaygain value \"%s\" at line %i is invalid\n", - param->value.c_str(), param->line); + try { + if (param != nullptr) + replay_gain_mode = FromString(param->value.c_str()); + } catch (...) { + std::throw_with_nested(FormatRuntimeError("Failed to parse line %i", + param->line)); } param = config_get_param(ConfigOption::REPLAYGAIN_PREAMP); diff --git a/src/ReplayGainConfig.hxx b/src/ReplayGainConfig.hxx index 5c1953c3b..3d3154c3f 100644 --- a/src/ReplayGainConfig.hxx +++ b/src/ReplayGainConfig.hxx @@ -22,7 +22,6 @@ #include "check.h" #include "ReplayGainMode.hxx" -#include "Compiler.h" extern ReplayGainMode replay_gain_mode; extern float replay_gain_preamp; @@ -32,19 +31,4 @@ extern bool replay_gain_limit; void replay_gain_global_init(); -/** - * Returns the current replay gain mode as a machine-readable string. - */ -gcc_pure -const char * -replay_gain_get_mode_string(); - -/** - * Sets the replay gain mode, parsed from a string. - * - * @return true on success, false if the string could not be parsed - */ -bool -replay_gain_set_mode_string(const char *p); - #endif diff --git a/src/ReplayGainMode.cxx b/src/ReplayGainMode.cxx new file mode 100644 index 000000000..2f600519e --- /dev/null +++ b/src/ReplayGainMode.cxx @@ -0,0 +1,63 @@ +/* + * Copyright 2003-2016 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "ReplayGainMode.hxx" + +#include + +#include +#include + +const char * +ToString(ReplayGainMode mode) +{ + switch (mode) { + case ReplayGainMode::AUTO: + return "auto"; + + case ReplayGainMode::OFF: + return "off"; + + case ReplayGainMode::TRACK: + return "track"; + + case ReplayGainMode::ALBUM: + return "album"; + } + + assert(false); + gcc_unreachable(); +} + +ReplayGainMode +FromString(const char *s) +{ + assert(s != nullptr); + + if (strcmp(s, "off") == 0) + return ReplayGainMode::OFF; + else if (strcmp(s, "track") == 0) + return ReplayGainMode::TRACK; + else if (strcmp(s, "album") == 0) + return ReplayGainMode::ALBUM; + else if (strcmp(s, "auto") == 0) + return ReplayGainMode::AUTO; + else + throw std::invalid_argument("Unrecognized replay gain mode"); +} diff --git a/src/ReplayGainMode.hxx b/src/ReplayGainMode.hxx index c564cda9c..118bb8011 100644 --- a/src/ReplayGainMode.hxx +++ b/src/ReplayGainMode.hxx @@ -20,6 +20,8 @@ #ifndef MPD_REPLAY_GAIN_MODE_HXX #define MPD_REPLAY_GAIN_MODE_HXX +#include "Compiler.h" + #include enum class ReplayGainMode : uint8_t { @@ -29,4 +31,19 @@ enum class ReplayGainMode : uint8_t { AUTO, }; +/** + * Return the string representation of a #ReplayGainMode. + */ +gcc_pure +const char * +ToString(ReplayGainMode mode); + +/** + * Parse a string to a #ReplayGainMode. Throws std::runtime_error on + * error. + */ +gcc_pure +ReplayGainMode +FromString(const char *s); + #endif diff --git a/src/command/PlayerCommands.cxx b/src/command/PlayerCommands.cxx index 2eb1cd41b..f6f2af2fc 100644 --- a/src/command/PlayerCommands.cxx +++ b/src/command/PlayerCommands.cxx @@ -331,13 +331,9 @@ handle_mixrampdelay(Client &client, Request args, gcc_unused Response &r) } CommandResult -handle_replay_gain_mode(Client &client, Request args, Response &r) +handle_replay_gain_mode(Client &client, Request args, Response &) { - if (!replay_gain_set_mode_string(args.front())) { - r.Error(ACK_ERROR_ARG, "Unrecognized replay gain mode"); - return CommandResult::ERROR; - } - + replay_gain_mode = FromString(args.front()); client.partition.UpdateEffectiveReplayGainMode(replay_gain_mode); client.partition.EmitIdle(IDLE_OPTIONS); return CommandResult::OK; @@ -347,6 +343,6 @@ CommandResult handle_replay_gain_status(gcc_unused Client &client, gcc_unused Request args, Response &r) { - r.Format("replay_gain_mode: %s\n", replay_gain_get_mode_string()); + r.Format("replay_gain_mode: %s\n", ToString(replay_gain_mode)); return CommandResult::OK; }