File.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. std::cerr << "ERROR 5" << std::endl;
  178. exit(0);
  179. case AFTER_ATTRKEY:
  180. if (std::isspace(c))
  181. continue;
  182. else if (c == '=') {
  183. _s.s = AW_IN_ATTRVAL;
  184. continue;
  185. } else {
  186. // TODO: error
  187. continue;
  188. }
  189. case IN_TAG_NAME:
  190. if (std::isspace(c)) {
  191. *_c = 0;
  192. _s.s = IN_TAG;
  193. continue;
  194. } else if (c == '>') {
  195. *_c = 0;
  196. _s.hanging++;
  197. _s.tagStack.push(_ret.name);
  198. _s.s = WS_SKIP;
  199. continue;
  200. } else if (c == '/') {
  201. *_c = 0;
  202. _s.s = AW_CLOSING;
  203. continue;
  204. } else if (std::isalnum(c) || c == '-' || c == '_' || c == '.') {
  205. continue;
  206. }
  207. case IN_TAG_NAME_META:
  208. // TODO: read meta tags!
  209. if (c == '>') {
  210. _s.s = NONE;
  211. continue;
  212. }
  213. continue;
  214. case IN_TAG_NAME_CLOSE:
  215. if (std::isspace(c)) {
  216. *_c = 0;
  217. _s.s = IN_TAG_CLOSE;
  218. continue;
  219. } else if (std::isalnum(c) || c == '-' || c == '_' || c == '.') {
  220. continue;
  221. } else if (c == '>') {
  222. *_c = 0;
  223. if (_tmp != _s.tagStack.top()) {
  224. throw XmlFileException("closing wrong tag");
  225. }
  226. _s.tagStack.pop();
  227. _s.s = NONE;
  228. continue;
  229. }
  230. case IN_TAG_CLOSE:
  231. if (std::isspace(c))
  232. continue;
  233. else if (c == '>') {
  234. if (_tmp != _s.tagStack.top()) {
  235. throw XmlFileException("closing wrong tag");
  236. }
  237. _s.tagStack.pop();
  238. _s.s = NONE;
  239. continue;
  240. } else {
  241. throw XmlFileException("expected '>'");
  242. }
  243. case AW_CLOSING:
  244. if (c == '>') {
  245. _s.s = WS_SKIP;
  246. continue;
  247. }
  248. case WS_SKIP:
  249. if (std::isspace(c))
  250. continue;
  251. else {
  252. _s.s = NONE;
  253. return true;
  254. }
  255. }
  256. }
  257. // buffer ended, read new stuff, but copy remaining if needed
  258. size_t off = 0;
  259. if (_s.s == IN_TAG_NAME) { //|| IN_TAG_NAME_META) {
  260. off = _lastBytes - (_ret.name - _buffer[_which]);
  261. memmove(_buffer[!_which], _ret.name, off);
  262. _ret.name = _buffer[!_which];
  263. } else if (_s.s == IN_TAG_NAME_CLOSE || _s.s == IN_ATTRKEY ||
  264. _s.s == IN_TEXT) {
  265. off = _lastBytes - (_tmp - _buffer[_which]);
  266. memmove(_buffer[!_which], _tmp, off);
  267. _tmp = _buffer[!_which];
  268. } else if (_s.s == IN_ATTRVAL_SQ || _s.s == IN_ATTRVAL_DQ) {
  269. off = _lastBytes - (_tmp2 - _buffer[_which]);
  270. memmove(_buffer[!_which], _tmp2, off);
  271. _tmp2 = _buffer[!_which];
  272. }
  273. assert(off <= BUFFER_S);
  274. size_t readb = read(_file, _buffer[!_which] + off, BUFFER_S - off);
  275. if (!readb) break;
  276. _totReadBef += _lastNewData;
  277. _which = !_which;
  278. _lastNewData = readb;
  279. _lastBytes = _lastNewData + off;
  280. _c = _buffer[_which] + off;
  281. }
  282. if (_s.tagStack.size()) {
  283. if (_s.tagStack.top() != "[root]") {
  284. // TODO error
  285. throw XmlFileException("XML tree not complete");
  286. } else {
  287. _s.tagStack.pop();
  288. }
  289. }
  290. _s.s = NONE;
  291. _ret.name = "[root]";
  292. return false;
  293. }
  294. // _____________________________________________________________________________
  295. std::string File::decode(const std::string& str) { return decode(str.c_str()); }
  296. // _____________________________________________________________________________
  297. std::string File::decode(const char* str) {
  298. const char* c = strchr(str, '&');
  299. if (!c) return str;
  300. char decRet[strlen(str) + 1];
  301. const char* last = str;
  302. char* dstPt = decRet;
  303. for (; c != 0; c = strchr(c + 1, '&')) {
  304. memcpy(dstPt, last, c - last);
  305. dstPt += c - last;
  306. last = c;
  307. if (*(c + 1) == '#') {
  308. uint64_t cp = -1;
  309. char* tail;
  310. errno = 0;
  311. if (*(c + 2) == 'x' || *(c + 2) == 'X')
  312. cp = strtoul(c + 3, &tail, 16);
  313. else
  314. cp = strtoul(c + 2, &tail, 10);
  315. if (*tail == ';' && cp <= 0x1FFFFF && !errno) {
  316. dstPt += utf8(cp, dstPt);
  317. last = tail + 1;
  318. }
  319. } else {
  320. const char* e = strchr(c, ';');
  321. if (e) {
  322. char ent[e - 1 - c + 1];
  323. memcpy(ent, c + 1, e - 1 - c);
  324. ent[e - 1 - c] = 0;
  325. const auto it = xml::ENTITIES.find(ent);
  326. if (it != xml::ENTITIES.end()) {
  327. const char* utf8 = it->second;
  328. memcpy(dstPt, utf8, strlen(utf8));
  329. dstPt += strlen(utf8);
  330. last += strlen(ent) + 2;
  331. }
  332. }
  333. }
  334. }
  335. strcpy(dstPt, last);
  336. return decRet;
  337. }
  338. // _____________________________________________________________________________
  339. size_t File::utf8(size_t cp, char* out) {
  340. if (cp <= 0x7F) {
  341. out[0] = cp & 0x7F;
  342. return 1;
  343. } else if (cp <= 0x7FF) {
  344. out[0] = 0xC0 | (cp >> 6);
  345. out[1] = 0x80 | (cp & 0x3F);
  346. return 2;
  347. } else if (cp <= 0xFFFF) {
  348. out[0] = 0xE0 | (cp >> 12);
  349. out[1] = 0x80 | ((cp >> 6) & 0x3F);
  350. out[2] = 0x80 | (cp & 0x3F);
  351. return 3;
  352. } else if (cp <= 0x1FFFFF) {
  353. out[0] = 0xF0 | (cp >> 18);
  354. out[1] = 0x80 | ((cp >> 12) & 0x3F);
  355. out[2] = 0x80 | ((cp >> 6) & 0x3F);
  356. out[3] = 0x80 | (cp & 0x3F);
  357. return 4;
  358. }
  359. return 0;
  360. }