|
@@ -0,0 +1,399 @@
|
|
1
|
+// Copyright 2017 Patrick Brosi
|
|
2
|
+// info@patrickbrosi.de
|
|
3
|
+
|
|
4
|
+#include <fcntl.h>
|
|
5
|
+#include <sys/stat.h>
|
|
6
|
+#include <sys/types.h>
|
|
7
|
+#include <unistd.h>
|
|
8
|
+#include <cassert>
|
|
9
|
+#include <cstring>
|
|
10
|
+#include <fstream>
|
|
11
|
+#include <iostream>
|
|
12
|
+#include <map>
|
|
13
|
+#include "xml/File.h"
|
|
14
|
+#include "xml/NamedEnts.h"
|
|
15
|
+
|
|
16
|
+using namespace xml;
|
|
17
|
+
|
|
18
|
+// _____________________________________________________________________________
|
|
19
|
+File::File(const std::string& path)
|
|
20
|
+ : _file(0), _c(0), _lastBytes(0), _which(0), _path(path), _totReadBef(0) {
|
|
21
|
+ _buffer = new char*[2];
|
|
22
|
+ _buffer[0] = new char[BUFFER_S + 1];
|
|
23
|
+ _buffer[1] = new char[BUFFER_S + 1];
|
|
24
|
+
|
|
25
|
+ reset();
|
|
26
|
+}
|
|
27
|
+
|
|
28
|
+// _____________________________________________________________________________
|
|
29
|
+File::~File() {
|
|
30
|
+ delete[] _buffer[0];
|
|
31
|
+ delete[] _buffer[1];
|
|
32
|
+ delete[] _buffer;
|
|
33
|
+ close(_file);
|
|
34
|
+}
|
|
35
|
+
|
|
36
|
+// _____________________________________________________________________________
|
|
37
|
+void File::reset() {
|
|
38
|
+ _which = 0;
|
|
39
|
+ _s.s = NONE;
|
|
40
|
+ _s.hanging = 0;
|
|
41
|
+ _totReadBef = 0;
|
|
42
|
+
|
|
43
|
+ if (_file) close(_file);
|
|
44
|
+ _file = open(_path.c_str(), O_RDONLY);
|
|
45
|
+ posix_fadvise(_file, 0, 0, POSIX_FADV_SEQUENTIAL);
|
|
46
|
+
|
|
47
|
+ _lastBytes = read(_file, _buffer[_which], BUFFER_S);
|
|
48
|
+ _lastNewData = _lastBytes;
|
|
49
|
+ _c = _buffer[_which];
|
|
50
|
+ while (!_s.tagStack.empty()) _s.tagStack.pop();
|
|
51
|
+ _s.tagStack.push("[root]");
|
|
52
|
+ _prevs = _s;
|
|
53
|
+}
|
|
54
|
+
|
|
55
|
+// _____________________________________________________________________________
|
|
56
|
+size_t File::level() const { return _s.tagStack.size() - _s.hanging; }
|
|
57
|
+
|
|
58
|
+// _____________________________________________________________________________
|
|
59
|
+ParserState File::state() { return _prevs; }
|
|
60
|
+
|
|
61
|
+// _____________________________________________________________________________
|
|
62
|
+void File::setState(const ParserState& s) {
|
|
63
|
+ _s = s;
|
|
64
|
+ _prevs = s;
|
|
65
|
+
|
|
66
|
+ lseek(_file, _s.off, SEEK_SET);
|
|
67
|
+ _totReadBef = _s.off;
|
|
68
|
+ _lastBytes = read(_file, _buffer[_which], BUFFER_S);
|
|
69
|
+ _lastNewData = _lastBytes;
|
|
70
|
+ _c = _buffer[_which];
|
|
71
|
+
|
|
72
|
+ next();
|
|
73
|
+}
|
|
74
|
+
|
|
75
|
+// _____________________________________________________________________________
|
|
76
|
+const Tag& File::get() const { return _ret; }
|
|
77
|
+
|
|
78
|
+// _____________________________________________________________________________
|
|
79
|
+bool File::next() {
|
|
80
|
+ // avoid too much stack copying
|
|
81
|
+ if (_prevs.tagStack.size() != _s.tagStack.size() ||
|
|
82
|
+ _prevs.tagStack.top() != _s.tagStack.top()) {
|
|
83
|
+ _prevs.tagStack = _s.tagStack;
|
|
84
|
+ }
|
|
85
|
+ _prevs.s = _s.s;
|
|
86
|
+ _prevs.hanging = _s.hanging;
|
|
87
|
+ _prevs.off =
|
|
88
|
+ _totReadBef + (_c - _buffer[_which]) - (_lastBytes - _lastNewData);
|
|
89
|
+
|
|
90
|
+ if (_s.hanging) _s.hanging--;
|
|
91
|
+ _ret.name = 0;
|
|
92
|
+ _ret.attrs.clear();
|
|
93
|
+ void* i;
|
|
94
|
+ while (_lastBytes) {
|
|
95
|
+ for (; _c - _buffer[_which] < _lastBytes; ++_c) {
|
|
96
|
+ char c = *_c;
|
|
97
|
+ switch (_s.s) {
|
|
98
|
+ case NONE:
|
|
99
|
+ if (std::isspace(c))
|
|
100
|
+ continue;
|
|
101
|
+ else if (c == '<') {
|
|
102
|
+ _s.s = IN_TAG_TENTATIVE;
|
|
103
|
+ continue;
|
|
104
|
+ } else {
|
|
105
|
+ _s.s = IN_TEXT;
|
|
106
|
+ continue;
|
|
107
|
+ }
|
|
108
|
+ case IN_TEXT:
|
|
109
|
+ throw XmlFileException("text nodes not yet supported");
|
|
110
|
+ case IN_TAG_TENTATIVE:
|
|
111
|
+ if (c == '/') {
|
|
112
|
+ _s.s = IN_TAG_NAME_CLOSE;
|
|
113
|
+ _tmp = _c + 1;
|
|
114
|
+ continue;
|
|
115
|
+ } else if (c == '?') {
|
|
116
|
+ _s.s = IN_TAG_NAME_META;
|
|
117
|
+ continue;
|
|
118
|
+ } else if (std::isalnum(c) || c == '-' || c == '_' || c == '.') {
|
|
119
|
+ _s.s = IN_TAG_NAME;
|
|
120
|
+ _ret.name = _c;
|
|
121
|
+ continue;
|
|
122
|
+ }
|
|
123
|
+
|
|
124
|
+ case IN_TAG:
|
|
125
|
+ if (std::isspace(c))
|
|
126
|
+ continue;
|
|
127
|
+ else if (std::isalnum(c) || c == '-' || c == '_' || c == '.') {
|
|
128
|
+ _s.s = IN_ATTRKEY;
|
|
129
|
+ _tmp = _c;
|
|
130
|
+ continue;
|
|
131
|
+ } else if (c == '/') {
|
|
132
|
+ _s.s = AW_CLOSING;
|
|
133
|
+ continue;
|
|
134
|
+ } else if (c == '>') {
|
|
135
|
+ _s.hanging++;
|
|
136
|
+ _s.tagStack.push(_ret.name);
|
|
137
|
+ _s.s = WS_SKIP;
|
|
138
|
+ continue;
|
|
139
|
+ } else {
|
|
140
|
+ throw XmlFileException("expected valid tag");
|
|
141
|
+ }
|
|
142
|
+
|
|
143
|
+ case IN_ATTRVAL_SQ:
|
|
144
|
+ i = memchr(_c, '\'', _lastBytes - (_c - _buffer[_which]));
|
|
145
|
+ if (!i) {
|
|
146
|
+ _c = _buffer[_which] + _lastBytes;
|
|
147
|
+ continue;
|
|
148
|
+ } else {
|
|
149
|
+ _c = (char*)i;
|
|
150
|
+ _s.s = IN_TAG;
|
|
151
|
+ *_c = 0;
|
|
152
|
+ _ret.attrs[_tmp] = _tmp2;
|
|
153
|
+ continue;
|
|
154
|
+ }
|
|
155
|
+
|
|
156
|
+ case IN_ATTRVAL_DQ:
|
|
157
|
+ i = memchr(_c, '"', _lastBytes - (_c - _buffer[_which]));
|
|
158
|
+ if (!i) {
|
|
159
|
+ _c = _buffer[_which] + _lastBytes;
|
|
160
|
+ continue;
|
|
161
|
+ } else {
|
|
162
|
+ _c = (char*)i;
|
|
163
|
+ _s.s = IN_TAG;
|
|
164
|
+ *_c = 0;
|
|
165
|
+ _ret.attrs[_tmp] = _tmp2;
|
|
166
|
+ continue;
|
|
167
|
+ }
|
|
168
|
+
|
|
169
|
+ case AW_IN_ATTRVAL:
|
|
170
|
+ if (std::isspace(c))
|
|
171
|
+ continue;
|
|
172
|
+ else if (c == '\'') {
|
|
173
|
+ _s.s = IN_ATTRVAL_SQ;
|
|
174
|
+ _tmp2 = _c + 1;
|
|
175
|
+ continue;
|
|
176
|
+ } else if (c == '"') {
|
|
177
|
+ _s.s = IN_ATTRVAL_DQ;
|
|
178
|
+ _tmp2 = _c + 1;
|
|
179
|
+ continue;
|
|
180
|
+ } else {
|
|
181
|
+ throw XmlFileException("expected attribute value");
|
|
182
|
+ }
|
|
183
|
+
|
|
184
|
+ case IN_ATTRKEY:
|
|
185
|
+ if (std::isspace(c)) {
|
|
186
|
+ *_c = 0;
|
|
187
|
+ _s.s = AFTER_ATTRKEY;
|
|
188
|
+ continue;
|
|
189
|
+ } else if (std::isalnum(c) || c == '-' || c == '_' || c == '.') {
|
|
190
|
+ continue;
|
|
191
|
+ } else if (c == '=') {
|
|
192
|
+ *_c = 0;
|
|
193
|
+ _s.s = AW_IN_ATTRVAL;
|
|
194
|
+ continue;
|
|
195
|
+ }
|
|
196
|
+
|
|
197
|
+ std::cerr << "ERROR 5" << std::endl;
|
|
198
|
+ exit(0);
|
|
199
|
+
|
|
200
|
+ case AFTER_ATTRKEY:
|
|
201
|
+ if (std::isspace(c))
|
|
202
|
+ continue;
|
|
203
|
+ else if (c == '=') {
|
|
204
|
+ _s.s = AW_IN_ATTRVAL;
|
|
205
|
+ continue;
|
|
206
|
+ } else {
|
|
207
|
+ // TODO: error
|
|
208
|
+ continue;
|
|
209
|
+ }
|
|
210
|
+
|
|
211
|
+ case IN_TAG_NAME:
|
|
212
|
+ if (std::isspace(c)) {
|
|
213
|
+ *_c = 0;
|
|
214
|
+ _s.s = IN_TAG;
|
|
215
|
+ continue;
|
|
216
|
+ } else if (c == '>') {
|
|
217
|
+ *_c = 0;
|
|
218
|
+ _s.hanging++;
|
|
219
|
+ _s.tagStack.push(_ret.name);
|
|
220
|
+ _s.s = WS_SKIP;
|
|
221
|
+ continue;
|
|
222
|
+ } else if (c == '/') {
|
|
223
|
+ *_c = 0;
|
|
224
|
+ _s.s = AW_CLOSING;
|
|
225
|
+ continue;
|
|
226
|
+ } else if (std::isalnum(c) || c == '-' || c == '_' || c == '.') {
|
|
227
|
+ continue;
|
|
228
|
+ }
|
|
229
|
+
|
|
230
|
+ case IN_TAG_NAME_META:
|
|
231
|
+ // TODO: read meta tags!
|
|
232
|
+ if (c == '>') {
|
|
233
|
+ _s.s = NONE;
|
|
234
|
+ continue;
|
|
235
|
+ }
|
|
236
|
+
|
|
237
|
+ continue;
|
|
238
|
+
|
|
239
|
+ case IN_TAG_NAME_CLOSE:
|
|
240
|
+ if (std::isspace(c)) {
|
|
241
|
+ *_c = 0;
|
|
242
|
+ _s.s = IN_TAG_CLOSE;
|
|
243
|
+ continue;
|
|
244
|
+ } else if (std::isalnum(c) || c == '-' || c == '_' || c == '.') {
|
|
245
|
+ continue;
|
|
246
|
+ } else if (c == '>') {
|
|
247
|
+ *_c = 0;
|
|
248
|
+ if (_tmp != _s.tagStack.top()) {
|
|
249
|
+ throw XmlFileException("closing wrong tag");
|
|
250
|
+ }
|
|
251
|
+ _s.tagStack.pop();
|
|
252
|
+ _s.s = NONE;
|
|
253
|
+ continue;
|
|
254
|
+ }
|
|
255
|
+
|
|
256
|
+ case IN_TAG_CLOSE:
|
|
257
|
+ if (std::isspace(c))
|
|
258
|
+ continue;
|
|
259
|
+ else if (c == '>') {
|
|
260
|
+ if (_tmp != _s.tagStack.top()) {
|
|
261
|
+ throw XmlFileException("closing wrong tag");
|
|
262
|
+ }
|
|
263
|
+ _s.tagStack.pop();
|
|
264
|
+ _s.s = NONE;
|
|
265
|
+ continue;
|
|
266
|
+ } else {
|
|
267
|
+ throw XmlFileException("expected '>'");
|
|
268
|
+ }
|
|
269
|
+
|
|
270
|
+ case AW_CLOSING:
|
|
271
|
+ if (c == '>') {
|
|
272
|
+ _s.s = WS_SKIP;
|
|
273
|
+ continue;
|
|
274
|
+ }
|
|
275
|
+
|
|
276
|
+ case WS_SKIP:
|
|
277
|
+ if (std::isspace(c))
|
|
278
|
+ continue;
|
|
279
|
+ else {
|
|
280
|
+ _s.s = NONE;
|
|
281
|
+ return true;
|
|
282
|
+ }
|
|
283
|
+ }
|
|
284
|
+ }
|
|
285
|
+
|
|
286
|
+ // buffer ended, read new stuff, but copy remaining if needed
|
|
287
|
+ size_t off = 0;
|
|
288
|
+ if (_s.s == IN_TAG_NAME) { //|| IN_TAG_NAME_META) {
|
|
289
|
+ off = _lastBytes - (_ret.name - _buffer[_which]);
|
|
290
|
+ memmove(_buffer[!_which], _ret.name, off);
|
|
291
|
+ _ret.name = _buffer[!_which];
|
|
292
|
+ } else if (_s.s == IN_TAG_NAME_CLOSE || _s.s == IN_ATTRKEY ||
|
|
293
|
+ _s.s == IN_TEXT) {
|
|
294
|
+ off = _lastBytes - (_tmp - _buffer[_which]);
|
|
295
|
+ memmove(_buffer[!_which], _tmp, off);
|
|
296
|
+ _tmp = _buffer[!_which];
|
|
297
|
+ } else if (_s.s == IN_ATTRVAL_SQ || _s.s == IN_ATTRVAL_DQ) {
|
|
298
|
+ off = _lastBytes - (_tmp2 - _buffer[_which]);
|
|
299
|
+ memmove(_buffer[!_which], _tmp2, off);
|
|
300
|
+ _tmp2 = _buffer[!_which];
|
|
301
|
+ }
|
|
302
|
+
|
|
303
|
+ assert(off <= BUFFER_S);
|
|
304
|
+
|
|
305
|
+ size_t readb = read(_file, _buffer[!_which] + off, BUFFER_S - off);
|
|
306
|
+ if (!readb) break;
|
|
307
|
+ _totReadBef += _lastNewData;
|
|
308
|
+ _which = !_which;
|
|
309
|
+ _lastNewData = readb;
|
|
310
|
+ _lastBytes = _lastNewData + off;
|
|
311
|
+ _c = _buffer[_which] + off;
|
|
312
|
+ }
|
|
313
|
+
|
|
314
|
+ if (_s.tagStack.top() != "[root]") {
|
|
315
|
+ // TODO error
|
|
316
|
+ throw XmlFileException("XML tree not complete");
|
|
317
|
+ } else {
|
|
318
|
+ _s.tagStack.pop();
|
|
319
|
+ }
|
|
320
|
+ _s.s = NONE;
|
|
321
|
+ _ret.name = "[root]";
|
|
322
|
+ return false;
|
|
323
|
+}
|
|
324
|
+
|
|
325
|
+// _____________________________________________________________________________
|
|
326
|
+std::string File::decode(const std::string& str) { return decode(str.c_str()); }
|
|
327
|
+
|
|
328
|
+// _____________________________________________________________________________
|
|
329
|
+std::string File::decode(const char* str) {
|
|
330
|
+ const char* c = strchr(str, '&');
|
|
331
|
+ if (!c) return str;
|
|
332
|
+
|
|
333
|
+ char decRet[strlen(str) + 1];
|
|
334
|
+ const char* last = str;
|
|
335
|
+ char* dstPt = decRet;
|
|
336
|
+
|
|
337
|
+ for (; c != 0; c = strchr(c + 1, '&')) {
|
|
338
|
+ memcpy(dstPt, last, c - last);
|
|
339
|
+ dstPt += c - last;
|
|
340
|
+ last = c;
|
|
341
|
+
|
|
342
|
+ if (*(c + 1) == '#') {
|
|
343
|
+ uint64_t cp = -1;
|
|
344
|
+ char* tail;
|
|
345
|
+ errno = 0;
|
|
346
|
+ if (*(c + 2) == 'x' || *(c + 2) == 'X')
|
|
347
|
+ cp = strtoul(c + 3, &tail, 16);
|
|
348
|
+ else
|
|
349
|
+ cp = strtoul(c + 2, &tail, 10);
|
|
350
|
+
|
|
351
|
+ if (*tail == ';' && cp <= 0x1FFFFF && !errno) {
|
|
352
|
+ dstPt += utf8(cp, dstPt);
|
|
353
|
+ last = tail + 1;
|
|
354
|
+ }
|
|
355
|
+ } else {
|
|
356
|
+ const char* e = strchr(c, ';');
|
|
357
|
+ if (e) {
|
|
358
|
+ char ent[e - 1 - c + 1];
|
|
359
|
+ memcpy(ent, c + 1, e - 1 - c);
|
|
360
|
+ ent[e - 1 - c] = 0;
|
|
361
|
+ const auto it = xml::ENTITIES.find(ent);
|
|
362
|
+ if (it != xml::ENTITIES.end()) {
|
|
363
|
+ const char* utf8 = it->second;
|
|
364
|
+ memcpy(dstPt, utf8, strlen(utf8));
|
|
365
|
+ dstPt += strlen(utf8);
|
|
366
|
+ last += strlen(ent) + 2;
|
|
367
|
+ }
|
|
368
|
+ }
|
|
369
|
+ }
|
|
370
|
+ }
|
|
371
|
+
|
|
372
|
+ strcpy(dstPt, last);
|
|
373
|
+ return decRet;
|
|
374
|
+}
|
|
375
|
+
|
|
376
|
+// _____________________________________________________________________________
|
|
377
|
+size_t File::utf8(size_t cp, char* out) {
|
|
378
|
+ if (cp <= 0x7F) {
|
|
379
|
+ out[0] = cp & 0x7F;
|
|
380
|
+ return 1;
|
|
381
|
+ } else if (cp <= 0x7FF) {
|
|
382
|
+ out[0] = 0xC0 | (cp >> 6);
|
|
383
|
+ out[1] = 0x80 | (cp & 0x3F);
|
|
384
|
+ return 2;
|
|
385
|
+ } else if (cp <= 0xFFFF) {
|
|
386
|
+ out[0] = 0xE0 | (cp >> 12);
|
|
387
|
+ out[1] = 0x80 | ((cp >> 6) & 0x3F);
|
|
388
|
+ out[2] = 0x80 | (cp & 0x3F);
|
|
389
|
+ return 3;
|
|
390
|
+ } else if (cp <= 0x1FFFFF) {
|
|
391
|
+ out[0] = 0xF0 | (cp >> 18);
|
|
392
|
+ out[1] = 0x80 | ((cp >> 12) & 0x3F);
|
|
393
|
+ out[2] = 0x80 | ((cp >> 6) & 0x3F);
|
|
394
|
+ out[3] = 0x80 | (cp & 0x3F);
|
|
395
|
+ return 4;
|
|
396
|
+ }
|
|
397
|
+
|
|
398
|
+ return 0;
|
|
399
|
+}
|