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