ConfigGlobal: add config_find_block()

Merge duplicate code.
This commit is contained in:
Max Kellermann 2014-01-24 16:55:17 +01:00
parent 97391fd4b9
commit 9b1fbdbca6
6 changed files with 38 additions and 122 deletions

View File

@ -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)
{

View File

@ -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.

View File

@ -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 <string.h>
@ -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 = &empty;

View File

@ -31,45 +31,17 @@
#include <string.h>
/**
* 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);

View File

@ -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 <assert.h>
#include <string.h>
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 = &empty;
} else if (!param->GetBlockValue("enabled", true))
/* the plugin is disabled in mpd.conf */

View File

@ -37,7 +37,6 @@
#include "util/Macros.hxx"
#include "config/ConfigGlobal.hxx"
#include "config/ConfigData.hxx"
#include "system/FatalError.hxx"
#include "Log.hxx"
#include <assert.h>
@ -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 = &empty;
else if (!param->GetBlockValue("enabled", true))