forked from forhappy/OSSC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oss_ttxml.c
379 lines (333 loc) · 7.59 KB
/
oss_ttxml.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <string.h>
#include "include/oss_ttxml.h"
#define XML_LETTER 1
#define XML_NUMBER 2
#define XML_SPACE 4
#define XML_SLASH 8
#define XML_OPEN 16
#define XML_EQUALS 32
#define XML_CLOSE 64
#define XML_QUOTE 128
#define XML_OTHER 256
#define XML_ALL 0xFFFFFFFF
typedef struct XMLBUF
{
FILE * fptr;
char * buf;
int len;
int read_index;
int eof;
int error;
} XMLBUF;
static int feed_mask = 0;
/* character matching tests for the feed functions */
static char quotechar = 0;
static XmlNode* xml_new(char * name)
{
XmlNode * ret = malloc(sizeof(XmlNode));
if(!ret)return NULL;
ret->attrib = NULL;
ret->nattrib = 0;
ret->child = ret->next = NULL;
ret->name = name;
return ret;
}
/* free a previously allocated XmlNode */
void xml_free(XmlNode *target)
{
int i;
for(i=0; i<target->nattrib*2; i++)
if(target->attrib[i])
free(target->attrib[i]);
if(target->attrib)free(target->attrib);
if(target->child)xml_free(target->child);
if(target->next)xml_free(target->next);
free(target->name);
free(target);
}
/* Raise flags if we have a character of special meaning.
* This is where I've hidden the switch statements :-p
*/
static int is_special(char item)
{
if((item >= 'a' && item <= 'z') || (item >= 'A' && item <='Z'))
return XML_LETTER;
if( item >= '0' && item <='9' )
return XML_NUMBER;
if( item == 0x20 || item == '\t' || item == 0x0D || item == 0x0A )
return XML_SPACE;
if( item == '/' )
return XML_SLASH;
if( item == '<' )
return XML_OPEN;
if( item == '=' )
return XML_EQUALS;
if( item == '>' )
return XML_CLOSE;
if( item == '"' || item == '\'' )
return XML_QUOTE;
return 128;
}
/* Refresh the buffer, if possible */
static void xml_read_file(XMLBUF *xml)
{
int size;
if(xml->eof)return;
size = fread( xml->buf, 1, xml->len, xml->fptr);
if( size != xml->len )
{
xml->len = size;
xml->buf[size]=0;
xml->eof = 1;
}
}
static void xml_end_file(XMLBUF *xml)
{
xml->len = 0;
xml->eof = 1;
xml->read_index = 0 ;
xml->error = 1;
}
static int test_quote(const char x)
{
static int escaped=0;
if( escaped || '\\' == x )
{
escaped = !escaped;
return 1;
}
if( x != quotechar )
return 1;
return 0;
}
static int test_mask(const char x)
{
return !(is_special(x) & feed_mask);
}
static char* xml_feed( XMLBUF *xml, int (*test)(char) )
{
int offset = xml->read_index;
int delta;
char *ret = NULL;
char *tmp = NULL;
int size = 0;
/* perform first and N middle realloc()'s */
while( test(xml->buf[offset]) )
{
offset ++;
if(offset >= xml->len)
{
delta = offset - xml->read_index;
tmp = realloc(ret, size + delta + 1);
if(!tmp)goto xml_feed_malloc;
ret = tmp;
memcpy(ret+size, xml->buf + xml->read_index, delta);
size += delta;
ret[size]=0;
if(xml->eof)return ret;
xml_read_file(xml);
xml->read_index = 0;
offset = 0;
}
}
/* perform final realloc() if needed */
if(offset > xml->read_index)
{
delta = offset - xml->read_index;
tmp = realloc(ret, size + delta + 1);
if(!tmp)goto xml_feed_malloc;
ret = tmp;
memcpy(ret+size, xml->buf + xml->read_index, delta);
xml->read_index = offset;
size += delta;
ret[size]=0;
}
return ret;
xml_feed_malloc:
free(ret);
xml_end_file(xml);
return 0;
}
/* All reading of the XML buffer done through these two functions */
/*** read a byte without advancing the offset */
static char xml_peek(XMLBUF *xml)
{
return xml->buf[xml->read_index];
}
/*** read a byte and advance the offset */
static char xml_read_byte(XMLBUF *xml)
{
char ret = xml_peek(xml);
xml->read_index++;
if(xml->read_index >= xml->len)
{
if(xml->eof)
{
xml->read_index = xml->len;
return ret;
}
xml->read_index = 0 ;
xml_read_file(xml);
}
return ret;
}
/* skip over bytes matching the is_special mask */
static void xml_skip( XMLBUF *xml, int mask)
{
while( is_special(xml_peek(xml)) & mask && !(xml->eof && xml->read_index >= xml->len) )
xml_read_byte(xml);
}
/* this reads attributes from tags, of the form...
*
* <tag attr1="some arguments" attr2=argument>
*
* It is aware of quotes, and will allow anything inside quoted arguments
*/
static void xml_read_attr(struct XMLBUF *xml, XmlNode *node)
{
int n=0;
char **tmp;
// how does this tag finish?
while(xml->len)
{
if( is_special(xml_peek(xml)) & (XML_CLOSE | XML_SLASH) )
return;
n = ++node->nattrib;
tmp = realloc(node->attrib, n * 2 * sizeof(char*) );
if(!tmp)goto xml_read_attr_malloc;
node->attrib = tmp;
node->attrib[--n*2+1] = 0;
feed_mask = XML_EQUALS | XML_SPACE | XML_CLOSE | XML_SLASH;
node->attrib[n*2] = xml_feed(xml, test_mask );
if( xml_peek(xml) == '=' )
{
xml_read_byte(xml);
if( is_special(xml_peek(xml)) & XML_QUOTE )
{
quotechar = xml_read_byte(xml);
node->attrib[n*2+1] = xml_feed(xml, test_quote);
xml_read_byte(xml);
}
else
{
feed_mask = XML_SPACE | XML_CLOSE | XML_SLASH;
node->attrib[n*2+1] = xml_feed(xml, test_mask);
}
}
xml_skip(xml, XML_SPACE);
}
return;
xml_read_attr_malloc:
xml_end_file(xml);
}
static XmlNode* xml_parse(struct XMLBUF *xml)
{
// int offset;
int toff;
char **tmp;
char *stmp;
XmlNode **this, *ret = NULL;
this = &ret;
xml_skip(xml, XML_SPACE); // skip whitespace
// offset=0;
while( (xml->read_index < xml->len) || !xml->eof )
{
switch(is_special(xml_peek(xml)))
{
case XML_OPEN:
xml_read_byte(xml);
if(xml_peek(xml) == '/')
return ret; // parents close tag
// read the tag name
feed_mask = XML_SPACE | XML_SLASH | XML_CLOSE;
*this = xml_new( xml_feed(xml, test_mask));
if(xml->error)goto xml_parse_malloc;
xml_skip(xml, XML_SPACE); // skip any whitespace
xml_read_attr(xml, *this); // read attributes
// how does this tag finish?
switch(is_special(xml_peek(xml)))
{
case XML_CLOSE: // child-nodes ahead
xml_read_byte(xml);
(*this)->child = xml_parse(xml);
xml_skip(xml, XML_ALL ^ XML_CLOSE);
xml_read_byte(xml);
break;
case XML_SLASH: // self closing tag
xml_read_byte(xml);
xml_read_byte(xml);
break;
}
break;
default: // text node
*this = xml_new(0);
xml_skip(xml, XML_SPACE); // skip any whitespace
feed_mask = XML_OPEN;
(*this)->nattrib=1;
tmp = malloc(sizeof(char*)*2);
if(!tmp)goto xml_parse_malloc;
(*this)->attrib = tmp;
(*this)->attrib[1] = NULL;
stmp = (*this)->attrib[0] = xml_feed(xml, test_mask);
/* trim the whitespace off the end of text nodes,
* by overwriting the spaces will null termination. */
toff = strlen(stmp)-1;
while( ( is_special(stmp[toff]) & XML_SPACE ) )
{
stmp[toff] = 0;
toff --;
}
break;
}
this = &(*this)->next;
xml_skip(xml, XML_SPACE); // skip whitespace
}
return ret;
xml_parse_malloc:
xml_end_file(xml);
if(ret)xml_free(ret);
return 0;
}
XmlNode * xml_find(XmlNode *xml, const char *name)
{
XmlNode * ret;
if(xml->name)if(!strcmp(xml->name, name))return xml;
if(xml->child)
{
ret = xml_find(xml->child, name);
if(ret)return ret;
}
if(xml->next)
{
ret = xml_find(xml->next, name);
if(ret)return ret;
}
return NULL;
}
XmlNode* xml_load_buffer(const char * buffer, unsigned int buffer_len)
{
struct XMLBUF xml;
XmlNode *ret = NULL;
xml.error = 0;
xml.eof = 1;
xml.read_index = 0;
xml.fptr = NULL;
xml.buf = (char *)malloc(sizeof(char) * buffer_len + 1);
if(!xml.buf)
goto xml_load_fail_malloc_buf;
memset(xml.buf, 0, buffer_len + 1);
xml.len = buffer_len;
memcpy(xml.buf, buffer, buffer_len);
ret = xml_parse(&xml);
if(xml.error)
{
xml_free(ret);
ret = NULL;
}
free(xml.buf);
xml_load_fail_malloc_buf:
return ret;
}