forked from xythian/wp-lambdamoo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ext-xml.c
241 lines (209 loc) · 5.35 KB
/
ext-xml.c
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
* XML for the MOO Server using the expat library
*/
#include "bf_register.h"
#include "functions.h"
#include "db_tune.h"
#include "storage.h"
#include "list.h"
#include "streams.h"
#include "utils.h"
#include "exceptions.h"
#include "tasks.h"
#include "xmlparse.h"
/*
* quick'n'dirty
* <foo a="1">
* <bar>11</bar>
* </foo>
* =
* {"foo", {{"a", "1"}}, {{"bar", {}, {"11"}}}}
*/
typedef struct XMLdata XMLdata;
struct XMLdata {
XMLdata *parent;
Stream *body;
Var element;
};
static XMLdata *
new_node(XMLdata *parent, const char *name)
{
/* TODO: may want a suballocator here; gonna be needing a lot of
* these 2-ptr + 1 var nodes
*/
XMLdata *node;
Var element;
node = (XMLdata *)mymalloc(1*sizeof(XMLdata), M_XML_DATA);
element = new_list(4);
/* {name, attribs, body, children} */
element.v.list[1].type = TYPE_STR;
element.v.list[1].v.str = str_dup(name);
element.v.list[2] = new_list(0);
element.v.list[3].type = TYPE_INT;
element.v.list[3].v.num = 0;
element.v.list[4] = new_list(0);
node->body = NULL;
node->element = element;
node->parent = parent;
return node;
}
static void
finish_node(XMLdata *data)
{
XMLdata *parent = data->parent;
Var element = data->element;
Var body;
Stream *s = data->body;
body.type = TYPE_STR;
if(s == NULL) {
body.v.str = str_dup("");
} else {
body.v.str = str_dup(reset_stream(s));
}
element.v.list[3] = body;
if(parent != NULL) {
Var pelement = parent->element;
pelement.v.list[4] = listappend(pelement.v.list[4], var_ref(element));
}
}
static void
free_node(XMLdata *data)
{
free_var(data->element);
if(data->body != NULL)
free_stream(data->body);
myfree(data, M_XML_DATA);
}
static void
flush_nodes(XMLdata *bottom)
{
XMLdata *parent = bottom->parent;
free_node(bottom);
if(parent != NULL) {
flush_nodes(parent);
}
}
static char *
process_attribute_string(const char *value)
{
return str_dup(raw_bytes_to_binary(value, strlen(value)));
}
static void
xml_startElement(void *userData, const char *name, const char **atts)
{
XMLdata **data = (XMLdata**)userData;
XMLdata *parent = *data;
XMLdata *node = new_node(parent, name);
const char **patts = atts;
while(*patts != NULL) {
Var pair = new_list(2);
pair.v.list[1].type = TYPE_STR;
pair.v.list[1].v.str = process_attribute_string(patts[0]);
pair.v.list[2].type = TYPE_STR;
pair.v.list[2].v.str = process_attribute_string(patts[1]);
patts += 2;
node->element.v.list[2] = listappend(node->element.v.list[2], pair);
}
*data = node;
}
static void
xml_characterDataHandler(void *userData, const XML_Char *s, int len)
{
XMLdata **data = (XMLdata**)userData;
XMLdata *node = *data;
Stream *sp = node->body;
if(sp == NULL) {
node->body = new_stream(len);
sp = node->body;
}
stream_add_string(sp, raw_bytes_to_binary(s, len));
}
static void
xml_streamCharacterDataHandler(void *userData, const XML_Char *s, int len)
{
XMLdata **data = (XMLdata**)userData;
XMLdata *node = *data;
Var element = node->element;
Var v;
v.type = TYPE_STR;
v.v.str = str_dup(raw_bytes_to_binary(s, len));
element.v.list[4] = listappend(element.v.list[4], v);
}
static void
xml_endElement(void *userData, const char *name)
{
XMLdata **data = (XMLdata**)userData;
XMLdata *node = *data;
XMLdata *parent = node->parent;
finish_node(node);
free_node(node);
*data = parent;
}
/**
* Parse an XML string into a nested list.
* The second parameter indicates if body text (text within XML tags)
* should show up among the children of the tag or in its own
* section.
*
* See documentation (ext-xml.README) for examples.
*/
static package
parse_xml(const char *data, int bool_stream)
{
/*
* FIXME: Feed expat smaller chunks of the string and
* check for task timeout between chunks
*
*/
int decoded_length;
const char *decoded;
package result;
XML_Parser parser = XML_ParserCreate(NULL);
XMLdata *root = new_node(NULL, "");
XMLdata *child = root;
decoded_length = strlen(data);
decoded = data;
XML_SetUserData(parser, &child);
XML_SetElementHandler(parser, xml_startElement, xml_endElement);
if(bool_stream) {
XML_SetCharacterDataHandler(parser, xml_streamCharacterDataHandler);
} else {
XML_SetCharacterDataHandler(parser, xml_characterDataHandler);
}
if (!XML_Parse(parser, decoded, decoded_length, 1)) {
Var r;
r.type = TYPE_INT;
r.v.num = XML_GetCurrentByteIndex(parser);
flush_nodes(child);
result = make_raise_pack(E_INVARG,
XML_ErrorString(XML_GetErrorCode(parser)),
r);
} else {
finish_node(root);
result = make_var_pack(var_ref(root->element.v.list[4].v.list[1]));
free_node(root);
}
XML_ParserFree(parser);
return result;
}
static package
bf_parse_xml_document(Var arglist, Byte next, void *vdata, Objid progr)
{
package result = parse_xml(arglist.v.list[1].v.str, 1);
free_var(arglist);
return result;
}
static package
bf_parse_xml_tree(Var arglist, Byte next, void *vdata, Objid progr)
{
package result = parse_xml(arglist.v.list[1].v.str, 0);
free_var(arglist);
return result;
}
void
register_xml()
{
register_function("xml_parse_tree", 1, 1, bf_parse_xml_tree, TYPE_STR);
register_function("xml_parse_document", 1, 1, bf_parse_xml_document, TYPE_STR);
}
char rcsid_xml[] = "$Id: ext-xml.c,v 1.2 2004/01/28 01:49:01 fox Exp $";