2 get.c : irssi configuration - get settings from memory
4 Copyright (C) 1999 Timo Sirainen
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.
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.
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
23 CONFIG_NODE *config_node_find(CONFIG_NODE *node, const char *key)
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);
31 for (tmp = node->value; tmp != NULL; tmp = tmp->next) {
32 CONFIG_NODE *node = tmp->data;
34 if (node->key != NULL && g_strcasecmp(node->key, key) == 0)
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)
45 return config_node_section_index(parent, key, -1, new_type);
48 CONFIG_NODE *config_node_section_index(CONFIG_NODE *parent, const char *key,
49 int index, int new_type)
54 g_return_val_if_fail(parent != NULL, NULL);
55 g_return_val_if_fail(is_node_list(parent), NULL);
57 node = key == NULL ? NULL : config_node_find(parent, key);
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);
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);
77 node->type = new_type;
78 node->key = key == NULL ? NULL : g_strdup(key);
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)
88 char **list, **tmp, *str;
89 int is_list, new_type;
91 g_return_val_if_fail(rec != NULL, NULL);
93 if (section == NULL || *section == '\0')
96 /* check if it already exists in cache */
97 node = g_hash_table_lookup(rec->cache, section);
98 if (node != NULL) return node;
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;
108 node = config_node_section(node, *tmp + is_list, new_type);
117 str = g_strdup(section);
118 g_hash_table_insert(rec->cache, str, node);
119 g_hash_table_insert(rec->cache_nodes, node, str);
123 char *config_get_str(CONFIG_REC *rec, const char *section, const char *key, const char *def)
125 CONFIG_NODE *parent, *node;
128 g_return_val_if_fail(rec != NULL, (char *) def);
129 g_return_val_if_fail(key != NULL, (char *) def);
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);
138 parent = config_node_traverse(rec, section, FALSE);
139 node = parent == NULL ? NULL :
140 config_node_find(parent, key);
146 g_hash_table_insert(rec->cache, path, node);
147 g_hash_table_insert(rec->cache_nodes, node, path);
151 return (node == NULL || !has_node_value(node)) ? (char *) def : node->value;
154 int config_get_int(CONFIG_REC *rec, const char *section, const char *key, int def)
158 str = config_get_str(rec, section, key, NULL);
159 if (str == NULL) return def;
164 int config_get_bool(CONFIG_REC *rec, const char *section, const char *key, int def)
168 str = config_get_str(rec, section, key, NULL);
169 if (str == NULL) return def;
171 return i_toupper(*str) == 'T' || i_toupper(*str) == 'Y';
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)
179 node = config_list_find_node(rec, section, key, value, value_key);
180 return node != NULL && node->type == NODE_TYPE_KEY ?
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)
187 CONFIG_NODE *node, *keynode;
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);
194 node = config_node_traverse(rec, section, FALSE);
195 if (node == NULL || !is_node_list(node)) return NULL;
197 for (tmp = node->value; tmp != NULL; tmp = tmp->next) {
200 if (node->type != NODE_TYPE_BLOCK)
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;
208 return config_node_find(node, value_key);
214 char *config_node_get_str(CONFIG_NODE *parent, const char *key, const char *def)
218 if (parent == NULL) return (char *) def;
220 node = config_node_find(parent, key);
221 return (char *) ((node != NULL && has_node_value(node)) ?
225 int config_node_get_int(CONFIG_NODE *parent, const char *key, int def)
229 str = config_node_get_str(parent, key, NULL);
230 if (str == NULL) return def;
235 int config_node_get_bool(CONFIG_NODE *parent, const char *key, int def)
239 str = config_node_get_str(parent, key, NULL);
240 if (str == NULL) return def;
242 return i_toupper(*str) == 'T' || i_toupper(*str) == 'Y' ||
243 (i_toupper(*str) == 'O' && i_toupper(str[1]) == 'N');
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)
250 CONFIG_NODE *keynode, *valuenode;
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);
259 for (tmp = node->value; tmp != NULL; tmp = tmp->next) {
262 if (node->type != NODE_TYPE_BLOCK)
265 keynode = config_node_find(node, key);
266 if (keynode == NULL || keynode->type != NODE_TYPE_KEY)
269 valuenode = config_node_find(node, value_key);
271 *ret_key = keynode->key;
272 *ret_value = valuenode != NULL && valuenode->type == NODE_TYPE_KEY ?
273 valuenode->value : NULL;
280 /* Return all values from from the list `node' in a g_strsplit() array */
281 char **config_node_get_list(CONFIG_NODE *node)
287 g_return_val_if_fail(node != NULL, NULL);
288 g_return_val_if_fail(is_node_list(node), NULL);
290 /* put values to string */
291 values = g_string_new(NULL);
292 for (tmp = node->value; tmp != NULL; tmp = tmp->next) {
295 if (node->type == NODE_TYPE_VALUE)
296 g_string_sprintfa(values, "%s ", (char *) node->value);
299 /* split the values to **str array */
300 if (values->len == 0)
303 g_string_truncate(values, values->len-1);
304 ret = g_strsplit(values->str, " ", -1);
307 g_string_free(values, TRUE);
311 /* Returns n'th node from list. */
312 CONFIG_NODE *config_node_nth(CONFIG_NODE *node, int index)
316 g_return_val_if_fail(node != NULL, NULL);
317 g_return_val_if_fail(is_node_list(node), NULL);
319 for (tmp = node->value; tmp != NULL; tmp = tmp->next, index--) {
327 /* Returns index for given key */
328 int config_node_index(CONFIG_NODE *parent, const char *key)
332 g_return_val_if_fail(parent != NULL, -1);
333 g_return_val_if_fail(key != NULL, -1);
335 node = config_node_find(parent, key);
339 return g_slist_index(parent->value, node);
342 /* Returns the first non-comment node in list */
343 GSList *config_node_first(GSList *list)
345 while (list != NULL) {
346 CONFIG_NODE *node = list->data;
348 if (node->type != NODE_TYPE_COMMENT)
355 /* Returns the next non-comment node in list */
356 GSList *config_node_next(GSList *list)
359 return config_node_first(list);