updates.
[crypto.git] / apps / irssi / src / lib-config / get.c
1 /*
2  get.c : irssi configuration - get settings from memory
3
4     Copyright (C) 1999 Timo Sirainen
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #include "module.h"
22
23 CONFIG_NODE *config_node_find(CONFIG_NODE *node, const char *key)
24 {
25         GSList *tmp;
26
27         g_return_val_if_fail(node != NULL, NULL);
28         g_return_val_if_fail(key != NULL, NULL);
29         g_return_val_if_fail(is_node_list(node), NULL);
30
31         for (tmp = node->value; tmp != NULL; tmp = tmp->next) {
32                 CONFIG_NODE *node = tmp->data;
33
34                 if (node->key != NULL && g_strcasecmp(node->key, key) == 0)
35                         return node;
36         }
37
38         return NULL;
39 }
40
41 /* find the section from node - if not found create it unless new_type is -1.
42    you can also specify in new_type if it's NODE_TYPE_LIST or NODE_TYPE_BLOCK */
43 CONFIG_NODE *config_node_section(CONFIG_NODE *parent, const char *key, int new_type)
44 {
45         return config_node_section_index(parent, key, -1, new_type);
46 }
47
48 CONFIG_NODE *config_node_section_index(CONFIG_NODE *parent, const char *key,
49                                        int index, int new_type)
50 {
51         CONFIG_NODE *node;
52         int nindex;
53
54         g_return_val_if_fail(parent != NULL, NULL);
55         g_return_val_if_fail(is_node_list(parent), NULL);
56
57         node = key == NULL ? NULL : config_node_find(parent, key);
58         if (node != NULL) {
59                 g_return_val_if_fail(new_type == -1 || new_type == node->type, NULL);
60                 nindex = g_slist_index(parent->value, node);
61                 if (index >= 0 && nindex != index &&
62                     nindex <= g_slist_length(parent->value)) {
63                         /* move it to wanted position */
64                         parent->value = g_slist_remove(parent->value, node);
65                         parent->value = g_slist_insert(parent->value, node, index);
66                 }
67                 return node;
68         }
69
70         if (new_type == -1)
71                 return NULL;
72
73         node = g_new0(CONFIG_NODE, 1);
74         parent->value = index < 0 ? g_slist_append(parent->value, node) :
75                 g_slist_insert(parent->value, node, index);
76
77         node->type = new_type;
78         node->key = key == NULL ? NULL : g_strdup(key);
79
80         return node;
81 }
82
83 /* find the section with the whole path.
84    create the path if necessary `create' is TRUE. */
85 CONFIG_NODE *config_node_traverse(CONFIG_REC *rec, const char *section, int create)
86 {
87         CONFIG_NODE *node;
88         char **list, **tmp, *str;
89         int is_list, new_type;
90
91         g_return_val_if_fail(rec != NULL, NULL);
92
93         if (section == NULL || *section == '\0')
94                 return rec->mainnode;
95
96         /* check if it already exists in cache */
97         node = g_hash_table_lookup(rec->cache, section);
98         if (node != NULL) return node;
99
100         new_type = -1;
101
102         node = rec->mainnode;
103         list = g_strsplit(section, "/", -1);
104         for (tmp = list; *tmp != NULL; tmp++) {
105                 is_list = **tmp == '(';
106                 if (create) new_type = is_list ? NODE_TYPE_LIST : NODE_TYPE_BLOCK;
107
108                 node = config_node_section(node, *tmp + is_list, new_type);
109                 if (node == NULL) {
110                         g_strfreev(list);
111                         return NULL;
112                 }
113         }
114         g_strfreev(list);
115
116         /* save to cache */
117         str = g_strdup(section);
118         g_hash_table_insert(rec->cache, str, node);
119         g_hash_table_insert(rec->cache_nodes, node, str);
120         return node;
121 }
122
123 char *config_get_str(CONFIG_REC *rec, const char *section, const char *key, const char *def)
124 {
125         CONFIG_NODE *parent, *node;
126         char *path;
127
128         g_return_val_if_fail(rec != NULL, (char *) def);
129         g_return_val_if_fail(key != NULL, (char *) def);
130
131         /* check if it already exists in cache */
132         path = g_strconcat(section == NULL ? "" : section, "/", key, NULL);
133         node = g_hash_table_lookup(rec->cache, path);
134
135         if (node != NULL)
136                 g_free(path);
137         else {
138                 parent = config_node_traverse(rec, section, FALSE);
139                 node = parent == NULL ? NULL :
140                         config_node_find(parent, key);
141
142                 /* save to cache */
143                 if (node == NULL)
144                         g_free(path);
145                 else {
146                         g_hash_table_insert(rec->cache, path, node);
147                         g_hash_table_insert(rec->cache_nodes, node, path);
148                 }
149         }
150
151         return (node == NULL || !has_node_value(node)) ? (char *) def : node->value;
152 }
153
154 int config_get_int(CONFIG_REC *rec, const char *section, const char *key, int def)
155 {
156         char *str;
157
158         str = config_get_str(rec, section, key, NULL);
159         if (str == NULL) return def;
160
161         return atoi(str);
162 }
163
164 int config_get_bool(CONFIG_REC *rec, const char *section, const char *key, int def)
165 {
166         char *str;
167
168         str = config_get_str(rec, section, key, NULL);
169         if (str == NULL) return def;
170
171         return i_toupper(*str) == 'T' || i_toupper(*str) == 'Y';
172 }
173
174 /* Return value of key `value_key' from list item where `key' is `value' */
175 const char *config_list_find(CONFIG_REC *rec, const char *section, const char *key, const char *value, const char *value_key)
176 {
177         CONFIG_NODE *node;
178
179         node = config_list_find_node(rec, section, key, value, value_key);
180         return node != NULL && node->type == NODE_TYPE_KEY ?
181                 node->value : NULL;
182 }
183
184 /* Like config_list_find(), but return node instead of it's value */
185 CONFIG_NODE *config_list_find_node(CONFIG_REC *rec, const char *section, const char *key, const char *value, const char *value_key)
186 {
187         CONFIG_NODE *node, *keynode;
188         GSList *tmp;
189
190         g_return_val_if_fail(rec != NULL, NULL);
191         g_return_val_if_fail(key != NULL, NULL);
192         g_return_val_if_fail(value_key != NULL, NULL);
193
194         node = config_node_traverse(rec, section, FALSE);
195         if (node == NULL || !is_node_list(node)) return NULL;
196
197         for (tmp = node->value; tmp != NULL; tmp = tmp->next) {
198                 node = tmp->data;
199
200                 if (node->type != NODE_TYPE_BLOCK)
201                         continue;
202
203                 /* key matches value? */
204                 keynode = config_node_find(node, key);
205                 if (keynode == NULL || keynode->type != NODE_TYPE_KEY ||
206                     g_strcasecmp(keynode->value, value) != 0) continue;
207
208                 return config_node_find(node, value_key);
209         }
210
211         return NULL;
212 }
213
214 char *config_node_get_str(CONFIG_NODE *parent, const char *key, const char *def)
215 {
216         CONFIG_NODE *node;
217
218         if (parent == NULL) return (char *) def;
219
220         node = config_node_find(parent, key);
221         return (char *) ((node != NULL && has_node_value(node)) ?
222                          node->value : def);
223 }
224
225 int config_node_get_int(CONFIG_NODE *parent, const char *key, int def)
226 {
227         char *str;
228
229         str = config_node_get_str(parent, key, NULL);
230         if (str == NULL) return def;
231
232         return atoi(str);
233 }
234
235 int config_node_get_bool(CONFIG_NODE *parent, const char *key, int def)
236 {
237         char *str;
238
239         str = config_node_get_str(parent, key, NULL);
240         if (str == NULL) return def;
241
242         return i_toupper(*str) == 'T' || i_toupper(*str) == 'Y' ||
243                 (i_toupper(*str) == 'O' && i_toupper(str[1]) == 'N');
244 }
245
246 /* Get the value of keys `key' and `key_value' and put them to
247    `ret_key' and `ret_value'. Returns -1 if not found. */
248 int config_node_get_keyvalue(CONFIG_NODE *node, const char *key, const char *value_key, char **ret_key, char **ret_value)
249 {
250         CONFIG_NODE *keynode, *valuenode;
251         GSList *tmp;
252
253         g_return_val_if_fail(node != NULL, -1);
254         g_return_val_if_fail(key != NULL, -1);
255         g_return_val_if_fail(value_key != NULL, -1);
256         g_return_val_if_fail(ret_key != NULL, -1);
257         g_return_val_if_fail(ret_value != NULL, -1);
258
259         for (tmp = node->value; tmp != NULL; tmp = tmp->next) {
260                 node = tmp->data;
261
262                 if (node->type != NODE_TYPE_BLOCK)
263                         continue;
264
265                 keynode = config_node_find(node, key);
266                 if (keynode == NULL || keynode->type != NODE_TYPE_KEY)
267                         continue;
268
269                 valuenode = config_node_find(node, value_key);
270
271                 *ret_key = keynode->key;
272                 *ret_value = valuenode != NULL && valuenode->type == NODE_TYPE_KEY ?
273                         valuenode->value : NULL;
274                 return 0;
275         }
276
277         return -1;
278 }
279
280 /* Return all values from from the list `node' in a g_strsplit() array */
281 char **config_node_get_list(CONFIG_NODE *node)
282 {
283         GString *values;
284         GSList *tmp;
285         char **ret;
286
287         g_return_val_if_fail(node != NULL, NULL);
288         g_return_val_if_fail(is_node_list(node), NULL);
289
290         /* put values to string */
291         values = g_string_new(NULL);
292         for (tmp = node->value; tmp != NULL; tmp = tmp->next) {
293                 node = tmp->data;
294
295                 if (node->type == NODE_TYPE_VALUE)
296                         g_string_sprintfa(values, "%s ", (char *) node->value);
297         }
298
299         /* split the values to **str array */
300         if (values->len == 0)
301                 ret = NULL;
302         else {
303                 g_string_truncate(values, values->len-1);
304                 ret = g_strsplit(values->str, " ", -1);
305         }
306
307         g_string_free(values, TRUE);
308         return ret;
309 }
310
311 /* Returns n'th node from list. */
312 CONFIG_NODE *config_node_nth(CONFIG_NODE *node, int index)
313 {
314         GSList *tmp;
315
316         g_return_val_if_fail(node != NULL, NULL);
317         g_return_val_if_fail(is_node_list(node), NULL);
318
319         for (tmp = node->value; tmp != NULL; tmp = tmp->next, index--) {
320                 if (index == 0)
321                         return tmp->data;
322         }
323
324         return NULL;
325 }
326
327 /* Returns index for given key */
328 int config_node_index(CONFIG_NODE *parent, const char *key)
329 {
330         CONFIG_NODE *node;
331
332         g_return_val_if_fail(parent != NULL, -1);
333         g_return_val_if_fail(key != NULL, -1);
334
335         node = config_node_find(parent, key);
336         if (node == NULL)
337                 return -1;
338
339         return g_slist_index(parent->value, node);
340 }
341
342 /* Returns the first non-comment node in list */
343 GSList *config_node_first(GSList *list)
344 {
345         while (list != NULL) {
346                 CONFIG_NODE *node = list->data;
347
348                 if (node->type != NODE_TYPE_COMMENT)
349                         break;
350                 list = list->next;
351         }
352         return list;
353 }
354
355 /* Returns the next non-comment node in list */
356 GSList *config_node_next(GSList *list)
357 {
358         list = list->next;
359         return config_node_first(list);
360 }