]>
Dogcows Code - chaz/openbox/blob - obt/parse.c
61fe5b821e3a09b02002b0a846925792a12a06aa
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"
29 #ifdef HAVE_SYS_STAT_H
30 # include <sys/stat.h>
32 #ifdef HAVE_SYS_TYPES_H
33 # include <sys/types.h>
39 static gboolean xdg_start
;
40 static gchar
*xdg_config_home_path
;
41 static gchar
*xdg_data_home_path
;
42 static GSList
*xdg_config_dir_paths
;
43 static GSList
*xdg_data_dir_paths
;
47 ObtParseCallback func
;
51 struct _ObtParseInst
{
53 GHashTable
*callbacks
;
59 static void destfunc(struct Callback
*c
)
65 ObtParseInst
* obt_parse_instance_new(void)
67 ObtParseInst
*i
= g_new(ObtParseInst
, 1);
69 i
->callbacks
= g_hash_table_new_full(g_str_hash
, g_str_equal
, NULL
,
70 (GDestroyNotify
)destfunc
);
77 void obt_parse_instance_ref(ObtParseInst
*i
)
82 void obt_parse_instance_unref(ObtParseInst
*i
)
84 if (i
&& --i
->ref
== 0) {
85 g_hash_table_destroy(i
->callbacks
);
90 xmlDocPtr
obt_parse_instance_doc(ObtParseInst
*i
)
92 g_assert(i
->doc
); /* a doc is open? */
96 xmlNodePtr
obt_parse_instance_root(ObtParseInst
*i
)
98 g_assert(i
->doc
); /* a doc is open? */
102 void obt_parse_register(ObtParseInst
*i
, const gchar
*tag
,
103 ObtParseCallback func
, gpointer data
)
107 if ((c
= g_hash_table_lookup(i
->callbacks
, tag
))) {
108 g_error("Tag '%s' already registered", tag
);
112 c
= g_new(struct Callback
, 1);
113 c
->tag
= g_strdup(tag
);
116 g_hash_table_insert(i
->callbacks
, c
->tag
, c
);
119 static gboolean
load_file(ObtParseInst
*i
,
121 const gchar
*filename
,
122 const gchar
*root_node
,
128 g_assert(i
->doc
== NULL
); /* another doc isn't open already? */
130 for (it
= paths
; !r
&& it
; it
= g_slist_next(it
)) {
134 if (!domain
&& !filename
) /* given a full path to the file */
135 path
= g_strdup(it
->data
);
137 path
= g_build_filename(it
->data
, domain
, filename
, NULL
);
139 if (stat(path
, &s
) >= 0) {
140 /* XML_PARSE_BLANKS is needed apparently, or the tree can end up
141 with extra nodes in it. */
142 i
->doc
= xmlReadFile(path
, NULL
, (XML_PARSE_NOBLANKS
|
145 i
->root
= xmlDocGetRootElement(i
->doc
);
149 g_message("%s is an empty XML document", path
);
151 else if (xmlStrcmp(i
->root
->name
,
152 (const xmlChar
*)root_node
)) {
156 g_message("XML document %s is of wrong type. Root "
157 "node is not '%s'", path
, root_node
);
160 i
->path
= g_strdup(path
);
172 gboolean
obt_parse_load_file(ObtParseInst
*i
,
174 const gchar
*root_node
)
179 paths
= g_slist_append(NULL
, g_strdup(path
));
181 r
= load_file(i
, NULL
, NULL
, root_node
, paths
);
185 paths
= g_slist_delete_link(paths
, paths
);
190 gboolean
obt_parse_load_config_file(ObtParseInst
*i
,
192 const gchar
*filename
,
193 const gchar
*root_node
)
195 GSList
*it
, *paths
= NULL
;
198 for (it
= xdg_config_dir_paths
; it
; it
= g_slist_next(it
))
199 paths
= g_slist_append(paths
, g_strdup(it
->data
));
201 r
= load_file(i
, domain
, filename
, root_node
, paths
);
205 paths
= g_slist_delete_link(paths
, paths
);
210 gboolean
obt_parse_load_data_file(ObtParseInst
*i
,
212 const gchar
*filename
,
213 const gchar
*root_node
)
215 GSList
*it
, *paths
= NULL
;
218 for (it
= xdg_data_dir_paths
; it
; it
= g_slist_next(it
))
219 paths
= g_slist_append(paths
, g_strdup(it
->data
));
221 r
= load_file(i
, domain
, filename
, root_node
, paths
);
225 paths
= g_slist_delete_link(paths
, paths
);
230 gboolean
obt_parse_load_theme_file(ObtParseInst
*i
,
233 const gchar
*filename
,
234 const gchar
*root_node
)
236 GSList
*it
, *paths
= NULL
;
239 /* use ~/.themes for backwards compatibility */
240 paths
= g_slist_append
241 (paths
, g_build_filename(g_get_home_dir(), ".themes", theme
, NULL
));
243 for (it
= xdg_data_dir_paths
; it
; it
= g_slist_next(it
))
244 paths
= g_slist_append
245 (paths
, g_build_filename(it
->data
, "themes", theme
, NULL
));
247 r
= load_file(i
, domain
, filename
, root_node
, paths
);
251 paths
= g_slist_delete_link(paths
, paths
);
257 gboolean
obt_parse_load_mem(ObtParseInst
*i
,
258 gpointer data
, guint len
, const gchar
*root_node
)
262 g_assert(i
->doc
== NULL
); /* another doc isn't open already? */
264 i
->doc
= xmlParseMemory(data
, len
);
266 i
->root
= xmlDocGetRootElement(i
->doc
);
270 g_message("Given memory is an empty document");
272 else if (xmlStrcmp(i
->root
->name
, (const xmlChar
*)root_node
)) {
276 g_message("XML Document in given memory is of wrong "
277 "type. Root node is not '%s'\n", root_node
);
285 void obt_parse_close(ObtParseInst
*i
)
296 void obt_parse_tree(ObtParseInst
*i
, xmlNodePtr node
)
298 g_assert(i
->doc
); /* a doc is open? */
301 struct Callback
*c
= g_hash_table_lookup(i
->callbacks
, node
->name
);
302 if (c
) c
->func(node
, c
->data
);
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
);
415 static gint
slist_path_cmp(const gchar
*a
, const gchar
*b
)
420 typedef GSList
* (*GSListFunc
) (gpointer list
, gconstpointer data
);
422 static GSList
* slist_path_add(GSList
*list
, gpointer data
, GSListFunc func
)
429 if (!g_slist_find_custom(list
, data
, (GCompareFunc
) slist_path_cmp
))
430 list
= func(list
, data
);
437 static GSList
* split_paths(const gchar
*paths
)
444 spl
= g_strsplit(paths
, ":", -1);
445 for (it
= spl
; *it
; ++it
)
446 list
= slist_path_add(list
, *it
, (GSListFunc
) g_slist_append
);
451 void parse_paths_startup(void)
459 path
= g_getenv("XDG_CONFIG_HOME");
460 if (path
&& path
[0] != '\0') /* not unset or empty */
461 xdg_config_home_path
= g_build_filename(path
, NULL
);
463 xdg_config_home_path
= g_build_filename(g_get_home_dir(), ".config",
466 path
= g_getenv("XDG_DATA_HOME");
467 if (path
&& path
[0] != '\0') /* not unset or empty */
468 xdg_data_home_path
= g_build_filename(path
, NULL
);
470 xdg_data_home_path
= g_build_filename(g_get_home_dir(), ".local",
473 path
= g_getenv("XDG_CONFIG_DIRS");
474 if (path
&& path
[0] != '\0') /* not unset or empty */
475 xdg_config_dir_paths
= split_paths(path
);
477 xdg_config_dir_paths
= slist_path_add(xdg_config_dir_paths
,
479 (GSListFunc
) g_slist_append
);
480 xdg_config_dir_paths
= slist_path_add(xdg_config_dir_paths
,
484 (GSListFunc
) g_slist_append
);
486 xdg_config_dir_paths
= slist_path_add(xdg_config_dir_paths
,
487 g_strdup(xdg_config_home_path
),
488 (GSListFunc
) g_slist_prepend
);
490 path
= g_getenv("XDG_DATA_DIRS");
491 if (path
&& path
[0] != '\0') /* not unset or empty */
492 xdg_data_dir_paths
= split_paths(path
);
494 xdg_data_dir_paths
= slist_path_add(xdg_data_dir_paths
,
496 (GSListFunc
) g_slist_append
);
497 xdg_data_dir_paths
= slist_path_add(xdg_data_dir_paths
,
500 "usr", "local", "share", NULL
),
501 (GSListFunc
) g_slist_append
);
502 xdg_data_dir_paths
= slist_path_add(xdg_data_dir_paths
,
505 "usr", "share", NULL
),
506 (GSListFunc
) g_slist_append
);
508 xdg_data_dir_paths
= slist_path_add(xdg_data_dir_paths
,
509 g_strdup(xdg_data_home_path
),
510 (GSListFunc
) g_slist_prepend
);
513 void parse_paths_shutdown(void)
521 for (it
= xdg_config_dir_paths
; it
; it
= g_slist_next(it
))
523 g_slist_free(xdg_config_dir_paths
);
524 xdg_config_dir_paths
= NULL
;
525 for (it
= xdg_data_dir_paths
; it
; it
= g_slist_next(it
))
527 g_slist_free(xdg_data_dir_paths
);
528 xdg_data_dir_paths
= NULL
;
529 g_free(xdg_config_home_path
);
530 xdg_config_home_path
= NULL
;
531 g_free(xdg_data_home_path
);
532 xdg_data_home_path
= NULL
;
535 gchar
*parse_expand_tilde(const gchar
*f
)
542 spl
= g_strsplit(f
, "~", 0);
543 ret
= g_strjoinv(g_get_home_dir(), spl
);
548 gboolean
parse_mkdir(const gchar
*path
, gint mode
)
552 g_return_val_if_fail(path
!= NULL
, FALSE
);
553 g_return_val_if_fail(path
[0] != '\0', FALSE
);
555 if (!g_file_test(path
, G_FILE_TEST_IS_DIR
))
556 if (mkdir(path
, mode
) == -1)
562 gboolean
parse_mkdir_path(const gchar
*path
, gint mode
)
566 g_return_val_if_fail(path
!= NULL
, FALSE
);
567 g_return_val_if_fail(path
[0] == '/', FALSE
);
569 if (!g_file_test(path
, G_FILE_TEST_IS_DIR
)) {
574 while ((e
= strchr(e
+ 1, '/'))) {
576 if (!(ret
= parse_mkdir(c
, mode
)))
577 goto parse_mkdir_path_end
;
580 ret
= parse_mkdir(c
, mode
);
582 parse_mkdir_path_end
:
589 const gchar
* parse_xdg_config_home_path(void)
591 return xdg_config_home_path
;
594 const gchar
* parse_xdg_data_home_path(void)
596 return xdg_data_home_path
;
599 GSList
* parse_xdg_config_dir_paths(void)
601 return xdg_config_dir_paths
;
604 GSList
* parse_xdg_data_dir_paths(void)
606 return xdg_data_dir_paths
;
This page took 0.057593 seconds and 4 git commands to generate.