1 /* -*-c-*- */
2 /* This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, see: <http://www.gnu.org/licenses/>
14  */
15 
16 /* ---------------------------- included header files ---------------------- */
17 
18 #include "config.h"
19 #include <stdio.h>
20 #include "fvwm.h"
21 #include "externs.h"
22 #include "libs/FGettext.h"
23 #include "libs/Parse.h"
24 #include "libs/Strings.h"
25 #include "infostore.h"
26 #include "misc.h"
27 #include "functions.h"
28 
29 /* ---------------------------- local definitions -------------------------- */
30 
31 /* ---------------------------- local macros ------------------------------- */
32 
33 /* ---------------------------- imports ------------------------------------ */
34 
35 /* ---------------------------- included code files ------------------------ */
36 
37 /* ---------------------------- local types -------------------------------- */
38 
39 static MetaInfo *mi_store;
40 
41 /* ---------------------------- forward declarations ----------------------- */
42 
43 static void delete_metainfo(const char *);
44 
45 /* ---------------------------- local variables ---------------------------- */
46 
47 /* ---------------------------- exported variables (globals) --------------- */
48 
49 /* ---------------------------- local functions ---------------------------- */
50 
new_metainfo(void)51 MetaInfo *new_metainfo(void)
52 {
53 	MetaInfo *mi;
54 
55 	mi = (MetaInfo *)safemalloc(sizeof(MetaInfo));
56 	memset(mi, '\0', sizeof(MetaInfo));
57 
58 	return mi;
59 }
60 
insert_metainfo(char * key,char * value)61 void insert_metainfo(char *key, char *value)
62 {
63 	MetaInfo *mi;
64 	MetaInfo *mi_new;
65 
66 	for (mi = mi_store; mi; mi = mi->next)
67 	{
68 		if (StrEquals(mi->key, key))
69 		{
70 			/* We already have an entry in the list with that key, so
71 			 * update the value of it only.
72 			 */
73 			free (mi->value);
74 			CopyString(&mi->value, value);
75 
76 			return;
77 		}
78 	}
79 
80 	/* It's a new item, add it to the list. */
81 	mi_new = new_metainfo();
82 	mi_new->key = key;
83 	CopyString(&mi_new->value, value);
84 
85 	mi_new->next = mi_store;
86 	mi_store = mi_new;
87 
88 	return;
89 }
90 
delete_metainfo(const char * key)91 static void delete_metainfo(const char *key)
92 {
93 	MetaInfo *mi_current, *mi_prev;
94 	mi_prev = NULL;
95 
96 	for(mi_current = mi_store; mi_current != NULL;
97 		mi_prev = mi_current, mi_current = mi_current->next)
98 	{
99 		if (StrEquals(mi_current->key, key)) {
100 			if (mi_prev == NULL)
101 				mi_store = mi_current->next;
102 			else
103 				mi_prev->next = mi_current->next;
104 
105 			free(mi_current->key);
106 			free(mi_current->value);
107 			free(mi_current);
108 
109 			break;
110 		}
111 	}
112 
113 	return;
114 }
115 
get_metainfo_value(const char * key)116 inline char *get_metainfo_value(const char *key)
117 {
118 	MetaInfo *mi_current;
119 
120 	for(mi_current = mi_store; mi_current; mi_current = mi_current->next)
121 	{
122 		if (StrEquals(mi_current->key, key))
123 			return mi_current->value;
124 	}
125 
126 	return NULL;
127 }
128 
get_metainfo_length(void)129 int get_metainfo_length(void)
130 {
131 	MetaInfo *mi;
132 	int count;
133 
134 	count = 0;
135 
136 	for(mi = mi_store; mi; mi = mi->next)
137 		count++;
138 
139 	return count;
140 }
141 
142 MetaInfo *
get_metainfo(void)143 get_metainfo(void)
144 {
145 	return mi_store;
146 }
147 
print_infostore(void)148 void print_infostore(void)
149 {
150 	MetaInfo *mi;
151 
152 	fprintf(stderr, "Current items in infostore (key, value):\n\n");
153 
154 	if (get_metainfo_length() == 0)
155 	{
156 		fprintf(stderr,
157 			"No items are currently stored in the infostore.\n");
158 		return;
159 	}
160 
161 	for(mi = mi_store; mi; mi = mi->next)
162 	{
163 		fprintf(stderr, "%s\t%s\n", mi->key, mi->value);
164 	}
165 
166 	return;
167 
168 }
169 
170 /* ---------------------------- interface functions ------------------------ */
171 
172 /* ---------------------------- builtin commands --------------------------- */
CMD_InfoStoreAdd(F_CMD_ARGS)173 void CMD_InfoStoreAdd(F_CMD_ARGS)
174 {
175 	char *key, *value;
176 	char *token;
177 
178 	token = PeekToken(action, &action);
179 	key = value = NULL;
180 
181 	if (token)
182 		key = strdup(token);
183 
184 	token = PeekToken(action, &action);
185 
186 	if (token)
187 		value = strdup(token);
188 
189 	if (!key || !value)
190 	{
191 		fvwm_msg(ERR, "CMD_InfoStore", "Bad arguments given.");
192 		return;
193 	}
194 
195 	insert_metainfo(key, value);
196 	free(value);
197 
198 	return;
199 }
200 
CMD_InfoStoreRemove(F_CMD_ARGS)201 void CMD_InfoStoreRemove(F_CMD_ARGS)
202 {
203 	char *token;
204 
205 	token = PeekToken(action, &action);
206 
207 	if (!token)
208 	{
209 		fvwm_msg(ERR, "CMD_InfoStoreRemove", "No key given to remove item.");
210 		return;
211 	}
212 
213 	delete_metainfo(token);
214 
215 	return;
216 }
217 
CMD_InfoStoreClear(F_CMD_ARGS)218 void CMD_InfoStoreClear(F_CMD_ARGS)
219 {
220 	MetaInfo	*mi;
221 
222 	if (get_metainfo_length() == 0)
223 		return;
224 
225 	for (mi = mi_store; mi; mi = mi->next)
226 		delete_metainfo(mi->key);
227 }
228