Added SILC Thread Queue API
[crypto.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
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
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 /* Remove n'th node from a list */
63 void config_node_list_remove(CONFIG_REC *rec, CONFIG_NODE *node, int index)
64 {
65         CONFIG_NODE *child;
66
67         g_return_if_fail(node != NULL);
68         g_return_if_fail(is_node_list(node));
69
70         child = config_node_nth(node, index);
71         if (child != NULL) config_node_remove(rec, node, child);
72 }
73
74 /* Clear all data inside node, but leave the node */
75 void config_node_clear(CONFIG_REC *rec, CONFIG_NODE *node)
76 {
77         g_return_if_fail(node != NULL);
78         g_return_if_fail(is_node_list(node));
79
80         while (node->value != NULL)
81                 config_node_remove(rec, node, ((GSList *) node->value)->data);
82 }
83
84 void config_nodes_remove_all(CONFIG_REC *rec)
85 {
86         g_return_if_fail(rec != NULL);
87
88         while (rec->mainnode->value != NULL)
89                 config_node_remove(rec, rec->mainnode, ((GSList *) rec->mainnode->value)->data);
90 }
91
92 void config_node_set_str(CONFIG_REC *rec, CONFIG_NODE *parent, const char *key, const char *value)
93 {
94         CONFIG_NODE *node;
95         int no_key;
96
97         g_return_if_fail(rec != NULL);
98         g_return_if_fail(parent != NULL);
99
100         no_key = key == NULL;
101         node = no_key ? NULL : config_node_find(parent, key);
102
103         if (value == NULL) {
104                 /* remove the key */
105                 if (node != NULL) config_node_remove(rec, parent, node);
106                 return;
107         }
108
109         if (node != NULL) {
110                 if (strcmp(node->value, value) == 0)
111                         return;
112                 g_free(node->value);
113         } else {
114                 node = g_new0(CONFIG_NODE, 1);
115                 parent->value = g_slist_append(parent->value, node);
116
117                 node->type = no_key ? NODE_TYPE_VALUE : NODE_TYPE_KEY;
118                 node->key = no_key ? NULL : g_strdup(key);
119         }
120
121         node->value = g_strdup(value);
122         rec->modifycounter++;
123 }
124
125 void config_node_set_int(CONFIG_REC *rec, CONFIG_NODE *parent, const char *key, int value)
126 {
127         char str[MAX_INT_STRLEN];
128
129         g_snprintf(str, sizeof(str), "%d", value);
130         config_node_set_str(rec, parent, key, str);
131 }
132
133 void config_node_set_bool(CONFIG_REC *rec, CONFIG_NODE *parent, const char *key, int value)
134 {
135         config_node_set_str(rec, parent, key, value ? "yes" : "no");
136 }
137
138 int config_set_str(CONFIG_REC *rec, const char *section, const char *key, const char *value)
139 {
140         CONFIG_NODE *parent;
141
142         g_return_val_if_fail(rec != NULL, -1);
143
144         parent = config_node_traverse(rec, section, TRUE);
145         if (parent == NULL) return -1;
146
147         config_node_set_str(rec, parent, key, value);
148         return 0;
149 }
150
151 int config_set_int(CONFIG_REC *rec, const char *section, const char *key, int value)
152 {
153         char str[MAX_INT_STRLEN];
154
155         g_snprintf(str, sizeof(str), "%d", value);
156         return config_set_str(rec, section, key, str);
157 }
158
159 int config_set_bool(CONFIG_REC *rec, const char *section, const char *key, int value)
160 {
161         return config_set_str(rec, section, key, value ? "yes" : "no");
162 }
163
164 /* Add all values in `array' to `node' */
165 void config_node_add_list(CONFIG_REC *rec, CONFIG_NODE *node, char **array)
166 {
167         char **tmp;
168
169         for (tmp = array; *tmp != NULL; tmp++)
170                 config_node_set_str(rec, node, NULL, *tmp);
171 }