diff options
author | Max Kellermann <max@duempel.org> | 2013-01-30 19:44:59 +0100 |
---|---|---|
committer | Max Kellermann <max@duempel.org> | 2013-01-30 21:39:43 +0100 |
commit | e294ccae245a3ec86f9427075a06f045714e9b08 (patch) | |
tree | ff7310e06900b19e6300660dbce1b68970005cc9 | |
parent | 72070f292b98ee8b38217b43dc046b047bcccd8e (diff) | |
download | mpd-e294ccae245a3ec86f9427075a06f045714e9b08.tar.gz mpd-e294ccae245a3ec86f9427075a06f045714e9b08.tar.xz mpd-e294ccae245a3ec86f9427075a06f045714e9b08.zip |
ConfigFile: move code to ConfigGlobal.cxx
-rw-r--r-- | Makefile.am | 3 | ||||
-rw-r--r-- | src/ConfigFile.cxx | 165 | ||||
-rw-r--r-- | src/ConfigFile.hxx | 31 | ||||
-rw-r--r-- | src/ConfigGlobal.cxx | 199 | ||||
-rw-r--r-- | src/ConfigGlobal.hxx | 109 | ||||
-rw-r--r-- | src/conf.h | 74 |
6 files changed, 346 insertions, 235 deletions
diff --git a/Makefile.am b/Makefile.am index 63090e298..167b61768 100644 --- a/Makefile.am +++ b/Makefile.am @@ -436,7 +436,8 @@ endif libconf_a_SOURCES = \ src/ConfigData.cxx src/ConfigData.hxx \ src/ConfigParser.cxx src/ConfigParser.hxx \ - src/ConfigFile.cxx \ + src/ConfigGlobal.cxx src/ConfigGlobal.hxx \ + src/ConfigFile.cxx src/ConfigFile.hxx \ src/ConfigTemplates.cxx src/ConfigTemplates.hxx \ src/ConfigOption.hxx diff --git a/src/ConfigFile.cxx b/src/ConfigFile.cxx index d10c90463..254d18410 100644 --- a/src/ConfigFile.cxx +++ b/src/ConfigFile.cxx @@ -18,26 +18,24 @@ */ #include "config.h" -#include "conf.h" +#include "ConfigFile.hxx" +#include "ConfigData.hxx" #include "ConfigTemplates.hxx" -#include "ConfigParser.hxx" +#include "conf.h" extern "C" { -#include "utils.h" #include "string_util.h" #include "tokenizer.h" } #include "fs/Path.hxx" #include "fs/FileSystem.hxx" -#include "mpd_error.h" #include <glib.h> #include <assert.h> #include <string.h> #include <stdio.h> -#include <stdlib.h> #include <errno.h> #undef G_LOG_DOMAIN @@ -47,54 +45,6 @@ extern "C" { #define CONF_COMMENT '#' -static ConfigData config_data; - -static void -config_param_free_callback(gpointer data, G_GNUC_UNUSED gpointer user_data) -{ - struct config_param *param = (struct config_param *)data; - - config_param_free(param); -} - -void config_global_finish(void) -{ - for (auto i : config_data.params) { - g_slist_foreach(i, config_param_free_callback, NULL); - g_slist_free(i); - } -} - -void config_global_init(void) -{ -} - -static void -config_param_check(gpointer data, G_GNUC_UNUSED gpointer user_data) -{ - struct config_param *param = (struct config_param *)data; - - if (!param->used) - /* this whole config_param was not queried at all - - the feature might be disabled at compile time? - Silently ignore it here. */ - return; - - for (unsigned i = 0; i < param->num_block_params; i++) { - struct block_param *bp = ¶m->block_params[i]; - - if (!bp->used) - g_warning("option '%s' on line %i was not recognized", - bp->name, bp->line); - } -} - -void config_global_check(void) -{ - for (auto i : config_data.params) - g_slist_foreach(i, config_param_check, NULL); -} - static bool config_read_name_value(struct config_param *param, char *input, unsigned line, GError **error_r) @@ -187,7 +137,7 @@ config_read_block(FILE *fp, int *count, char *string, GError **error_r) } bool -ReadConfigFile(const Path &path, GError **error_r) +ReadConfigFile(ConfigData &config_data, const Path &path, GError **error_r) { assert(!path.IsNull()); const std::string path_utf8 = path.ToUTF8(); @@ -318,110 +268,3 @@ ReadConfigFile(const Path &path, GError **error_r) return true; } - -const struct config_param * -config_get_next_param(ConfigOption option, const struct config_param * last) -{ - GSList *node = config_data.params[unsigned(option)]; - - if (last) { - node = g_slist_find(node, last); - if (node == NULL) - return NULL; - - node = g_slist_next(node); - } - - if (node == NULL) - return NULL; - - struct config_param *param = (struct config_param *)node->data; - param->used = true; - return param; -} - -const char * -config_get_string(ConfigOption option, const char *default_value) -{ - const struct config_param *param = config_get_param(option); - - if (param == NULL) - return default_value; - - return param->value; -} - -char * -config_dup_path(ConfigOption option, GError **error_r) -{ - assert(error_r != NULL); - assert(*error_r == NULL); - - const struct config_param *param = config_get_param(option); - if (param == NULL) - return NULL; - - char *path = parsePath(param->value, error_r); - if (G_UNLIKELY(path == NULL)) - g_prefix_error(error_r, - "Invalid path at line %i: ", - param->line); - - return path; -} - -unsigned -config_get_unsigned(ConfigOption option, unsigned default_value) -{ - const struct config_param *param = config_get_param(option); - long value; - char *endptr; - - if (param == NULL) - return default_value; - - value = strtol(param->value, &endptr, 0); - if (*endptr != 0 || value < 0) - MPD_ERROR("Not a valid non-negative number in line %i", - param->line); - - return (unsigned)value; -} - -unsigned -config_get_positive(ConfigOption option, unsigned default_value) -{ - const struct config_param *param = config_get_param(option); - long value; - char *endptr; - - if (param == NULL) - return default_value; - - value = strtol(param->value, &endptr, 0); - if (*endptr != 0) - MPD_ERROR("Not a valid number in line %i", param->line); - - if (value <= 0) - MPD_ERROR("Not a positive number in line %i", param->line); - - return (unsigned)value; -} - -bool -config_get_bool(ConfigOption option, bool default_value) -{ - const struct config_param *param = config_get_param(option); - bool success, value; - - if (param == NULL) - return default_value; - - success = get_bool(param->value, &value); - if (!success) - MPD_ERROR("Expected boolean value (yes, true, 1) or " - "(no, false, 0) on line %i\n", - param->line); - - return value; -} diff --git a/src/ConfigFile.hxx b/src/ConfigFile.hxx new file mode 100644 index 000000000..49c0d31ec --- /dev/null +++ b/src/ConfigFile.hxx @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2003-2013 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. + */ + +#ifndef MPD_CONFIG_FILE_HXX +#define MPD_CONFIG_FILE_HXX + +#include "gerror.h" + +class Path; +struct ConfigData; + +bool +ReadConfigFile(ConfigData &data, const Path &path, GError **error_r); + +#endif diff --git a/src/ConfigGlobal.cxx b/src/ConfigGlobal.cxx new file mode 100644 index 000000000..01b8d09c5 --- /dev/null +++ b/src/ConfigGlobal.cxx @@ -0,0 +1,199 @@ +/* + * Copyright (C) 2003-2013 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 "config.h" +#include "ConfigGlobal.hxx" +#include "ConfigParser.hxx" +#include "ConfigData.hxx" +#include "ConfigFile.hxx" + +extern "C" { +#include "utils.h" +} + +#include "mpd_error.h" + +#include <glib.h> + +#include <assert.h> +#include <string.h> + +#undef G_LOG_DOMAIN +#define G_LOG_DOMAIN "config" + +static ConfigData config_data; + +static void +config_param_free_callback(gpointer data, G_GNUC_UNUSED gpointer user_data) +{ + struct config_param *param = (struct config_param *)data; + + config_param_free(param); +} + +void config_global_finish(void) +{ + for (auto i : config_data.params) { + g_slist_foreach(i, config_param_free_callback, NULL); + g_slist_free(i); + } +} + +void config_global_init(void) +{ +} + +bool +ReadConfigFile(const Path &path, GError **error_r) +{ + return ReadConfigFile(config_data, path, error_r); +} + +static void +config_param_check(gpointer data, G_GNUC_UNUSED gpointer user_data) +{ + struct config_param *param = (struct config_param *)data; + + if (!param->used) + /* this whole config_param was not queried at all - + the feature might be disabled at compile time? + Silently ignore it here. */ + return; + + for (unsigned i = 0; i < param->num_block_params; i++) { + struct block_param *bp = ¶m->block_params[i]; + + if (!bp->used) + g_warning("option '%s' on line %i was not recognized", + bp->name, bp->line); + } +} + +void config_global_check(void) +{ + for (auto i : config_data.params) + g_slist_foreach(i, config_param_check, NULL); +} + +const struct config_param * +config_get_next_param(ConfigOption option, const struct config_param * last) +{ + GSList *node = config_data.params[unsigned(option)]; + + if (last) { + node = g_slist_find(node, last); + if (node == NULL) + return NULL; + + node = g_slist_next(node); + } + + if (node == NULL) + return NULL; + + struct config_param *param = (struct config_param *)node->data; + param->used = true; + return param; +} + +const char * +config_get_string(ConfigOption option, const char *default_value) +{ + const struct config_param *param = config_get_param(option); + + if (param == NULL) + return default_value; + + return param->value; +} + +char * +config_dup_path(ConfigOption option, GError **error_r) +{ + assert(error_r != NULL); + assert(*error_r == NULL); + + const struct config_param *param = config_get_param(option); + if (param == NULL) + return NULL; + + char *path = parsePath(param->value, error_r); + if (G_UNLIKELY(path == NULL)) + g_prefix_error(error_r, + "Invalid path at line %i: ", + param->line); + + return path; +} + +unsigned +config_get_unsigned(ConfigOption option, unsigned default_value) +{ + const struct config_param *param = config_get_param(option); + long value; + char *endptr; + + if (param == NULL) + return default_value; + + value = strtol(param->value, &endptr, 0); + if (*endptr != 0 || value < 0) + MPD_ERROR("Not a valid non-negative number in line %i", + param->line); + + return (unsigned)value; +} + +unsigned +config_get_positive(ConfigOption option, unsigned default_value) +{ + const struct config_param *param = config_get_param(option); + long value; + char *endptr; + + if (param == NULL) + return default_value; + + value = strtol(param->value, &endptr, 0); + if (*endptr != 0) + MPD_ERROR("Not a valid number in line %i", param->line); + + if (value <= 0) + MPD_ERROR("Not a positive number in line %i", param->line); + + return (unsigned)value; +} + +bool +config_get_bool(ConfigOption option, bool default_value) +{ + const struct config_param *param = config_get_param(option); + bool success, value; + + if (param == NULL) + return default_value; + + success = get_bool(param->value, &value); + if (!success) + MPD_ERROR("Expected boolean value (yes, true, 1) or " + "(no, false, 0) on line %i\n", + param->line); + + return value; +} diff --git a/src/ConfigGlobal.hxx b/src/ConfigGlobal.hxx new file mode 100644 index 000000000..9abfb2b5d --- /dev/null +++ b/src/ConfigGlobal.hxx @@ -0,0 +1,109 @@ +/* + * Copyright (C) 2003-2013 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. + */ + +#ifndef MPD_CONFIG_GLOBAL_HXX +#define MPD_CONFIG_GLOBAL_HXX + +#include "ConfigOption.hxx" +#include "gerror.h" +#include "gcc.h" + +#include <stdbool.h> +#include <stddef.h> + +#define DEFAULT_PLAYLIST_MAX_LENGTH (1024*16) +#define DEFAULT_PLAYLIST_SAVE_ABSOLUTE_PATHS false + +#ifdef __cplusplus +class Path; +#endif + +void config_global_init(void); +void config_global_finish(void); + +/** + * Call this function after all configuration has been evaluated. It + * checks for unused parameters, and logs warnings. + */ +void config_global_check(void); + +#ifdef __cplusplus + +bool +ReadConfigFile(const Path &path, GError **error_r); + +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +/* don't free the returned value + set _last_ to NULL to get first entry */ +gcc_pure +const struct config_param * +config_get_next_param(enum ConfigOption option, + const struct config_param *last); + +gcc_pure +static inline const struct config_param * +config_get_param(enum ConfigOption option) +{ + return config_get_next_param(option, NULL); +} + +/* 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. + However, if the argument was already validated or we don't care + about the argument at all, this may be ignored so in the end, we + should be fine with calling those functions pure. */ + +gcc_pure +const char * +config_get_string(enum ConfigOption option, const char *default_value); + +/** + * Returns an optional configuration variable which contains an + * absolute path. If there is a tilde prefix, it is expanded. + * Returns NULL if the value is not present. If the path could not be + * parsed, returns NULL and sets the error. + * + * The return value must be freed with g_free(). + */ +gcc_malloc +char * +config_dup_path(enum ConfigOption option, GError **error_r); + +gcc_pure +unsigned +config_get_unsigned(enum ConfigOption option, unsigned default_value); + +gcc_pure +unsigned +config_get_positive(enum ConfigOption option, unsigned default_value); + +gcc_pure +bool config_get_bool(enum ConfigOption option, bool default_value); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/conf.h b/src/conf.h index b81b9ccdc..bc17a0c79 100644 --- a/src/conf.h +++ b/src/conf.h @@ -20,11 +20,11 @@ #ifndef MPD_CONF_H #define MPD_CONF_H +#include "ConfigGlobal.hxx" #include "ConfigOption.hxx" #include "ConfigData.hxx" #include "gcc.h" -#include <stdbool.h> #include <glib.h> #define DEFAULT_PLAYLIST_MAX_LENGTH (1024*16) @@ -32,10 +32,6 @@ #define MAX_FILTER_CHAIN_LENGTH 255 -#ifdef __cplusplus -class Path; -#endif - /** * A GQuark for GError instances, resulting from malformed * configuration. @@ -47,72 +43,4 @@ config_quark(void) return g_quark_from_static_string("config"); } -void config_global_init(void); -void config_global_finish(void); - -/** - * Call this function after all configuration has been evaluated. It - * checks for unused parameters, and logs warnings. - */ -void config_global_check(void); - -#ifdef __cplusplus - -bool -ReadConfigFile(const Path &path, GError **error_r); - -#endif - -G_BEGIN_DECLS - -/* don't free the returned value - set _last_ to NULL to get first entry */ -G_GNUC_PURE -const struct config_param * -config_get_next_param(enum ConfigOption option, - const struct config_param *last); - -G_GNUC_PURE -static inline const struct config_param * -config_get_param(enum ConfigOption option) -{ - return config_get_next_param(option, NULL); -} - -/* Note on G_GNUC_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. - However, if the argument was already validated or we don't care - about the argument at all, this may be ignored so in the end, we - should be fine with calling those functions pure. */ - -G_GNUC_PURE -const char * -config_get_string(enum ConfigOption option, const char *default_value); - -/** - * Returns an optional configuration variable which contains an - * absolute path. If there is a tilde prefix, it is expanded. - * Returns NULL if the value is not present. If the path could not be - * parsed, returns NULL and sets the error. - * - * The return value must be freed with g_free(). - */ -G_GNUC_MALLOC -char * -config_dup_path(enum ConfigOption option, GError **error_r); - -G_GNUC_PURE -unsigned -config_get_unsigned(enum ConfigOption option, unsigned default_value); - -G_GNUC_PURE -unsigned -config_get_positive(enum ConfigOption option, unsigned default_value); - -G_GNUC_PURE -bool config_get_bool(enum ConfigOption option, bool default_value); - -G_END_DECLS - #endif |