File.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright 2017 Patrick Brosi
  2. // info@patrickbrosi.de
  3. #ifndef XML_FILE_H_
  4. #define XML_FILE_H_
  5. #include <cstring>
  6. #include <fstream>
  7. #include <map>
  8. #include <stack>
  9. #include <string>
  10. namespace xml {
  11. const static size_t BUFFER_S = 16 * 1024;
  12. class XmlFileException : public std::exception {
  13. public:
  14. XmlFileException(std::string msg) : _msg(msg) {}
  15. ~XmlFileException() throw() {}
  16. virtual const char* what() const throw() { return _msg.c_str(); };
  17. private:
  18. std::string _msg;
  19. };
  20. enum State {
  21. NONE,
  22. IN_TAG_NAME,
  23. IN_TAG_NAME_META,
  24. IN_TAG,
  25. IN_TAG_CLOSE,
  26. IN_TAG_NAME_CLOSE,
  27. IN_TAG_TENTATIVE,
  28. IN_ATTRKEY,
  29. AFTER_ATTRKEY,
  30. AW_IN_ATTRVAL,
  31. IN_ATTRVAL_SQ,
  32. IN_ATTRVAL_DQ,
  33. IN_TEXT,
  34. AW_CLOSING,
  35. WS_SKIP
  36. };
  37. struct AttrCmp {
  38. bool operator()(const char* const& a, const char* const& b) const {
  39. return std::strcmp(a, b) < 0;
  40. }
  41. };
  42. struct ParserState {
  43. ParserState() : s(NONE), hanging(0), off(0){};
  44. std::stack<std::string> tagStack;
  45. State s;
  46. size_t hanging;
  47. int64_t off;
  48. };
  49. typedef std::map<const char*, const char*, AttrCmp> AttrMap;
  50. struct Tag {
  51. const char* name;
  52. AttrMap attrs;
  53. };
  54. class File {
  55. public:
  56. File(const std::string& path);
  57. ~File();
  58. const Tag& get() const;
  59. bool next();
  60. size_t level() const;
  61. void reset();
  62. ParserState state();
  63. void setState(const ParserState& s);
  64. static std::string decode(const char* str);
  65. static std::string decode(const std::string& str);
  66. private:
  67. int _file;
  68. ParserState _s;
  69. ParserState _prevs;
  70. char** _buffer;
  71. char* _c;
  72. int64_t _lastBytes;
  73. const char* _tmp;
  74. const char* _tmp2;
  75. size_t _which;
  76. std::string _path;
  77. int64_t _totReadBef;
  78. int64_t _lastNewData;
  79. Tag _ret;
  80. static size_t utf8(size_t cp, char* out);
  81. };
  82. }
  83. #endif // XML_FILE_H_