@@ -19,79 +19,70 @@ static struct ctx {
19
19
xmlXPathContextPtr xpath_ctx_ptr ;
20
20
char * nodename ;
21
21
char * value ;
22
+ xmlNode * node ;
22
23
enum {
23
24
XML_MODE_SETTING = 0 ,
24
25
XML_MODE_GETTING ,
25
26
} mode ;
26
27
} ctx ;
27
28
28
-
29
- static void
30
- string_truncate_at_pattern (char * buf , const char * pattern )
31
- {
32
- char * p = strstr (buf , pattern );
33
- if (!p ) {
34
- return ;
35
- }
36
- * p = '\0' ;
37
- }
38
-
39
29
static void
40
30
entry (xmlNode * node , char * nodename , char * content )
41
31
{
42
32
if (!nodename )
43
33
return ;
44
- string_truncate_at_pattern (nodename , ".openbox_config" );
45
- string_truncate_at_pattern (nodename , ".labwc_config" );
46
- if (!strcmp (nodename , ctx .nodename )) {
34
+ if (!strcasecmp (nodename , ctx .nodename )) {
47
35
if (ctx .mode == XML_MODE_SETTING ) {
48
36
xmlNodeSetContent (node , (const xmlChar * )ctx .value );
49
37
} else if (ctx .mode == XML_MODE_GETTING ) {
50
38
ctx .value = (char * )content ;
39
+ ctx .node = node ;
51
40
}
52
41
}
53
42
}
54
43
44
+ /**
45
+ * nodename - return simplistic xpath style nodename
46
+ * For example: <A><B><C></C></B></A> is represented by nodename /a/b/c
47
+ */
55
48
static char *
56
- nodename (xmlNode * node , char * buf , int len , bool is_attribute )
49
+ nodename (xmlNode * node , char * buf , int len )
57
50
{
58
51
if (!node || !node -> name ) {
59
52
return NULL ;
60
53
}
61
54
62
- /* Ignore superflous 'text. ' in node name */
55
+ /* Ignore superflous '/ text' in node name */
63
56
if (node -> parent && !strcmp ((char * )node -> name , "text" )) {
64
57
node = node -> parent ;
65
58
}
66
59
67
- char * p = buf ;
68
- p [-- len ] = 0 ;
69
- if (is_attribute ) {
70
- * p ++ = '@' ;
71
- -- len ;
72
- }
73
- for (;;) {
60
+ buf += len ;
61
+ * -- buf = 0 ;
62
+ len -- ;
63
+
64
+ for (;;) {
74
65
const char * name = (char * )node -> name ;
75
- char c ;
76
- while ((c = * name ++ ) != 0 ) {
77
- * p ++ = tolower (c );
66
+ int i = strlen (name );
67
+ while (-- i >= 0 ) {
68
+ unsigned char c = name [i ];
69
+ * -- buf = tolower (c );
78
70
if (!-- len )
79
71
return buf ;
80
72
}
81
- * p = 0 ;
82
73
node = node -> parent ;
83
74
if (!node || !node -> name ) {
75
+ * -- buf = '/' ;
84
76
return buf ;
85
77
}
86
- * p ++ = '. ' ;
87
- if (!-- len ) {
78
+ * -- buf = '/ ' ;
79
+ if (!-- len )
88
80
return buf ;
89
- }
90
81
}
91
82
}
92
83
93
84
static void
94
- process_node (xmlNode * node , bool is_attribute )
85
+ process_node (xmlNode * node )
95
86
{
96
87
char * content ;
97
88
static char buffer [256 ];
@@ -101,7 +92,7 @@ process_node(xmlNode *node, bool is_attribute)
101
92
if (xmlIsBlankNode (node )) {
102
93
return ;
103
94
}
104
- name = nodename (node , buffer , sizeof (buffer ), is_attribute );
95
+ name = nodename (node , buffer , sizeof (buffer ));
105
96
entry (node , name , content );
106
97
}
107
98
@@ -110,14 +101,10 @@ static void xml_tree_walk(xmlNode *node);
110
101
static void
111
102
traverse (xmlNode * n )
112
103
{
113
- static bool is_attribute ;
114
-
115
- process_node (n , is_attribute );
116
- is_attribute = true;
104
+ process_node (n );
117
105
for (xmlAttr * attr = n -> properties ; attr ; attr = attr -> next ) {
118
106
xml_tree_walk (attr -> children );
119
107
}
120
- is_attribute = false;
121
108
xml_tree_walk (n -> children );
122
109
}
123
110
@@ -232,6 +219,17 @@ xml_get_bool_text(char *nodename)
232
219
}
233
220
}
234
221
222
+ /* case-insensitive */
223
+ static xmlNode *
224
+ xml_get_node (char * nodename )
225
+ {
226
+ ctx .node = NULL ;
227
+ ctx .nodename = nodename ;
228
+ ctx .mode = XML_MODE_GETTING ;
229
+ xml_tree_walk (xmlDocGetRootElement (ctx .doc ));
230
+ return ctx .node ;
231
+ }
232
+
235
233
char *
236
234
xml_get_content (char * xpath_expr )
237
235
{
@@ -266,6 +264,7 @@ xml_get_content(char *xpath_expr)
266
264
return (char * )ret ;
267
265
}
268
266
267
+ /* case-sensitive */
269
268
static xmlNode *
270
269
get_node (xmlChar * expr )
271
270
{
@@ -295,6 +294,10 @@ get_node(xmlChar *expr)
295
294
void
296
295
xml_add_node (char * xpath_expr )
297
296
{
297
+ if (xml_get_node (xpath_expr )) {
298
+ return ;
299
+ }
300
+
298
301
/* find existing parent */
299
302
char * parent_expr = strdup (xpath_expr );
300
303
xmlNode * parent_node = NULL ;
0 commit comments