Commit 6e47e797 authored by Max Kellermann's avatar Max Kellermann

ConfigData: move functions into the class

parent d9ea3082
...@@ -39,22 +39,10 @@ config_param::~config_param() ...@@ -39,22 +39,10 @@ config_param::~config_param()
g_free(value); g_free(value);
} }
void const block_param *
config_add_block_param(struct config_param * param, const char *name, config_param::GetBlockParam(const char *name) const
const char *value, int line)
{ {
assert(config_get_block_param(param, name) == NULL); for (const auto &i : block_params) {
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) {
if (i.name == name) { if (i.name == name) {
i.used = true; i.used = true;
return &i; return &i;
...@@ -68,8 +56,10 @@ const char * ...@@ -68,8 +56,10 @@ const char *
config_get_block_string(const struct config_param *param, const char *name, config_get_block_string(const struct config_param *param, const char *name,
const char *default_value) 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) if (bp == NULL)
return default_value; return default_value;
...@@ -90,7 +80,10 @@ config_dup_block_path(const struct config_param *param, const char *name, ...@@ -90,7 +80,10 @@ config_dup_block_path(const struct config_param *param, const char *name,
assert(error_r != NULL); assert(error_r != NULL);
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) if (bp == NULL)
return NULL; return NULL;
...@@ -107,14 +100,15 @@ unsigned ...@@ -107,14 +100,15 @@ unsigned
config_get_block_unsigned(const struct config_param *param, const char *name, config_get_block_unsigned(const struct config_param *param, const char *name,
unsigned default_value) unsigned default_value)
{ {
const struct block_param *bp = config_get_block_param(param, name); if (param == nullptr)
long value; return default_value;
char *endptr;
const block_param *bp = param->GetBlockParam(name);
if (bp == NULL) if (bp == NULL)
return default_value; 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) if (*endptr != 0)
MPD_ERROR("Not a valid number in line %i", bp->line); MPD_ERROR("Not a valid number in line %i", bp->line);
...@@ -128,7 +122,10 @@ bool ...@@ -128,7 +122,10 @@ bool
config_get_block_bool(const struct config_param *param, const char *name, config_get_block_bool(const struct config_param *param, const char *name,
bool default_value) 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; bool success, value;
if (bp == NULL) if (bp == NULL)
......
...@@ -68,8 +68,20 @@ struct config_param { ...@@ -68,8 +68,20 @@ struct config_param {
config_param(int _line=-1) config_param(int _line=-1)
:value(nullptr), line(_line), used(false) {} :value(nullptr), line(_line), used(false) {}
gcc_nonnull_all
config_param(const char *_value, int _line=-1); config_param(const char *_value, int _line=-1);
~config_param(); ~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 #endif
}; };
...@@ -85,14 +97,6 @@ struct ConfigData { ...@@ -85,14 +97,6 @@ struct ConfigData {
extern "C" { extern "C" {
#endif #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 gcc_pure
const char * const char *
config_get_block_string(const struct config_param *param, const char *name, config_get_block_string(const struct config_param *param, const char *name,
......
...@@ -75,7 +75,7 @@ config_read_name_value(struct config_param *param, char *input, unsigned line, ...@@ -75,7 +75,7 @@ config_read_name_value(struct config_param *param, char *input, unsigned line,
return false; return false;
} }
const struct block_param *bp = config_get_block_param(param, name); const struct block_param *bp = param->GetBlockParam(name);
if (bp != NULL) { if (bp != NULL) {
g_set_error(error_r, config_quark(), 0, g_set_error(error_r, config_quark(), 0,
"\"%s\" is duplicate, first defined on line %i", "\"%s\" is duplicate, first defined on line %i",
...@@ -83,7 +83,7 @@ config_read_name_value(struct config_param *param, char *input, unsigned line, ...@@ -83,7 +83,7 @@ config_read_name_value(struct config_param *param, char *input, unsigned line,
return false; return false;
} }
config_add_block_param(param, name, value, line); param->AddBlockParam(name, value, line);
return true; return true;
} }
......
...@@ -190,8 +190,7 @@ glue_db_init_and_load(void) ...@@ -190,8 +190,7 @@ glue_db_init_and_load(void)
if (param == NULL && path != NULL) { if (param == NULL && path != NULL) {
allocated = new config_param("database", path->line); allocated = new config_param("database", path->line);
config_add_block_param(allocated, "path", allocated->AddBlockParam("path", path->value, path->line);
path->value, path->line);
param = allocated; param = allocated;
} }
......
...@@ -108,8 +108,7 @@ main(int argc, char **argv) ...@@ -108,8 +108,7 @@ main(int argc, char **argv)
const struct config_param *path = config_get_param(CONF_DB_FILE); const struct config_param *path = config_get_param(CONF_DB_FILE);
config_param param("database", path->line); config_param param("database", path->line);
if (path != nullptr) if (path != nullptr)
config_add_block_param(&param, "path", path->value, param.AddBlockParam("path", path->value, path->line);
path->line);
Database *db = plugin->create(&param, &error); Database *db = plugin->create(&param, &error);
......
...@@ -74,7 +74,7 @@ int main(int argc, char **argv) ...@@ -74,7 +74,7 @@ int main(int argc, char **argv)
} }
config_param param; config_param param;
config_add_block_param(&param, "quality", "5.0", -1); param.AddBlockParam("quality", "5.0", -1);
encoder = encoder_init(plugin, &param, &error); encoder = encoder_init(plugin, &param, &error);
if (encoder == NULL) { if (encoder == NULL) {
......
...@@ -54,7 +54,7 @@ main(G_GNUC_UNUSED int argc, G_GNUC_UNUSED char **argv) ...@@ -54,7 +54,7 @@ main(G_GNUC_UNUSED int argc, G_GNUC_UNUSED char **argv)
assert(plugin != NULL); assert(plugin != NULL);
config_param param; 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); struct encoder *encoder = encoder_init(plugin, &param, NULL);
assert(encoder != NULL); assert(encoder != NULL);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment