parser.ipp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. //
  2. // Copyright (c) 2019 Vinnie Falco ([email protected])
  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_PARSER_IPP
  10. #define BOOST_JSON_IMPL_PARSER_IPP
  11. #include <boost/json/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. namespace boost {
  18. namespace json {
  19. parser::
  20. parser(
  21. storage_ptr sp,
  22. parse_options const& opt,
  23. unsigned char* buffer,
  24. std::size_t size) noexcept
  25. : p_(
  26. opt,
  27. std::move(sp),
  28. buffer,
  29. size)
  30. {
  31. reset();
  32. }
  33. parser::
  34. parser(
  35. storage_ptr sp,
  36. parse_options const& opt) noexcept
  37. : p_(
  38. opt,
  39. std::move(sp),
  40. nullptr,
  41. 0)
  42. {
  43. reset();
  44. }
  45. void
  46. parser::
  47. reset(storage_ptr sp) noexcept
  48. {
  49. p_.reset();
  50. p_.handler().st.reset(sp);
  51. }
  52. std::size_t
  53. parser::
  54. write_some(
  55. char const* data,
  56. std::size_t size,
  57. system::error_code& ec)
  58. {
  59. auto const n = p_.write_some(
  60. false, data, size, ec);
  61. BOOST_ASSERT(ec || p_.done());
  62. return n;
  63. }
  64. std::size_t
  65. parser::
  66. write_some(
  67. char const* data,
  68. std::size_t size,
  69. std::error_code& ec)
  70. {
  71. system::error_code jec;
  72. std::size_t const result = write_some(data, size, jec);
  73. ec = jec;
  74. return result;
  75. }
  76. std::size_t
  77. parser::
  78. write_some(
  79. char const* data,
  80. std::size_t size)
  81. {
  82. system::error_code ec;
  83. auto const n = write_some(
  84. data, size, ec);
  85. if(ec)
  86. detail::throw_system_error( ec );
  87. return n;
  88. }
  89. std::size_t
  90. parser::
  91. write(
  92. char const* data,
  93. std::size_t size,
  94. system::error_code& ec)
  95. {
  96. auto const n = write_some(
  97. data, size, ec);
  98. if(! ec && n < size)
  99. {
  100. BOOST_JSON_FAIL(ec, error::extra_data);
  101. p_.fail(ec);
  102. }
  103. return n;
  104. }
  105. std::size_t
  106. parser::
  107. write(
  108. char const* data,
  109. std::size_t size,
  110. std::error_code& ec)
  111. {
  112. system::error_code jec;
  113. std::size_t const result = write(data, size, jec);
  114. ec = jec;
  115. return result;
  116. }
  117. std::size_t
  118. parser::
  119. write(
  120. char const* data,
  121. std::size_t size)
  122. {
  123. system::error_code ec;
  124. auto const n = write(
  125. data, size, ec);
  126. if(ec)
  127. detail::throw_system_error( ec );
  128. return n;
  129. }
  130. value
  131. parser::
  132. release()
  133. {
  134. if( ! p_.done())
  135. {
  136. // prevent undefined behavior
  137. if(! p_.last_error())
  138. {
  139. system::error_code ec;
  140. BOOST_JSON_FAIL(ec, error::incomplete);
  141. p_.fail(ec);
  142. }
  143. detail::throw_system_error(
  144. p_.last_error());
  145. }
  146. return p_.handler().st.release();
  147. }
  148. } // namespace json
  149. } // namespace boost
  150. #endif