blob: 97ed879e36b9d4be387776289c86bb78645e0ceb [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001
2#include "minixml.h"
3
4/* parseatt : used to parse the argument list
5 * return 0 (false) in case of success and -1 (true) if the end
6 * of the xmlbuffer is reached. */
7int parseatt(struct xmlparser * p)
8{
9 const char * attname;
10 int attnamelen;
11 const char * attvalue;
12 int attvaluelen;
13 while(p->xml < p->xmlend)
14 {
15 if(*p->xml=='/' || *p->xml=='>')
16 return 0;
17 if( !IS_WHITE_SPACE(*p->xml) )
18 {
19 char sep;
20 attname = p->xml;
21 attnamelen = 0;
22 while(*p->xml!='=' && !IS_WHITE_SPACE(*p->xml) )
23 {
24 attnamelen++; p->xml++;
25 if(p->xml >= p->xmlend)
26 return -1;
27 }
28 while(*(p->xml++) != '=')
29 {
30 if(p->xml >= p->xmlend)
31 return -1;
32 }
33 while(IS_WHITE_SPACE(*p->xml))
34 {
35 p->xml++;
36 if(p->xml >= p->xmlend)
37 return -1;
38 }
39 sep = *p->xml;
40 if(sep=='\'' || sep=='\"')
41 {
42 p->xml++;
43 if(p->xml >= p->xmlend)
44 return -1;
45 attvalue = p->xml;
46 attvaluelen = 0;
47 while(*p->xml != sep)
48 {
49 attvaluelen++; p->xml++;
50 if(p->xml >= p->xmlend)
51 return -1;
52 }
53 }
54 else
55 {
56 attvalue = p->xml;
57 attvaluelen = 0;
58 while( !IS_WHITE_SPACE(*p->xml)
59 && *p->xml != '>' && *p->xml != '/')
60 {
61 attvaluelen++; p->xml++;
62 if(p->xml >= p->xmlend)
63 return -1;
64 }
65 }
66 /*printf("%.*s='%.*s'\n",
67 attnamelen, attname, attvaluelen, attvalue);*/
68 if(p->attfunc)
69 p->attfunc(p->data, attname, attnamelen, attvalue, attvaluelen);
70 }
71 p->xml++;
72 }
73 return -1;
74}
75
76/* parseelt parse the xml stream and
77 * call the callback functions when needed... */
78void parseelt(struct xmlparser * p)
79{
80 int i;
81 const char * elementname;
82 while(p->xml < (p->xmlend - 1))
83 {
84 if((p->xml)[0]=='<' && (p->xml)[1]!='?')
85 {
86 i = 0; elementname = ++p->xml;
87 while( !IS_WHITE_SPACE(*p->xml)
88 && (*p->xml!='>') && (*p->xml!='/')
89 )
90 {
91 i++; p->xml++;
92 if (p->xml >= p->xmlend)
93 return;
94 /* to ignore namespace : */
95 if(*p->xml==':')
96 {
97 i = 0;
98 elementname = ++p->xml;
99 }
100 }
101 if(i>0)
102 {
103 if(p->starteltfunc)
104 p->starteltfunc(p->data, elementname, i);
105 if(parseatt(p))
106 return;
107 if(*p->xml!='/')
108 {
109 const char * data;
110 i = 0; data = ++p->xml;
111 if (p->xml >= p->xmlend)
112 return;
113 while( IS_WHITE_SPACE(*p->xml) )
114 {
115 i++; p->xml++; // support HNAP1
116 if (p->xml >= p->xmlend)
117 return;
118 }
119 while(*p->xml!='<')
120 {
121 i++; p->xml++;
122 if (p->xml >= p->xmlend)
123 return;
124 }
125 if(i>0 && p->datafunc)
126 p->datafunc(p->data, data, i);
127 }
128 }
129 else if(*p->xml == '/')
130 {
131 i = 0; elementname = ++p->xml;
132 if (p->xml >= p->xmlend)
133 return;
134 while((*p->xml != '>'))
135 {
136 i++; p->xml++;
137 if (p->xml >= p->xmlend)
138 return;
139 }
140 if(p->endeltfunc)
141 p->endeltfunc(p->data, elementname, i);
142 p->xml++;
143 }
144 }
145 else
146 {
147 p->xml++;
148 }
149 }
150}
151
152/* the parser must be initialized before calling this function */
153void parsexml(struct xmlparser * parser)
154{
155 parser->xml = parser->xmlstart;
156 parser->xmlend = parser->xmlstart + parser->xmlsize;
157 parseelt(parser);
158}
159
160