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
char *config_node_get_str(CONFIG_NODE *parent, const char *key, const char *def)
178
if (parent == NULL) return (char *) def;
180
node = config_node_find(parent, key);
181
return (char *) ((node != NULL && has_node_value(node)) ?
185
int config_node_get_int(CONFIG_NODE *parent, const char *key, int def)
189
str = config_node_get_str(parent, key, NULL);
190
if (str == NULL) return def;
195
int config_node_get_bool(CONFIG_NODE *parent, const char *key, int def)
199
str = config_node_get_str(parent, key, NULL);
200
if (str == NULL) return def;
202
return i_toupper(*str) == 'T' || i_toupper(*str) == 'Y' ||
203
(i_toupper(*str) == 'O' && i_toupper(str[1]) == 'N');
206
/* Get the value of keys `key' and `key_value' and put them to
207
`ret_key' and `ret_value'. Returns -1 if not found. */
208
int config_node_get_keyvalue(CONFIG_NODE *node, const char *key, const char *value_key, char **ret_key, char **ret_value)
210
CONFIG_NODE *keynode, *valuenode;
213
g_return_val_if_fail(node != NULL, -1);
214
g_return_val_if_fail(key != NULL, -1);
215
g_return_val_if_fail(value_key != NULL, -1);
216
g_return_val_if_fail(ret_key != NULL, -1);
217
g_return_val_if_fail(ret_value != NULL, -1);
219
for (tmp = node->value; tmp != NULL; tmp = tmp->next) {
222
if (node->type != NODE_TYPE_BLOCK)
225
keynode = config_node_find(node, key);
226
if (keynode == NULL || keynode->type != NODE_TYPE_KEY)
229
valuenode = config_node_find(node, value_key);
231
*ret_key = keynode->key;
232
*ret_value = valuenode != NULL && valuenode->type == NODE_TYPE_KEY ?
233
valuenode->value : NULL;
240
/* Return all values from from the list `node' in a g_strsplit() array */
241
char **config_node_get_list(CONFIG_NODE *node)
247
g_return_val_if_fail(node != NULL, NULL);
248
g_return_val_if_fail(is_node_list(node), NULL);
250
/* put values to string */
251
values = g_string_new(NULL);
252
for (tmp = node->value; tmp != NULL; tmp = tmp->next) {
255
if (node->type == NODE_TYPE_VALUE)
256
g_string_sprintfa(values, "%s ", (char *) node->value);
259
/* split the values to **str array */
260
if (values->len == 0)
263
g_string_truncate(values, values->len-1);
264
ret = g_strsplit(values->str, " ", -1);
267
g_string_free(values, TRUE);
271
/* Returns n'th node from list. */
272
CONFIG_NODE *config_node_nth(CONFIG_NODE *node, int index)
276
g_return_val_if_fail(node != NULL, NULL);
277
g_return_val_if_fail(is_node_list(node), NULL);
279
for (tmp = node->value; tmp != NULL; tmp = tmp->next) {
280
CONFIG_NODE *node = tmp->data;
282
if (node->type != NODE_TYPE_COMMENT) {
292
/* Returns index for given key */
293
int config_node_index(CONFIG_NODE *parent, const char *key)
299
g_return_val_if_fail(parent != NULL, -1);
300
g_return_val_if_fail(key != NULL, -1);
302
node = config_node_find(parent, key);
307
for (tmp = parent->value; tmp != NULL; tmp = tmp->next) {
308
CONFIG_NODE *tmpnode = tmp->data;
313
if (tmpnode->type != NODE_TYPE_COMMENT)
320
/* Returns the first non-comment node in list */
321
GSList *config_node_first(GSList *list)
323
while (list != NULL) {
324
CONFIG_NODE *node = list->data;
326
if (node->type != NODE_TYPE_COMMENT)
333
/* Returns the next non-comment node in list */
334
GSList *config_node_next(GSList *list)
337
return config_node_first(list);