-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexample2.c
More file actions
125 lines (97 loc) · 2.79 KB
/
Copy pathexample2.c
File metadata and controls
125 lines (97 loc) · 2.79 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bstring.h"
#include "libhpxml.h"
int is_doctype(const hpx_tag_t *tag)
{
return tag->type == HPX_ATT && tag->tag.len >= 8 && isspace(tag->tag.buf[7]) && !strncasecmp(tag->tag.buf, "DOCTYPE", 7);
}
int proc_subset(hpx_tag_t *tag)
{
hpx_ctrl_t _ctl, *ctl = &_ctl;
int i, sqcnt;
bstring_t b;
long lno;
printf("===== BEGIN PROCESS SUBSET=====\n");
b = tag->tag;
// skip until opening tag '['
for (; b.len && *b.buf != '['; b.buf++, b.len--);
// no data or start tag
if (b.len < 2 || *b.buf != '[')
return -1;
// find end tag ']'
for (i = 0, sqcnt = 0; i < b.len; i++)
{
if (b.buf[i] == '[')
{
sqcnt++;
}
else if (b.buf[i] == ']')
{
sqcnt--;
if (!sqcnt)
break;
}
}
// no closing tag or too short?
if (i >= b.len || i < 2)
return -1;
//printf("i = %d, len = %d, \"%.*s\"\n", i, b.len, b.len, b.buf);
// remove enclosing square brackets
b.len = i - 2;
b.buf++;
//printf("i = %d, len = %d, \"%.*s\"\n", i, b.len, b.len, b.buf);
hpx_init_membuf(ctl, b.buf, b.len);
// loop as long as XML elements are available
while (hpx_get_elem(ctl, &b, NULL, &lno) > 0)
{
// parse XML element
if (!hpx_process_elem(b, tag))
{
// element successfully parsed, do something with it
// ...
// ...
printf("[%ld] type=%d, name=%.*s, nattr=%d\n", lno, tag->type, tag->tag.len, tag->tag.buf, tag->nattr);
}
else
printf("[%ld] ERROR in element: %.*s\n", lno, b.len, b.buf);
}
printf("===== END PROCESS SUBSET =====\n");
return 0;
}
int main(int argc, char *argv[])
{
hpx_ctrl_t *ctl;
hpx_tag_t *tag;
bstring_t b;
long lno;
// initialize control structure, stdin, 100MB buffer
if ((ctl = hpx_init(0, 100*1024*1024)) == NULL)
perror("hpx_init"), exit(EXIT_FAILURE);
// initialize tag structure with maximum 16 attributes
if ((tag = hpx_tm_create(64)) == NULL)
perror("hpx_tm_create"), exit(EXIT_FAILURE);
// loop as long as XML elements are available
while (hpx_get_elem(ctl, &b, NULL, &lno) > 0)
{
// parse XML element
if (!hpx_process_elem(b, tag))
{
// element successfully parsed, do something with it
// ...
// ...
printf("[%ld] type=%d, name=%.*s, nattr=%d\n", lno, tag->type, tag->tag.len, tag->tag.buf, tag->nattr);
// process doctype subtypes
if (is_doctype(tag))
proc_subset(tag);
}
else
printf("[%ld] ERROR in element: %.*s\n", lno, b.len, b.buf);
}
if (!ctl->eof)
perror("hpx_get_elem"), exit(EXIT_FAILURE);
hpx_tm_free(tag);
hpx_free(ctl);
exit(EXIT_SUCCESS);
}