streamConsumer.hh 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /**
  2. * streamConsumer.hh for jsonstroller
  3. *
  4. * Author: isundil <isundill@gmail.com>
  5. **/
  6. #pragma once
  7. #include <istream>
  8. #include "params.hh"
  9. #include "jsonObject.hh"
  10. #include "jsonArray.hh"
  11. #include "jsonPrimitive.hh"
  12. #include "linearHistory.hh"
  13. #include "warning.hh"
  14. class StreamConsumer
  15. {
  16. public:
  17. /**
  18. * constructor
  19. * does not read yet
  20. **/
  21. StreamConsumer(std::istream &stream);
  22. virtual ~StreamConsumer();
  23. /**
  24. * read from stream and set root data
  25. * @return current instance
  26. **/
  27. StreamConsumer *read();
  28. /**
  29. * get root node
  30. **/
  31. const JSonElement * getRoot() const;
  32. JSonElement * getRoot();
  33. /**
  34. * return non-blocking error messages
  35. **/
  36. const std::list<Warning> &getMessages() const;
  37. /**
  38. * set builder's params
  39. **/
  40. StreamConsumer *withConfig(const AParams *);
  41. /**
  42. * find \uXXXX in buffer and replace them
  43. **/
  44. static std::string extractUnicode(const char *);
  45. static std::string extractUnicode(const std::string &);
  46. protected:
  47. /**
  48. * @return non-null on successfully read JSonElement, or null if token (',', '[', ...)
  49. **/
  50. JSonElement *consumeToken(JSonContainer *parent, std::stringstream &buf);
  51. JSonElement *consumeString(JSonContainer *parent, std::stringstream &buf);
  52. JSonElement *consumeBool(JSonContainer *parent, std::stringstream &buf, char c);
  53. JSonElement *consumeNumber(JSonContainer *parent, std::stringstream &buf, char c);
  54. JSonElement *consumeNull(JSonContainer *parent, std::stringstream &buf);
  55. /**
  56. * read next item, fill object or array if found
  57. **/
  58. JSonElement *readNext(JSonContainer *parent);
  59. /**
  60. * fill object
  61. **/
  62. JSonObject *readObject(JSonContainer *parent);
  63. /**
  64. * fill array
  65. **/
  66. JSonArray *readArray(JSonContainer *parent);
  67. /**
  68. * out of token, should we ignore that char ?
  69. **/
  70. bool ignoreChar(char c) const noexcept;
  71. /**
  72. * compute unicode value and append it to buffer
  73. **/
  74. static void appendUnicode(const char [4], std::stringstream &);
  75. /**
  76. * input stream
  77. **/
  78. std::istream &stream;
  79. /**
  80. * once read, contains root element
  81. **/
  82. JSonElement *root;
  83. /**
  84. * builder params (program's one in fact)
  85. **/
  86. const AParams *params;
  87. /**
  88. * non-blocking errors
  89. **/
  90. std::list<Warning> warnings;
  91. /**
  92. * last read input
  93. **/
  94. LinearHistory history;
  95. };