2 set.c : irssi configuration - change settings in 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 static void cache_remove(CONFIG_REC *rec, CONFIG_NODE *node)
27 path = g_hash_table_lookup(rec->cache_nodes, node);
29 g_hash_table_remove(rec->cache, path);
30 g_hash_table_remove(rec->cache_nodes, node);
35 void config_node_remove(CONFIG_REC *rec, CONFIG_NODE *parent, CONFIG_NODE *node)
37 g_return_if_fail(node != NULL);
40 parent = rec->mainnode;
43 cache_remove(rec, node);
44 parent->value = g_slist_remove(parent->value, node);
49 case NODE_TYPE_COMMENT:
50 g_free_not_null(node->value);
54 while (node->value != NULL)
55 config_node_remove(rec, node, ((GSList *) node->value)->data);
58 g_free_not_null(node->key);
62 void config_node_list_remove(CONFIG_REC *rec, CONFIG_NODE *node, int index)
66 g_return_if_fail(node != NULL);
67 g_return_if_fail(is_node_list(node));
69 child = config_node_nth(node, index);
70 if (child != NULL) config_node_remove(rec, node, child);
73 void config_node_clear(CONFIG_REC *rec, CONFIG_NODE *node)
75 g_return_if_fail(node != NULL);
76 g_return_if_fail(is_node_list(node));
78 while (node->value != NULL)
79 config_node_remove(rec, node, ((GSList *) node->value)->data);
82 void config_nodes_remove_all(CONFIG_REC *rec)
84 g_return_if_fail(rec != NULL);
86 while (rec->mainnode->value != NULL)
87 config_node_remove(rec, rec->mainnode, ((GSList *) rec->mainnode->value)->data);
90 void config_node_set_str(CONFIG_REC *rec, CONFIG_NODE *parent, const char *key, const char *value)
95 g_return_if_fail(rec != NULL);
96 g_return_if_fail(parent != NULL);
99 node = no_key ? NULL : config_node_find(parent, key);
103 if (node != NULL) config_node_remove(rec, parent, node);
108 if (strcmp(node->value, value) == 0)
112 node = g_new0(CONFIG_NODE, 1);
113 parent->value = g_slist_append(parent->value, node);
115 node->type = no_key ? NODE_TYPE_VALUE : NODE_TYPE_KEY;
116 node->key = no_key ? NULL : g_strdup(key);
119 node->value = g_strdup(value);
120 rec->modifycounter++;
123 void config_node_set_int(CONFIG_REC *rec, CONFIG_NODE *parent, const char *key, int value)
125 char str[MAX_INT_STRLEN];
127 g_snprintf(str, sizeof(str), "%d", value);
128 config_node_set_str(rec, parent, key, str);
131 void config_node_set_bool(CONFIG_REC *rec, CONFIG_NODE *parent, const char *key, int value)
133 config_node_set_str(rec, parent, key, value ? "yes" : "no");
136 int config_set_str(CONFIG_REC *rec, const char *section, const char *key, const char *value)
140 g_return_val_if_fail(rec != NULL, -1);
142 parent = config_node_traverse(rec, section, TRUE);
143 if (parent == NULL) return -1;
145 config_node_set_str(rec, parent, key, value);
149 int config_set_int(CONFIG_REC *rec, const char *section, const char *key, int value)
151 char str[MAX_INT_STRLEN];
153 g_snprintf(str, sizeof(str), "%d", value);
154 return config_set_str(rec, section, key, str);
157 int config_set_bool(CONFIG_REC *rec, const char *section, const char *key, int value)
159 return config_set_str(rec, section, key, value ? "yes" : "no");
162 void config_node_add_list(CONFIG_REC *rec, CONFIG_NODE *node, char **array)
166 for (tmp = array; *tmp != NULL; tmp++)
167 config_node_set_str(rec, node, NULL, *tmp);