ConfigData: add constructors/destructors

This commit is contained in:
Max Kellermann 2013-01-30 22:00:32 +01:00
parent 2d63c26936
commit d9ea3082fb
8 changed files with 33 additions and 57 deletions

View File

@ -31,29 +31,12 @@ extern "C" {
#include <string.h>
#include <stdlib.h>
struct config_param *
config_new_param(const char *value, int line)
config_param::config_param(const char *_value, int _line)
:value(g_strdup(_value)), line(_line) {}
config_param::~config_param()
{
config_param *ret = new config_param();
if (!value)
ret->value = NULL;
else
ret->value = g_strdup(value);
ret->line = line;
ret->used = false;
return ret;
}
void
config_param_free(struct config_param *param)
{
g_free(param->value);
delete param;
g_free(value);
}
void
@ -62,13 +45,7 @@ config_add_block_param(struct config_param * param, const char *name,
{
assert(config_get_block_param(param, name) == NULL);
param->block_params.push_back(block_param());
struct block_param *bp = &param->block_params.back();
bp->name = name;
bp->value = value;
bp->line = line;
bp->used = false;
param->block_params.emplace_back(name, value, line);
}
const struct block_param *

View File

@ -45,6 +45,10 @@ struct block_param {
* this option yet.
*/
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
@ -61,6 +65,11 @@ struct config_param {
* this option yet.
*/
bool used;
config_param(int _line=-1)
:value(nullptr), line(_line), used(false) {}
config_param(const char *_value, int _line=-1);
~config_param();
#endif
};
@ -76,13 +85,6 @@ struct ConfigData {
extern "C" {
#endif
gcc_malloc
struct config_param *
config_new_param(const char *value, int line);
void
config_param_free(struct config_param *param);
void
config_add_block_param(struct config_param * param, const char *name,
const char *value, int line);

View File

@ -90,7 +90,7 @@ config_read_name_value(struct config_param *param, char *input, unsigned line,
static struct config_param *
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;
while (true) {
@ -98,7 +98,7 @@ config_read_block(FILE *fp, int *count, char *string, GError **error_r)
line = fgets(string, MAX_STRING_SIZE, fp);
if (line == NULL) {
config_param_free(ret);
delete ret;
g_set_error(error_r, config_quark(), 0,
"Expected '}' before end-of-file");
return NULL;
@ -115,7 +115,7 @@ config_read_block(FILE *fp, int *count, char *string, GError **error_r)
line = strchug_fast(line + 1);
if (*line != 0 && *line != CONF_COMMENT) {
config_param_free(ret);
delete ret;
g_set_error(error_r, config_quark(), 0,
"line %i: Unknown tokens after '}'",
*count);
@ -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)) {
assert(*line != 0);
config_param_free(ret);
delete ret;
g_propagate_prefixed_error(error_r, error,
"line %i: ", *count);
return NULL;
@ -241,7 +241,7 @@ ReadConfigFile(ConfigData &config_data, FILE *fp, GError **error_r)
return false;
}
param = config_new_param(value, count);
param = new config_param(value, count);
}
params = g_slist_append(params, param);

View File

@ -44,7 +44,7 @@ config_param_free_callback(gpointer data, G_GNUC_UNUSED gpointer user_data)
{
struct config_param *param = (struct config_param *)data;
config_param_free(param);
delete param;
}
void config_global_finish(void)

View File

@ -189,7 +189,7 @@ glue_db_init_and_load(void)
struct config_param *allocated = 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",
path->value, path->line);
param = allocated;
@ -198,8 +198,7 @@ glue_db_init_and_load(void)
if (!DatabaseGlobalInit(param, &error))
MPD_ERROR("%s", error->message);
if (allocated != NULL)
config_param_free(allocated);
delete allocated;
ret = DatabaseGlobalOpen(&error);
if (!ret)

View File

@ -106,13 +106,12 @@ main(int argc, char **argv)
/* do it */
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)
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);
config_param_free(param);
Database *db = plugin->create(&param, &error);
if (db == nullptr) {
cerr << error->message << endl;

View File

@ -49,7 +49,6 @@ int main(int argc, char **argv)
const char *encoder_name;
const struct encoder_plugin *plugin;
struct encoder *encoder;
struct config_param *param;
static char buffer[32768];
/* parse command line */
@ -74,10 +73,10 @@ int main(int argc, char **argv)
return 1;
}
param = config_new_param(NULL, -1);
config_add_block_param(param, "quality", "5.0", -1);
config_param param;
config_add_block_param(&param, "quality", "5.0", -1);
encoder = encoder_init(plugin, param, &error);
encoder = encoder_init(plugin, &param, &error);
if (encoder == NULL) {
g_printerr("Failed to initialize encoder: %s\n",
error->message);

View File

@ -53,10 +53,10 @@ main(G_GNUC_UNUSED int argc, G_GNUC_UNUSED char **argv)
const struct encoder_plugin *plugin = encoder_plugin_get("vorbis");
assert(plugin != NULL);
struct config_param *param = config_new_param(NULL, -1);
config_add_block_param(param, "quality", "5.0", -1);
config_param param;
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);
/* open the encoder */