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 along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 CONFIG_NODE *config_node_section(CONFIG_NODE *parent, const char *key, int new_type)
43 return config_node_section_index(parent, key, -1, new_type);
46 CONFIG_NODE *config_node_section_index(CONFIG_NODE *parent, const char *key,
47 int index, int new_type)
52 g_return_val_if_fail(parent != NULL, NULL);
53 g_return_val_if_fail(is_node_list(parent), NULL);
55 node = key == NULL ? NULL : config_node_find(parent, key);
57 g_return_val_if_fail(new_type == -1 || new_type == node->type, NULL);
58 nindex = g_slist_index(parent->value, node);
59 if (index >= 0 && nindex != index &&
60 nindex <= g_slist_length(parent->value)) {
61 /* move it to wanted position */
62 parent->value = g_slist_remove(parent->value, node);
63 parent->value = g_slist_insert(parent->value, node, index);
71 node = g_new0(CONFIG_NODE, 1);
72 parent->value = index < 0 ? g_slist_append(parent->value, node) :
73 g_slist_insert(parent->value, node, index);
75 node->type = new_type;
76 node->key = key == NULL ? NULL : g_strdup(key);
81 CONFIG_NODE *config_node_traverse(CONFIG_REC *rec, const char *section, int create)
84 char **list, **tmp, *str;
85 int is_list, new_type;
87 g_return_val_if_fail(rec != NULL, NULL);
89 if (section == NULL || *section == '\0')
92 /* check if it already exists in cache */
93 node = g_hash_table_lookup(rec->cache, section);
94 if (node != NULL) return node;
99 list = g_strsplit(section, "/", -1);
100 for (tmp = list; *tmp != NULL; tmp++) {
101 is_list = **tmp == '(';
102 if (create) new_type = is_list ? NODE_TYPE_LIST : NODE_TYPE_BLOCK;
104 node = config_node_section(node, *tmp + is_list, new_type);
113 str = g_strdup(section);
114 g_hash_table_insert(rec->cache, str, node);
115 g_hash_table_insert(rec->cache_nodes, node, str);
119 char *config_get_str(CONFIG_REC *rec, const char *section, const char *key, const char *def)
121 CONFIG_NODE *parent, *node;
124 g_return_val_if_fail(rec != NULL, (char *) def);
125 g_return_val_if_fail(key != NULL, (char *) def);
127 /* check if it already exists in cache */
128 path = g_strconcat(section == NULL ? "" : section, "/", key, NULL);
129 node = g_hash_table_lookup(rec->cache, path);
134 parent = config_node_traverse(rec, section, FALSE);
135 node = parent == NULL ? NULL :
136 config_node_find(parent, key);
142 g_hash_table_insert(rec->cache, path, node);
143 g_hash_table_insert(rec->cache_nodes, node, path);
147 return (node == NULL || !has_node_value(node)) ? (char *) def : node->value;
150 int config_get_int(CONFIG_REC *rec, const char *section, const char *key, int def)
154 str = config_get_str(rec, section, key, NULL);
155 if (str == NULL) return def;
160 int config_get_bool(CONFIG_REC *rec, const char *section, const char *key, int def)
164 str = config_get_str(rec, section, key, NULL);
165 if (str == NULL) return def;
167 return i_toupper(*str) == 'T' || i_toupper(*str) == 'Y';
170 char *config_node_get_str(CONFIG_NODE *parent, const char *key, const char *def)
174 if (parent == NULL) return (char *) def;
176 node = config_node_find(parent, key);
177 return (char *) ((node != NULL && has_node_value(node)) ?
181 int config_node_get_int(CONFIG_NODE *parent, const char *key, int def)
185 str = config_node_get_str(parent, key, NULL);
186 if (str == NULL) return def;
191 int config_node_get_bool(CONFIG_NODE *parent, const char *key, int def)
195 str = config_node_get_str(parent, key, NULL);
196 if (str == NULL) return def;
198 return i_toupper(*str) == 'T' || i_toupper(*str) == 'Y' ||
199 (i_toupper(*str) == 'O' && i_toupper(str[1]) == 'N');
202 char **config_node_get_list(CONFIG_NODE *node)
208 g_return_val_if_fail(node != NULL, NULL);
209 g_return_val_if_fail(is_node_list(node), NULL);
211 /* put values to string */
212 values = g_string_new(NULL);
213 for (tmp = node->value; tmp != NULL; tmp = tmp->next) {
216 if (node->type == NODE_TYPE_VALUE)
217 g_string_append_printf(values, "%s ", (char *) node->value);
220 /* split the values to **str array */
221 if (values->len == 0)
224 g_string_truncate(values, values->len-1);
225 ret = g_strsplit(values->str, " ", -1);
228 g_string_free(values, TRUE);
232 CONFIG_NODE *config_node_nth(CONFIG_NODE *node, int index)
236 g_return_val_if_fail(node != NULL, NULL);
237 g_return_val_if_fail(is_node_list(node), NULL);
239 for (tmp = node->value; tmp != NULL; tmp = tmp->next) {
240 CONFIG_NODE *node = tmp->data;
242 if (node->type != NODE_TYPE_COMMENT) {
252 int config_node_index(CONFIG_NODE *parent, const char *key)
258 g_return_val_if_fail(parent != NULL, -1);
259 g_return_val_if_fail(key != NULL, -1);
261 node = config_node_find(parent, key);
266 for (tmp = parent->value; tmp != NULL; tmp = tmp->next) {
267 CONFIG_NODE *tmpnode = tmp->data;
272 if (tmpnode->type != NODE_TYPE_COMMENT)
279 GSList *config_node_first(GSList *list)
281 while (list != NULL) {
282 CONFIG_NODE *node = list->data;
284 if (node->type != NODE_TYPE_COMMENT)
291 GSList *config_node_next(GSList *list)
294 return config_node_first(list);