From 9b1fbdbca63f03c357a2f9c01fca281f62b3570c Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 24 Jan 2014 16:55:17 +0100 Subject: [PATCH] ConfigGlobal: add config_find_block() Merge duplicate code. --- src/config/ConfigGlobal.cxx | 17 +++++++++++++ src/config/ConfigGlobal.hxx | 11 +++++++++ src/decoder/DecoderList.cxx | 27 +-------------------- src/filter/FilterConfig.cxx | 40 +++++-------------------------- src/input/Init.cxx | 35 +-------------------------- src/playlist/PlaylistRegistry.cxx | 30 ++--------------------- 6 files changed, 38 insertions(+), 122 deletions(-) diff --git a/src/config/ConfigGlobal.cxx b/src/config/ConfigGlobal.cxx index c7d16d3e7..dd76e3ca3 100644 --- a/src/config/ConfigGlobal.cxx +++ b/src/config/ConfigGlobal.cxx @@ -85,6 +85,23 @@ config_get_next_param(ConfigOption option, const struct config_param * last) return param; } +const config_param * +config_find_block(ConfigOption option, const char *key, const char *value) +{ + const config_param *param = nullptr; + while ((param = config_get_next_param(option, param)) != nullptr) { + const char *value2 = param->GetBlockValue(key); + if (value2 == nullptr) + FormatFatalError("block without '%s' name in line %d", + key, param->line); + + if (strcmp(value2, value) == 0) + return param; + } + + return nullptr; +} + const char * config_get_string(ConfigOption option, const char *default_value) { diff --git a/src/config/ConfigGlobal.hxx b/src/config/ConfigGlobal.hxx index abce424de..84ef7dd5f 100644 --- a/src/config/ConfigGlobal.hxx +++ b/src/config/ConfigGlobal.hxx @@ -53,6 +53,17 @@ config_get_param(enum ConfigOption option) return config_get_next_param(option, nullptr); } +/** + * Find a block with a matching attribute. + * + * @param option the blocks to search + * @param key the attribute name + * @param value the expected attribute value + */ +gcc_pure +const config_param * +config_find_block(ConfigOption option, const char *key, const char *value); + /* Note on gcc_pure: Some of the functions declared pure are not really pure in strict sense. They have side effect such that they validate parameter's value and signal an error if it's invalid. diff --git a/src/decoder/DecoderList.cxx b/src/decoder/DecoderList.cxx index 5423a85ab..5d9d44d34 100644 --- a/src/decoder/DecoderList.cxx +++ b/src/decoder/DecoderList.cxx @@ -43,7 +43,6 @@ #include "plugins/MpcdecDecoderPlugin.hxx" #include "plugins/FluidsynthDecoderPlugin.hxx" #include "plugins/SidplayDecoderPlugin.hxx" -#include "system/FatalError.hxx" #include "util/Macros.hxx" #include @@ -126,30 +125,6 @@ decoder_plugin_from_name(const char *name) }); } -/** - * Find the "decoder" configuration block for the specified plugin. - * - * @param plugin_name the name of the decoder plugin - * @return the configuration block, or nullptr if none was configured - */ -static const struct config_param * -decoder_plugin_config(const char *plugin_name) -{ - const struct config_param *param = nullptr; - - while ((param = config_get_next_param(CONF_DECODER, param)) != nullptr) { - const char *name = param->GetBlockValue("plugin"); - if (name == nullptr) - FormatFatalError("decoder configuration without 'plugin' name in line %d", - param->line); - - if (strcmp(name, plugin_name) == 0) - return param; - } - - return nullptr; -} - void decoder_plugin_init_all(void) { struct config_param empty; @@ -157,7 +132,7 @@ void decoder_plugin_init_all(void) for (unsigned i = 0; decoder_plugins[i] != nullptr; ++i) { const DecoderPlugin &plugin = *decoder_plugins[i]; const struct config_param *param = - decoder_plugin_config(plugin.name); + config_find_block(CONF_DECODER, "plugin", plugin.name); if (param == nullptr) param = ∅ diff --git a/src/filter/FilterConfig.cxx b/src/filter/FilterConfig.cxx index 5f2c3a95b..d8c1fc6c2 100644 --- a/src/filter/FilterConfig.cxx +++ b/src/filter/FilterConfig.cxx @@ -31,45 +31,17 @@ #include -/** - * Find the "filter" configuration block for the specified name. - * - * @param filter_template_name the name of the filter template - * @param error space to return an error description - * @return the configuration block, or nullptr if none was configured - */ -static const struct config_param * -filter_plugin_config(const char *filter_template_name, Error &error) -{ - const struct config_param *param = nullptr; - - while ((param = config_get_next_param(CONF_AUDIO_FILTER, param)) != nullptr) { - const char *name = param->GetBlockValue("name"); - if (name == nullptr) { - error.Format(config_domain, - "filter configuration without 'name' name in line %d", - param->line); - return nullptr; - } - - if (strcmp(name, filter_template_name) == 0) - return param; - } - - error.Format(config_domain, - "filter template not found: %s", - filter_template_name); - return nullptr; -} - static bool filter_chain_append_new(Filter &chain, const char *template_name, Error &error) { const struct config_param *cfg = - filter_plugin_config(template_name, error); - if (cfg == nullptr) - // The error has already been set, just stop. + config_find_block(CONF_AUDIO_FILTER, "name", template_name); + if (cfg == nullptr) { + error.Format(config_domain, + "filter template not found: %s", + template_name); return false; + } // Instantiate one of those filter plugins with the template name as a hint Filter *f = filter_configured_new(*cfg, error); diff --git a/src/input/Init.cxx b/src/input/Init.cxx index 14a7feef3..4b8eac320 100644 --- a/src/input/Init.cxx +++ b/src/input/Init.cxx @@ -22,7 +22,6 @@ #include "Registry.hxx" #include "InputPlugin.hxx" #include "util/Error.hxx" -#include "util/Domain.hxx" #include "config/ConfigGlobal.hxx" #include "config/ConfigOption.hxx" #include "config/ConfigData.hxx" @@ -30,35 +29,6 @@ #include #include -extern constexpr Domain input_domain("input"); - -/** - * Find the "input" configuration block for the specified plugin. - * - * @param plugin_name the name of the input plugin - * @return the configuration block, or nullptr if none was configured - */ -static const struct config_param * -input_plugin_config(const char *plugin_name, Error &error) -{ - const struct config_param *param = nullptr; - - while ((param = config_get_next_param(CONF_INPUT, param)) != nullptr) { - const char *name = param->GetBlockValue("plugin"); - if (name == nullptr) { - error.Format(input_domain, - "input configuration without 'plugin' name in line %d", - param->line); - return nullptr; - } - - if (strcmp(name, plugin_name) == 0) - return param; - } - - return nullptr; -} - bool input_stream_global_init(Error &error) { @@ -72,11 +42,8 @@ input_stream_global_init(Error &error) assert(plugin->open != nullptr); const struct config_param *param = - input_plugin_config(plugin->name, error); + config_find_block(CONF_INPUT, "plugin", plugin->name); if (param == nullptr) { - if (error.IsDefined()) - return false; - param = ∅ } else if (!param->GetBlockValue("enabled", true)) /* the plugin is disabled in mpd.conf */ diff --git a/src/playlist/PlaylistRegistry.cxx b/src/playlist/PlaylistRegistry.cxx index 5e6db15f4..55064849b 100644 --- a/src/playlist/PlaylistRegistry.cxx +++ b/src/playlist/PlaylistRegistry.cxx @@ -37,7 +37,6 @@ #include "util/Macros.hxx" #include "config/ConfigGlobal.hxx" #include "config/ConfigData.hxx" -#include "system/FatalError.hxx" #include "Log.hxx" #include @@ -73,32 +72,6 @@ static bool playlist_plugins_enabled[n_playlist_plugins]; playlist_plugins_for_each(plugin) \ if (playlist_plugins_enabled[playlist_plugin_iterator - playlist_plugins]) -/** - * Find the "playlist" configuration block for the specified plugin. - * - * @param plugin_name the name of the playlist plugin - * @return the configuration block, or nullptr if none was configured - */ -static const struct config_param * -playlist_plugin_config(const char *plugin_name) -{ - const struct config_param *param = nullptr; - - assert(plugin_name != nullptr); - - while ((param = config_get_next_param(CONF_PLAYLIST_PLUGIN, param)) != nullptr) { - const char *name = param->GetBlockValue("name"); - if (name == nullptr) - FormatFatalError("playlist configuration without 'plugin' name in line %d", - param->line); - - if (strcmp(name, plugin_name) == 0) - return param; - } - - return nullptr; -} - void playlist_list_global_init(void) { @@ -107,7 +80,8 @@ playlist_list_global_init(void) for (unsigned i = 0; playlist_plugins[i] != nullptr; ++i) { const struct playlist_plugin *plugin = playlist_plugins[i]; const struct config_param *param = - playlist_plugin_config(plugin->name); + config_find_block(CONF_PLAYLIST_PLUGIN, "name", + plugin->name); if (param == nullptr) param = ∅ else if (!param->GetBlockValue("enabled", true))