Merge Irssi 0.8.16-rc1
[silc.git] / apps / irssi / src / lib-config / set.c
1 /*
2  set.c : irssi configuration - change settings in memory
3
4     Copyright (C) 1999 Timo Sirainen
5
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.
10
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.
15
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.
19 */
20
21 #include "module.h"
22
23 static void cache_remove(CONFIG_REC *rec, CONFIG_NODE *node)
24 {
25         char *path;
26
27         path = g_hash_table_lookup(rec->cache_nodes, node);
28         if (path != NULL) {
29                 g_hash_table_remove(rec->cache, path);
30                 g_hash_table_remove(rec->cache_nodes, node);
31                 g_free(path);
32         }
33 }
34
35 void config_node_remove(CONFIG_REC *rec, CONFIG_NODE *parent, CONFIG_NODE *node)
36 {
37         g_return_if_fail(node != NULL);
38
39         if (parent == NULL)
40                 parent = rec->mainnode;
41
42         rec->modifycounter++;
43         cache_remove(rec, node);
44         parent->value = g_slist_remove(parent->value, node);
45
46         switch (node->type) {
47         case NODE_TYPE_KEY:
48         case NODE_TYPE_VALUE:
49         case NODE_TYPE_COMMENT:
50                 g_free_not_null(node->value);
51                 break;
52         case NODE_TYPE_BLOCK:
53         case NODE_TYPE_LIST:
54                 while (node->value != NULL)
55                         config_node_remove(rec, node, ((GSList *) node->value)->data);
56                 break;
57         }
58         g_free_not_null(node->key);
59         g_free(node);
60 }
61
62 void config_node_list_remove(CONFIG_REC *rec, CONFIG_NODE *node, int index)
63 {
64         CONFIG_NODE *child;
65
66         g_return_if_fail(node != NULL);
67         g_return_if_fail(is_node_list(node));
68
69         child = config_node_nth(node, index);
70         if (child != NULL) config_node_remove(rec, node, child);
71 }
72
73 void config_node_clear(CONFIG_REC *rec, CONFIG_NODE *node)
74 {
75         g_return_if_fail(node != NULL);
76         g_return_if_fail(is_node_list(node));
77
78         while (node->value != NULL)
79                 config_node_remove(rec, node, ((GSList *) node->value)->data);
80 }
81
82 void config_nodes_remove_all(CONFIG_REC *rec)
83 {
84         g_return_if_fail(rec != NULL);
85
86         while (rec->mainnode->value != NULL)
87                 config_node_remove(rec, rec->mainnode, ((GSList *) rec->mainnode->value)->data);
88 }
89
90 void config_node_set_str(CONFIG_REC *rec, CONFIG_NODE *parent, const char *key, const char *value)
91 {
92         CONFIG_NODE *node;
93         int no_key;
94
95         g_return_if_fail(rec != NULL);
96         g_return_if_fail(parent != NULL);
97
98         no_key = key == NULL;
99         node = no_key ? NULL : config_node_find(parent, key);
100
101         if (value == NULL) {
102                 /* remove the key */
103                 if (node != NULL) config_node_remove(rec, parent, node);
104                 return;
105         }
106
107         if (node != NULL) {
108                 if (strcmp(node->value, value) == 0)
109                         return;
110                 g_free(node->value);
111         } else {
112                 node = g_new0(CONFIG_NODE, 1);
113                 parent->value = g_slist_append(parent->value, node);
114
115                 node->type = no_key ? NODE_TYPE_VALUE : NODE_TYPE_KEY;
116                 node->key = no_key ? NULL : g_strdup(key);
117         }
118
119         node->value = g_strdup(value);
120         rec->modifycounter++;
121 }
122
123 void config_node_set_int(CONFIG_REC *rec, CONFIG_NODE *parent, const char *key, int value)
124 {
125         char str[MAX_INT_STRLEN];
126
127         g_snprintf(str, sizeof(str), "%d", value);
128         config_node_set_str(rec, parent, key, str);
129 }
130
131 void config_node_set_bool(CONFIG_REC *rec, CONFIG_NODE *parent, const char *key, int value)
132 {
133         config_node_set_str(rec, parent, key, value ? "yes" : "no");
134 }
135
136 int config_set_str(CONFIG_REC *rec, const char *section, const char *key, const char *value)
137 {
138         CONFIG_NODE *parent;
139
140         g_return_val_if_fail(rec != NULL, -1);
141
142         parent = config_node_traverse(rec, section, TRUE);
143         if (parent == NULL) return -1;
144
145         config_node_set_str(rec, parent, key, value);
146         return 0;
147 }
148
149 int config_set_int(CONFIG_REC *rec, const char *section, const char *key, int value)
150 {
151         char str[MAX_INT_STRLEN];
152
153         g_snprintf(str, sizeof(str), "%d", value);
154         return config_set_str(rec, section, key, str);
155 }
156
157 int config_set_bool(CONFIG_REC *rec, const char *section, const char *key, int value)
158 {
159         return config_set_str(rec, section, key, value ? "yes" : "no");
160 }
161
162 void config_node_add_list(CONFIG_REC *rec, CONFIG_NODE *node, char **array)
163 {
164         char **tmp;
165
166         for (tmp = array; *tmp != NULL; tmp++)
167                 config_node_set_str(rec, node, NULL, *tmp);
168 }