conf: make get_bool() return a bool
Instead of returning an artificial three-state integer, return a "success" value and put the boolean value into a "bool" pointer. That's a little bit more overhead, but an API which looks more natural.
This commit is contained in:
parent
093e900d44
commit
5d583c9b2d
37
src/conf.c
37
src/conf.c
@ -106,18 +106,23 @@ string_array_contains(const char *const* array, const char *value)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int get_bool(const char *value)
|
static bool
|
||||||
|
get_bool(const char *value, bool *value_r)
|
||||||
{
|
{
|
||||||
static const char *t[] = { "yes", "true", "1", NULL };
|
static const char *t[] = { "yes", "true", "1", NULL };
|
||||||
static const char *f[] = { "no", "false", "0", NULL };
|
static const char *f[] = { "no", "false", "0", NULL };
|
||||||
|
|
||||||
if (string_array_contains(t, value))
|
if (string_array_contains(t, value)) {
|
||||||
|
*value_r = true;
|
||||||
return true;
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
if (string_array_contains(f, value))
|
if (string_array_contains(f, value)) {
|
||||||
return false;
|
*value_r = false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
return CONF_BOOL_INVALID;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct config_param *
|
struct config_param *
|
||||||
@ -423,21 +428,18 @@ config_get_block_param(const struct config_param * param, const char *name)
|
|||||||
bool config_get_bool(const char *name, bool default_value)
|
bool config_get_bool(const char *name, bool default_value)
|
||||||
{
|
{
|
||||||
const struct config_param *param = config_get_param(name);
|
const struct config_param *param = config_get_param(name);
|
||||||
int value;
|
bool success, value;
|
||||||
|
|
||||||
if (param == NULL)
|
if (param == NULL)
|
||||||
return default_value;
|
return default_value;
|
||||||
|
|
||||||
value = get_bool(param->value);
|
success = get_bool(param->value, &value);
|
||||||
if (value == CONF_BOOL_INVALID)
|
if (!success)
|
||||||
g_error("%s is not a boolean value (yes, true, 1) or "
|
g_error("%s is not a boolean value (yes, true, 1) or "
|
||||||
"(no, false, 0) on line %i\n",
|
"(no, false, 0) on line %i\n",
|
||||||
name, param->line);
|
name, param->line);
|
||||||
|
|
||||||
if (value == CONF_BOOL_UNSET)
|
return value;
|
||||||
return default_value;
|
|
||||||
|
|
||||||
return !!value;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *
|
const char *
|
||||||
@ -478,19 +480,16 @@ config_get_block_bool(const struct config_param *param, const char *name,
|
|||||||
bool default_value)
|
bool default_value)
|
||||||
{
|
{
|
||||||
struct block_param *bp = config_get_block_param(param, name);
|
struct block_param *bp = config_get_block_param(param, name);
|
||||||
int value;
|
bool success, value;
|
||||||
|
|
||||||
if (bp == NULL)
|
if (bp == NULL)
|
||||||
return default_value;
|
return default_value;
|
||||||
|
|
||||||
value = get_bool(bp->value);
|
success = get_bool(param->value, &value);
|
||||||
if (value == CONF_BOOL_INVALID)
|
if (!success)
|
||||||
g_error("%s is not a boolean value (yes, true, 1) or "
|
g_error("%s is not a boolean value (yes, true, 1) or "
|
||||||
"(no, false, 0) on line %i\n",
|
"(no, false, 0) on line %i\n",
|
||||||
name, bp->line);
|
name, bp->line);
|
||||||
|
|
||||||
if (value == CONF_BOOL_UNSET)
|
return value;
|
||||||
return default_value;
|
|
||||||
|
|
||||||
return !!value;
|
|
||||||
}
|
}
|
||||||
|
@ -68,9 +68,6 @@
|
|||||||
#define CONF_INPUT "input"
|
#define CONF_INPUT "input"
|
||||||
#define CONF_GAPLESS_MP3_PLAYBACK "gapless_mp3_playback"
|
#define CONF_GAPLESS_MP3_PLAYBACK "gapless_mp3_playback"
|
||||||
|
|
||||||
#define CONF_BOOL_UNSET -1
|
|
||||||
#define CONF_BOOL_INVALID -2
|
|
||||||
|
|
||||||
#define DEFAULT_PLAYLIST_MAX_LENGTH (1024*16)
|
#define DEFAULT_PLAYLIST_MAX_LENGTH (1024*16)
|
||||||
#define DEFAULT_PLAYLIST_SAVE_ABSOLUTE_PATHS false
|
#define DEFAULT_PLAYLIST_SAVE_ABSOLUTE_PATHS false
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user