ConfigData: move code to block_param, config_param methods

This commit is contained in:
Max Kellermann 2013-08-04 12:03:56 +02:00
parent acd742d225
commit 83f4c48c8a
2 changed files with 113 additions and 42 deletions

View File

@ -28,6 +28,32 @@
#include <string.h>
#include <stdlib.h>
unsigned
block_param::GetUnsignedValue() const
{
char *endptr;
long value2 = strtol(value.c_str(), &endptr, 0);
if (*endptr != 0)
MPD_ERROR("Not a valid number in line %i", line);
if (value2 < 0)
MPD_ERROR("Not a positive number in line %i", line);
return (unsigned)value2;
}
bool
block_param::GetBoolValue() const
{
bool value2;
if (!get_bool(value.c_str(), &value2))
MPD_ERROR("%s is not a boolean value (yes, true, 1) or "
"(no, false, 0) on line %i\n",
name.c_str(), line);
return value2;
}
config_param::config_param(const char *_value, int _line)
:next(nullptr), value(g_strdup(_value)), line(_line) {}
@ -50,6 +76,62 @@ config_param::GetBlockParam(const char *name) const
return NULL;
}
const char *
config_param::GetBlockValue(const char *name, const char *default_value) const
{
const block_param *bp = GetBlockParam(name);
if (bp == nullptr)
return default_value;
return bp->value.c_str();
}
char *
config_param::DupBlockString(const char *name, const char *default_value) const
{
return g_strdup(GetBlockValue(name, default_value));
}
char *
config_param::DupBlockPath(const char *name, GError **error_r) const
{
assert(error_r != nullptr);
assert(*error_r == nullptr);
const block_param *bp = GetBlockParam(name);
if (bp == nullptr)
return nullptr;
char *path = parsePath(bp->value.c_str(), error_r);
if (G_UNLIKELY(path == nullptr))
g_prefix_error(error_r,
"Invalid path in \"%s\" at line %i: ",
name, bp->line);
return path;
}
unsigned
config_param::GetBlockValue(const char *name, unsigned default_value) const
{
const block_param *bp = GetBlockParam(name);
if (bp == nullptr)
return default_value;
return bp->GetUnsignedValue();
}
gcc_pure
bool
config_param::GetBlockValue(const char *name, bool default_value) const
{
const block_param *bp = GetBlockParam(name);
if (bp == NULL)
return default_value;
return bp->GetBoolValue();
}
const char *
config_get_block_string(const struct config_param *param, const char *name,
const char *default_value)
@ -57,11 +139,7 @@ config_get_block_string(const struct config_param *param, const char *name,
if (param == nullptr)
return default_value;
const block_param *bp = param->GetBlockParam(name);
if (bp == NULL)
return default_value;
return bp->value.c_str();
return param->GetBlockValue(name, default_value);
}
char *
@ -81,17 +159,7 @@ config_dup_block_path(const struct config_param *param, const char *name,
if (param == nullptr)
return nullptr;
const block_param *bp = param->GetBlockParam(name);
if (bp == NULL)
return NULL;
char *path = parsePath(bp->value.c_str(), error_r);
if (G_UNLIKELY(path == NULL))
g_prefix_error(error_r,
"Invalid path in \"%s\" at line %i: ",
name, bp->line);
return path;
return param->DupBlockPath(name, error_r);
}
unsigned
@ -101,19 +169,7 @@ config_get_block_unsigned(const struct config_param *param, const char *name,
if (param == nullptr)
return default_value;
const block_param *bp = param->GetBlockParam(name);
if (bp == NULL)
return default_value;
char *endptr;
long value = strtol(bp->value.c_str(), &endptr, 0);
if (*endptr != 0)
MPD_ERROR("Not a valid number in line %i", bp->line);
if (value < 0)
MPD_ERROR("Not a positive number in line %i", bp->line);
return (unsigned)value;
return param->GetBlockValue(name, default_value);
}
bool
@ -123,17 +179,5 @@ config_get_block_bool(const struct config_param *param, const char *name,
if (param == nullptr)
return default_value;
const block_param *bp = param->GetBlockParam(name);
bool success, value;
if (bp == NULL)
return default_value;
success = get_bool(bp->value.c_str(), &value);
if (!success)
MPD_ERROR("%s is not a boolean value (yes, true, 1) or "
"(no, false, 0) on line %i\n",
name, bp->line);
return value;
return param->GetBlockValue(name, default_value);
}

View File

@ -42,6 +42,12 @@ struct block_param {
gcc_nonnull_all
block_param(const char *_name, const char *_value, int _line=-1)
:name(_name), value(_value), line(_line), used(false) {}
gcc_pure
unsigned GetUnsignedValue() const;
gcc_pure
bool GetBoolValue() const;
};
struct config_param {
@ -82,6 +88,27 @@ struct config_param {
gcc_nonnull_all gcc_pure
const block_param *GetBlockParam(const char *_name) const;
gcc_pure
const char *GetBlockValue(const char *name,
const char *default_value=nullptr) const;
gcc_malloc
char *DupBlockString(const char *name,
const char *default_value=nullptr) const;
/**
* Same as config_dup_path(), but looks up the setting in the
* specified block.
*/
gcc_malloc
char *DupBlockPath(const char *name, GError **error_r) const;
gcc_pure
unsigned GetBlockValue(const char *name, unsigned default_value) const;
gcc_pure
bool GetBlockValue(const char *name, bool default_value) const;
};
struct ConfigData {