/src/boost/boost/json/impl/stream_parser.ipp
Line | Count | Source (jump to first uncovered line) |
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 | | namespace boost { |
21 | | namespace json { |
22 | | |
23 | | stream_parser:: |
24 | | stream_parser( |
25 | | storage_ptr sp, |
26 | | parse_options const& opt, |
27 | | unsigned char* buffer, |
28 | | std::size_t size) noexcept |
29 | | : p_( |
30 | | opt, |
31 | | std::move(sp), |
32 | | buffer, |
33 | | size) |
34 | 4.59k | { |
35 | 4.59k | reset(); |
36 | 4.59k | } |
37 | | |
38 | | stream_parser:: |
39 | | stream_parser( |
40 | | storage_ptr sp, |
41 | | parse_options const& opt) noexcept |
42 | | : p_( |
43 | | opt, |
44 | | std::move(sp), |
45 | | nullptr, |
46 | | 0) |
47 | 5.45k | { |
48 | 5.45k | reset(); |
49 | 5.45k | } |
50 | | |
51 | | void |
52 | | stream_parser:: |
53 | | reset(storage_ptr sp) noexcept |
54 | 15.5k | { |
55 | 15.5k | p_.reset(); |
56 | 15.5k | p_.handler().st.reset(sp); |
57 | 15.5k | } |
58 | | |
59 | | std::size_t |
60 | | stream_parser:: |
61 | | write_some( |
62 | | char const* data, |
63 | | std::size_t size, |
64 | | system::error_code& ec) |
65 | 10.0k | { |
66 | 10.0k | return p_.write_some( |
67 | 10.0k | true, data, size, ec); |
68 | 10.0k | } |
69 | | |
70 | | std::size_t |
71 | | stream_parser:: |
72 | | write_some( |
73 | | char const* data, |
74 | | std::size_t size, |
75 | | std::error_code& ec) |
76 | 0 | { |
77 | 0 | system::error_code jec; |
78 | 0 | std::size_t const result = write_some(data, size, jec); |
79 | 0 | ec = jec; |
80 | 0 | return result; |
81 | 0 | } |
82 | | |
83 | | std::size_t |
84 | | stream_parser:: |
85 | | write_some( |
86 | | char const* data, |
87 | | std::size_t size) |
88 | 0 | { |
89 | 0 | system::error_code ec; |
90 | 0 | auto const n = write_some( |
91 | 0 | data, size, ec); |
92 | 0 | if(ec) |
93 | 0 | detail::throw_system_error( ec ); |
94 | 0 | return n; |
95 | 0 | } |
96 | | |
97 | | std::size_t |
98 | | stream_parser:: |
99 | | write( |
100 | | char const* data, |
101 | | std::size_t size, |
102 | | system::error_code& ec) |
103 | 10.0k | { |
104 | 10.0k | auto const n = write_some( |
105 | 10.0k | data, size, ec); |
106 | 10.0k | if(! ec && n < size) |
107 | 1.11k | { |
108 | 1.11k | BOOST_JSON_FAIL(ec, error::extra_data); |
109 | 1.11k | p_.fail(ec); |
110 | 1.11k | } |
111 | 10.0k | return n; |
112 | 10.0k | } |
113 | | |
114 | | std::size_t |
115 | | stream_parser:: |
116 | | write( |
117 | | char const* data, |
118 | | std::size_t size, |
119 | | std::error_code& ec) |
120 | 0 | { |
121 | 0 | system::error_code jec; |
122 | 0 | std::size_t const result = write(data, size, jec); |
123 | 0 | ec = jec; |
124 | 0 | return result; |
125 | 0 | } |
126 | | |
127 | | std::size_t |
128 | | stream_parser:: |
129 | | write( |
130 | | char const* data, |
131 | | std::size_t size) |
132 | 0 | { |
133 | 0 | system::error_code ec; |
134 | 0 | auto const n = write( |
135 | 0 | data, size, ec); |
136 | 0 | if(ec) |
137 | 0 | detail::throw_system_error( ec ); |
138 | 0 | return n; |
139 | 0 | } |
140 | | |
141 | | void |
142 | | stream_parser:: |
143 | | finish(system::error_code& ec) |
144 | 6.49k | { |
145 | 6.49k | p_.write_some(false, nullptr, 0, ec); |
146 | 6.49k | } |
147 | | |
148 | | void |
149 | | stream_parser:: |
150 | | finish() |
151 | 0 | { |
152 | 0 | system::error_code ec; |
153 | 0 | finish(ec); |
154 | 0 | if(ec) |
155 | 0 | detail::throw_system_error( ec ); |
156 | 0 | } |
157 | | |
158 | | void |
159 | | stream_parser:: |
160 | | finish(std::error_code& ec) |
161 | 0 | { |
162 | 0 | system::error_code jec; |
163 | 0 | finish(jec); |
164 | 0 | ec = jec; |
165 | 0 | } |
166 | | |
167 | | value |
168 | | stream_parser:: |
169 | | release() |
170 | 4.17k | { |
171 | 4.17k | if(! p_.done()) |
172 | 0 | { |
173 | | // prevent undefined behavior |
174 | 0 | finish(); |
175 | 0 | } |
176 | 4.17k | return p_.handler().st.release(); |
177 | 4.17k | } |
178 | | |
179 | | } // namespace json |
180 | | } // namespace boost |
181 | | |
182 | | #endif |