Commit d9ea3082 authored by Max Kellermann's avatar Max Kellermann

ConfigData: add constructors/destructors

parent 2d63c269
...@@ -31,29 +31,12 @@ extern "C" { ...@@ -31,29 +31,12 @@ extern "C" {
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
struct config_param * config_param::config_param(const char *_value, int _line)
config_new_param(const char *value, int line) :value(g_strdup(_value)), line(_line) {}
{
config_param *ret = new config_param();
if (!value)
ret->value = NULL;
else
ret->value = g_strdup(value);
ret->line = line;
ret->used = false; config_param::~config_param()
return ret;
}
void
config_param_free(struct config_param *param)
{ {
g_free(param->value); g_free(value);
delete param;
} }
void void
...@@ -62,13 +45,7 @@ config_add_block_param(struct config_param * param, const char *name, ...@@ -62,13 +45,7 @@ config_add_block_param(struct config_param * param, const char *name,
{ {
assert(config_get_block_param(param, name) == NULL); assert(config_get_block_param(param, name) == NULL);
param->block_params.push_back(block_param()); param->block_params.emplace_back(name, value, line);
struct block_param *bp = &param->block_params.back();
bp->name = name;
bp->value = value;
bp->line = line;
bp->used = false;
} }
const struct block_param * const struct block_param *
......
...@@ -45,6 +45,10 @@ struct block_param { ...@@ -45,6 +45,10 @@ struct block_param {
* this option yet. * this option yet.
*/ */
mutable bool used; mutable bool used;
gcc_nonnull_all
block_param(const char *_name, const char *_value, int _line=-1)
:name(_name), value(_value), line(_line), used(false) {}
}; };
#endif #endif
...@@ -61,6 +65,11 @@ struct config_param { ...@@ -61,6 +65,11 @@ struct config_param {
* this option yet. * this option yet.
*/ */
bool used; bool used;
config_param(int _line=-1)
:value(nullptr), line(_line), used(false) {}
config_param(const char *_value, int _line=-1);
~config_param();
#endif #endif
}; };
...@@ -76,13 +85,6 @@ struct ConfigData { ...@@ -76,13 +85,6 @@ struct ConfigData {
extern "C" { extern "C" {
#endif #endif
gcc_malloc
struct config_param *
config_new_param(const char *value, int line);
void
config_param_free(struct config_param *param);
void void
config_add_block_param(struct config_param * param, const char *name, config_add_block_param(struct config_param * param, const char *name,
const char *value, int line); const char *value, int line);
......
...@@ -90,7 +90,7 @@ config_read_name_value(struct config_param *param, char *input, unsigned line, ...@@ -90,7 +90,7 @@ config_read_name_value(struct config_param *param, char *input, unsigned line,
static struct config_param * static struct config_param *
config_read_block(FILE *fp, int *count, char *string, GError **error_r) config_read_block(FILE *fp, int *count, char *string, GError **error_r)
{ {
struct config_param *ret = config_new_param(NULL, *count); struct config_param *ret = new config_param(*count);
GError *error = NULL; GError *error = NULL;
while (true) { while (true) {
...@@ -98,7 +98,7 @@ config_read_block(FILE *fp, int *count, char *string, GError **error_r) ...@@ -98,7 +98,7 @@ config_read_block(FILE *fp, int *count, char *string, GError **error_r)
line = fgets(string, MAX_STRING_SIZE, fp); line = fgets(string, MAX_STRING_SIZE, fp);
if (line == NULL) { if (line == NULL) {
config_param_free(ret); delete ret;
g_set_error(error_r, config_quark(), 0, g_set_error(error_r, config_quark(), 0,
"Expected '}' before end-of-file"); "Expected '}' before end-of-file");
return NULL; return NULL;
...@@ -115,7 +115,7 @@ config_read_block(FILE *fp, int *count, char *string, GError **error_r) ...@@ -115,7 +115,7 @@ config_read_block(FILE *fp, int *count, char *string, GError **error_r)
line = strchug_fast(line + 1); line = strchug_fast(line + 1);
if (*line != 0 && *line != CONF_COMMENT) { if (*line != 0 && *line != CONF_COMMENT) {
config_param_free(ret); delete ret;
g_set_error(error_r, config_quark(), 0, g_set_error(error_r, config_quark(), 0,
"line %i: Unknown tokens after '}'", "line %i: Unknown tokens after '}'",
*count); *count);
...@@ -129,7 +129,7 @@ config_read_block(FILE *fp, int *count, char *string, GError **error_r) ...@@ -129,7 +129,7 @@ config_read_block(FILE *fp, int *count, char *string, GError **error_r)
if (!config_read_name_value(ret, line, *count, &error)) { if (!config_read_name_value(ret, line, *count, &error)) {
assert(*line != 0); assert(*line != 0);
config_param_free(ret); delete ret;
g_propagate_prefixed_error(error_r, error, g_propagate_prefixed_error(error_r, error,
"line %i: ", *count); "line %i: ", *count);
return NULL; return NULL;
...@@ -241,7 +241,7 @@ ReadConfigFile(ConfigData &config_data, FILE *fp, GError **error_r) ...@@ -241,7 +241,7 @@ ReadConfigFile(ConfigData &config_data, FILE *fp, GError **error_r)
return false; return false;
} }
param = config_new_param(value, count); param = new config_param(value, count);
} }
params = g_slist_append(params, param); params = g_slist_append(params, param);
......
...@@ -44,7 +44,7 @@ config_param_free_callback(gpointer data, G_GNUC_UNUSED gpointer user_data) ...@@ -44,7 +44,7 @@ config_param_free_callback(gpointer data, G_GNUC_UNUSED gpointer user_data)
{ {
struct config_param *param = (struct config_param *)data; struct config_param *param = (struct config_param *)data;
config_param_free(param); delete param;
} }
void config_global_finish(void) void config_global_finish(void)
......
...@@ -189,7 +189,7 @@ glue_db_init_and_load(void) ...@@ -189,7 +189,7 @@ glue_db_init_and_load(void)
struct config_param *allocated = NULL; struct config_param *allocated = NULL;
if (param == NULL && path != NULL) { if (param == NULL && path != NULL) {
allocated = config_new_param("database", path->line); allocated = new config_param("database", path->line);
config_add_block_param(allocated, "path", config_add_block_param(allocated, "path",
path->value, path->line); path->value, path->line);
param = allocated; param = allocated;
...@@ -198,8 +198,7 @@ glue_db_init_and_load(void) ...@@ -198,8 +198,7 @@ glue_db_init_and_load(void)
if (!DatabaseGlobalInit(param, &error)) if (!DatabaseGlobalInit(param, &error))
MPD_ERROR("%s", error->message); MPD_ERROR("%s", error->message);
if (allocated != NULL) delete allocated;
config_param_free(allocated);
ret = DatabaseGlobalOpen(&error); ret = DatabaseGlobalOpen(&error);
if (!ret) if (!ret)
......
...@@ -106,13 +106,12 @@ main(int argc, char **argv) ...@@ -106,13 +106,12 @@ main(int argc, char **argv)
/* do it */ /* do it */
const struct config_param *path = config_get_param(CONF_DB_FILE); const struct config_param *path = config_get_param(CONF_DB_FILE);
struct config_param *param = config_new_param("database", path->line); config_param param("database", path->line);
if (path != nullptr) if (path != nullptr)
config_add_block_param(param, "path", path->value, path->line); config_add_block_param(&param, "path", path->value,
path->line);
Database *db = plugin->create(param, &error); Database *db = plugin->create(&param, &error);
config_param_free(param);
if (db == nullptr) { if (db == nullptr) {
cerr << error->message << endl; cerr << error->message << endl;
......
...@@ -49,7 +49,6 @@ int main(int argc, char **argv) ...@@ -49,7 +49,6 @@ int main(int argc, char **argv)
const char *encoder_name; const char *encoder_name;
const struct encoder_plugin *plugin; const struct encoder_plugin *plugin;
struct encoder *encoder; struct encoder *encoder;
struct config_param *param;
static char buffer[32768]; static char buffer[32768];
/* parse command line */ /* parse command line */
...@@ -74,10 +73,10 @@ int main(int argc, char **argv) ...@@ -74,10 +73,10 @@ int main(int argc, char **argv)
return 1; return 1;
} }
param = config_new_param(NULL, -1); config_param param;
config_add_block_param(param, "quality", "5.0", -1); config_add_block_param(&param, "quality", "5.0", -1);
encoder = encoder_init(plugin, param, &error); encoder = encoder_init(plugin, &param, &error);
if (encoder == NULL) { if (encoder == NULL) {
g_printerr("Failed to initialize encoder: %s\n", g_printerr("Failed to initialize encoder: %s\n",
error->message); error->message);
......
...@@ -53,10 +53,10 @@ main(G_GNUC_UNUSED int argc, G_GNUC_UNUSED char **argv) ...@@ -53,10 +53,10 @@ main(G_GNUC_UNUSED int argc, G_GNUC_UNUSED char **argv)
const struct encoder_plugin *plugin = encoder_plugin_get("vorbis"); const struct encoder_plugin *plugin = encoder_plugin_get("vorbis");
assert(plugin != NULL); assert(plugin != NULL);
struct config_param *param = config_new_param(NULL, -1); config_param param;
config_add_block_param(param, "quality", "5.0", -1); config_add_block_param(&param, "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);
/* open the encoder */ /* open the encoder */
......
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