Chris@16
|
1 //
|
Chris@16
|
2 // read_until.hpp
|
Chris@16
|
3 // ~~~~~~~~~~~~~~
|
Chris@16
|
4 //
|
Chris@101
|
5 // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
|
Chris@16
|
6 //
|
Chris@16
|
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
|
Chris@16
|
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
9 //
|
Chris@16
|
10
|
Chris@16
|
11 #ifndef BOOST_ASIO_READ_UNTIL_HPP
|
Chris@16
|
12 #define BOOST_ASIO_READ_UNTIL_HPP
|
Chris@16
|
13
|
Chris@16
|
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
|
Chris@16
|
15 # pragma once
|
Chris@16
|
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
|
Chris@16
|
17
|
Chris@16
|
18 #include <boost/asio/detail/config.hpp>
|
Chris@16
|
19
|
Chris@16
|
20 #if !defined(BOOST_ASIO_NO_IOSTREAM)
|
Chris@16
|
21
|
Chris@16
|
22 #include <cstddef>
|
Chris@16
|
23 #include <string>
|
Chris@16
|
24 #include <boost/asio/async_result.hpp>
|
Chris@16
|
25 #include <boost/asio/basic_streambuf.hpp>
|
Chris@16
|
26 #include <boost/asio/detail/regex_fwd.hpp>
|
Chris@16
|
27 #include <boost/asio/detail/type_traits.hpp>
|
Chris@16
|
28 #include <boost/asio/error.hpp>
|
Chris@16
|
29
|
Chris@16
|
30 #include <boost/asio/detail/push_options.hpp>
|
Chris@16
|
31
|
Chris@16
|
32 namespace boost {
|
Chris@16
|
33 namespace asio {
|
Chris@16
|
34
|
Chris@16
|
35 namespace detail
|
Chris@16
|
36 {
|
Chris@16
|
37 char (&has_result_type_helper(...))[2];
|
Chris@16
|
38
|
Chris@16
|
39 template <typename T>
|
Chris@16
|
40 char has_result_type_helper(T*, typename T::result_type* = 0);
|
Chris@16
|
41
|
Chris@16
|
42 template <typename T>
|
Chris@16
|
43 struct has_result_type
|
Chris@16
|
44 {
|
Chris@16
|
45 enum { value = (sizeof((has_result_type_helper)((T*)(0))) == 1) };
|
Chris@16
|
46 };
|
Chris@16
|
47 } // namespace detail
|
Chris@16
|
48
|
Chris@16
|
49 /// Type trait used to determine whether a type can be used as a match condition
|
Chris@16
|
50 /// function with read_until and async_read_until.
|
Chris@16
|
51 template <typename T>
|
Chris@16
|
52 struct is_match_condition
|
Chris@16
|
53 {
|
Chris@16
|
54 #if defined(GENERATING_DOCUMENTATION)
|
Chris@16
|
55 /// The value member is true if the type may be used as a match condition.
|
Chris@16
|
56 static const bool value;
|
Chris@16
|
57 #else
|
Chris@16
|
58 enum
|
Chris@16
|
59 {
|
Chris@16
|
60 value = boost::asio::is_function<
|
Chris@16
|
61 typename boost::asio::remove_pointer<T>::type>::value
|
Chris@16
|
62 || detail::has_result_type<T>::value
|
Chris@16
|
63 };
|
Chris@16
|
64 #endif
|
Chris@16
|
65 };
|
Chris@16
|
66
|
Chris@16
|
67 /**
|
Chris@16
|
68 * @defgroup read_until boost::asio::read_until
|
Chris@16
|
69 *
|
Chris@16
|
70 * @brief Read data into a streambuf until it contains a delimiter, matches a
|
Chris@16
|
71 * regular expression, or a function object indicates a match.
|
Chris@16
|
72 */
|
Chris@16
|
73 /*@{*/
|
Chris@16
|
74
|
Chris@16
|
75 /// Read data into a streambuf until it contains a specified delimiter.
|
Chris@16
|
76 /**
|
Chris@16
|
77 * This function is used to read data into the specified streambuf until the
|
Chris@16
|
78 * streambuf's get area contains the specified delimiter. The call will block
|
Chris@16
|
79 * until one of the following conditions is true:
|
Chris@16
|
80 *
|
Chris@16
|
81 * @li The get area of the streambuf contains the specified delimiter.
|
Chris@16
|
82 *
|
Chris@16
|
83 * @li An error occurred.
|
Chris@16
|
84 *
|
Chris@16
|
85 * This operation is implemented in terms of zero or more calls to the stream's
|
Chris@16
|
86 * read_some function. If the streambuf's get area already contains the
|
Chris@16
|
87 * delimiter, the function returns immediately.
|
Chris@16
|
88 *
|
Chris@16
|
89 * @param s The stream from which the data is to be read. The type must support
|
Chris@16
|
90 * the SyncReadStream concept.
|
Chris@16
|
91 *
|
Chris@16
|
92 * @param b A streambuf object into which the data will be read.
|
Chris@16
|
93 *
|
Chris@16
|
94 * @param delim The delimiter character.
|
Chris@16
|
95 *
|
Chris@16
|
96 * @returns The number of bytes in the streambuf's get area up to and including
|
Chris@16
|
97 * the delimiter.
|
Chris@16
|
98 *
|
Chris@16
|
99 * @throws boost::system::system_error Thrown on failure.
|
Chris@16
|
100 *
|
Chris@16
|
101 * @note After a successful read_until operation, the streambuf may contain
|
Chris@16
|
102 * additional data beyond the delimiter. An application will typically leave
|
Chris@16
|
103 * that data in the streambuf for a subsequent read_until operation to examine.
|
Chris@16
|
104 *
|
Chris@16
|
105 * @par Example
|
Chris@16
|
106 * To read data into a streambuf until a newline is encountered:
|
Chris@16
|
107 * @code boost::asio::streambuf b;
|
Chris@16
|
108 * boost::asio::read_until(s, b, '\n');
|
Chris@16
|
109 * std::istream is(&b);
|
Chris@16
|
110 * std::string line;
|
Chris@16
|
111 * std::getline(is, line); @endcode
|
Chris@16
|
112 * After the @c read_until operation completes successfully, the buffer @c b
|
Chris@16
|
113 * contains the delimiter:
|
Chris@16
|
114 * @code { 'a', 'b', ..., 'c', '\n', 'd', 'e', ... } @endcode
|
Chris@16
|
115 * The call to @c std::getline then extracts the data up to and including the
|
Chris@16
|
116 * delimiter, so that the string @c line contains:
|
Chris@16
|
117 * @code { 'a', 'b', ..., 'c', '\n' } @endcode
|
Chris@16
|
118 * The remaining data is left in the buffer @c b as follows:
|
Chris@16
|
119 * @code { 'd', 'e', ... } @endcode
|
Chris@16
|
120 * This data may be the start of a new line, to be extracted by a subsequent
|
Chris@16
|
121 * @c read_until operation.
|
Chris@16
|
122 */
|
Chris@16
|
123 template <typename SyncReadStream, typename Allocator>
|
Chris@16
|
124 std::size_t read_until(SyncReadStream& s,
|
Chris@16
|
125 boost::asio::basic_streambuf<Allocator>& b, char delim);
|
Chris@16
|
126
|
Chris@16
|
127 /// Read data into a streambuf until it contains a specified delimiter.
|
Chris@16
|
128 /**
|
Chris@16
|
129 * This function is used to read data into the specified streambuf until the
|
Chris@16
|
130 * streambuf's get area contains the specified delimiter. The call will block
|
Chris@16
|
131 * until one of the following conditions is true:
|
Chris@16
|
132 *
|
Chris@16
|
133 * @li The get area of the streambuf contains the specified delimiter.
|
Chris@16
|
134 *
|
Chris@16
|
135 * @li An error occurred.
|
Chris@16
|
136 *
|
Chris@16
|
137 * This operation is implemented in terms of zero or more calls to the stream's
|
Chris@16
|
138 * read_some function. If the streambuf's get area already contains the
|
Chris@16
|
139 * delimiter, the function returns immediately.
|
Chris@16
|
140 *
|
Chris@16
|
141 * @param s The stream from which the data is to be read. The type must support
|
Chris@16
|
142 * the SyncReadStream concept.
|
Chris@16
|
143 *
|
Chris@16
|
144 * @param b A streambuf object into which the data will be read.
|
Chris@16
|
145 *
|
Chris@16
|
146 * @param delim The delimiter character.
|
Chris@16
|
147 *
|
Chris@16
|
148 * @param ec Set to indicate what error occurred, if any.
|
Chris@16
|
149 *
|
Chris@16
|
150 * @returns The number of bytes in the streambuf's get area up to and including
|
Chris@16
|
151 * the delimiter. Returns 0 if an error occurred.
|
Chris@16
|
152 *
|
Chris@16
|
153 * @note After a successful read_until operation, the streambuf may contain
|
Chris@16
|
154 * additional data beyond the delimiter. An application will typically leave
|
Chris@16
|
155 * that data in the streambuf for a subsequent read_until operation to examine.
|
Chris@16
|
156 */
|
Chris@16
|
157 template <typename SyncReadStream, typename Allocator>
|
Chris@16
|
158 std::size_t read_until(SyncReadStream& s,
|
Chris@16
|
159 boost::asio::basic_streambuf<Allocator>& b, char delim,
|
Chris@16
|
160 boost::system::error_code& ec);
|
Chris@16
|
161
|
Chris@16
|
162 /// Read data into a streambuf until it contains a specified delimiter.
|
Chris@16
|
163 /**
|
Chris@16
|
164 * This function is used to read data into the specified streambuf until the
|
Chris@16
|
165 * streambuf's get area contains the specified delimiter. The call will block
|
Chris@16
|
166 * until one of the following conditions is true:
|
Chris@16
|
167 *
|
Chris@16
|
168 * @li The get area of the streambuf contains the specified delimiter.
|
Chris@16
|
169 *
|
Chris@16
|
170 * @li An error occurred.
|
Chris@16
|
171 *
|
Chris@16
|
172 * This operation is implemented in terms of zero or more calls to the stream's
|
Chris@16
|
173 * read_some function. If the streambuf's get area already contains the
|
Chris@16
|
174 * delimiter, the function returns immediately.
|
Chris@16
|
175 *
|
Chris@16
|
176 * @param s The stream from which the data is to be read. The type must support
|
Chris@16
|
177 * the SyncReadStream concept.
|
Chris@16
|
178 *
|
Chris@16
|
179 * @param b A streambuf object into which the data will be read.
|
Chris@16
|
180 *
|
Chris@16
|
181 * @param delim The delimiter string.
|
Chris@16
|
182 *
|
Chris@16
|
183 * @returns The number of bytes in the streambuf's get area up to and including
|
Chris@16
|
184 * the delimiter.
|
Chris@16
|
185 *
|
Chris@16
|
186 * @throws boost::system::system_error Thrown on failure.
|
Chris@16
|
187 *
|
Chris@16
|
188 * @note After a successful read_until operation, the streambuf may contain
|
Chris@16
|
189 * additional data beyond the delimiter. An application will typically leave
|
Chris@16
|
190 * that data in the streambuf for a subsequent read_until operation to examine.
|
Chris@16
|
191 *
|
Chris@16
|
192 * @par Example
|
Chris@16
|
193 * To read data into a streambuf until a newline is encountered:
|
Chris@16
|
194 * @code boost::asio::streambuf b;
|
Chris@16
|
195 * boost::asio::read_until(s, b, "\r\n");
|
Chris@16
|
196 * std::istream is(&b);
|
Chris@16
|
197 * std::string line;
|
Chris@16
|
198 * std::getline(is, line); @endcode
|
Chris@16
|
199 * After the @c read_until operation completes successfully, the buffer @c b
|
Chris@16
|
200 * contains the delimiter:
|
Chris@16
|
201 * @code { 'a', 'b', ..., 'c', '\r', '\n', 'd', 'e', ... } @endcode
|
Chris@16
|
202 * The call to @c std::getline then extracts the data up to and including the
|
Chris@16
|
203 * delimiter, so that the string @c line contains:
|
Chris@16
|
204 * @code { 'a', 'b', ..., 'c', '\r', '\n' } @endcode
|
Chris@16
|
205 * The remaining data is left in the buffer @c b as follows:
|
Chris@16
|
206 * @code { 'd', 'e', ... } @endcode
|
Chris@16
|
207 * This data may be the start of a new line, to be extracted by a subsequent
|
Chris@16
|
208 * @c read_until operation.
|
Chris@16
|
209 */
|
Chris@16
|
210 template <typename SyncReadStream, typename Allocator>
|
Chris@16
|
211 std::size_t read_until(SyncReadStream& s,
|
Chris@16
|
212 boost::asio::basic_streambuf<Allocator>& b, const std::string& delim);
|
Chris@16
|
213
|
Chris@16
|
214 /// Read data into a streambuf until it contains a specified delimiter.
|
Chris@16
|
215 /**
|
Chris@16
|
216 * This function is used to read data into the specified streambuf until the
|
Chris@16
|
217 * streambuf's get area contains the specified delimiter. The call will block
|
Chris@16
|
218 * until one of the following conditions is true:
|
Chris@16
|
219 *
|
Chris@16
|
220 * @li The get area of the streambuf contains the specified delimiter.
|
Chris@16
|
221 *
|
Chris@16
|
222 * @li An error occurred.
|
Chris@16
|
223 *
|
Chris@16
|
224 * This operation is implemented in terms of zero or more calls to the stream's
|
Chris@16
|
225 * read_some function. If the streambuf's get area already contains the
|
Chris@16
|
226 * delimiter, the function returns immediately.
|
Chris@16
|
227 *
|
Chris@16
|
228 * @param s The stream from which the data is to be read. The type must support
|
Chris@16
|
229 * the SyncReadStream concept.
|
Chris@16
|
230 *
|
Chris@16
|
231 * @param b A streambuf object into which the data will be read.
|
Chris@16
|
232 *
|
Chris@16
|
233 * @param delim The delimiter string.
|
Chris@16
|
234 *
|
Chris@16
|
235 * @param ec Set to indicate what error occurred, if any.
|
Chris@16
|
236 *
|
Chris@16
|
237 * @returns The number of bytes in the streambuf's get area up to and including
|
Chris@16
|
238 * the delimiter. Returns 0 if an error occurred.
|
Chris@16
|
239 *
|
Chris@16
|
240 * @note After a successful read_until operation, the streambuf may contain
|
Chris@16
|
241 * additional data beyond the delimiter. An application will typically leave
|
Chris@16
|
242 * that data in the streambuf for a subsequent read_until operation to examine.
|
Chris@16
|
243 */
|
Chris@16
|
244 template <typename SyncReadStream, typename Allocator>
|
Chris@16
|
245 std::size_t read_until(SyncReadStream& s,
|
Chris@16
|
246 boost::asio::basic_streambuf<Allocator>& b, const std::string& delim,
|
Chris@16
|
247 boost::system::error_code& ec);
|
Chris@16
|
248
|
Chris@16
|
249 #if defined(BOOST_ASIO_HAS_BOOST_REGEX) \
|
Chris@16
|
250 || defined(GENERATING_DOCUMENTATION)
|
Chris@16
|
251
|
Chris@16
|
252 /// Read data into a streambuf until some part of the data it contains matches
|
Chris@16
|
253 /// a regular expression.
|
Chris@16
|
254 /**
|
Chris@16
|
255 * This function is used to read data into the specified streambuf until the
|
Chris@16
|
256 * streambuf's get area contains some data that matches a regular expression.
|
Chris@16
|
257 * The call will block until one of the following conditions is true:
|
Chris@16
|
258 *
|
Chris@16
|
259 * @li A substring of the streambuf's get area matches the regular expression.
|
Chris@16
|
260 *
|
Chris@16
|
261 * @li An error occurred.
|
Chris@16
|
262 *
|
Chris@16
|
263 * This operation is implemented in terms of zero or more calls to the stream's
|
Chris@16
|
264 * read_some function. If the streambuf's get area already contains data that
|
Chris@16
|
265 * matches the regular expression, the function returns immediately.
|
Chris@16
|
266 *
|
Chris@16
|
267 * @param s The stream from which the data is to be read. The type must support
|
Chris@16
|
268 * the SyncReadStream concept.
|
Chris@16
|
269 *
|
Chris@16
|
270 * @param b A streambuf object into which the data will be read.
|
Chris@16
|
271 *
|
Chris@16
|
272 * @param expr The regular expression.
|
Chris@16
|
273 *
|
Chris@16
|
274 * @returns The number of bytes in the streambuf's get area up to and including
|
Chris@16
|
275 * the substring that matches the regular expression.
|
Chris@16
|
276 *
|
Chris@16
|
277 * @throws boost::system::system_error Thrown on failure.
|
Chris@16
|
278 *
|
Chris@16
|
279 * @note After a successful read_until operation, the streambuf may contain
|
Chris@16
|
280 * additional data beyond that which matched the regular expression. An
|
Chris@16
|
281 * application will typically leave that data in the streambuf for a subsequent
|
Chris@16
|
282 * read_until operation to examine.
|
Chris@16
|
283 *
|
Chris@16
|
284 * @par Example
|
Chris@16
|
285 * To read data into a streambuf until a CR-LF sequence is encountered:
|
Chris@16
|
286 * @code boost::asio::streambuf b;
|
Chris@16
|
287 * boost::asio::read_until(s, b, boost::regex("\r\n"));
|
Chris@16
|
288 * std::istream is(&b);
|
Chris@16
|
289 * std::string line;
|
Chris@16
|
290 * std::getline(is, line); @endcode
|
Chris@16
|
291 * After the @c read_until operation completes successfully, the buffer @c b
|
Chris@16
|
292 * contains the data which matched the regular expression:
|
Chris@16
|
293 * @code { 'a', 'b', ..., 'c', '\r', '\n', 'd', 'e', ... } @endcode
|
Chris@16
|
294 * The call to @c std::getline then extracts the data up to and including the
|
Chris@16
|
295 * match, so that the string @c line contains:
|
Chris@16
|
296 * @code { 'a', 'b', ..., 'c', '\r', '\n' } @endcode
|
Chris@16
|
297 * The remaining data is left in the buffer @c b as follows:
|
Chris@16
|
298 * @code { 'd', 'e', ... } @endcode
|
Chris@16
|
299 * This data may be the start of a new line, to be extracted by a subsequent
|
Chris@16
|
300 * @c read_until operation.
|
Chris@16
|
301 */
|
Chris@16
|
302 template <typename SyncReadStream, typename Allocator>
|
Chris@16
|
303 std::size_t read_until(SyncReadStream& s,
|
Chris@16
|
304 boost::asio::basic_streambuf<Allocator>& b, const boost::regex& expr);
|
Chris@16
|
305
|
Chris@16
|
306 /// Read data into a streambuf until some part of the data it contains matches
|
Chris@16
|
307 /// a regular expression.
|
Chris@16
|
308 /**
|
Chris@16
|
309 * This function is used to read data into the specified streambuf until the
|
Chris@16
|
310 * streambuf's get area contains some data that matches a regular expression.
|
Chris@16
|
311 * The call will block until one of the following conditions is true:
|
Chris@16
|
312 *
|
Chris@16
|
313 * @li A substring of the streambuf's get area matches the regular expression.
|
Chris@16
|
314 *
|
Chris@16
|
315 * @li An error occurred.
|
Chris@16
|
316 *
|
Chris@16
|
317 * This operation is implemented in terms of zero or more calls to the stream's
|
Chris@16
|
318 * read_some function. If the streambuf's get area already contains data that
|
Chris@16
|
319 * matches the regular expression, the function returns immediately.
|
Chris@16
|
320 *
|
Chris@16
|
321 * @param s The stream from which the data is to be read. The type must support
|
Chris@16
|
322 * the SyncReadStream concept.
|
Chris@16
|
323 *
|
Chris@16
|
324 * @param b A streambuf object into which the data will be read.
|
Chris@16
|
325 *
|
Chris@16
|
326 * @param expr The regular expression.
|
Chris@16
|
327 *
|
Chris@16
|
328 * @param ec Set to indicate what error occurred, if any.
|
Chris@16
|
329 *
|
Chris@16
|
330 * @returns The number of bytes in the streambuf's get area up to and including
|
Chris@16
|
331 * the substring that matches the regular expression. Returns 0 if an error
|
Chris@16
|
332 * occurred.
|
Chris@16
|
333 *
|
Chris@16
|
334 * @note After a successful read_until operation, the streambuf may contain
|
Chris@16
|
335 * additional data beyond that which matched the regular expression. An
|
Chris@16
|
336 * application will typically leave that data in the streambuf for a subsequent
|
Chris@16
|
337 * read_until operation to examine.
|
Chris@16
|
338 */
|
Chris@16
|
339 template <typename SyncReadStream, typename Allocator>
|
Chris@16
|
340 std::size_t read_until(SyncReadStream& s,
|
Chris@16
|
341 boost::asio::basic_streambuf<Allocator>& b, const boost::regex& expr,
|
Chris@16
|
342 boost::system::error_code& ec);
|
Chris@16
|
343
|
Chris@16
|
344 #endif // defined(BOOST_ASIO_HAS_BOOST_REGEX)
|
Chris@16
|
345 // || defined(GENERATING_DOCUMENTATION)
|
Chris@16
|
346
|
Chris@16
|
347 /// Read data into a streambuf until a function object indicates a match.
|
Chris@16
|
348 /**
|
Chris@16
|
349 * This function is used to read data into the specified streambuf until a
|
Chris@16
|
350 * user-defined match condition function object, when applied to the data
|
Chris@16
|
351 * contained in the streambuf, indicates a successful match. The call will
|
Chris@16
|
352 * block until one of the following conditions is true:
|
Chris@16
|
353 *
|
Chris@16
|
354 * @li The match condition function object returns a std::pair where the second
|
Chris@16
|
355 * element evaluates to true.
|
Chris@16
|
356 *
|
Chris@16
|
357 * @li An error occurred.
|
Chris@16
|
358 *
|
Chris@16
|
359 * This operation is implemented in terms of zero or more calls to the stream's
|
Chris@16
|
360 * read_some function. If the match condition function object already indicates
|
Chris@16
|
361 * a match, the function returns immediately.
|
Chris@16
|
362 *
|
Chris@16
|
363 * @param s The stream from which the data is to be read. The type must support
|
Chris@16
|
364 * the SyncReadStream concept.
|
Chris@16
|
365 *
|
Chris@16
|
366 * @param b A streambuf object into which the data will be read.
|
Chris@16
|
367 *
|
Chris@16
|
368 * @param match_condition The function object to be called to determine whether
|
Chris@16
|
369 * a match exists. The signature of the function object must be:
|
Chris@16
|
370 * @code pair<iterator, bool> match_condition(iterator begin, iterator end);
|
Chris@16
|
371 * @endcode
|
Chris@16
|
372 * where @c iterator represents the type:
|
Chris@16
|
373 * @code buffers_iterator<basic_streambuf<Allocator>::const_buffers_type>
|
Chris@16
|
374 * @endcode
|
Chris@16
|
375 * The iterator parameters @c begin and @c end define the range of bytes to be
|
Chris@16
|
376 * scanned to determine whether there is a match. The @c first member of the
|
Chris@16
|
377 * return value is an iterator marking one-past-the-end of the bytes that have
|
Chris@16
|
378 * been consumed by the match function. This iterator is used to calculate the
|
Chris@16
|
379 * @c begin parameter for any subsequent invocation of the match condition. The
|
Chris@16
|
380 * @c second member of the return value is true if a match has been found, false
|
Chris@16
|
381 * otherwise.
|
Chris@16
|
382 *
|
Chris@16
|
383 * @returns The number of bytes in the streambuf's get area that have been fully
|
Chris@16
|
384 * consumed by the match function.
|
Chris@16
|
385 *
|
Chris@16
|
386 * @throws boost::system::system_error Thrown on failure.
|
Chris@16
|
387 *
|
Chris@16
|
388 * @note After a successful read_until operation, the streambuf may contain
|
Chris@16
|
389 * additional data beyond that which matched the function object. An application
|
Chris@16
|
390 * will typically leave that data in the streambuf for a subsequent
|
Chris@16
|
391 *
|
Chris@16
|
392 * @note The default implementation of the @c is_match_condition type trait
|
Chris@16
|
393 * evaluates to true for function pointers and function objects with a
|
Chris@16
|
394 * @c result_type typedef. It must be specialised for other user-defined
|
Chris@16
|
395 * function objects.
|
Chris@16
|
396 *
|
Chris@16
|
397 * @par Examples
|
Chris@16
|
398 * To read data into a streambuf until whitespace is encountered:
|
Chris@16
|
399 * @code typedef boost::asio::buffers_iterator<
|
Chris@16
|
400 * boost::asio::streambuf::const_buffers_type> iterator;
|
Chris@16
|
401 *
|
Chris@16
|
402 * std::pair<iterator, bool>
|
Chris@16
|
403 * match_whitespace(iterator begin, iterator end)
|
Chris@16
|
404 * {
|
Chris@16
|
405 * iterator i = begin;
|
Chris@16
|
406 * while (i != end)
|
Chris@16
|
407 * if (std::isspace(*i++))
|
Chris@16
|
408 * return std::make_pair(i, true);
|
Chris@16
|
409 * return std::make_pair(i, false);
|
Chris@16
|
410 * }
|
Chris@16
|
411 * ...
|
Chris@16
|
412 * boost::asio::streambuf b;
|
Chris@16
|
413 * boost::asio::read_until(s, b, match_whitespace);
|
Chris@16
|
414 * @endcode
|
Chris@16
|
415 *
|
Chris@16
|
416 * To read data into a streambuf until a matching character is found:
|
Chris@16
|
417 * @code class match_char
|
Chris@16
|
418 * {
|
Chris@16
|
419 * public:
|
Chris@16
|
420 * explicit match_char(char c) : c_(c) {}
|
Chris@16
|
421 *
|
Chris@16
|
422 * template <typename Iterator>
|
Chris@16
|
423 * std::pair<Iterator, bool> operator()(
|
Chris@16
|
424 * Iterator begin, Iterator end) const
|
Chris@16
|
425 * {
|
Chris@16
|
426 * Iterator i = begin;
|
Chris@16
|
427 * while (i != end)
|
Chris@16
|
428 * if (c_ == *i++)
|
Chris@16
|
429 * return std::make_pair(i, true);
|
Chris@16
|
430 * return std::make_pair(i, false);
|
Chris@16
|
431 * }
|
Chris@16
|
432 *
|
Chris@16
|
433 * private:
|
Chris@16
|
434 * char c_;
|
Chris@16
|
435 * };
|
Chris@16
|
436 *
|
Chris@16
|
437 * namespace asio {
|
Chris@16
|
438 * template <> struct is_match_condition<match_char>
|
Chris@16
|
439 * : public boost::true_type {};
|
Chris@16
|
440 * } // namespace asio
|
Chris@16
|
441 * ...
|
Chris@16
|
442 * boost::asio::streambuf b;
|
Chris@16
|
443 * boost::asio::read_until(s, b, match_char('a'));
|
Chris@16
|
444 * @endcode
|
Chris@16
|
445 */
|
Chris@16
|
446 template <typename SyncReadStream, typename Allocator, typename MatchCondition>
|
Chris@16
|
447 std::size_t read_until(SyncReadStream& s,
|
Chris@16
|
448 boost::asio::basic_streambuf<Allocator>& b, MatchCondition match_condition,
|
Chris@16
|
449 typename enable_if<is_match_condition<MatchCondition>::value>::type* = 0);
|
Chris@16
|
450
|
Chris@16
|
451 /// Read data into a streambuf until a function object indicates a match.
|
Chris@16
|
452 /**
|
Chris@16
|
453 * This function is used to read data into the specified streambuf until a
|
Chris@16
|
454 * user-defined match condition function object, when applied to the data
|
Chris@16
|
455 * contained in the streambuf, indicates a successful match. The call will
|
Chris@16
|
456 * block until one of the following conditions is true:
|
Chris@16
|
457 *
|
Chris@16
|
458 * @li The match condition function object returns a std::pair where the second
|
Chris@16
|
459 * element evaluates to true.
|
Chris@16
|
460 *
|
Chris@16
|
461 * @li An error occurred.
|
Chris@16
|
462 *
|
Chris@16
|
463 * This operation is implemented in terms of zero or more calls to the stream's
|
Chris@16
|
464 * read_some function. If the match condition function object already indicates
|
Chris@16
|
465 * a match, the function returns immediately.
|
Chris@16
|
466 *
|
Chris@16
|
467 * @param s The stream from which the data is to be read. The type must support
|
Chris@16
|
468 * the SyncReadStream concept.
|
Chris@16
|
469 *
|
Chris@16
|
470 * @param b A streambuf object into which the data will be read.
|
Chris@16
|
471 *
|
Chris@16
|
472 * @param match_condition The function object to be called to determine whether
|
Chris@16
|
473 * a match exists. The signature of the function object must be:
|
Chris@16
|
474 * @code pair<iterator, bool> match_condition(iterator begin, iterator end);
|
Chris@16
|
475 * @endcode
|
Chris@16
|
476 * where @c iterator represents the type:
|
Chris@16
|
477 * @code buffers_iterator<basic_streambuf<Allocator>::const_buffers_type>
|
Chris@16
|
478 * @endcode
|
Chris@16
|
479 * The iterator parameters @c begin and @c end define the range of bytes to be
|
Chris@16
|
480 * scanned to determine whether there is a match. The @c first member of the
|
Chris@16
|
481 * return value is an iterator marking one-past-the-end of the bytes that have
|
Chris@16
|
482 * been consumed by the match function. This iterator is used to calculate the
|
Chris@16
|
483 * @c begin parameter for any subsequent invocation of the match condition. The
|
Chris@16
|
484 * @c second member of the return value is true if a match has been found, false
|
Chris@16
|
485 * otherwise.
|
Chris@16
|
486 *
|
Chris@16
|
487 * @param ec Set to indicate what error occurred, if any.
|
Chris@16
|
488 *
|
Chris@16
|
489 * @returns The number of bytes in the streambuf's get area that have been fully
|
Chris@16
|
490 * consumed by the match function. Returns 0 if an error occurred.
|
Chris@16
|
491 *
|
Chris@16
|
492 * @note After a successful read_until operation, the streambuf may contain
|
Chris@16
|
493 * additional data beyond that which matched the function object. An application
|
Chris@16
|
494 * will typically leave that data in the streambuf for a subsequent
|
Chris@16
|
495 *
|
Chris@16
|
496 * @note The default implementation of the @c is_match_condition type trait
|
Chris@16
|
497 * evaluates to true for function pointers and function objects with a
|
Chris@16
|
498 * @c result_type typedef. It must be specialised for other user-defined
|
Chris@16
|
499 * function objects.
|
Chris@16
|
500 */
|
Chris@16
|
501 template <typename SyncReadStream, typename Allocator, typename MatchCondition>
|
Chris@16
|
502 std::size_t read_until(SyncReadStream& s,
|
Chris@16
|
503 boost::asio::basic_streambuf<Allocator>& b,
|
Chris@16
|
504 MatchCondition match_condition, boost::system::error_code& ec,
|
Chris@16
|
505 typename enable_if<is_match_condition<MatchCondition>::value>::type* = 0);
|
Chris@16
|
506
|
Chris@16
|
507 /*@}*/
|
Chris@16
|
508 /**
|
Chris@16
|
509 * @defgroup async_read_until boost::asio::async_read_until
|
Chris@16
|
510 *
|
Chris@16
|
511 * @brief Start an asynchronous operation to read data into a streambuf until it
|
Chris@16
|
512 * contains a delimiter, matches a regular expression, or a function object
|
Chris@16
|
513 * indicates a match.
|
Chris@16
|
514 */
|
Chris@16
|
515 /*@{*/
|
Chris@16
|
516
|
Chris@16
|
517 /// Start an asynchronous operation to read data into a streambuf until it
|
Chris@16
|
518 /// contains a specified delimiter.
|
Chris@16
|
519 /**
|
Chris@16
|
520 * This function is used to asynchronously read data into the specified
|
Chris@16
|
521 * streambuf until the streambuf's get area contains the specified delimiter.
|
Chris@16
|
522 * The function call always returns immediately. The asynchronous operation
|
Chris@16
|
523 * will continue until one of the following conditions is true:
|
Chris@16
|
524 *
|
Chris@16
|
525 * @li The get area of the streambuf contains the specified delimiter.
|
Chris@16
|
526 *
|
Chris@16
|
527 * @li An error occurred.
|
Chris@16
|
528 *
|
Chris@16
|
529 * This operation is implemented in terms of zero or more calls to the stream's
|
Chris@16
|
530 * async_read_some function, and is known as a <em>composed operation</em>. If
|
Chris@16
|
531 * the streambuf's get area already contains the delimiter, this asynchronous
|
Chris@16
|
532 * operation completes immediately. The program must ensure that the stream
|
Chris@16
|
533 * performs no other read operations (such as async_read, async_read_until, the
|
Chris@16
|
534 * stream's async_read_some function, or any other composed operations that
|
Chris@16
|
535 * perform reads) until this operation completes.
|
Chris@16
|
536 *
|
Chris@16
|
537 * @param s The stream from which the data is to be read. The type must support
|
Chris@16
|
538 * the AsyncReadStream concept.
|
Chris@16
|
539 *
|
Chris@16
|
540 * @param b A streambuf object into which the data will be read. Ownership of
|
Chris@16
|
541 * the streambuf is retained by the caller, which must guarantee that it remains
|
Chris@16
|
542 * valid until the handler is called.
|
Chris@16
|
543 *
|
Chris@16
|
544 * @param delim The delimiter character.
|
Chris@16
|
545 *
|
Chris@16
|
546 * @param handler The handler to be called when the read operation completes.
|
Chris@16
|
547 * Copies will be made of the handler as required. The function signature of the
|
Chris@16
|
548 * handler must be:
|
Chris@16
|
549 * @code void handler(
|
Chris@16
|
550 * // Result of operation.
|
Chris@16
|
551 * const boost::system::error_code& error,
|
Chris@16
|
552 *
|
Chris@16
|
553 * // The number of bytes in the streambuf's get
|
Chris@16
|
554 * // area up to and including the delimiter.
|
Chris@16
|
555 * // 0 if an error occurred.
|
Chris@16
|
556 * std::size_t bytes_transferred
|
Chris@16
|
557 * ); @endcode
|
Chris@16
|
558 * Regardless of whether the asynchronous operation completes immediately or
|
Chris@16
|
559 * not, the handler will not be invoked from within this function. Invocation of
|
Chris@16
|
560 * the handler will be performed in a manner equivalent to using
|
Chris@16
|
561 * boost::asio::io_service::post().
|
Chris@16
|
562 *
|
Chris@16
|
563 * @note After a successful async_read_until operation, the streambuf may
|
Chris@16
|
564 * contain additional data beyond the delimiter. An application will typically
|
Chris@16
|
565 * leave that data in the streambuf for a subsequent async_read_until operation
|
Chris@16
|
566 * to examine.
|
Chris@16
|
567 *
|
Chris@16
|
568 * @par Example
|
Chris@16
|
569 * To asynchronously read data into a streambuf until a newline is encountered:
|
Chris@16
|
570 * @code boost::asio::streambuf b;
|
Chris@16
|
571 * ...
|
Chris@16
|
572 * void handler(const boost::system::error_code& e, std::size_t size)
|
Chris@16
|
573 * {
|
Chris@16
|
574 * if (!e)
|
Chris@16
|
575 * {
|
Chris@16
|
576 * std::istream is(&b);
|
Chris@16
|
577 * std::string line;
|
Chris@16
|
578 * std::getline(is, line);
|
Chris@16
|
579 * ...
|
Chris@16
|
580 * }
|
Chris@16
|
581 * }
|
Chris@16
|
582 * ...
|
Chris@16
|
583 * boost::asio::async_read_until(s, b, '\n', handler); @endcode
|
Chris@16
|
584 * After the @c async_read_until operation completes successfully, the buffer
|
Chris@16
|
585 * @c b contains the delimiter:
|
Chris@16
|
586 * @code { 'a', 'b', ..., 'c', '\n', 'd', 'e', ... } @endcode
|
Chris@16
|
587 * The call to @c std::getline then extracts the data up to and including the
|
Chris@16
|
588 * delimiter, so that the string @c line contains:
|
Chris@16
|
589 * @code { 'a', 'b', ..., 'c', '\n' } @endcode
|
Chris@16
|
590 * The remaining data is left in the buffer @c b as follows:
|
Chris@16
|
591 * @code { 'd', 'e', ... } @endcode
|
Chris@16
|
592 * This data may be the start of a new line, to be extracted by a subsequent
|
Chris@16
|
593 * @c async_read_until operation.
|
Chris@16
|
594 */
|
Chris@16
|
595 template <typename AsyncReadStream, typename Allocator, typename ReadHandler>
|
Chris@16
|
596 BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
|
Chris@16
|
597 void (boost::system::error_code, std::size_t))
|
Chris@16
|
598 async_read_until(AsyncReadStream& s,
|
Chris@16
|
599 boost::asio::basic_streambuf<Allocator>& b,
|
Chris@16
|
600 char delim, BOOST_ASIO_MOVE_ARG(ReadHandler) handler);
|
Chris@16
|
601
|
Chris@16
|
602 /// Start an asynchronous operation to read data into a streambuf until it
|
Chris@16
|
603 /// contains a specified delimiter.
|
Chris@16
|
604 /**
|
Chris@16
|
605 * This function is used to asynchronously read data into the specified
|
Chris@16
|
606 * streambuf until the streambuf's get area contains the specified delimiter.
|
Chris@16
|
607 * The function call always returns immediately. The asynchronous operation
|
Chris@16
|
608 * will continue until one of the following conditions is true:
|
Chris@16
|
609 *
|
Chris@16
|
610 * @li The get area of the streambuf contains the specified delimiter.
|
Chris@16
|
611 *
|
Chris@16
|
612 * @li An error occurred.
|
Chris@16
|
613 *
|
Chris@16
|
614 * This operation is implemented in terms of zero or more calls to the stream's
|
Chris@16
|
615 * async_read_some function, and is known as a <em>composed operation</em>. If
|
Chris@16
|
616 * the streambuf's get area already contains the delimiter, this asynchronous
|
Chris@16
|
617 * operation completes immediately. The program must ensure that the stream
|
Chris@16
|
618 * performs no other read operations (such as async_read, async_read_until, the
|
Chris@16
|
619 * stream's async_read_some function, or any other composed operations that
|
Chris@16
|
620 * perform reads) until this operation completes.
|
Chris@16
|
621 *
|
Chris@16
|
622 * @param s The stream from which the data is to be read. The type must support
|
Chris@16
|
623 * the AsyncReadStream concept.
|
Chris@16
|
624 *
|
Chris@16
|
625 * @param b A streambuf object into which the data will be read. Ownership of
|
Chris@16
|
626 * the streambuf is retained by the caller, which must guarantee that it remains
|
Chris@16
|
627 * valid until the handler is called.
|
Chris@16
|
628 *
|
Chris@16
|
629 * @param delim The delimiter string.
|
Chris@16
|
630 *
|
Chris@16
|
631 * @param handler The handler to be called when the read operation completes.
|
Chris@16
|
632 * Copies will be made of the handler as required. The function signature of the
|
Chris@16
|
633 * handler must be:
|
Chris@16
|
634 * @code void handler(
|
Chris@16
|
635 * // Result of operation.
|
Chris@16
|
636 * const boost::system::error_code& error,
|
Chris@16
|
637 *
|
Chris@16
|
638 * // The number of bytes in the streambuf's get
|
Chris@16
|
639 * // area up to and including the delimiter.
|
Chris@16
|
640 * // 0 if an error occurred.
|
Chris@16
|
641 * std::size_t bytes_transferred
|
Chris@16
|
642 * ); @endcode
|
Chris@16
|
643 * Regardless of whether the asynchronous operation completes immediately or
|
Chris@16
|
644 * not, the handler will not be invoked from within this function. Invocation of
|
Chris@16
|
645 * the handler will be performed in a manner equivalent to using
|
Chris@16
|
646 * boost::asio::io_service::post().
|
Chris@16
|
647 *
|
Chris@16
|
648 * @note After a successful async_read_until operation, the streambuf may
|
Chris@16
|
649 * contain additional data beyond the delimiter. An application will typically
|
Chris@16
|
650 * leave that data in the streambuf for a subsequent async_read_until operation
|
Chris@16
|
651 * to examine.
|
Chris@16
|
652 *
|
Chris@16
|
653 * @par Example
|
Chris@16
|
654 * To asynchronously read data into a streambuf until a newline is encountered:
|
Chris@16
|
655 * @code boost::asio::streambuf b;
|
Chris@16
|
656 * ...
|
Chris@16
|
657 * void handler(const boost::system::error_code& e, std::size_t size)
|
Chris@16
|
658 * {
|
Chris@16
|
659 * if (!e)
|
Chris@16
|
660 * {
|
Chris@16
|
661 * std::istream is(&b);
|
Chris@16
|
662 * std::string line;
|
Chris@16
|
663 * std::getline(is, line);
|
Chris@16
|
664 * ...
|
Chris@16
|
665 * }
|
Chris@16
|
666 * }
|
Chris@16
|
667 * ...
|
Chris@16
|
668 * boost::asio::async_read_until(s, b, "\r\n", handler); @endcode
|
Chris@16
|
669 * After the @c async_read_until operation completes successfully, the buffer
|
Chris@16
|
670 * @c b contains the delimiter:
|
Chris@16
|
671 * @code { 'a', 'b', ..., 'c', '\r', '\n', 'd', 'e', ... } @endcode
|
Chris@16
|
672 * The call to @c std::getline then extracts the data up to and including the
|
Chris@16
|
673 * delimiter, so that the string @c line contains:
|
Chris@16
|
674 * @code { 'a', 'b', ..., 'c', '\r', '\n' } @endcode
|
Chris@16
|
675 * The remaining data is left in the buffer @c b as follows:
|
Chris@16
|
676 * @code { 'd', 'e', ... } @endcode
|
Chris@16
|
677 * This data may be the start of a new line, to be extracted by a subsequent
|
Chris@16
|
678 * @c async_read_until operation.
|
Chris@16
|
679 */
|
Chris@16
|
680 template <typename AsyncReadStream, typename Allocator, typename ReadHandler>
|
Chris@16
|
681 BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
|
Chris@16
|
682 void (boost::system::error_code, std::size_t))
|
Chris@16
|
683 async_read_until(AsyncReadStream& s,
|
Chris@16
|
684 boost::asio::basic_streambuf<Allocator>& b, const std::string& delim,
|
Chris@16
|
685 BOOST_ASIO_MOVE_ARG(ReadHandler) handler);
|
Chris@16
|
686
|
Chris@16
|
687 #if defined(BOOST_ASIO_HAS_BOOST_REGEX) \
|
Chris@16
|
688 || defined(GENERATING_DOCUMENTATION)
|
Chris@16
|
689
|
Chris@16
|
690 /// Start an asynchronous operation to read data into a streambuf until some
|
Chris@16
|
691 /// part of its data matches a regular expression.
|
Chris@16
|
692 /**
|
Chris@16
|
693 * This function is used to asynchronously read data into the specified
|
Chris@16
|
694 * streambuf until the streambuf's get area contains some data that matches a
|
Chris@16
|
695 * regular expression. The function call always returns immediately. The
|
Chris@16
|
696 * asynchronous operation will continue until one of the following conditions
|
Chris@16
|
697 * is true:
|
Chris@16
|
698 *
|
Chris@16
|
699 * @li A substring of the streambuf's get area matches the regular expression.
|
Chris@16
|
700 *
|
Chris@16
|
701 * @li An error occurred.
|
Chris@16
|
702 *
|
Chris@16
|
703 * This operation is implemented in terms of zero or more calls to the stream's
|
Chris@16
|
704 * async_read_some function, and is known as a <em>composed operation</em>. If
|
Chris@16
|
705 * the streambuf's get area already contains data that matches the regular
|
Chris@16
|
706 * expression, this asynchronous operation completes immediately. The program
|
Chris@16
|
707 * must ensure that the stream performs no other read operations (such as
|
Chris@16
|
708 * async_read, async_read_until, the stream's async_read_some function, or any
|
Chris@16
|
709 * other composed operations that perform reads) until this operation
|
Chris@16
|
710 * completes.
|
Chris@16
|
711 *
|
Chris@16
|
712 * @param s The stream from which the data is to be read. The type must support
|
Chris@16
|
713 * the AsyncReadStream concept.
|
Chris@16
|
714 *
|
Chris@16
|
715 * @param b A streambuf object into which the data will be read. Ownership of
|
Chris@16
|
716 * the streambuf is retained by the caller, which must guarantee that it remains
|
Chris@16
|
717 * valid until the handler is called.
|
Chris@16
|
718 *
|
Chris@16
|
719 * @param expr The regular expression.
|
Chris@16
|
720 *
|
Chris@16
|
721 * @param handler The handler to be called when the read operation completes.
|
Chris@16
|
722 * Copies will be made of the handler as required. The function signature of the
|
Chris@16
|
723 * handler must be:
|
Chris@16
|
724 * @code void handler(
|
Chris@16
|
725 * // Result of operation.
|
Chris@16
|
726 * const boost::system::error_code& error,
|
Chris@16
|
727 *
|
Chris@16
|
728 * // The number of bytes in the streambuf's get
|
Chris@16
|
729 * // area up to and including the substring
|
Chris@16
|
730 * // that matches the regular. expression.
|
Chris@16
|
731 * // 0 if an error occurred.
|
Chris@16
|
732 * std::size_t bytes_transferred
|
Chris@16
|
733 * ); @endcode
|
Chris@16
|
734 * Regardless of whether the asynchronous operation completes immediately or
|
Chris@16
|
735 * not, the handler will not be invoked from within this function. Invocation of
|
Chris@16
|
736 * the handler will be performed in a manner equivalent to using
|
Chris@16
|
737 * boost::asio::io_service::post().
|
Chris@16
|
738 *
|
Chris@16
|
739 * @note After a successful async_read_until operation, the streambuf may
|
Chris@16
|
740 * contain additional data beyond that which matched the regular expression. An
|
Chris@16
|
741 * application will typically leave that data in the streambuf for a subsequent
|
Chris@16
|
742 * async_read_until operation to examine.
|
Chris@16
|
743 *
|
Chris@16
|
744 * @par Example
|
Chris@16
|
745 * To asynchronously read data into a streambuf until a CR-LF sequence is
|
Chris@16
|
746 * encountered:
|
Chris@16
|
747 * @code boost::asio::streambuf b;
|
Chris@16
|
748 * ...
|
Chris@16
|
749 * void handler(const boost::system::error_code& e, std::size_t size)
|
Chris@16
|
750 * {
|
Chris@16
|
751 * if (!e)
|
Chris@16
|
752 * {
|
Chris@16
|
753 * std::istream is(&b);
|
Chris@16
|
754 * std::string line;
|
Chris@16
|
755 * std::getline(is, line);
|
Chris@16
|
756 * ...
|
Chris@16
|
757 * }
|
Chris@16
|
758 * }
|
Chris@16
|
759 * ...
|
Chris@16
|
760 * boost::asio::async_read_until(s, b, boost::regex("\r\n"), handler); @endcode
|
Chris@16
|
761 * After the @c async_read_until operation completes successfully, the buffer
|
Chris@16
|
762 * @c b contains the data which matched the regular expression:
|
Chris@16
|
763 * @code { 'a', 'b', ..., 'c', '\r', '\n', 'd', 'e', ... } @endcode
|
Chris@16
|
764 * The call to @c std::getline then extracts the data up to and including the
|
Chris@16
|
765 * match, so that the string @c line contains:
|
Chris@16
|
766 * @code { 'a', 'b', ..., 'c', '\r', '\n' } @endcode
|
Chris@16
|
767 * The remaining data is left in the buffer @c b as follows:
|
Chris@16
|
768 * @code { 'd', 'e', ... } @endcode
|
Chris@16
|
769 * This data may be the start of a new line, to be extracted by a subsequent
|
Chris@16
|
770 * @c async_read_until operation.
|
Chris@16
|
771 */
|
Chris@16
|
772 template <typename AsyncReadStream, typename Allocator, typename ReadHandler>
|
Chris@16
|
773 BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
|
Chris@16
|
774 void (boost::system::error_code, std::size_t))
|
Chris@16
|
775 async_read_until(AsyncReadStream& s,
|
Chris@16
|
776 boost::asio::basic_streambuf<Allocator>& b, const boost::regex& expr,
|
Chris@16
|
777 BOOST_ASIO_MOVE_ARG(ReadHandler) handler);
|
Chris@16
|
778
|
Chris@16
|
779 #endif // defined(BOOST_ASIO_HAS_BOOST_REGEX)
|
Chris@16
|
780 // || defined(GENERATING_DOCUMENTATION)
|
Chris@16
|
781
|
Chris@16
|
782 /// Start an asynchronous operation to read data into a streambuf until a
|
Chris@16
|
783 /// function object indicates a match.
|
Chris@16
|
784 /**
|
Chris@16
|
785 * This function is used to asynchronously read data into the specified
|
Chris@16
|
786 * streambuf until a user-defined match condition function object, when applied
|
Chris@16
|
787 * to the data contained in the streambuf, indicates a successful match. The
|
Chris@16
|
788 * function call always returns immediately. The asynchronous operation will
|
Chris@16
|
789 * continue until one of the following conditions is true:
|
Chris@16
|
790 *
|
Chris@16
|
791 * @li The match condition function object returns a std::pair where the second
|
Chris@16
|
792 * element evaluates to true.
|
Chris@16
|
793 *
|
Chris@16
|
794 * @li An error occurred.
|
Chris@16
|
795 *
|
Chris@16
|
796 * This operation is implemented in terms of zero or more calls to the stream's
|
Chris@16
|
797 * async_read_some function, and is known as a <em>composed operation</em>. If
|
Chris@16
|
798 * the match condition function object already indicates a match, this
|
Chris@16
|
799 * asynchronous operation completes immediately. The program must ensure that
|
Chris@16
|
800 * the stream performs no other read operations (such as async_read,
|
Chris@16
|
801 * async_read_until, the stream's async_read_some function, or any other
|
Chris@16
|
802 * composed operations that perform reads) until this operation completes.
|
Chris@16
|
803 *
|
Chris@16
|
804 * @param s The stream from which the data is to be read. The type must support
|
Chris@16
|
805 * the AsyncReadStream concept.
|
Chris@16
|
806 *
|
Chris@16
|
807 * @param b A streambuf object into which the data will be read.
|
Chris@16
|
808 *
|
Chris@16
|
809 * @param match_condition The function object to be called to determine whether
|
Chris@16
|
810 * a match exists. The signature of the function object must be:
|
Chris@16
|
811 * @code pair<iterator, bool> match_condition(iterator begin, iterator end);
|
Chris@16
|
812 * @endcode
|
Chris@16
|
813 * where @c iterator represents the type:
|
Chris@16
|
814 * @code buffers_iterator<basic_streambuf<Allocator>::const_buffers_type>
|
Chris@16
|
815 * @endcode
|
Chris@16
|
816 * The iterator parameters @c begin and @c end define the range of bytes to be
|
Chris@16
|
817 * scanned to determine whether there is a match. The @c first member of the
|
Chris@16
|
818 * return value is an iterator marking one-past-the-end of the bytes that have
|
Chris@16
|
819 * been consumed by the match function. This iterator is used to calculate the
|
Chris@16
|
820 * @c begin parameter for any subsequent invocation of the match condition. The
|
Chris@16
|
821 * @c second member of the return value is true if a match has been found, false
|
Chris@16
|
822 * otherwise.
|
Chris@16
|
823 *
|
Chris@16
|
824 * @param handler The handler to be called when the read operation completes.
|
Chris@16
|
825 * Copies will be made of the handler as required. The function signature of the
|
Chris@16
|
826 * handler must be:
|
Chris@16
|
827 * @code void handler(
|
Chris@16
|
828 * // Result of operation.
|
Chris@16
|
829 * const boost::system::error_code& error,
|
Chris@16
|
830 *
|
Chris@16
|
831 * // The number of bytes in the streambuf's get
|
Chris@16
|
832 * // area that have been fully consumed by the
|
Chris@16
|
833 * // match function. O if an error occurred.
|
Chris@16
|
834 * std::size_t bytes_transferred
|
Chris@16
|
835 * ); @endcode
|
Chris@16
|
836 * Regardless of whether the asynchronous operation completes immediately or
|
Chris@16
|
837 * not, the handler will not be invoked from within this function. Invocation of
|
Chris@16
|
838 * the handler will be performed in a manner equivalent to using
|
Chris@16
|
839 * boost::asio::io_service::post().
|
Chris@16
|
840 *
|
Chris@16
|
841 * @note After a successful async_read_until operation, the streambuf may
|
Chris@16
|
842 * contain additional data beyond that which matched the function object. An
|
Chris@16
|
843 * application will typically leave that data in the streambuf for a subsequent
|
Chris@16
|
844 * async_read_until operation to examine.
|
Chris@16
|
845 *
|
Chris@16
|
846 * @note The default implementation of the @c is_match_condition type trait
|
Chris@16
|
847 * evaluates to true for function pointers and function objects with a
|
Chris@16
|
848 * @c result_type typedef. It must be specialised for other user-defined
|
Chris@16
|
849 * function objects.
|
Chris@16
|
850 *
|
Chris@16
|
851 * @par Examples
|
Chris@16
|
852 * To asynchronously read data into a streambuf until whitespace is encountered:
|
Chris@16
|
853 * @code typedef boost::asio::buffers_iterator<
|
Chris@16
|
854 * boost::asio::streambuf::const_buffers_type> iterator;
|
Chris@16
|
855 *
|
Chris@16
|
856 * std::pair<iterator, bool>
|
Chris@16
|
857 * match_whitespace(iterator begin, iterator end)
|
Chris@16
|
858 * {
|
Chris@16
|
859 * iterator i = begin;
|
Chris@16
|
860 * while (i != end)
|
Chris@16
|
861 * if (std::isspace(*i++))
|
Chris@16
|
862 * return std::make_pair(i, true);
|
Chris@16
|
863 * return std::make_pair(i, false);
|
Chris@16
|
864 * }
|
Chris@16
|
865 * ...
|
Chris@16
|
866 * void handler(const boost::system::error_code& e, std::size_t size);
|
Chris@16
|
867 * ...
|
Chris@16
|
868 * boost::asio::streambuf b;
|
Chris@16
|
869 * boost::asio::async_read_until(s, b, match_whitespace, handler);
|
Chris@16
|
870 * @endcode
|
Chris@16
|
871 *
|
Chris@16
|
872 * To asynchronously read data into a streambuf until a matching character is
|
Chris@16
|
873 * found:
|
Chris@16
|
874 * @code class match_char
|
Chris@16
|
875 * {
|
Chris@16
|
876 * public:
|
Chris@16
|
877 * explicit match_char(char c) : c_(c) {}
|
Chris@16
|
878 *
|
Chris@16
|
879 * template <typename Iterator>
|
Chris@16
|
880 * std::pair<Iterator, bool> operator()(
|
Chris@16
|
881 * Iterator begin, Iterator end) const
|
Chris@16
|
882 * {
|
Chris@16
|
883 * Iterator i = begin;
|
Chris@16
|
884 * while (i != end)
|
Chris@16
|
885 * if (c_ == *i++)
|
Chris@16
|
886 * return std::make_pair(i, true);
|
Chris@16
|
887 * return std::make_pair(i, false);
|
Chris@16
|
888 * }
|
Chris@16
|
889 *
|
Chris@16
|
890 * private:
|
Chris@16
|
891 * char c_;
|
Chris@16
|
892 * };
|
Chris@16
|
893 *
|
Chris@16
|
894 * namespace asio {
|
Chris@16
|
895 * template <> struct is_match_condition<match_char>
|
Chris@16
|
896 * : public boost::true_type {};
|
Chris@16
|
897 * } // namespace asio
|
Chris@16
|
898 * ...
|
Chris@16
|
899 * void handler(const boost::system::error_code& e, std::size_t size);
|
Chris@16
|
900 * ...
|
Chris@16
|
901 * boost::asio::streambuf b;
|
Chris@16
|
902 * boost::asio::async_read_until(s, b, match_char('a'), handler);
|
Chris@16
|
903 * @endcode
|
Chris@16
|
904 */
|
Chris@16
|
905 template <typename AsyncReadStream, typename Allocator,
|
Chris@16
|
906 typename MatchCondition, typename ReadHandler>
|
Chris@16
|
907 BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
|
Chris@16
|
908 void (boost::system::error_code, std::size_t))
|
Chris@16
|
909 async_read_until(AsyncReadStream& s,
|
Chris@16
|
910 boost::asio::basic_streambuf<Allocator>& b,
|
Chris@16
|
911 MatchCondition match_condition, BOOST_ASIO_MOVE_ARG(ReadHandler) handler,
|
Chris@16
|
912 typename enable_if<is_match_condition<MatchCondition>::value>::type* = 0);
|
Chris@16
|
913
|
Chris@16
|
914 /*@}*/
|
Chris@16
|
915
|
Chris@16
|
916 } // namespace asio
|
Chris@16
|
917 } // namespace boost
|
Chris@16
|
918
|
Chris@16
|
919 #include <boost/asio/detail/pop_options.hpp>
|
Chris@16
|
920
|
Chris@16
|
921 #include <boost/asio/impl/read_until.hpp>
|
Chris@16
|
922
|
Chris@16
|
923 #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
|
Chris@16
|
924
|
Chris@16
|
925 #endif // BOOST_ASIO_READ_UNTIL_HPP
|