stream_parser.ipp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //
  2. // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // Official repository: https://github.com/boostorg/json
  8. //
  9. #ifndef BOOST_JSON_IMPL_STREAM_PARSER_IPP
  10. #define BOOST_JSON_IMPL_STREAM_PARSER_IPP
  11. #include <boost/json/stream_parser.hpp>
  12. #include <boost/json/basic_parser_impl.hpp>
  13. #include <boost/json/error.hpp>
  14. #include <cstring>
  15. #include <stdexcept>
  16. #include <utility>
  17. BOOST_JSON_NS_BEGIN
  18. stream_parser::
  19. stream_parser(
  20. storage_ptr sp,
  21. parse_options const& opt,
  22. unsigned char* buffer,
  23. std::size_t size) noexcept
  24. : p_(
  25. opt,
  26. std::move(sp),
  27. buffer,
  28. size)
  29. {
  30. reset();
  31. }
  32. stream_parser::
  33. stream_parser(
  34. storage_ptr sp,
  35. parse_options const& opt) noexcept
  36. : p_(
  37. opt,
  38. std::move(sp),
  39. nullptr,
  40. 0)
  41. {
  42. reset();
  43. }
  44. void
  45. stream_parser::
  46. reset(storage_ptr sp) noexcept
  47. {
  48. p_.reset();
  49. p_.handler().st.reset(sp);
  50. }
  51. std::size_t
  52. stream_parser::
  53. write_some(
  54. char const* data,
  55. std::size_t size,
  56. error_code& ec)
  57. {
  58. return p_.write_some(
  59. true, data, size, ec);
  60. }
  61. std::size_t
  62. stream_parser::
  63. write_some(
  64. char const* data,
  65. std::size_t size)
  66. {
  67. error_code ec;
  68. auto const n = write_some(
  69. data, size, ec);
  70. if(ec)
  71. detail::throw_system_error(ec,
  72. BOOST_JSON_SOURCE_POS);
  73. return n;
  74. }
  75. std::size_t
  76. stream_parser::
  77. write(
  78. char const* data,
  79. std::size_t size,
  80. error_code& ec)
  81. {
  82. auto const n = write_some(
  83. data, size, ec);
  84. if(! ec && n < size)
  85. {
  86. ec = error::extra_data;
  87. p_.fail(ec);
  88. }
  89. return n;
  90. }
  91. std::size_t
  92. stream_parser::
  93. write(
  94. char const* data,
  95. std::size_t size)
  96. {
  97. error_code ec;
  98. auto const n = write(
  99. data, size, ec);
  100. if(ec)
  101. detail::throw_system_error(ec,
  102. BOOST_JSON_SOURCE_POS);
  103. return n;
  104. }
  105. void
  106. stream_parser::
  107. finish(error_code& ec)
  108. {
  109. p_.write_some(false, nullptr, 0, ec);
  110. }
  111. void
  112. stream_parser::
  113. finish()
  114. {
  115. error_code ec;
  116. finish(ec);
  117. if(ec)
  118. detail::throw_system_error(ec,
  119. BOOST_JSON_SOURCE_POS);
  120. }
  121. value
  122. stream_parser::
  123. release()
  124. {
  125. if(! p_.done())
  126. {
  127. // prevent undefined behavior
  128. finish();
  129. }
  130. return p_.handler().st.release();
  131. }
  132. BOOST_JSON_NS_END
  133. #endif