File.cpp 11 KB

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