]> Dogcows Code - chaz/openbox/blob - obt/xml.c
Remove some evil tabs
[chaz/openbox] / obt / xml.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3 obt/xml.c for the Openbox window manager
4 Copyright (c) 2003-2007 Dana Jansens
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 See the COPYING file for a copy of the GNU General Public License.
17 */
18
19 #include "obt/xml.h"
20 #include "obt/paths.h"
21
22 #include <libxml/xinclude.h>
23 #include <glib.h>
24
25 #ifdef HAVE_STDLIB_H
26 # include <stdlib.h>
27 #endif
28 #ifdef HAVE_SYS_STAT_H
29 # include <sys/stat.h>
30 #endif
31 #ifdef HAVE_SYS_TYPES_H
32 # include <sys/types.h>
33 #endif
34 #ifdef HAVE_UNISTD_H
35 # include <unistd.h>
36 #endif
37
38 struct Callback {
39 gchar *tag;
40 ObtXmlCallback func;
41 gpointer data;
42 };
43
44 struct _ObtXmlInst {
45 gint ref;
46 ObtPaths *xdg_paths;
47 GHashTable *callbacks;
48 xmlDocPtr doc;
49 xmlNodePtr root;
50 gchar *path;
51 };
52
53 static void destfunc(struct Callback *c)
54 {
55 g_free(c->tag);
56 g_slice_free(struct Callback, c);
57 }
58
59 ObtXmlInst* obt_xml_instance_new(void)
60 {
61 ObtXmlInst *i = g_slice_new(ObtXmlInst);
62 i->ref = 1;
63 i->xdg_paths = obt_paths_new();
64 i->callbacks = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
65 (GDestroyNotify)destfunc);
66 i->doc = NULL;
67 i->root = NULL;
68 i->path = NULL;
69 return i;
70 }
71
72 void obt_xml_instance_ref(ObtXmlInst *i)
73 {
74 ++i->ref;
75 }
76
77 void obt_xml_instance_unref(ObtXmlInst *i)
78 {
79 if (i && --i->ref == 0) {
80 obt_paths_unref(i->xdg_paths);
81 g_hash_table_destroy(i->callbacks);
82 g_slice_free(ObtXmlInst, i);
83 }
84 }
85
86 xmlDocPtr obt_xml_doc(ObtXmlInst *i)
87 {
88 g_assert(i->doc); /* a doc is open? */
89 return i->doc;
90 }
91
92 xmlNodePtr obt_xml_root(ObtXmlInst *i)
93 {
94 g_assert(i->doc); /* a doc is open? */
95 return i->root;
96 }
97
98 void obt_xml_register(ObtXmlInst *i, const gchar *tag,
99 ObtXmlCallback func, gpointer data)
100 {
101 struct Callback *c;
102
103 if (g_hash_table_lookup(i->callbacks, tag)) {
104 g_error("Tag '%s' already registered", tag);
105 return;
106 }
107
108 c = g_slice_new(struct Callback);
109 c->tag = g_strdup(tag);
110 c->func = func;
111 c->data = data;
112 g_hash_table_insert(i->callbacks, c->tag, c);
113 }
114
115 void obt_xml_unregister(ObtXmlInst *i, const gchar *tag)
116 {
117 g_hash_table_remove(i->callbacks, tag);
118 }
119
120 static gboolean load_file(ObtXmlInst *i,
121 const gchar *domain,
122 const gchar *filename,
123 const gchar *root_node,
124 GSList *paths)
125 {
126 GSList *it;
127 gboolean r = FALSE;
128
129 g_assert(i->doc == NULL); /* another doc isn't open already? */
130
131 for (it = paths; !r && it; it = g_slist_next(it)) {
132 gchar *path;
133 struct stat s;
134
135 if (!domain && !filename) /* given a full path to the file */
136 path = g_strdup(it->data);
137 else
138 path = g_build_filename(it->data, domain, filename, NULL);
139
140 if (stat(path, &s) >= 0) {
141 /* XML_PARSE_BLANKS is needed apparently, or the tree can end up
142 with extra nodes in it. */
143 i->doc = xmlReadFile(path, NULL, (XML_PARSE_NOBLANKS |
144 XML_PARSE_RECOVER));
145 xmlXIncludeProcessFlags(i->doc, (XML_PARSE_NOBLANKS |
146 XML_PARSE_RECOVER));
147 if (i->doc) {
148 i->root = xmlDocGetRootElement(i->doc);
149 if (!i->root) {
150 xmlFreeDoc(i->doc);
151 i->doc = NULL;
152 g_message("%s is an empty XML document", path);
153 }
154 else if (xmlStrcmp(i->root->name,
155 (const xmlChar*)root_node)) {
156 xmlFreeDoc(i->doc);
157 i->doc = NULL;
158 i->root = NULL;
159 g_message("XML document %s is of wrong type. Root "
160 "node is not '%s'", path, root_node);
161 }
162 else {
163 i->path = g_strdup(path);
164 r = TRUE; /* ok! */
165 }
166 }
167 }
168
169 g_free(path);
170 }
171
172 return r;
173 }
174
175 gboolean obt_xml_load_file(ObtXmlInst *i,
176 const gchar *path,
177 const gchar *root_node)
178 {
179 GSList *paths;
180 gboolean r;
181
182 paths = g_slist_append(NULL, g_strdup(path));
183
184 r = load_file(i, NULL, NULL, root_node, paths);
185
186 while (paths) {
187 g_free(paths->data);
188 paths = g_slist_delete_link(paths, paths);
189 }
190 return r;
191 }
192
193 gboolean obt_xml_load_config_file(ObtXmlInst *i,
194 const gchar *domain,
195 const gchar *filename,
196 const gchar *root_node)
197 {
198 GSList *it, *paths = NULL;
199 gboolean r;
200
201 for (it = obt_paths_config_dirs(i->xdg_paths); it; it = g_slist_next(it))
202 paths = g_slist_append(paths, g_strdup(it->data));
203
204 r = load_file(i, domain, filename, root_node, paths);
205
206 while (paths) {
207 g_free(paths->data);
208 paths = g_slist_delete_link(paths, paths);
209 }
210 return r;
211 }
212
213 gboolean obt_xml_load_data_file(ObtXmlInst *i,
214 const gchar *domain,
215 const gchar *filename,
216 const gchar *root_node)
217 {
218 GSList *it, *paths = NULL;
219 gboolean r;
220
221 for (it = obt_paths_data_dirs(i->xdg_paths); it; it = g_slist_next(it))
222 paths = g_slist_append(paths, g_strdup(it->data));
223
224 r = load_file(i, domain, filename, root_node, paths);
225
226 while (paths) {
227 g_free(paths->data);
228 paths = g_slist_delete_link(paths, paths);
229 }
230 return r;
231 }
232
233 gboolean obt_xml_load_theme_file(ObtXmlInst *i,
234 const gchar *theme,
235 const gchar *domain,
236 const gchar *filename,
237 const gchar *root_node)
238 {
239 GSList *it, *paths = NULL;
240 gboolean r;
241
242 /* use ~/.themes for backwards compatibility */
243 paths = g_slist_append
244 (paths, g_build_filename(g_get_home_dir(), ".themes", theme, NULL));
245
246 for (it = obt_paths_data_dirs(i->xdg_paths); it; it = g_slist_next(it))
247 paths = g_slist_append
248 (paths, g_build_filename(it->data, "themes", theme, NULL));
249
250 r = load_file(i, domain, filename, root_node, paths);
251
252 while (paths) {
253 g_free(paths->data);
254 paths = g_slist_delete_link(paths, paths);
255 }
256 return r;
257 }
258
259
260 gboolean obt_xml_load_mem(ObtXmlInst *i,
261 gpointer data, guint len, const gchar *root_node)
262 {
263 gboolean r = FALSE;
264
265 g_assert(i->doc == NULL); /* another doc isn't open already? */
266
267 i->doc = xmlParseMemory(data, len);
268 if (i) {
269 i->root = xmlDocGetRootElement(i->doc);
270 if (!i->root) {
271 xmlFreeDoc(i->doc);
272 i->doc = NULL;
273 g_message("Given memory is an empty document");
274 }
275 else if (xmlStrcmp(i->root->name, (const xmlChar*)root_node)) {
276 xmlFreeDoc(i->doc);
277 i->doc = NULL;
278 i->root = NULL;
279 g_message("XML Document in given memory is of wrong "
280 "type. Root node is not '%s'\n", root_node);
281 }
282 else
283 r = TRUE; /* ok ! */
284 }
285 return r;
286 }
287
288 gboolean obt_xml_save_file(ObtXmlInst *inst,
289 const gchar *path,
290 gboolean pretty)
291 {
292 return xmlSaveFormatFile(path, inst->doc, pretty) != -1;
293 }
294
295 void obt_xml_close(ObtXmlInst *i)
296 {
297 if (i && i->doc) {
298 xmlFreeDoc(i->doc);
299 g_free(i->path);
300 i->doc = NULL;
301 i->root = NULL;
302 i->path = NULL;
303 }
304 }
305
306 void obt_xml_tree(ObtXmlInst *i, xmlNodePtr node)
307 {
308 g_assert(i->doc); /* a doc is open? */
309
310 while (node) {
311 if (node->name) {
312 struct Callback *c = g_hash_table_lookup(i->callbacks, node->name);
313 if (c) c->func(node, c->data);
314 }
315 node = node->next;
316 }
317 }
318
319 void obt_xml_tree_from_root(ObtXmlInst *i)
320 {
321 obt_xml_tree(i, i->root->children);
322 }
323
324 gchar *obt_xml_node_string_unstripped(xmlNodePtr node)
325 {
326 xmlChar *c = xmlNodeGetContent(node);
327 gchar *s;
328 s = g_strdup(c ? (gchar*)c : "");
329 xmlFree(c);
330 return s;
331 }
332
333 gchar *obt_xml_node_string(xmlNodePtr node)
334 {
335 gchar* result = obt_xml_node_string_unstripped(node);
336 g_strstrip(result); /* strip leading/trailing whitespace */
337 return result;
338 }
339
340 gint obt_xml_node_int(xmlNodePtr node)
341 {
342 xmlChar *c = xmlNodeGetContent(node);
343 gint i;
344 if (c) g_strstrip((char*)c); /* strip leading/trailing whitespace */
345 i = c ? atoi((gchar*)c) : 0;
346 xmlFree(c);
347 return i;
348 }
349
350 gboolean obt_xml_node_bool(xmlNodePtr node)
351 {
352 xmlChar *c = xmlNodeGetContent(node);
353 gboolean b = FALSE;
354 if (c) g_strstrip((char*)c); /* strip leading/trailing whitespace */
355 if (c && !xmlStrcasecmp(c, (const xmlChar*) "true"))
356 b = TRUE;
357 else if (c && !xmlStrcasecmp(c, (const xmlChar*) "yes"))
358 b = TRUE;
359 else if (c && !xmlStrcasecmp(c, (const xmlChar*) "on"))
360 b = TRUE;
361 xmlFree(c);
362 return b;
363 }
364
365 gboolean obt_xml_node_contains(xmlNodePtr node, const gchar *val)
366 {
367 xmlChar *c = xmlNodeGetContent(node);
368 gboolean r;
369 if (c) g_strstrip((char*)c); /* strip leading/trailing whitespace */
370 r = !xmlStrcasecmp(c, (const xmlChar*) val);
371 xmlFree(c);
372 return r;
373 }
374
375 xmlNodePtr obt_xml_find_node(xmlNodePtr node, const gchar *tag)
376 {
377 while (node) {
378 if (!xmlStrcmp(node->name, (const xmlChar*) tag))
379 return node;
380 node = node->next;
381 }
382 return NULL;
383 }
384
385 gboolean obt_xml_attr_bool(xmlNodePtr node, const gchar *name,
386 gboolean *value)
387 {
388 xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
389 gboolean r = FALSE;
390 if (c) {
391 g_strstrip((char*)c); /* strip leading/trailing whitespace */
392 if (!xmlStrcasecmp(c, (const xmlChar*) "true"))
393 *value = TRUE, r = TRUE;
394 else if (!xmlStrcasecmp(c, (const xmlChar*) "yes"))
395 *value = TRUE, r = TRUE;
396 else if (!xmlStrcasecmp(c, (const xmlChar*) "on"))
397 *value = TRUE, r = TRUE;
398 else if (!xmlStrcasecmp(c, (const xmlChar*) "false"))
399 *value = FALSE, r = TRUE;
400 else if (!xmlStrcasecmp(c, (const xmlChar*) "no"))
401 *value = FALSE, r = TRUE;
402 else if (!xmlStrcasecmp(c, (const xmlChar*) "off"))
403 *value = FALSE, r = TRUE;
404 }
405 xmlFree(c);
406 return r;
407 }
408
409 gboolean obt_xml_attr_int(xmlNodePtr node, const gchar *name, gint *value)
410 {
411 xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
412 gboolean r = FALSE;
413 if (c) {
414 g_strstrip((char*)c); /* strip leading/trailing whitespace */
415 *value = atoi((gchar*)c);
416 r = TRUE;
417 }
418 xmlFree(c);
419 return r;
420 }
421
422 gboolean obt_xml_attr_string_unstripped(xmlNodePtr node, const gchar *name,
423 gchar **value)
424 {
425 xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
426 gboolean r = FALSE;
427 if (c) {
428 *value = g_strdup((gchar*)c);
429 r = TRUE;
430 }
431 xmlFree(c);
432 return r;
433 }
434
435 gboolean obt_xml_attr_string(xmlNodePtr node, const gchar *name,
436 gchar **value)
437 {
438 gboolean result = obt_xml_attr_string_unstripped(node, name, value);
439 if (result)
440 g_strstrip(*value); /* strip leading/trailing whitespace */
441 return result;
442 }
443
444 gboolean obt_xml_attr_contains(xmlNodePtr node, const gchar *name,
445 const gchar *val)
446 {
447 xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
448 gboolean r = FALSE;
449 if (c) {
450 g_strstrip((char*)c); /* strip leading/trailing whitespace */
451 r = !xmlStrcasecmp(c, (const xmlChar*) val);
452 }
453 xmlFree(c);
454 return r;
455 }
This page took 0.049018 seconds and 4 git commands to generate.