-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathxml3.c
executable file
·224 lines (200 loc) · 6 KB
/
xml3.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
// SPDX-License-Identifier: Apache-2.0
// Copyright 2019 Gliim LLC.
// Licensed under Apache License v2. See LICENSE file.
// On the web http://golf-lang.com/ - this file is part of Golf framework.
//
// gcc -o xml3 xml3.c $(pkg-config --cflags libxml-2.0) -lxml2 -g -std=gnu99
//
//
// This is XML module. At this point it's inoperative, and it's just a first cut for the future XML feature.
//
//
// https://github.com/tiran/defusedxml SECURITY OF XML, NO DTD, NO ENTITY EXPANSION, NO EXTERNAL RESOLUTION, LIMIT PARSE DEPTH, LIMIT INPUT SIZE, LIMIT PARSE TIME, SAX, NO XPATH NO XSL
//<!DOCTYPE note SYSTEM \"Note.dtd\">
/*char *xmld = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
<countries>\n\
<country id='1'>\n\
<name>USA&America</name>\n\
<state id='1'>Arizona\n\
<city id=\"1\">\n\
<name>Phoenix</name>\n\
<population>5000000</population>\n\
</city>\n\
<city id=\"2\">\n\
<name>Tuscon</name>\n\
<population>1000000</population>\n\
</city>\n\
</state>\n\
<state id='2'>California\n\
<city id=\"1\">\n\
<name>Los Angeles</name>\n\
<population>19000000</population>\n\
</city>\n\
<city id=\"2\">\n\
<name>Irvine</name>\n\
</city>\n\
</state>\n\
</country>\n\
<country id='2'>\n\
<name>Mexico</name>\n\
<state id='1'>Veracruz\n\
<city id=\"1\">\n\
<name>Xalapa-Enriquez</name>\n\
<population>8000000</population>\n\
</city>\n\
<city id=\"2\">\n\
<name>C\u00F3rdoba</name>\n\
<population>220000</population>\n\
</city>\n\
</state>\n\
<state id='2'>Sinaloa\n\
<city id=\"1\">\n\
<name>Culiac\u00E1n Rosales</name>\n\
<population>3000000</population>\n\
</city>\n\
</state>\n\
</country>\n\
</countries>\n";
//#include "golf.h"
#include <libxml/parser.h>
#include <libxml/SAX.h>
#include <string.h>
#include <ctype.h>
#include <libxml/parserInternals.h>
xmlParserCtxtPtr ctxt;
char ename[20][400];
char eval[20][400];
int elen[20];
int dep;
int main_xml() ;
void process_start(
void *ctx,
const xmlChar *localname,
const xmlChar *prefix,
const xmlChar *URI,
int nb_namespaces,
const xmlChar **namespaces,
int nb_attributes,
int nb_defaulted,
const xmlChar **attributes
);
void xml_err(void *userData, xmlErrorPtr error);
void process_end(
void* ctx,
const xmlChar* localname,
const xmlChar* prefix,
const xmlChar* URI
);
void process_chars(void* ctx, const xmlChar * ch, int len);
// Your custom SAX handler functions
void process_start(
void *ctx,
const xmlChar *localname,
const xmlChar *prefix,
const xmlChar *URI,
int nb_namespaces,
const xmlChar **namespaces,
int nb_attributes,
int nb_defaulted,
const xmlChar **attributes
)
{
//GG_UNUSED(ctx);
//GG_UNUSED(prefix);
//GG_UNUSED(URI);
//GG_UNUSED(nb_namespaces);
//GG_UNUSED(namespaces);
//GG_UNUSED(nb_defaulted);
//printf("\n------\n");
strcpy (ename[dep+1], ename[dep]);
elen[dep+1]=elen[dep];
dep++;
elen[dep]+=snprintf (ename[dep]+elen[dep], sizeof(ename[dep])-elen[dep], "%s/", localname);
//printf ("'%s'=", ename[dep]);
unsigned int index = 0;
// now the attributes
int i_att;
for (i_att = 0; i_att < nb_attributes; ++i_att, index += 5)
{
const xmlChar *localname = attributes[index];
const xmlChar *begv = attributes[index + 3];
const xmlChar *endv = attributes[index + 4];
int alen = endv-begv;
strcpy (ename[dep+1], ename[dep]);
elen[dep+1]=elen[dep];
dep++;
elen[dep]+=snprintf (ename[dep]+elen[dep], sizeof(ename[dep])-elen[dep], "%s/@", localname);
printf ("'%s'='%.*s'\n", ename[dep], alen, begv);
dep--;
}
}
void process_end(
void* ctx,
const xmlChar* localname,
const xmlChar* prefix,
const xmlChar* URI
)
{
//GG_UNUSED(ctx);
//GG_UNUSED(prefix);
//GG_UNUSED(URI);
//GG_UNUSED(localname);
//
//TODO:check localname to be in eval[]
//
//printf("\nENDOF '%s'", localname);
int i = 0;
while (isspace(eval[dep][i])) i++;
if (eval[dep][i] != 0)
{
int j = strlen(eval[dep]);
while (isspace(eval[dep][j-1])) j--;
eval[dep][j] = 0;
printf("'%s$'='%s'\n", ename[dep], eval[dep]+i);
}
eval[dep][0] = 0;
dep--;
}
void process_chars(void* ctx, const xmlChar * ch, int len)
{
//GG_UNUSED(ctx);
int l;
memcpy (eval[dep]+(l=strlen(eval[dep])), ch, len);
(eval[dep]+l)[len]= 0;
}
void xml_err(void *userData, xmlErrorPtr error) {
//GG_UNUSED(userData);
fprintf(stderr, "Error: %s %d %s %s %s %d\n", error->message, error->line, error->str1, error->str2, error->str3, error->int2);
xmlSetStructuredErrorFunc(NULL, NULL);
}
int main_xml() {
int i;
for (i = 0; i < 20; i ++)
{
ename[i][0] = 0;
eval[i][0] = 0;
elen[i] = 0;
}
dep = 0;
xmlSAXHandler handler;
memset(&handler, 0, sizeof(handler));
// Set up handler functions
handler.startElementNs = process_start;
handler.endElementNs = process_end;
handler.characters = process_chars;
handler.initialized = XML_SAX2_MAGIC;
// ... set other handler functions as needed
xmlSetStructuredErrorFunc(NULL, xml_err);
xmlDocPtr doc;
ctxt = xmlCreatePushParserCtxt( &handler, NULL, xmld, strlen(xmld), NULL);
xmlCtxtUseOptions (ctxt, XML_PARSE_NONET|XML_PARSE_HUGE|XML_PARSE_IGNORE_ENC);
// finish parsing
xmlParseChunk(ctxt, "", 0, 1);
doc = ctxt->myDoc; //destroy the tree since not used
xmlFreeParserCtxt(ctxt);
if (doc != NULL) xmlFreeDoc(doc);
// this is once per program
xmlCleanupParser();
return 0;
}
int main() { main_xml(); }*/