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
This commit is contained in:
parent
ac58dce7df
commit
4a2122eaf4
64
src/conf.c
64
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;
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user