1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 obt/parse.c for the Openbox window manager
4 Copyright (c) 2003-2007 Dana Jansens
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 See the COPYING file for a copy of the GNU General Public License.
19 #include "obt/parse.h"
20 #include "obt/paths.h"
27 #ifdef HAVE_SYS_STAT_H
28 # include <sys/stat.h>
30 #ifdef HAVE_SYS_TYPES_H
31 # include <sys/types.h>
39 ObtParseCallback func;
43 struct _ObtParseInst {
46 GHashTable *callbacks;
52 static void destfunc(struct Callback *c)
58 ObtParseInst* obt_parse_instance_new(void)
60 ObtParseInst *i = g_new(ObtParseInst, 1);
62 i->xdg_paths = obt_paths_new();
63 i->callbacks = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
64 (GDestroyNotify)destfunc);
71 void obt_parse_instance_ref(ObtParseInst *i)
76 void obt_parse_instance_unref(ObtParseInst *i)
78 if (i && --i->ref == 0) {
79 obt_paths_unref(i->xdg_paths);
80 g_hash_table_destroy(i->callbacks);
85 xmlDocPtr obt_parse_doc(ObtParseInst *i)
87 g_assert(i->doc); /* a doc is open? */
91 xmlNodePtr obt_parse_root(ObtParseInst *i)
93 g_assert(i->doc); /* a doc is open? */
97 void obt_parse_register(ObtParseInst *i, const gchar *tag,
98 ObtParseCallback func, gpointer data)
102 if (g_hash_table_lookup(i->callbacks, tag)) {
103 g_error("Tag '%s' already registered", tag);
107 c = g_new(struct Callback, 1);
108 c->tag = g_strdup(tag);
111 g_hash_table_insert(i->callbacks, c->tag, c);
114 static gboolean load_file(ObtParseInst *i,
116 const gchar *filename,
117 const gchar *root_node,
123 g_assert(i->doc == NULL); /* another doc isn't open already? */
125 for (it = paths; !r && it; it = g_slist_next(it)) {
129 if (!domain && !filename) /* given a full path to the file */
130 path = g_strdup(it->data);
132 path = g_build_filename(it->data, domain, filename, NULL);
134 if (stat(path, &s) >= 0) {
135 /* XML_PARSE_BLANKS is needed apparently, or the tree can end up
136 with extra nodes in it. */
137 i->doc = xmlReadFile(path, NULL, (XML_PARSE_NOBLANKS |
140 i->root = xmlDocGetRootElement(i->doc);
144 g_message("%s is an empty XML document", path);
146 else if (xmlStrcmp(i->root->name,
147 (const xmlChar*)root_node)) {
151 g_message("XML document %s is of wrong type. Root "
152 "node is not '%s'", path, root_node);
155 i->path = g_strdup(path);
167 gboolean obt_parse_load_file(ObtParseInst *i,
169 const gchar *root_node)
174 paths = g_slist_append(NULL, g_strdup(path));
176 r = load_file(i, NULL, NULL, root_node, paths);
180 paths = g_slist_delete_link(paths, paths);
185 gboolean obt_parse_load_config_file(ObtParseInst *i,
187 const gchar *filename,
188 const gchar *root_node)
190 GSList *it, *paths = NULL;
193 for (it = obt_paths_config_dirs(i->xdg_paths); it; it = g_slist_next(it))
194 paths = g_slist_append(paths, g_strdup(it->data));
196 r = load_file(i, domain, filename, root_node, paths);
200 paths = g_slist_delete_link(paths, paths);
205 gboolean obt_parse_load_data_file(ObtParseInst *i,
207 const gchar *filename,
208 const gchar *root_node)
210 GSList *it, *paths = NULL;
213 for (it = obt_paths_data_dirs(i->xdg_paths); it; it = g_slist_next(it))
214 paths = g_slist_append(paths, g_strdup(it->data));
216 r = load_file(i, domain, filename, root_node, paths);
220 paths = g_slist_delete_link(paths, paths);
225 gboolean obt_parse_load_theme_file(ObtParseInst *i,
228 const gchar *filename,
229 const gchar *root_node)
231 GSList *it, *paths = NULL;
234 /* use ~/.themes for backwards compatibility */
235 paths = g_slist_append
236 (paths, g_build_filename(g_get_home_dir(), ".themes", theme, NULL));
238 for (it = obt_paths_data_dirs(i->xdg_paths); it; it = g_slist_next(it))
239 paths = g_slist_append
240 (paths, g_build_filename(it->data, "themes", theme, NULL));
242 r = load_file(i, domain, filename, root_node, paths);
246 paths = g_slist_delete_link(paths, paths);
252 gboolean obt_parse_load_mem(ObtParseInst *i,
253 gpointer data, guint len, const gchar *root_node)
257 g_assert(i->doc == NULL); /* another doc isn't open already? */
259 i->doc = xmlParseMemory(data, len);
261 i->root = xmlDocGetRootElement(i->doc);
265 g_message("Given memory is an empty document");
267 else if (xmlStrcmp(i->root->name, (const xmlChar*)root_node)) {
271 g_message("XML Document in given memory is of wrong "
272 "type. Root node is not '%s'\n", root_node);
280 void obt_parse_close(ObtParseInst *i)
291 void obt_parse_tree(ObtParseInst *i, xmlNodePtr node)
293 g_assert(i->doc); /* a doc is open? */
296 struct Callback *c = g_hash_table_lookup(i->callbacks, node->name);
297 if (c) c->func(node, c->data);
302 void obt_parse_tree_from_root(ObtParseInst *i)
304 obt_parse_tree(i, i->root->children);
307 gchar *obt_parse_node_string(xmlNodePtr node)
309 xmlChar *c = xmlNodeGetContent(node);
310 gchar *s = g_strdup(c ? (gchar*)c : "");
315 gint obt_parse_node_int(xmlNodePtr node)
317 xmlChar *c = xmlNodeGetContent(node);
318 gint i = c ? atoi((gchar*)c) : 0;
323 gboolean obt_parse_node_bool(xmlNodePtr node)
325 xmlChar *c = xmlNodeGetContent(node);
327 if (c && !xmlStrcasecmp(c, (const xmlChar*) "true"))
329 else if (c && !xmlStrcasecmp(c, (const xmlChar*) "yes"))
331 else if (c && !xmlStrcasecmp(c, (const xmlChar*) "on"))
337 gboolean obt_parse_node_contains(xmlNodePtr node, const gchar *val)
339 xmlChar *c = xmlNodeGetContent(node);
341 r = !xmlStrcasecmp(c, (const xmlChar*) val);
346 xmlNodePtr obt_parse_find_node(xmlNodePtr node, const gchar *tag)
349 if (!xmlStrcmp(node->name, (const xmlChar*) tag))
356 gboolean obt_parse_attr_bool(xmlNodePtr node, const gchar *name,
359 xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
362 if (!xmlStrcasecmp(c, (const xmlChar*) "true"))
363 *value = TRUE, r = TRUE;
364 else if (!xmlStrcasecmp(c, (const xmlChar*) "yes"))
365 *value = TRUE, r = TRUE;
366 else if (!xmlStrcasecmp(c, (const xmlChar*) "on"))
367 *value = TRUE, r = TRUE;
368 else if (!xmlStrcasecmp(c, (const xmlChar*) "false"))
369 *value = FALSE, r = TRUE;
370 else if (!xmlStrcasecmp(c, (const xmlChar*) "no"))
371 *value = FALSE, r = TRUE;
372 else if (!xmlStrcasecmp(c, (const xmlChar*) "off"))
373 *value = FALSE, r = TRUE;
379 gboolean obt_parse_attr_int(xmlNodePtr node, const gchar *name, gint *value)
381 xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
384 *value = atoi((gchar*)c);
391 gboolean obt_parse_attr_string(xmlNodePtr node, const gchar *name,
394 xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
397 *value = g_strdup((gchar*)c);
404 gboolean obt_parse_attr_contains(xmlNodePtr node, const gchar *name,
407 xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
410 r = !xmlStrcasecmp(c, (const xmlChar*) val);