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.
This commit is contained in:
LemonBoy 2017-06-23 13:30:13 +02:00
commit 795301f679
2 changed files with 15 additions and 13 deletions

View file

@ -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);

View file

@ -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)