File.cpp 11 KB

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