2003-05-24 21:47:06 +00:00
|
|
|
#include "parse.h"
|
|
|
|
#include <glib.h>
|
|
|
|
|
|
|
|
struct Callback {
|
|
|
|
char *tag;
|
|
|
|
ParseCallback func;
|
|
|
|
void *data;
|
|
|
|
};
|
|
|
|
|
2003-08-12 19:14:53 +00:00
|
|
|
struct _ObParseInst {
|
|
|
|
GHashTable *callbacks;
|
|
|
|
};
|
2003-05-24 21:47:06 +00:00
|
|
|
|
|
|
|
static void destfunc(struct Callback *c)
|
|
|
|
{
|
|
|
|
g_free(c->tag);
|
|
|
|
g_free(c);
|
|
|
|
}
|
|
|
|
|
2003-08-12 19:14:53 +00:00
|
|
|
ObParseInst* parse_startup()
|
2003-05-24 21:47:06 +00:00
|
|
|
{
|
2003-08-12 19:14:53 +00:00
|
|
|
ObParseInst *i = g_new(ObParseInst, 1);
|
|
|
|
i->callbacks = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
|
|
|
|
(GDestroyNotify)destfunc);
|
|
|
|
return i;
|
2003-05-24 21:47:06 +00:00
|
|
|
}
|
|
|
|
|
2003-08-12 19:14:53 +00:00
|
|
|
void parse_shutdown(ObParseInst *i)
|
2003-05-24 21:47:06 +00:00
|
|
|
{
|
2003-08-12 19:14:53 +00:00
|
|
|
if (i) {
|
|
|
|
g_hash_table_destroy(i->callbacks);
|
|
|
|
g_free(i);
|
|
|
|
}
|
2003-05-24 21:47:06 +00:00
|
|
|
}
|
|
|
|
|
2003-08-12 19:14:53 +00:00
|
|
|
void parse_register(ObParseInst *i, const char *tag,
|
|
|
|
ParseCallback func, void *data)
|
2003-05-24 21:47:06 +00:00
|
|
|
{
|
|
|
|
struct Callback *c;
|
|
|
|
|
2003-08-12 19:14:53 +00:00
|
|
|
if ((c = g_hash_table_lookup(i->callbacks, tag))) {
|
2003-05-24 21:47:06 +00:00
|
|
|
g_warning("tag '%s' already registered", tag);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
c = g_new(struct Callback, 1);
|
|
|
|
c->tag = g_strdup(tag);
|
|
|
|
c->func = func;
|
|
|
|
c->data = data;
|
2003-08-12 19:14:53 +00:00
|
|
|
g_hash_table_insert(i->callbacks, c->tag, c);
|
2003-05-24 21:47:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gboolean parse_load_rc(xmlDocPtr *doc, xmlNodePtr *root)
|
|
|
|
{
|
|
|
|
char *path;
|
|
|
|
gboolean r = FALSE;
|
|
|
|
|
2003-09-01 17:01:21 +00:00
|
|
|
path = g_build_filename(g_get_home_dir(), ".openbox", "rc.xml", NULL);
|
2003-05-24 21:47:06 +00:00
|
|
|
if (parse_load(path, "openbox_config", doc, root)) {
|
|
|
|
r = TRUE;
|
|
|
|
} else {
|
|
|
|
g_free(path);
|
2003-09-01 17:01:21 +00:00
|
|
|
path = g_build_filename(RCDIR, "rc.xml", NULL);
|
2003-05-24 21:47:06 +00:00
|
|
|
if (parse_load(path, "openbox_config", doc, root)) {
|
|
|
|
r = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g_free(path);
|
|
|
|
if (!r)
|
2003-07-24 06:02:38 +00:00
|
|
|
g_warning("unable to find a valid config file, using defaults");
|
2003-05-24 21:47:06 +00:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean parse_load(const char *path, const char *rootname,
|
|
|
|
xmlDocPtr *doc, xmlNodePtr *root)
|
|
|
|
{
|
|
|
|
if ((*doc = xmlParseFile(path))) {
|
|
|
|
*root = xmlDocGetRootElement(*doc);
|
|
|
|
if (!*root) {
|
|
|
|
xmlFreeDoc(*doc);
|
|
|
|
*doc = NULL;
|
|
|
|
g_warning("%s is an empty document", path);
|
|
|
|
} else {
|
|
|
|
if (xmlStrcasecmp((*root)->name, (const xmlChar*)rootname)) {
|
|
|
|
xmlFreeDoc(*doc);
|
|
|
|
*doc = NULL;
|
2003-08-12 19:14:53 +00:00
|
|
|
g_warning("document %s is of wrong type. root node is "
|
|
|
|
"not '%s'", path, rootname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!*doc)
|
|
|
|
return FALSE;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean parse_load_mem(gpointer data, guint len, const char *rootname,
|
|
|
|
xmlDocPtr *doc, xmlNodePtr *root)
|
|
|
|
{
|
|
|
|
if ((*doc = xmlParseMemory(data, len))) {
|
|
|
|
*root = xmlDocGetRootElement(*doc);
|
|
|
|
if (!*root) {
|
|
|
|
xmlFreeDoc(*doc);
|
|
|
|
*doc = NULL;
|
|
|
|
g_warning("Given memory is an empty document");
|
|
|
|
} else {
|
|
|
|
if (xmlStrcasecmp((*root)->name, (const xmlChar*)rootname)) {
|
|
|
|
xmlFreeDoc(*doc);
|
|
|
|
*doc = NULL;
|
|
|
|
g_warning("document in given memory is of wrong type. root "
|
|
|
|
"node is not '%s'", rootname);
|
2003-05-24 21:47:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!*doc)
|
|
|
|
return FALSE;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void parse_close(xmlDocPtr doc)
|
|
|
|
{
|
|
|
|
xmlFree(doc);
|
|
|
|
}
|
|
|
|
|
2003-08-12 19:14:53 +00:00
|
|
|
void parse_tree(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node)
|
2003-05-24 21:47:06 +00:00
|
|
|
{
|
|
|
|
while (node) {
|
2003-08-12 19:14:53 +00:00
|
|
|
struct Callback *c = g_hash_table_lookup(i->callbacks, node->name);
|
2003-05-24 21:47:06 +00:00
|
|
|
|
|
|
|
if (c)
|
2003-08-12 19:14:53 +00:00
|
|
|
c->func(i, doc, node, c->data);
|
2003-05-24 21:47:06 +00:00
|
|
|
|
|
|
|
node = node->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
char *parse_string(xmlDocPtr doc, xmlNodePtr node)
|
|
|
|
{
|
|
|
|
xmlChar *c = xmlNodeListGetString(doc, node->xmlChildrenNode, TRUE);
|
2003-07-31 09:05:45 +00:00
|
|
|
char *s = g_strdup(c ? (char*)c : "");
|
2003-05-24 21:47:06 +00:00
|
|
|
xmlFree(c);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
int parse_int(xmlDocPtr doc, xmlNodePtr node)
|
|
|
|
{
|
|
|
|
xmlChar *c = xmlNodeListGetString(doc, node->xmlChildrenNode, TRUE);
|
|
|
|
int i = atoi((char*)c);
|
|
|
|
xmlFree(c);
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean parse_bool(xmlDocPtr doc, xmlNodePtr node)
|
|
|
|
{
|
|
|
|
xmlChar *c = xmlNodeListGetString(doc, node->xmlChildrenNode, TRUE);
|
|
|
|
gboolean b = FALSE;
|
|
|
|
if (!xmlStrcasecmp(c, (const xmlChar*) "true"))
|
|
|
|
b = TRUE;
|
|
|
|
else if (!xmlStrcasecmp(c, (const xmlChar*) "yes"))
|
|
|
|
b = TRUE;
|
|
|
|
else if (!xmlStrcasecmp(c, (const xmlChar*) "on"))
|
|
|
|
b = TRUE;
|
|
|
|
xmlFree(c);
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean parse_contains(const char *val, xmlDocPtr doc, xmlNodePtr node)
|
|
|
|
{
|
|
|
|
xmlChar *c = xmlNodeListGetString(doc, node->xmlChildrenNode, TRUE);
|
|
|
|
gboolean r;
|
|
|
|
r = !xmlStrcasecmp(c, (const xmlChar*) val);
|
|
|
|
xmlFree(c);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
xmlNodePtr parse_find_node(const char *tag, xmlNodePtr node)
|
|
|
|
{
|
|
|
|
while (node) {
|
|
|
|
if (!xmlStrcasecmp(node->name, (const xmlChar*) tag))
|
|
|
|
return node;
|
|
|
|
node = node->next;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean parse_attr_int(const char *name, xmlNodePtr node, int *value)
|
|
|
|
{
|
|
|
|
xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
|
|
|
|
gboolean r = FALSE;
|
|
|
|
if (c) {
|
|
|
|
*value = atoi((char*)c);
|
|
|
|
r = TRUE;
|
|
|
|
}
|
|
|
|
xmlFree(c);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean parse_attr_string(const char *name, xmlNodePtr node, char **value)
|
|
|
|
{
|
|
|
|
xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
|
|
|
|
gboolean r = FALSE;
|
|
|
|
if (c) {
|
|
|
|
*value = g_strdup((char*)c);
|
|
|
|
r = TRUE;
|
|
|
|
}
|
|
|
|
xmlFree(c);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean parse_attr_contains(const char *val, xmlNodePtr node,
|
|
|
|
const char *name)
|
|
|
|
{
|
|
|
|
xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
|
|
|
|
gboolean r;
|
|
|
|
r = !xmlStrcasecmp(c, (const xmlChar*) val);
|
|
|
|
xmlFree(c);
|
|
|
|
return r;
|
|
|
|
}
|