File: xml.h

package info (click to toggle)
labwc 0.9.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,992 kB
  • sloc: ansic: 34,788; perl: 5,837; xml: 873; sh: 162; python: 131; makefile: 12
file content (66 lines) | stat: -rw-r--r-- 1,714 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/* SPDX-License-Identifier: GPL-2.0-only */
#ifndef LABWC_XML_H
#define LABWC_XML_H

#include <libxml/tree.h>
#include <stdbool.h>

/*
 * Converts dotted attributes into nested nodes.
 * For example, the following node:
 *
 *   <keybind name.action="ShowMenu" menu.action="root-menu"
 *            x.position.action="1" y.position.action="2" />
 *
 * is converted to:
 *
 *   <keybind>
 *     <action>
 *       <name>ShowMenu</name>
 *       <menu>root-menu</menu>
 *       <position>
 *         <x>1</x>
 *         <y>2</y>
 *       </position>
 *     </action>
 *   </keybind>
 */
void lab_xml_expand_dotted_attributes(xmlNode *parent);

/* Returns true if the node only contains a string or is empty */
bool lab_xml_node_is_leaf(xmlNode *node);

bool lab_xml_get_string(xmlNode *node, const char *key, char *s, size_t len);
bool lab_xml_get_int(xmlNode *node, const char *key, int *i);
bool lab_xml_get_bool(xmlNode *node, const char *key, bool *b);

/* also skips other unusual nodes like comments */
static inline xmlNode *
lab_xml_skip_text(xmlNode *child)
{
	while (child && child->type != XML_ELEMENT_NODE) {
		child = child->next;
	}
	return child;
}

static inline void
lab_xml_get_key_and_content(xmlNode *node, char **name, char **content)
{
	if (node) {
		*name = (char *)node->name;
		*content = (char *)xmlNodeGetContent(node);
	}
}

#define LAB_XML_FOR_EACH(parent, child, key, content) \
	for ((child) = lab_xml_skip_text((parent)->children), \
		lab_xml_get_key_and_content((child), &(key), &(content)); \
		\
		(child); \
		\
		xmlFree((xmlChar *)(content)), \
		(child) = lab_xml_skip_text((child)->next), \
		lab_xml_get_key_and_content((child), &(key), &(content)))

#endif /* LABWC_XML_H */