File.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 <sstream>
  9. #include <stack>
  10. #include <string>
  11. namespace xml {
  12. const static size_t BUFFER_S = 16 * 1024;
  13. class XmlFileException : public std::exception {
  14. public:
  15. XmlFileException(std::string msg, std::string file, const char* p, char* buff,
  16. size_t offset) {
  17. std::stringstream ss;
  18. ss << file << " at position " << (offset + (p - buff)) << ": " << msg;
  19. _msg = ss.str();
  20. }
  21. ~XmlFileException() throw() {}
  22. virtual const char* what() const throw() { return _msg.c_str(); };
  23. private:
  24. std::string _msg;
  25. };
  26. enum State {
  27. NONE,
  28. IN_TAG_NAME,
  29. IN_TAG_NAME_META,
  30. IN_TAG,
  31. IN_TAG_CLOSE,
  32. IN_TAG_NAME_CLOSE,
  33. IN_TAG_TENTATIVE,
  34. IN_ATTRKEY,
  35. AFTER_ATTRKEY,
  36. AW_IN_ATTRVAL,
  37. IN_ATTRVAL_SQ,
  38. IN_ATTRVAL_DQ,
  39. IN_TEXT,
  40. IN_COMMENT_TENTATIVE,
  41. IN_COMMENT_TENTATIVE2,
  42. IN_COMMENT,
  43. IN_COMMENT_CL_TENTATIVE,
  44. IN_COMMENT_CL_TENTATIVE2,
  45. AW_CLOSING,
  46. WS_SKIP
  47. };
  48. struct AttrCmp {
  49. bool operator()(const char* const& a, const char* const& b) const {
  50. return std::strcmp(a, b) < 0;
  51. }
  52. };
  53. struct ParserState {
  54. ParserState() : s(NONE), hanging(0), off(0){};
  55. std::stack<std::string> tagStack;
  56. State s;
  57. size_t hanging;
  58. int64_t off;
  59. };
  60. typedef std::map<const char*, const char*, AttrCmp> AttrMap;
  61. struct Tag {
  62. const char* name;
  63. const char* text;
  64. AttrMap attrs;
  65. };
  66. class File {
  67. public:
  68. File(const std::string& path);
  69. ~File();
  70. const Tag& get() const;
  71. bool next();
  72. size_t level() const;
  73. void reset();
  74. ParserState state();
  75. void setState(const ParserState& s);
  76. static std::string decode(const char* str);
  77. static std::string decode(const std::string& str);
  78. private:
  79. int _file;
  80. ParserState _s;
  81. ParserState _prevs;
  82. char** _buffer;
  83. char* _c;
  84. int64_t _lastBytes;
  85. const char* _tmp;
  86. const char* _tmp2;
  87. size_t _which;
  88. std::string _path;
  89. int64_t _totReadBef;
  90. int64_t _lastNewData;
  91. Tag _ret;
  92. static size_t utf8(size_t cp, char* out);
  93. const char* emptyStr = "";
  94. };
  95. }
  96. #endif // XML_FILE_H_