1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
/*
set.c : irssi configuration - change settings in memory
Copyright (C) 1999 Timo Sirainen
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "module.h"
static void cache_remove(CONFIG_REC *rec, CONFIG_NODE *node)
{
char *path;
path = g_hash_table_lookup(rec->cache_nodes, node);
if (path != NULL) {
g_hash_table_remove(rec->cache, path);
g_hash_table_remove(rec->cache_nodes, node);
g_free(path);
}
}
void config_node_remove(CONFIG_REC *rec, CONFIG_NODE *parent, CONFIG_NODE *node)
{
g_return_if_fail(parent != NULL);
g_return_if_fail(node != NULL);
rec->modifycounter++;
cache_remove(rec, node);
parent->value = g_slist_remove(parent->value, node);
switch (node->type) {
case NODE_TYPE_KEY:
case NODE_TYPE_VALUE:
case NODE_TYPE_COMMENT:
g_free_not_null(node->value);
break;
case NODE_TYPE_BLOCK:
case NODE_TYPE_LIST:
while (node->value != NULL)
config_node_remove(rec, node, ((GSList *) node->value)->data);
break;
}
g_free_not_null(node->key);
g_free(node);
}
/* Remove n'th node from a list */
void config_node_list_remove(CONFIG_REC *rec, CONFIG_NODE *node, int index)
{
CONFIG_NODE *child;
g_return_if_fail(node != NULL);
g_return_if_fail(is_node_list(node));
child = config_node_index(node, index);
if (child != NULL) config_node_remove(rec, node, child);
}
/* Clear all data inside node, but leave the node */
void config_node_clear(CONFIG_REC *rec, CONFIG_NODE *node)
{
g_return_if_fail(node != NULL);
g_return_if_fail(is_node_list(node));
while (node->value != NULL)
config_node_remove(rec, node, ((GSList *) node->value)->data);
}
void config_nodes_remove_all(CONFIG_REC *rec)
{
g_return_if_fail(rec != NULL);
while (rec->mainnode->value != NULL)
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 *node;
int no_key;
g_return_if_fail(rec != NULL);
g_return_if_fail(parent != NULL);
no_key = key == NULL;
node = no_key ? NULL : config_node_find(parent, key);
if (value == NULL) {
/* remove the key */
if (node != NULL) config_node_remove(rec, parent, node);
return;
}
if (node != NULL) {
if (strcmp(node->value, value) == 0)
return;
g_free(node->value);
} else {
node = g_new0(CONFIG_NODE, 1);
parent->value = g_slist_append(parent->value, node);
node->type = no_key ? NODE_TYPE_VALUE : NODE_TYPE_KEY;
node->key = no_key ? NULL : g_strdup(key);
}
node->value = g_strdup(value);
rec->modifycounter++;
}
void 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);
}
void 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");
}
int config_set_str(CONFIG_REC *rec, const char *section, const char *key, const char *value)
{
CONFIG_NODE *parent;
g_return_val_if_fail(rec != NULL, -1);
parent = config_node_traverse(rec, section, TRUE);
if (parent == NULL) return -1;
config_node_set_str(rec, parent, key, value);
return 0;
}
int config_set_int(CONFIG_REC *rec, const char *section, const char *key, int value)
{
char str[MAX_INT_STRLEN];
g_snprintf(str, sizeof(str), "%d", value);
return config_set_str(rec, section, key, str);
}
int config_set_bool(CONFIG_REC *rec, const char *section, const char *key, int value)
{
return config_set_str(rec, section, key, value ? "yes" : "no");
}
/* Add all values in `array' to `node' */
void config_node_add_list(CONFIG_REC *rec, CONFIG_NODE *node, char **array)
{
char **tmp;
for (tmp = array; *tmp != NULL; tmp++)
config_node_set_str(rec, node, NULL, *tmp);
}
|