From 795301f679093489eab1c977f3f9b750a5301056 Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Fri, 23 Jun 2017 13:30:13 +0200 Subject: [PATCH] Return the freshly created config nodes The caller may want to modify the node itself and you cannot easily grab a reference to that after it is created. --- src/lib-config/iconfig.h | 6 +++--- src/lib-config/set.c | 22 ++++++++++++---------- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/lib-config/iconfig.h b/src/lib-config/iconfig.h index 91583e40..9b6e5c1e 100644 --- a/src/lib-config/iconfig.h +++ b/src/lib-config/iconfig.h @@ -141,9 +141,9 @@ int config_node_get_bool(CONFIG_NODE *parent, const char *key, int def); * if a node with key 'key' exists change its value to 'value', * otherwise create a new node with type NODE_TYPE_KEY, key 'key' and value 'value' * */ -void config_node_set_str(CONFIG_REC *rec, CONFIG_NODE *parent, const char *key, const char *value); -void config_node_set_int(CONFIG_REC *rec, CONFIG_NODE *parent, const char *key, int value); -void config_node_set_bool(CONFIG_REC *rec, CONFIG_NODE *parent, const char *key, int value); +CONFIG_NODE *config_node_set_str(CONFIG_REC *rec, CONFIG_NODE *parent, const char *key, const char *value); +CONFIG_NODE *config_node_set_int(CONFIG_REC *rec, CONFIG_NODE *parent, const char *key, int value); +CONFIG_NODE *config_node_set_bool(CONFIG_REC *rec, CONFIG_NODE *parent, const char *key, int value); /* Remove one node from block/list. */ void config_node_remove(CONFIG_REC *rec, CONFIG_NODE *parent, CONFIG_NODE *node); diff --git a/src/lib-config/set.c b/src/lib-config/set.c index 0de6c503..974b0c0f 100644 --- a/src/lib-config/set.c +++ b/src/lib-config/set.c @@ -88,14 +88,14 @@ void config_nodes_remove_all(CONFIG_REC *rec) config_node_remove(rec, rec->mainnode, ((GSList *) rec->mainnode->value)->data); } -void config_node_set_str(CONFIG_REC *rec, CONFIG_NODE *parent, const char *key, const char *value) +CONFIG_NODE *config_node_set_str(CONFIG_REC *rec, CONFIG_NODE *parent, const char *key, const char *value) { CONFIG_NODE *node; int no_key; - g_return_if_fail(rec != NULL); - g_return_if_fail(parent != NULL); - g_return_if_fail(is_node_list(parent)); + g_return_val_if_fail(rec != NULL, NULL); + g_return_val_if_fail(parent != NULL, NULL); + g_return_val_if_fail(is_node_list(parent), NULL); no_key = key == NULL; node = no_key ? NULL : config_node_find(parent, key); @@ -103,7 +103,7 @@ void config_node_set_str(CONFIG_REC *rec, CONFIG_NODE *parent, const char *key, if (value == NULL) { /* remove the key */ if (node != NULL) config_node_remove(rec, parent, node); - return; + return NULL; } if (node != NULL && !has_node_value(node)) { @@ -114,7 +114,7 @@ void config_node_set_str(CONFIG_REC *rec, CONFIG_NODE *parent, const char *key, } if (node != NULL) { if (g_strcmp0(node->value, value) == 0) - return; + return node; g_free(node->value); } else { node = g_new0(CONFIG_NODE, 1); @@ -126,19 +126,21 @@ void config_node_set_str(CONFIG_REC *rec, CONFIG_NODE *parent, const char *key, node->value = g_strdup(value); rec->modifycounter++; + + return node; } -void config_node_set_int(CONFIG_REC *rec, CONFIG_NODE *parent, const char *key, int value) +CONFIG_NODE *config_node_set_int(CONFIG_REC *rec, CONFIG_NODE *parent, const char *key, int value) { char str[MAX_INT_STRLEN]; g_snprintf(str, sizeof(str), "%d", value); - config_node_set_str(rec, parent, key, str); + return config_node_set_str(rec, parent, key, str); } -void config_node_set_bool(CONFIG_REC *rec, CONFIG_NODE *parent, const char *key, int value) +CONFIG_NODE *config_node_set_bool(CONFIG_REC *rec, CONFIG_NODE *parent, const char *key, int value) { - config_node_set_str(rec, parent, key, value ? "yes" : "no"); + return config_node_set_str(rec, parent, key, value ? "yes" : "no"); } int config_set_str(CONFIG_REC *rec, const char *section, const char *key, const char *value)