]>
Dogcows Code - chaz/openbox/blob - parser/parse.c
10 static GHashTable
*callbacks
;
12 static void destfunc(struct Callback
*c
)
20 callbacks
= g_hash_table_new_full(g_str_hash
, g_str_equal
, NULL
,
21 (GDestroyNotify
)destfunc
);
26 g_hash_table_destroy(callbacks
);
29 void parse_register(const char *tag
, ParseCallback func
, void *data
)
33 if ((c
= g_hash_table_lookup(callbacks
, tag
))) {
34 g_warning("tag '%s' already registered", tag
);
38 c
= g_new(struct Callback
, 1);
39 c
->tag
= g_strdup(tag
);
42 g_hash_table_insert(callbacks
, c
->tag
, c
);
45 gboolean
parse_load_rc(xmlDocPtr
*doc
, xmlNodePtr
*root
)
50 path
= g_build_filename(g_get_home_dir(), ".openbox", "rc3", NULL
);
51 if (parse_load(path
, "openbox_config", doc
, root
)) {
55 path
= g_build_filename(RCDIR
, "rc3", NULL
);
56 if (parse_load(path
, "openbox_config", doc
, root
)) {
62 g_message("unable to find a valid config file, using defaults");
66 gboolean
parse_load(const char *path
, const char *rootname
,
67 xmlDocPtr
*doc
, xmlNodePtr
*root
)
70 xmlLineNumbersDefault(1);
72 if ((*doc
= xmlParseFile(path
))) {
73 *root
= xmlDocGetRootElement(*doc
);
77 g_warning("%s is an empty document", path
);
79 if (xmlStrcasecmp((*root
)->name
, (const xmlChar
*)rootname
)) {
82 g_warning("document %s is of wrong type. root *root is "
83 "not 'openbox_config'", path
);
92 void parse_close(xmlDocPtr doc
)
97 void parse_tree(xmlDocPtr doc
, xmlNodePtr node
, void *nothing
)
100 struct Callback
*c
= g_hash_table_lookup(callbacks
, node
->name
);
103 c
->func(doc
, node
->xmlChildrenNode
, c
->data
);
109 char *parse_string(xmlDocPtr doc
, xmlNodePtr node
)
111 xmlChar
*c
= xmlNodeListGetString(doc
, node
->xmlChildrenNode
, TRUE
);
112 char *s
= g_strdup((char*)c
);
117 int parse_int(xmlDocPtr doc
, xmlNodePtr node
)
119 xmlChar
*c
= xmlNodeListGetString(doc
, node
->xmlChildrenNode
, TRUE
);
120 int i
= atoi((char*)c
);
125 gboolean
parse_bool(xmlDocPtr doc
, xmlNodePtr node
)
127 xmlChar
*c
= xmlNodeListGetString(doc
, node
->xmlChildrenNode
, TRUE
);
129 if (!xmlStrcasecmp(c
, (const xmlChar
*) "true"))
131 else if (!xmlStrcasecmp(c
, (const xmlChar
*) "yes"))
133 else if (!xmlStrcasecmp(c
, (const xmlChar
*) "on"))
139 gboolean
parse_contains(const char *val
, xmlDocPtr doc
, xmlNodePtr node
)
141 xmlChar
*c
= xmlNodeListGetString(doc
, node
->xmlChildrenNode
, TRUE
);
143 r
= !xmlStrcasecmp(c
, (const xmlChar
*) val
);
148 xmlNodePtr
parse_find_node(const char *tag
, xmlNodePtr node
)
151 if (!xmlStrcasecmp(node
->name
, (const xmlChar
*) tag
))
158 gboolean
parse_attr_int(const char *name
, xmlNodePtr node
, int *value
)
160 xmlChar
*c
= xmlGetProp(node
, (const xmlChar
*) name
);
163 *value
= atoi((char*)c
);
170 gboolean
parse_attr_string(const char *name
, xmlNodePtr node
, char **value
)
172 xmlChar
*c
= xmlGetProp(node
, (const xmlChar
*) name
);
175 *value
= g_strdup((char*)c
);
182 gboolean
parse_attr_contains(const char *val
, xmlNodePtr node
,
185 xmlChar
*c
= xmlGetProp(node
, (const xmlChar
*) name
);
187 r
= !xmlStrcasecmp(c
, (const xmlChar
*) val
);
This page took 0.04266 seconds and 4 git commands to generate.