parser.ipp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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_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. BOOST_JSON_NS_BEGIN
  18. parser::
  19. 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. parser::
  33. 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. parser::
  46. reset(storage_ptr sp) noexcept
  47. {
  48. p_.reset();
  49. p_.handler().st.reset(sp);
  50. }
  51. std::size_t
  52. parser::
  53. write_some(
  54. char const* data,
  55. std::size_t size,
  56. error_code& ec)
  57. {
  58. auto const n = p_.write_some(
  59. false, data, size, ec);
  60. BOOST_ASSERT(ec || p_.done());
  61. return n;
  62. }
  63. std::size_t
  64. parser::
  65. write_some(
  66. char const* data,
  67. std::size_t size)
  68. {
  69. error_code ec;
  70. auto const n = write_some(
  71. data, size, ec);
  72. if(ec)
  73. detail::throw_system_error(ec,
  74. BOOST_JSON_SOURCE_POS);
  75. return n;
  76. }
  77. std::size_t
  78. parser::
  79. write(
  80. char const* data,
  81. std::size_t size,
  82. error_code& ec)
  83. {
  84. auto const n = write_some(
  85. data, size, ec);
  86. if(! ec && n < size)
  87. {
  88. ec = error::extra_data;
  89. p_.fail(ec);
  90. }
  91. return n;
  92. }
  93. std::size_t
  94. parser::
  95. write(
  96. char const* data,
  97. std::size_t size)
  98. {
  99. error_code ec;
  100. auto const n = write(
  101. data, size, ec);
  102. if(ec)
  103. detail::throw_system_error(ec,
  104. BOOST_JSON_SOURCE_POS);
  105. return n;
  106. }
  107. value
  108. parser::
  109. release()
  110. {
  111. if( ! p_.done())
  112. {
  113. // prevent undefined behavior
  114. if(! p_.last_error())
  115. p_.fail(error::incomplete);
  116. detail::throw_system_error(
  117. p_.last_error(),
  118. BOOST_JSON_SOURCE_POS);
  119. }
  120. return p_.handler().st.release();
  121. }
  122. BOOST_JSON_NS_END
  123. #endif