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
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 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 /* Remove n'th node from a list */
63 void config_node_list_remove(CONFIG_REC *rec, CONFIG_NODE *node, int index)
67 g_return_if_fail(node != NULL);
68 g_return_if_fail(is_node_list(node));
70 child = config_node_nth(node, index);
71 if (child != NULL) config_node_remove(rec, node, child);
74 /* Clear all data inside node, but leave the node */
75 void config_node_clear(CONFIG_REC *rec, CONFIG_NODE *node)
77 g_return_if_fail(node != NULL);
78 g_return_if_fail(is_node_list(node));
80 while (node->value != NULL)
81 config_node_remove(rec, node, ((GSList *) node->value)->data);
84 void config_nodes_remove_all(CONFIG_REC *rec)
86 g_return_if_fail(rec != NULL);
88 while (rec->mainnode->value != NULL)
89 config_node_remove(rec, rec->mainnode, ((GSList *) rec->mainnode->value)->data);
92 void config_node_set_str(CONFIG_REC *rec, CONFIG_NODE *parent, const char *key, const char *value)
97 g_return_if_fail(rec != NULL);
98 g_return_if_fail(parent != NULL);
100 no_key = key == NULL;
101 node = no_key ? NULL : config_node_find(parent, key);
105 if (node != NULL) config_node_remove(rec, parent, node);
110 if (strcmp(node->value, value) == 0)
114 node = g_new0(CONFIG_NODE, 1);
115 parent->value = g_slist_append(parent->value, node);
117 node->type = no_key ? NODE_TYPE_VALUE : NODE_TYPE_KEY;
118 node->key = no_key ? NULL : g_strdup(key);
121 node->value = g_strdup(value);
122 rec->modifycounter++;
125 void config_node_set_int(CONFIG_REC *rec, CONFIG_NODE *parent, const char *key, int value)
127 char str[MAX_INT_STRLEN];
129 g_snprintf(str, sizeof(str), "%d", value);
130 config_node_set_str(rec, parent, key, str);
133 void config_node_set_bool(CONFIG_REC *rec, CONFIG_NODE *parent, const char *key, int value)
135 config_node_set_str(rec, parent, key, value ? "yes" : "no");
138 int config_set_str(CONFIG_REC *rec, const char *section, const char *key, const char *value)
142 g_return_val_if_fail(rec != NULL, -1);
144 parent = config_node_traverse(rec, section, TRUE);
145 if (parent == NULL) return -1;
147 config_node_set_str(rec, parent, key, value);
151 int config_set_int(CONFIG_REC *rec, const char *section, const char *key, int value)
153 char str[MAX_INT_STRLEN];
155 g_snprintf(str, sizeof(str), "%d", value);
156 return config_set_str(rec, section, key, str);
159 int config_set_bool(CONFIG_REC *rec, const char *section, const char *key, int value)
161 return config_set_str(rec, section, key, value ? "yes" : "no");
164 /* Add all values in `array' to `node' */
165 void config_node_add_list(CONFIG_REC *rec, CONFIG_NODE *node, char **array)
169 for (tmp = array; *tmp != NULL; tmp++)
170 config_node_set_str(rec, node, NULL, *tmp);