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. 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. if (!_s.tagStack.size()) return false;
  66. // avoid too much stack copying
  67. if (_prevs.tagStack.size() != _s.tagStack.size() ||
  68. _prevs.tagStack.top() != _s.tagStack.top()) {
  69. _prevs.tagStack = _s.tagStack;
  70. }
  71. _prevs.s = _s.s;
  72. _prevs.hanging = _s.hanging;
  73. _prevs.off =
  74. _totReadBef + (_c - _buffer[_which]) - (_lastBytes - _lastNewData);
  75. if (_s.hanging) _s.hanging--;
  76. _ret.name = 0;
  77. _ret.attrs.clear();
  78. void* i;
  79. while (_lastBytes) {
  80. for (; _c - _buffer[_which] < _lastBytes; ++_c) {
  81. char c = *_c;
  82. switch (_s.s) {
  83. case NONE:
  84. if (std::isspace(c))
  85. continue;
  86. else if (c == '<') {
  87. _s.s = IN_TAG_TENTATIVE;
  88. continue;
  89. } else {
  90. _s.s = IN_TEXT;
  91. continue;
  92. }
  93. case IN_TEXT:
  94. throw XmlFileException("text nodes not yet supported");
  95. case IN_TAG_TENTATIVE:
  96. if (c == '/') {
  97. _s.s = IN_TAG_NAME_CLOSE;
  98. _tmp = _c + 1;
  99. continue;
  100. } else if (c == '?') {
  101. _s.s = IN_TAG_NAME_META;
  102. continue;
  103. } else if (std::isalnum(c) || c == '-' || c == '_' || c == '.') {
  104. _s.s = IN_TAG_NAME;
  105. _ret.name = _c;
  106. continue;
  107. }
  108. case IN_TAG:
  109. if (std::isspace(c))
  110. continue;
  111. else if (std::isalnum(c) || c == '-' || c == '_' || c == '.') {
  112. _s.s = IN_ATTRKEY;
  113. _tmp = _c;
  114. continue;
  115. } else if (c == '/') {
  116. _s.s = AW_CLOSING;
  117. continue;
  118. } else if (c == '>') {
  119. _s.hanging++;
  120. _s.tagStack.push(_ret.name);
  121. _s.s = WS_SKIP;
  122. continue;
  123. } else {
  124. throw XmlFileException("expected valid tag");
  125. }
  126. case IN_ATTRVAL_SQ:
  127. i = memchr(_c, '\'', _lastBytes - (_c - _buffer[_which]));
  128. if (!i) {
  129. _c = _buffer[_which] + _lastBytes;
  130. continue;
  131. } else {
  132. _c = (char*)i;
  133. _s.s = IN_TAG;
  134. *_c = 0;
  135. _ret.attrs[_tmp] = _tmp2;
  136. continue;
  137. }
  138. case IN_ATTRVAL_DQ:
  139. i = memchr(_c, '"', _lastBytes - (_c - _buffer[_which]));
  140. if (!i) {
  141. _c = _buffer[_which] + _lastBytes;
  142. continue;
  143. } else {
  144. _c = (char*)i;
  145. _s.s = IN_TAG;
  146. *_c = 0;
  147. _ret.attrs[_tmp] = _tmp2;
  148. continue;
  149. }
  150. case AW_IN_ATTRVAL:
  151. if (std::isspace(c))
  152. continue;
  153. else if (c == '\'') {
  154. _s.s = IN_ATTRVAL_SQ;
  155. _tmp2 = _c + 1;
  156. continue;
  157. } else if (c == '"') {
  158. _s.s = IN_ATTRVAL_DQ;
  159. _tmp2 = _c + 1;
  160. continue;
  161. } else {
  162. throw XmlFileException("expected attribute value");
  163. }
  164. case IN_ATTRKEY:
  165. if (std::isspace(c)) {
  166. *_c = 0;
  167. _s.s = AFTER_ATTRKEY;
  168. continue;
  169. } else if (std::isalnum(c) || c == '-' || c == '_' || c == '.') {
  170. continue;
  171. } else if (c == '=') {
  172. *_c = 0;
  173. _s.s = AW_IN_ATTRVAL;
  174. continue;
  175. }
  176. std::cerr << "ERROR 5" << std::endl;
  177. exit(0);
  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. }