From 9f4fc8ad33470d2f82faafb96d5db41967faa151 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Wed, 24 Sep 2014 22:19:55 +0200 Subject: [PATCH] tag/ReplayGain: move code to template function --- src/tag/ReplayGain.cxx | 50 ++++++++++++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 14 deletions(-) diff --git a/src/tag/ReplayGain.cxx b/src/tag/ReplayGain.cxx index f2f52e59c..d2347dba5 100644 --- a/src/tag/ReplayGain.cxx +++ b/src/tag/ReplayGain.cxx @@ -25,24 +25,46 @@ #include #include +template +static bool +ParseReplayGainTagTemplate(ReplayGainInfo &info, const T t) +{ + const char *value; + + if ((value = t["replaygain_track_gain"]) != nullptr) { + info.tuples[REPLAY_GAIN_TRACK].gain = atof(value); + return true; + } else if ((value = t["replaygain_album_gain"]) != nullptr) { + info.tuples[REPLAY_GAIN_ALBUM].gain = atof(value); + return true; + } else if ((value = t["replaygain_track_peak"]) != nullptr) { + info.tuples[REPLAY_GAIN_TRACK].peak = atof(value); + return true; + } else if ((value = t["replaygain_album_peak"]) != nullptr) { + info.tuples[REPLAY_GAIN_ALBUM].peak = atof(value); + return true; + } else + return false; + +} + bool ParseReplayGainTag(ReplayGainInfo &info, const char *name, const char *value) { assert(name != nullptr); assert(value != nullptr); - if (StringEqualsCaseASCII(name, "replaygain_track_gain")) { - info.tuples[REPLAY_GAIN_TRACK].gain = atof(value); - return true; - } else if (StringEqualsCaseASCII(name, "replaygain_album_gain")) { - info.tuples[REPLAY_GAIN_ALBUM].gain = atof(value); - return true; - } else if (StringEqualsCaseASCII(name, "replaygain_track_peak")) { - info.tuples[REPLAY_GAIN_TRACK].peak = atof(value); - return true; - } else if (StringEqualsCaseASCII(name, "replaygain_album_peak")) { - info.tuples[REPLAY_GAIN_ALBUM].peak = atof(value); - return true; - } else - return false; + struct NameValue { + const char *name; + const char *value; + + gcc_pure + const char *operator[](const char *n) const { + return StringEqualsCaseASCII(name, n) + ? value + : nullptr; + } + }; + + return ParseReplayGainTagTemplate(info, NameValue{name, value}); }