From 4a2122eaf4bffdf7ebb012fe6348602d8e54ced0 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Wed, 5 Sep 2007 23:59:33 +0000 Subject: conf: improved boolean config parameter handling from -ke the force flag will issue FATAL() if an invalid value is specified git-svn-id: https://svn.musicpd.org/mpd/trunk@6857 09075e82-0dd4-0310-85a5-a0d7c8717e4f --- src/conf.c | 64 +++++++++++++++++++++++++++++++++---------- src/conf.h | 9 ++++-- src/inputPlugins/mp3_plugin.c | 7 ++--- src/normalize.c | 6 ++-- src/playlist.c | 10 +++---- src/zeroconf.c | 6 ++-- 6 files changed, 68 insertions(+), 34 deletions(-) diff --git a/src/conf.c b/src/conf.c index d76d4ad9b..b93f7c561 100644 --- a/src/conf.c +++ b/src/conf.c @@ -50,6 +50,23 @@ typedef struct _configEntry { static List *configEntriesList; +static int get_bool(const char *value) +{ + const char **x; + static const char *t[] = { "yes", "true", "1", NULL }; + static const char *f[] = { "no", "false", "0", NULL }; + + for (x = t; *x; x++) { + if (!strcasecmp(*x, value)) + return 1; + } + for (x = f; *x; x++) { + if (!strcasecmp(*x, value)) + return 0; + } + return CONF_BOOL_INVALID; +} + static ConfigParam *newConfigParam(char *value, int line) { ConfigParam *ret = xmalloc(sizeof(ConfigParam)); @@ -352,21 +369,6 @@ char *getConfigParamValue(char *name) return param->value; } -int getBoolConfigParam(char *name) -{ - ConfigParam *param; - - param = getConfigParam(name); - if (!param) return -1; - - if (strcmp("yes", param->value) == 0) return 1; - else if (strcmp("no", param->value) == 0) return 0; - - ERROR("%s is not \"yes\" or \"no\" on line %i\n", name, param->line); - - return -2; -} - BlockParam *getBlockParam(ConfigParam * param, char *name) { BlockParam *ret = NULL; @@ -406,3 +408,35 @@ ConfigParam *parseConfigFilePath(char *name, int force) return param; } + +int getBoolConfigParam(char *name, int force) +{ + int ret; + ConfigParam *param = getConfigParam(name); + + if (!param) + return CONF_BOOL_UNSET; + + ret = get_bool(param->value); + if (force && ret == CONF_BOOL_INVALID) + FATAL("%s is not a boolean value (yes, true, 1) or " + "(no, false, 0) on line %i\n", + name, param->line); + return ret; +} + +int getBoolBlockParam(ConfigParam *param, char *name, int force) +{ + int ret; + BlockParam *bp = getBlockParam(param, name); + + if (!bp) + return CONF_BOOL_UNSET; + + ret = get_bool(bp->value); + if (force && ret == CONF_BOOL_INVALID) + FATAL("%s is not a boolean value (yes, true, 1) or " + "(no, false, 0) on line %i\n", bp->value, bp->line); + return ret; +} + diff --git a/src/conf.h b/src/conf.h index f5ef07525..01337d349 100644 --- a/src/conf.h +++ b/src/conf.h @@ -64,6 +64,9 @@ #define CONF_SAVE_ABSOLUTE_PATHS "save_absolute_paths_in_playlists" #define CONF_GAPLESS_MP3_PLAYBACK "gapless_mp3_playback" +#define CONF_BOOL_UNSET -1 +#define CONF_BOOL_INVALID -2 + typedef struct _BlockParam { char *name; char *value; @@ -90,10 +93,12 @@ ConfigParam *getNextConfigParam(char *name, ConfigParam * last); char *getConfigParamValue(char *name); -int getBoolConfigParam(char *name); - BlockParam *getBlockParam(ConfigParam * param, char *name); ConfigParam *parseConfigFilePath(char *name, int force); +int getBoolConfigParam(char *name, int force); + +int getBoolBlockParam(ConfigParam *param, char *name, int force); + #endif diff --git a/src/inputPlugins/mp3_plugin.c b/src/inputPlugins/mp3_plugin.c index 43783070d..dc9e04e5f 100644 --- a/src/inputPlugins/mp3_plugin.c +++ b/src/inputPlugins/mp3_plugin.c @@ -120,11 +120,10 @@ static signed long audio_linear_dither(unsigned int bits, mad_fixed_t sample, static int mp3_plugin_init(void) { - gaplessPlaybackEnabled = getBoolConfigParam(CONF_GAPLESS_MP3_PLAYBACK); - if (gaplessPlaybackEnabled == -1) + gaplessPlaybackEnabled = getBoolConfigParam(CONF_GAPLESS_MP3_PLAYBACK, + 1); + if (gaplessPlaybackEnabled == CONF_BOOL_UNSET) gaplessPlaybackEnabled = DEFAULT_GAPLESS_MP3_PLAYBACK; - else if (gaplessPlaybackEnabled < 0) - exit(EXIT_FAILURE); return 1; } diff --git a/src/normalize.c b/src/normalize.c index 504656c1d..ee21fc919 100644 --- a/src/normalize.c +++ b/src/normalize.c @@ -28,11 +28,9 @@ int normalizationEnabled; void initNormalization(void) { - normalizationEnabled = getBoolConfigParam(CONF_VOLUME_NORMALIZATION); - if (normalizationEnabled == -1) + normalizationEnabled = getBoolConfigParam(CONF_VOLUME_NORMALIZATION, 1); + if (normalizationEnabled == CONF_BOOL_UNSET) normalizationEnabled = DEFAULT_VOLUME_NORMALIZATION; - else if (normalizationEnabled < 0) - exit(EXIT_FAILURE); if (normalizationEnabled) CompressCfg(0, ANTICLIP, TARGET, GAINMAX, GAINSMOOTH, BUCKETS); diff --git a/src/playlist.c b/src/playlist.c index 8b2bc1f42..f4fd02ce1 100644 --- a/src/playlist.c +++ b/src/playlist.c @@ -140,11 +140,11 @@ void initPlaylist(void) } } - playlist_saveAbsolutePaths = getBoolConfigParam(CONF_SAVE_ABSOLUTE_PATHS); - if (playlist_saveAbsolutePaths == -1) - playlist_saveAbsolutePaths = DEFAULT_PLAYLIST_SAVE_ABSOLUTE_PATHS; - else if (playlist_saveAbsolutePaths < 0) - exit(EXIT_FAILURE); + playlist_saveAbsolutePaths = getBoolConfigParam( + CONF_SAVE_ABSOLUTE_PATHS, 1); + if (playlist_saveAbsolutePaths == CONF_BOOL_UNSET) + playlist_saveAbsolutePaths = + DEFAULT_PLAYLIST_SAVE_ABSOLUTE_PATHS; playlist.songs = xmalloc(sizeof(Song *) * playlist_max_length); playlist.songMod = xmalloc(sizeof(mpd_uint32) * playlist_max_length); diff --git a/src/zeroconf.c b/src/zeroconf.c index 1836c64ca..aab09143a 100644 --- a/src/zeroconf.c +++ b/src/zeroconf.c @@ -558,11 +558,9 @@ void initZeroconf(void) const char *serviceName = SERVICE_NAME; ConfigParam *param; - zeroconfEnabled = getBoolConfigParam(CONF_ZEROCONF_ENABLED); - if (zeroconfEnabled == -1) + zeroconfEnabled = getBoolConfigParam(CONF_ZEROCONF_ENABLED, 1); + if (zeroconfEnabled == CONF_BOOL_UNSET) zeroconfEnabled = DEFAULT_ZEROCONF_ENABLED; - else if (zeroconfEnabled < 0) - exit(EXIT_FAILURE); if (!zeroconfEnabled) return; -- cgit v1.2.3