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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
/*
get.c : irssi configuration - get settings from 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.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "module.h"
CONFIG_NODE *config_node_find(CONFIG_NODE *node, const char *key)
{
GSList *tmp;
g_return_val_if_fail(node != NULL, NULL);
g_return_val_if_fail(key != NULL, NULL);
g_return_val_if_fail(is_node_list(node), NULL);
for (tmp = node->value; tmp != NULL; tmp = tmp->next) {
CONFIG_NODE *node = tmp->data;
if (node->key != NULL && g_ascii_strcasecmp(node->key, key) == 0)
return node;
}
return NULL;
}
CONFIG_NODE *config_node_section(CONFIG_NODE *parent, const char *key, int new_type)
{
return config_node_section_index(parent, key, -1, new_type);
}
CONFIG_NODE *config_node_section_index(CONFIG_NODE *parent, const char *key,
int index, int new_type)
{
CONFIG_NODE *node;
int nindex;
g_return_val_if_fail(parent != NULL, NULL);
g_return_val_if_fail(is_node_list(parent), NULL);
node = key == NULL ? NULL : config_node_find(parent, key);
if (node != NULL) {
g_return_val_if_fail(new_type == -1 || new_type == node->type, NULL);
nindex = g_slist_index(parent->value, node);
if (index >= 0 && nindex != index &&
nindex <= g_slist_length(parent->value)) {
/* move it to wanted position */
parent->value = g_slist_remove(parent->value, node);
parent->value = g_slist_insert(parent->value, node, index);
}
return node;
}
if (new_type == -1)
return NULL;
node = g_new0(CONFIG_NODE, 1);
parent->value = index < 0 ? g_slist_append(parent->value, node) :
g_slist_insert(parent->value, node, index);
node->type = new_type;
node->key = key == NULL ? NULL : g_strdup(key);
return node;
}
CONFIG_NODE *config_node_traverse(CONFIG_REC *rec, const char *section, int create)
{
CONFIG_NODE *node;
char **list, **tmp, *str;
int is_list, new_type;
g_return_val_if_fail(rec != NULL, NULL);
if (section == NULL || *section == '\0')
return rec->mainnode;
/* check if it already exists in cache */
node = g_hash_table_lookup(rec->cache, section);
if (node != NULL) return node;
new_type = -1;
node = rec->mainnode;
list = g_strsplit(section, "/", -1);
for (tmp = list; *tmp != NULL; tmp++) {
is_list = **tmp == '(';
if (create) new_type = is_list ? NODE_TYPE_LIST : NODE_TYPE_BLOCK;
node = config_node_section(node, *tmp + is_list, new_type);
if (node == NULL) {
g_strfreev(list);
return NULL;
}
}
g_strfreev(list);
if (!is_node_list(node)) {
/* Will die. Better to not corrupt the config further in this case. */
g_error("Attempt to use non-list node as list. Corrupt config?");
return NULL;
}
/* save to cache */
str = g_strdup(section);
g_hash_table_insert(rec->cache, str, node);
g_hash_table_insert(rec->cache_nodes, node, str);
return node;
}
char *config_get_str(CONFIG_REC *rec, const char *section, const char *key, const char *def)
{
CONFIG_NODE *parent, *node;
char *path;
g_return_val_if_fail(rec != NULL, (char *) def);
g_return_val_if_fail(key != NULL, (char *) def);
/* check if it already exists in cache */
path = g_strconcat(section == NULL ? "" : section, "/", key, NULL);
node = g_hash_table_lookup(rec->cache, path);
if (node != NULL)
g_free(path);
else {
parent = config_node_traverse(rec, section, FALSE);
node = parent == NULL ? NULL :
config_node_find(parent, key);
/* save to cache */
if (node == NULL)
g_free(path);
else {
g_hash_table_insert(rec->cache, path, node);
g_hash_table_insert(rec->cache_nodes, node, path);
}
}
return (node == NULL || !has_node_value(node)) ? (char *) def : node->value;
}
int config_get_int(CONFIG_REC *rec, const char *section, const char *key, int def)
{
char *str;
str = config_get_str(rec, section, key, NULL);
if (str == NULL) return def;
return atoi(str);
}
int config_get_bool(CONFIG_REC *rec, const char *section, const char *key, int def)
{
char *str;
str = config_get_str(rec, section, key, NULL);
if (str == NULL) return def;
return i_toupper(*str) == 'T' || i_toupper(*str) == 'Y';
}
char *config_node_get_str(CONFIG_NODE *parent, const char *key, const char *def)
{
CONFIG_NODE *node;
if (parent == NULL) return (char *) def;
node = config_node_find(parent, key);
return (char *) ((node != NULL && has_node_value(node)) ?
node->value : def);
}
int config_node_get_int(CONFIG_NODE *parent, const char *key, int def)
{
char *str;
str = config_node_get_str(parent, key, NULL);
if (str == NULL) return def;
return atoi(str);
}
int config_node_get_bool(CONFIG_NODE *parent, const char *key, int def)
{
char *str;
str = config_node_get_str(parent, key, NULL);
if (str == NULL) return def;
return i_toupper(*str) == 'T' || i_toupper(*str) == 'Y' ||
(i_toupper(*str) == 'O' && i_toupper(str[1]) == 'N');
}
char **config_node_get_list(CONFIG_NODE *node)
{
GString *values;
GSList *tmp;
char **ret;
g_return_val_if_fail(node != NULL, NULL);
g_return_val_if_fail(is_node_list(node), NULL);
/* put values to string */
values = g_string_new(NULL);
for (tmp = node->value; tmp != NULL; tmp = tmp->next) {
node = tmp->data;
if (node->type == NODE_TYPE_VALUE)
g_string_append_printf(values, "%s ", (char *) node->value);
}
/* split the values to **str array */
if (values->len == 0)
ret = NULL;
else {
g_string_truncate(values, values->len-1);
ret = g_strsplit(values->str, " ", -1);
}
g_string_free(values, TRUE);
return ret;
}
CONFIG_NODE *config_node_nth(CONFIG_NODE *node, int index)
{
GSList *tmp;
g_return_val_if_fail(node != NULL, NULL);
g_return_val_if_fail(is_node_list(node), NULL);
for (tmp = node->value; tmp != NULL; tmp = tmp->next) {
CONFIG_NODE *node = tmp->data;
if (node->type != NODE_TYPE_COMMENT) {
if (index == 0)
return node;
index--;
}
}
return NULL;
}
int config_node_index(CONFIG_NODE *parent, const char *key)
{
CONFIG_NODE *node;
GSList *tmp;
int index;
g_return_val_if_fail(parent != NULL, -1);
g_return_val_if_fail(key != NULL, -1);
node = config_node_find(parent, key);
if (node == NULL)
return -1;
index = 0;
for (tmp = parent->value; tmp != NULL; tmp = tmp->next) {
CONFIG_NODE *tmpnode = tmp->data;
if (tmpnode == node)
return index;
if (tmpnode->type != NODE_TYPE_COMMENT)
index++;
}
return -1;
}
GSList *config_node_first(GSList *list)
{
while (list != NULL) {
CONFIG_NODE *node = list->data;
if (node->type != NODE_TYPE_COMMENT)
break;
list = list->next;
}
return list;
}
GSList *config_node_next(GSList *list)
{
list = list->next;
return config_node_first(list);
}
|