ConfigData: move functions into the class

This commit is contained in:
Max Kellermann 2013-01-30 22:25:17 +01:00
parent d9ea3082fb
commit 6e47e79790
7 changed files with 37 additions and 38 deletions

View File

@ -39,22 +39,10 @@ config_param::~config_param()
g_free(value);
}
void
config_add_block_param(struct config_param * param, const char *name,
const char *value, int line)
const block_param *
config_param::GetBlockParam(const char *name) const
{
assert(config_get_block_param(param, name) == NULL);
param->block_params.emplace_back(name, value, line);
}
const struct block_param *
config_get_block_param(const struct config_param * param, const char *name)
{
if (param == NULL)
return NULL;
for (auto &i : param->block_params) {
for (const auto &i : block_params) {
if (i.name == name) {
i.used = true;
return &i;
@ -68,8 +56,10 @@ const char *
config_get_block_string(const struct config_param *param, const char *name,
const char *default_value)
{
const struct block_param *bp = config_get_block_param(param, name);
if (param == nullptr)
return default_value;
const block_param *bp = param->GetBlockParam(name);
if (bp == NULL)
return default_value;
@ -90,7 +80,10 @@ config_dup_block_path(const struct config_param *param, const char *name,
assert(error_r != NULL);
assert(*error_r == NULL);
const struct block_param *bp = config_get_block_param(param, name);
if (param == nullptr)
return nullptr;
const block_param *bp = param->GetBlockParam(name);
if (bp == NULL)
return NULL;
@ -107,14 +100,15 @@ unsigned
config_get_block_unsigned(const struct config_param *param, const char *name,
unsigned default_value)
{
const struct block_param *bp = config_get_block_param(param, name);
long value;
char *endptr;
if (param == nullptr)
return default_value;
const block_param *bp = param->GetBlockParam(name);
if (bp == NULL)
return default_value;
value = strtol(bp->value.c_str(), &endptr, 0);
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);
@ -128,7 +122,10 @@ bool
config_get_block_bool(const struct config_param *param, const char *name,
bool default_value)
{
const struct block_param *bp = config_get_block_param(param, name);
if (param == nullptr)
return default_value;
const block_param *bp = param->GetBlockParam(name);
bool success, value;
if (bp == NULL)

View File

@ -68,8 +68,20 @@ struct config_param {
config_param(int _line=-1)
:value(nullptr), line(_line), used(false) {}
gcc_nonnull_all
config_param(const char *_value, int _line=-1);
~config_param();
gcc_nonnull_all
void AddBlockParam(const char *_name, const char *_value,
int _line=-1) {
block_params.emplace_back(_name, _value, _line);
}
gcc_nonnull_all gcc_pure
const block_param *GetBlockParam(const char *_name) const;
#endif
};
@ -85,14 +97,6 @@ struct ConfigData {
extern "C" {
#endif
void
config_add_block_param(struct config_param * param, const char *name,
const char *value, int line);
gcc_pure
const struct block_param *
config_get_block_param(const struct config_param *param, const char *name);
gcc_pure
const char *
config_get_block_string(const struct config_param *param, const char *name,

View File

@ -75,7 +75,7 @@ config_read_name_value(struct config_param *param, char *input, unsigned line,
return false;
}
const struct block_param *bp = config_get_block_param(param, name);
const struct block_param *bp = param->GetBlockParam(name);
if (bp != NULL) {
g_set_error(error_r, config_quark(), 0,
"\"%s\" is duplicate, first defined on line %i",
@ -83,7 +83,7 @@ config_read_name_value(struct config_param *param, char *input, unsigned line,
return false;
}
config_add_block_param(param, name, value, line);
param->AddBlockParam(name, value, line);
return true;
}

View File

@ -190,8 +190,7 @@ glue_db_init_and_load(void)
if (param == NULL && path != NULL) {
allocated = new config_param("database", path->line);
config_add_block_param(allocated, "path",
path->value, path->line);
allocated->AddBlockParam("path", path->value, path->line);
param = allocated;
}

View File

@ -108,8 +108,7 @@ main(int argc, char **argv)
const struct config_param *path = config_get_param(CONF_DB_FILE);
config_param param("database", path->line);
if (path != nullptr)
config_add_block_param(&param, "path", path->value,
path->line);
param.AddBlockParam("path", path->value, path->line);
Database *db = plugin->create(&param, &error);

View File

@ -74,7 +74,7 @@ int main(int argc, char **argv)
}
config_param param;
config_add_block_param(&param, "quality", "5.0", -1);
param.AddBlockParam("quality", "5.0", -1);
encoder = encoder_init(plugin, &param, &error);
if (encoder == NULL) {

View File

@ -54,7 +54,7 @@ main(G_GNUC_UNUSED int argc, G_GNUC_UNUSED char **argv)
assert(plugin != NULL);
config_param param;
config_add_block_param(&param, "quality", "5.0", -1);
param.AddBlockParam("quality", "5.0", -1);
struct encoder *encoder = encoder_init(plugin, &param, NULL);
assert(encoder != NULL);