]>
Commit | Line | Data |
---|---|---|
b32b8144 FG |
1 | // |
2 | // read_until.hpp | |
3 | // ~~~~~~~~~~~~~~ | |
4 | // | |
11fdf7f2 | 5 | // Copyright (c) 2003-2018 Christopher M. Kohlhoff (chris at kohlhoff dot com) |
b32b8144 FG |
6 | // |
7 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |
8 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
9 | // | |
10 | ||
11 | #ifndef BOOST_ASIO_READ_UNTIL_HPP | |
12 | #define BOOST_ASIO_READ_UNTIL_HPP | |
13 | ||
14 | #if defined(_MSC_VER) && (_MSC_VER >= 1200) | |
15 | # pragma once | |
16 | #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) | |
17 | ||
18 | #include <boost/asio/detail/config.hpp> | |
19 | #include <cstddef> | |
20 | #include <string> | |
21 | #include <boost/asio/async_result.hpp> | |
22 | #include <boost/asio/detail/regex_fwd.hpp> | |
23 | #include <boost/asio/detail/string_view.hpp> | |
24 | #include <boost/asio/detail/type_traits.hpp> | |
25 | #include <boost/asio/error.hpp> | |
26 | ||
27 | #if !defined(BOOST_ASIO_NO_EXTENSIONS) | |
28 | # include <boost/asio/basic_streambuf_fwd.hpp> | |
29 | #endif // !defined(BOOST_ASIO_NO_EXTENSIONS) | |
30 | ||
31 | #include <boost/asio/detail/push_options.hpp> | |
32 | ||
33 | namespace boost { | |
34 | namespace asio { | |
35 | ||
36 | namespace detail | |
37 | { | |
38 | char (&has_result_type_helper(...))[2]; | |
39 | ||
40 | template <typename T> | |
41 | char has_result_type_helper(T*, typename T::result_type* = 0); | |
42 | ||
43 | template <typename T> | |
44 | struct has_result_type | |
45 | { | |
46 | enum { value = (sizeof((has_result_type_helper)((T*)(0))) == 1) }; | |
47 | }; | |
48 | } // namespace detail | |
49 | ||
50 | /// Type trait used to determine whether a type can be used as a match condition | |
51 | /// function with read_until and async_read_until. | |
52 | template <typename T> | |
53 | struct is_match_condition | |
54 | { | |
55 | #if defined(GENERATING_DOCUMENTATION) | |
56 | /// The value member is true if the type may be used as a match condition. | |
57 | static const bool value; | |
58 | #else | |
59 | enum | |
60 | { | |
61 | value = boost::asio::is_function< | |
62 | typename boost::asio::remove_pointer<T>::type>::value | |
63 | || detail::has_result_type<T>::value | |
64 | }; | |
65 | #endif | |
66 | }; | |
67 | ||
68 | /** | |
69 | * @defgroup read_until boost::asio::read_until | |
70 | * | |
71 | * @brief Read data into a dynamic buffer sequence, or into a streambuf, until | |
72 | * it contains a delimiter, matches a regular expression, or a function object | |
73 | * indicates a match. | |
74 | */ | |
75 | /*@{*/ | |
76 | ||
77 | /// Read data into a dynamic buffer sequence until it contains a specified | |
78 | /// delimiter. | |
79 | /** | |
80 | * This function is used to read data into the specified dynamic buffer | |
81 | * sequence until the dynamic buffer sequence's get area contains the specified | |
82 | * delimiter. The call will block until one of the following conditions is | |
83 | * true: | |
84 | * | |
85 | * @li The get area of the dynamic buffer sequence contains the specified | |
86 | * delimiter. | |
87 | * | |
88 | * @li An error occurred. | |
89 | * | |
90 | * This operation is implemented in terms of zero or more calls to the stream's | |
91 | * read_some function. If the dynamic buffer sequence's get area already | |
92 | * contains the delimiter, the function returns immediately. | |
93 | * | |
94 | * @param s The stream from which the data is to be read. The type must support | |
95 | * the SyncReadStream concept. | |
96 | * | |
97 | * @param buffers The dynamic buffer sequence into which the data will be read. | |
98 | * | |
99 | * @param delim The delimiter character. | |
100 | * | |
101 | * @returns The number of bytes in the dynamic buffer sequence's get area up to | |
102 | * and including the delimiter. | |
103 | * | |
104 | * @throws boost::system::system_error Thrown on failure. | |
105 | * | |
106 | * @note After a successful read_until operation, the dynamic buffer sequence | |
107 | * may contain additional data beyond the delimiter. An application will | |
108 | * typically leave that data in the dynamic buffer sequence for a subsequent | |
109 | * read_until operation to examine. | |
110 | * | |
111 | * @par Example | |
112 | * To read data into a @c std::string until a newline is encountered: | |
113 | * @code std::string data; | |
114 | * std::string n = boost::asio::read_until(s, | |
115 | * boost::asio::dynamic_buffer(data), '\n'); | |
116 | * std::string line = data.substr(0, n); | |
117 | * data.erase(0, n); @endcode | |
118 | * After the @c read_until operation completes successfully, the string @c data | |
119 | * contains the delimiter: | |
120 | * @code { 'a', 'b', ..., 'c', '\n', 'd', 'e', ... } @endcode | |
121 | * The call to @c substr then extracts the data up to and including the | |
122 | * delimiter, so that the string @c line contains: | |
123 | * @code { 'a', 'b', ..., 'c', '\n' } @endcode | |
124 | * After the call to @c erase, the remaining data is left in the buffer @c b as | |
125 | * follows: | |
126 | * @code { 'd', 'e', ... } @endcode | |
127 | * This data may be the start of a new line, to be extracted by a subsequent | |
128 | * @c read_until operation. | |
129 | */ | |
130 | template <typename SyncReadStream, typename DynamicBuffer> | |
131 | std::size_t read_until(SyncReadStream& s, | |
132 | BOOST_ASIO_MOVE_ARG(DynamicBuffer) buffers, char delim); | |
133 | ||
134 | /// Read data into a dynamic buffer sequence until it contains a specified | |
135 | /// delimiter. | |
136 | /** | |
137 | * This function is used to read data into the specified dynamic buffer | |
138 | * sequence until the dynamic buffer sequence's get area contains the specified | |
139 | * delimiter. The call will block until one of the following conditions is | |
140 | * true: | |
141 | * | |
142 | * @li The get area of the dynamic buffer sequence contains the specified | |
143 | * delimiter. | |
144 | * | |
145 | * @li An error occurred. | |
146 | * | |
147 | * This operation is implemented in terms of zero or more calls to the stream's | |
148 | * read_some function. If the dynamic buffer sequence's get area already | |
149 | * contains the delimiter, the function returns immediately. | |
150 | * | |
151 | * @param s The stream from which the data is to be read. The type must support | |
152 | * the SyncReadStream concept. | |
153 | * | |
154 | * @param buffers The dynamic buffer sequence into which the data will be read. | |
155 | * | |
156 | * @param delim The delimiter character. | |
157 | * | |
158 | * @param ec Set to indicate what error occurred, if any. | |
159 | * | |
160 | * @returns The number of bytes in the dynamic buffer sequence's get area up to | |
161 | * and including the delimiter. Returns 0 if an error occurred. | |
162 | * | |
163 | * @note After a successful read_until operation, the dynamic buffer sequence | |
164 | * may contain additional data beyond the delimiter. An application will | |
165 | * typically leave that data in the dynamic buffer sequence for a subsequent | |
166 | * read_until operation to examine. | |
167 | */ | |
168 | template <typename SyncReadStream, typename DynamicBuffer> | |
169 | std::size_t read_until(SyncReadStream& s, | |
170 | BOOST_ASIO_MOVE_ARG(DynamicBuffer) buffers, | |
171 | char delim, boost::system::error_code& ec); | |
172 | ||
173 | /// Read data into a dynamic buffer sequence until it contains a specified | |
174 | /// delimiter. | |
175 | /** | |
176 | * This function is used to read data into the specified dynamic buffer | |
177 | * sequence until the dynamic buffer sequence's get area contains the specified | |
178 | * delimiter. The call will block until one of the following conditions is | |
179 | * true: | |
180 | * | |
181 | * @li The get area of the dynamic buffer sequence contains the specified | |
182 | * delimiter. | |
183 | * | |
184 | * @li An error occurred. | |
185 | * | |
186 | * This operation is implemented in terms of zero or more calls to the stream's | |
187 | * read_some function. If the dynamic buffer sequence's get area already | |
188 | * contains the delimiter, the function returns immediately. | |
189 | * | |
190 | * @param s The stream from which the data is to be read. The type must support | |
191 | * the SyncReadStream concept. | |
192 | * | |
193 | * @param buffers The dynamic buffer sequence into which the data will be read. | |
194 | * | |
195 | * @param delim The delimiter string. | |
196 | * | |
197 | * @returns The number of bytes in the dynamic buffer sequence's get area up to | |
198 | * and including the delimiter. | |
199 | * | |
200 | * @note After a successful read_until operation, the dynamic buffer sequence | |
201 | * may contain additional data beyond the delimiter. An application will | |
202 | * typically leave that data in the dynamic buffer sequence for a subsequent | |
203 | * read_until operation to examine. | |
204 | * | |
205 | * @par Example | |
206 | * To read data into a @c std::string until a CR-LF sequence is encountered: | |
207 | * @code std::string data; | |
208 | * std::string n = boost::asio::read_until(s, | |
209 | * boost::asio::dynamic_buffer(data), "\r\n"); | |
210 | * std::string line = data.substr(0, n); | |
211 | * data.erase(0, n); @endcode | |
212 | * After the @c read_until operation completes successfully, the string @c data | |
213 | * contains the delimiter: | |
214 | * @code { 'a', 'b', ..., 'c', '\r', '\n', 'd', 'e', ... } @endcode | |
215 | * The call to @c substr then extracts the data up to and including the | |
216 | * delimiter, so that the string @c line contains: | |
217 | * @code { 'a', 'b', ..., 'c', '\r', '\n' } @endcode | |
218 | * After the call to @c erase, the remaining data is left in the buffer @c b as | |
219 | * follows: | |
220 | * @code { 'd', 'e', ... } @endcode | |
221 | * This data may be the start of a new line, to be extracted by a subsequent | |
222 | * @c read_until operation. | |
223 | */ | |
224 | template <typename SyncReadStream, typename DynamicBuffer> | |
225 | std::size_t read_until(SyncReadStream& s, | |
226 | BOOST_ASIO_MOVE_ARG(DynamicBuffer) buffers, | |
227 | BOOST_ASIO_STRING_VIEW_PARAM delim); | |
228 | ||
229 | /// Read data into a dynamic buffer sequence until it contains a specified | |
230 | /// delimiter. | |
231 | /** | |
232 | * This function is used to read data into the specified dynamic buffer | |
233 | * sequence until the dynamic buffer sequence's get area contains the specified | |
234 | * delimiter. The call will block until one of the following conditions is | |
235 | * true: | |
236 | * | |
237 | * @li The get area of the dynamic buffer sequence contains the specified | |
238 | * delimiter. | |
239 | * | |
240 | * @li An error occurred. | |
241 | * | |
242 | * This operation is implemented in terms of zero or more calls to the stream's | |
243 | * read_some function. If the dynamic buffer sequence's get area already | |
244 | * contains the delimiter, the function returns immediately. | |
245 | * | |
246 | * @param s The stream from which the data is to be read. The type must support | |
247 | * the SyncReadStream concept. | |
248 | * | |
249 | * @param buffers The dynamic buffer sequence into which the data will be read. | |
250 | * | |
251 | * @param delim The delimiter string. | |
252 | * | |
253 | * @param ec Set to indicate what error occurred, if any. | |
254 | * | |
255 | * @returns The number of bytes in the dynamic buffer sequence's get area up to | |
256 | * and including the delimiter. Returns 0 if an error occurred. | |
257 | * | |
258 | * @note After a successful read_until operation, the dynamic buffer sequence | |
259 | * may contain additional data beyond the delimiter. An application will | |
260 | * typically leave that data in the dynamic buffer sequence for a subsequent | |
261 | * read_until operation to examine. | |
262 | */ | |
263 | template <typename SyncReadStream, typename DynamicBuffer> | |
264 | std::size_t read_until(SyncReadStream& s, | |
265 | BOOST_ASIO_MOVE_ARG(DynamicBuffer) buffers, | |
266 | BOOST_ASIO_STRING_VIEW_PARAM delim, | |
267 | boost::system::error_code& ec); | |
268 | ||
269 | #if !defined(BOOST_ASIO_NO_EXTENSIONS) | |
270 | #if defined(BOOST_ASIO_HAS_BOOST_REGEX) \ | |
271 | || defined(GENERATING_DOCUMENTATION) | |
272 | ||
273 | /// Read data into a dynamic buffer sequence until some part of the data it | |
274 | /// contains matches a regular expression. | |
275 | /** | |
276 | * This function is used to read data into the specified dynamic buffer | |
277 | * sequence until the dynamic buffer sequence's get area contains some data | |
278 | * that matches a regular expression. The call will block until one of the | |
279 | * following conditions is true: | |
280 | * | |
281 | * @li A substring of the dynamic buffer sequence's get area matches the | |
282 | * regular expression. | |
283 | * | |
284 | * @li An error occurred. | |
285 | * | |
286 | * This operation is implemented in terms of zero or more calls to the stream's | |
287 | * read_some function. If the dynamic buffer sequence's get area already | |
288 | * contains data that matches the regular expression, the function returns | |
289 | * immediately. | |
290 | * | |
291 | * @param s The stream from which the data is to be read. The type must support | |
292 | * the SyncReadStream concept. | |
293 | * | |
294 | * @param buffers A dynamic buffer sequence into which the data will be read. | |
295 | * | |
296 | * @param expr The regular expression. | |
297 | * | |
298 | * @returns The number of bytes in the dynamic buffer sequence's get area up to | |
299 | * and including the substring that matches the regular expression. | |
300 | * | |
301 | * @throws boost::system::system_error Thrown on failure. | |
302 | * | |
303 | * @note After a successful read_until operation, the dynamic buffer sequence | |
304 | * may contain additional data beyond that which matched the regular | |
305 | * expression. An application will typically leave that data in the dynamic | |
306 | * buffer sequence for a subsequent read_until operation to examine. | |
307 | * | |
308 | * @par Example | |
309 | * To read data into a @c std::string until a CR-LF sequence is encountered: | |
310 | * @code std::string data; | |
311 | * std::string n = boost::asio::read_until(s, | |
312 | * boost::asio::dynamic_buffer(data), boost::regex("\r\n")); | |
313 | * std::string line = data.substr(0, n); | |
314 | * data.erase(0, n); @endcode | |
315 | * After the @c read_until operation completes successfully, the string @c data | |
316 | * contains the delimiter: | |
317 | * @code { 'a', 'b', ..., 'c', '\r', '\n', 'd', 'e', ... } @endcode | |
318 | * The call to @c substr then extracts the data up to and including the | |
319 | * delimiter, so that the string @c line contains: | |
320 | * @code { 'a', 'b', ..., 'c', '\r', '\n' } @endcode | |
321 | * After the call to @c erase, the remaining data is left in the buffer @c b as | |
322 | * follows: | |
323 | * @code { 'd', 'e', ... } @endcode | |
324 | * This data may be the start of a new line, to be extracted by a subsequent | |
325 | * @c read_until operation. | |
326 | */ | |
327 | template <typename SyncReadStream, typename DynamicBuffer> | |
328 | std::size_t read_until(SyncReadStream& s, | |
329 | BOOST_ASIO_MOVE_ARG(DynamicBuffer) buffers, | |
330 | const boost::regex& expr); | |
331 | ||
332 | /// Read data into a dynamic buffer sequence until some part of the data it | |
333 | /// contains matches a regular expression. | |
334 | /** | |
335 | * This function is used to read data into the specified dynamic buffer | |
336 | * sequence until the dynamic buffer sequence's get area contains some data | |
337 | * that matches a regular expression. The call will block until one of the | |
338 | * following conditions is true: | |
339 | * | |
340 | * @li A substring of the dynamic buffer sequence's get area matches the | |
341 | * regular expression. | |
342 | * | |
343 | * @li An error occurred. | |
344 | * | |
345 | * This operation is implemented in terms of zero or more calls to the stream's | |
346 | * read_some function. If the dynamic buffer sequence's get area already | |
347 | * contains data that matches the regular expression, the function returns | |
348 | * immediately. | |
349 | * | |
350 | * @param s The stream from which the data is to be read. The type must support | |
351 | * the SyncReadStream concept. | |
352 | * | |
353 | * @param buffers A dynamic buffer sequence into which the data will be read. | |
354 | * | |
355 | * @param expr The regular expression. | |
356 | * | |
357 | * @param ec Set to indicate what error occurred, if any. | |
358 | * | |
359 | * @returns The number of bytes in the dynamic buffer sequence's get area up to | |
360 | * and including the substring that matches the regular expression. Returns 0 | |
361 | * if an error occurred. | |
362 | * | |
363 | * @note After a successful read_until operation, the dynamic buffer sequence | |
364 | * may contain additional data beyond that which matched the regular | |
365 | * expression. An application will typically leave that data in the dynamic | |
366 | * buffer sequence for a subsequent read_until operation to examine. | |
367 | */ | |
368 | template <typename SyncReadStream, typename DynamicBuffer> | |
369 | std::size_t read_until(SyncReadStream& s, | |
370 | BOOST_ASIO_MOVE_ARG(DynamicBuffer) buffers, | |
371 | const boost::regex& expr, boost::system::error_code& ec); | |
372 | ||
373 | #endif // defined(BOOST_ASIO_HAS_BOOST_REGEX) | |
374 | // || defined(GENERATING_DOCUMENTATION) | |
375 | ||
376 | /// Read data into a dynamic buffer sequence until a function object indicates a | |
377 | /// match. | |
378 | ||
379 | /** | |
380 | * This function is used to read data into the specified dynamic buffer | |
381 | * sequence until a user-defined match condition function object, when applied | |
382 | * to the data contained in the dynamic buffer sequence, indicates a successful | |
383 | * match. The call will block until one of the following conditions is true: | |
384 | * | |
385 | * @li The match condition function object returns a std::pair where the second | |
386 | * element evaluates to true. | |
387 | * | |
388 | * @li An error occurred. | |
389 | * | |
390 | * This operation is implemented in terms of zero or more calls to the stream's | |
391 | * read_some function. If the match condition function object already indicates | |
392 | * a match, the function returns immediately. | |
393 | * | |
394 | * @param s The stream from which the data is to be read. The type must support | |
395 | * the SyncReadStream concept. | |
396 | * | |
397 | * @param buffers A dynamic buffer sequence into which the data will be read. | |
398 | * | |
399 | * @param match_condition The function object to be called to determine whether | |
400 | * a match exists. The signature of the function object must be: | |
401 | * @code pair<iterator, bool> match_condition(iterator begin, iterator end); | |
402 | * @endcode | |
403 | * where @c iterator represents the type: | |
404 | * @code buffers_iterator<typename DynamicBuffer::const_buffers_type> | |
405 | * @endcode | |
406 | * The iterator parameters @c begin and @c end define the range of bytes to be | |
407 | * scanned to determine whether there is a match. The @c first member of the | |
408 | * return value is an iterator marking one-past-the-end of the bytes that have | |
409 | * been consumed by the match function. This iterator is used to calculate the | |
410 | * @c begin parameter for any subsequent invocation of the match condition. The | |
411 | * @c second member of the return value is true if a match has been found, false | |
412 | * otherwise. | |
413 | * | |
414 | * @returns The number of bytes in the dynamic_buffer's get area that | |
415 | * have been fully consumed by the match function. | |
416 | * | |
417 | * @throws boost::system::system_error Thrown on failure. | |
418 | * | |
419 | * @note After a successful read_until operation, the dynamic buffer sequence | |
420 | * may contain additional data beyond that which matched the function object. | |
421 | * An application will typically leave that data in the dynamic buffer sequence | |
422 | * for a subsequent read_until operation to examine. | |
423 | ||
424 | * @note The default implementation of the @c is_match_condition type trait | |
425 | * evaluates to true for function pointers and function objects with a | |
426 | * @c result_type typedef. It must be specialised for other user-defined | |
427 | * function objects. | |
428 | * | |
429 | * @par Examples | |
430 | * To read data into a dynamic buffer sequence until whitespace is encountered: | |
431 | * @code typedef boost::asio::buffers_iterator< | |
432 | * boost::asio::const_buffers_1> iterator; | |
433 | * | |
434 | * std::pair<iterator, bool> | |
435 | * match_whitespace(iterator begin, iterator end) | |
436 | * { | |
437 | * iterator i = begin; | |
438 | * while (i != end) | |
439 | * if (std::isspace(*i++)) | |
440 | * return std::make_pair(i, true); | |
441 | * return std::make_pair(i, false); | |
442 | * } | |
443 | * ... | |
444 | * std::string data; | |
445 | * boost::asio::read_until(s, data, match_whitespace); | |
446 | * @endcode | |
447 | * | |
448 | * To read data into a @c std::string until a matching character is found: | |
449 | * @code class match_char | |
450 | * { | |
451 | * public: | |
452 | * explicit match_char(char c) : c_(c) {} | |
453 | * | |
454 | * template <typename Iterator> | |
455 | * std::pair<Iterator, bool> operator()( | |
456 | * Iterator begin, Iterator end) const | |
457 | * { | |
458 | * Iterator i = begin; | |
459 | * while (i != end) | |
460 | * if (c_ == *i++) | |
461 | * return std::make_pair(i, true); | |
462 | * return std::make_pair(i, false); | |
463 | * } | |
464 | * | |
465 | * private: | |
466 | * char c_; | |
467 | * }; | |
468 | * | |
469 | * namespace asio { | |
470 | * template <> struct is_match_condition<match_char> | |
471 | * : public boost::true_type {}; | |
472 | * } // namespace asio | |
473 | * ... | |
474 | * std::string data; | |
475 | * boost::asio::read_until(s, data, match_char('a')); | |
476 | * @endcode | |
477 | */ | |
478 | template <typename SyncReadStream, | |
479 | typename DynamicBuffer, typename MatchCondition> | |
480 | std::size_t read_until(SyncReadStream& s, | |
481 | BOOST_ASIO_MOVE_ARG(DynamicBuffer) buffers, | |
482 | MatchCondition match_condition, | |
483 | typename enable_if<is_match_condition<MatchCondition>::value>::type* = 0); | |
484 | ||
485 | /// Read data into a dynamic buffer sequence until a function object indicates a | |
486 | /// match. | |
487 | /** | |
488 | * This function is used to read data into the specified dynamic buffer | |
489 | * sequence until a user-defined match condition function object, when applied | |
490 | * to the data contained in the dynamic buffer sequence, indicates a successful | |
491 | * match. The call will block until one of the following conditions is true: | |
492 | * | |
493 | * @li The match condition function object returns a std::pair where the second | |
494 | * element evaluates to true. | |
495 | * | |
496 | * @li An error occurred. | |
497 | * | |
498 | * This operation is implemented in terms of zero or more calls to the stream's | |
499 | * read_some function. If the match condition function object already indicates | |
500 | * a match, the function returns immediately. | |
501 | * | |
502 | * @param s The stream from which the data is to be read. The type must support | |
503 | * the SyncReadStream concept. | |
504 | * | |
505 | * @param buffers A dynamic buffer sequence into which the data will be read. | |
506 | * | |
507 | * @param match_condition The function object to be called to determine whether | |
508 | * a match exists. The signature of the function object must be: | |
509 | * @code pair<iterator, bool> match_condition(iterator begin, iterator end); | |
510 | * @endcode | |
511 | * where @c iterator represents the type: | |
512 | * @code buffers_iterator<DynamicBuffer::const_buffers_type> | |
513 | * @endcode | |
514 | * The iterator parameters @c begin and @c end define the range of bytes to be | |
515 | * scanned to determine whether there is a match. The @c first member of the | |
516 | * return value is an iterator marking one-past-the-end of the bytes that have | |
517 | * been consumed by the match function. This iterator is used to calculate the | |
518 | * @c begin parameter for any subsequent invocation of the match condition. The | |
519 | * @c second member of the return value is true if a match has been found, false | |
520 | * otherwise. | |
521 | * | |
522 | * @param ec Set to indicate what error occurred, if any. | |
523 | * | |
524 | * @returns The number of bytes in the dynamic buffer sequence's get area that | |
525 | * have been fully consumed by the match function. Returns 0 if an error | |
526 | * occurred. | |
527 | * | |
528 | * @note After a successful read_until operation, the dynamic buffer sequence | |
529 | * may contain additional data beyond that which matched the function object. | |
530 | * An application will typically leave that data in the dynamic buffer sequence | |
531 | * for a subsequent read_until operation to examine. | |
532 | * | |
533 | * @note The default implementation of the @c is_match_condition type trait | |
534 | * evaluates to true for function pointers and function objects with a | |
535 | * @c result_type typedef. It must be specialised for other user-defined | |
536 | * function objects. | |
537 | */ | |
538 | template <typename SyncReadStream, | |
539 | typename DynamicBuffer, typename MatchCondition> | |
540 | std::size_t read_until(SyncReadStream& s, | |
541 | BOOST_ASIO_MOVE_ARG(DynamicBuffer) buffers, | |
542 | MatchCondition match_condition, boost::system::error_code& ec, | |
543 | typename enable_if<is_match_condition<MatchCondition>::value>::type* = 0); | |
544 | ||
545 | #if !defined(BOOST_ASIO_NO_IOSTREAM) | |
546 | ||
547 | /// Read data into a streambuf until it contains a specified delimiter. | |
548 | /** | |
549 | * This function is used to read data into the specified streambuf until the | |
550 | * streambuf's get area contains the specified delimiter. The call will block | |
551 | * until one of the following conditions is true: | |
552 | * | |
553 | * @li The get area of the streambuf contains the specified delimiter. | |
554 | * | |
555 | * @li An error occurred. | |
556 | * | |
557 | * This operation is implemented in terms of zero or more calls to the stream's | |
558 | * read_some function. If the streambuf's get area already contains the | |
559 | * delimiter, the function returns immediately. | |
560 | * | |
561 | * @param s The stream from which the data is to be read. The type must support | |
562 | * the SyncReadStream concept. | |
563 | * | |
564 | * @param b A streambuf object into which the data will be read. | |
565 | * | |
566 | * @param delim The delimiter character. | |
567 | * | |
568 | * @returns The number of bytes in the streambuf's get area up to and including | |
569 | * the delimiter. | |
570 | * | |
571 | * @throws boost::system::system_error Thrown on failure. | |
572 | * | |
573 | * @note After a successful read_until operation, the streambuf may contain | |
574 | * additional data beyond the delimiter. An application will typically leave | |
575 | * that data in the streambuf for a subsequent read_until operation to examine. | |
576 | * | |
577 | * @par Example | |
578 | * To read data into a streambuf until a newline is encountered: | |
579 | * @code boost::asio::streambuf b; | |
580 | * boost::asio::read_until(s, b, '\n'); | |
581 | * std::istream is(&b); | |
582 | * std::string line; | |
583 | * std::getline(is, line); @endcode | |
584 | * After the @c read_until operation completes successfully, the buffer @c b | |
585 | * contains the delimiter: | |
586 | * @code { 'a', 'b', ..., 'c', '\n', 'd', 'e', ... } @endcode | |
587 | * The call to @c std::getline then extracts the data up to and including the | |
588 | * newline (which is discarded), so that the string @c line contains: | |
589 | * @code { 'a', 'b', ..., 'c' } @endcode | |
590 | * The remaining data is left in the buffer @c b as follows: | |
591 | * @code { 'd', 'e', ... } @endcode | |
592 | * This data may be the start of a new line, to be extracted by a subsequent | |
593 | * @c read_until operation. | |
594 | */ | |
595 | template <typename SyncReadStream, typename Allocator> | |
596 | std::size_t read_until(SyncReadStream& s, | |
597 | boost::asio::basic_streambuf<Allocator>& b, char delim); | |
598 | ||
599 | /// Read data into a streambuf until it contains a specified delimiter. | |
600 | /** | |
601 | * This function is used to read data into the specified streambuf until the | |
602 | * streambuf's get area contains the specified delimiter. The call will block | |
603 | * until one of the following conditions is true: | |
604 | * | |
605 | * @li The get area of the streambuf contains the specified delimiter. | |
606 | * | |
607 | * @li An error occurred. | |
608 | * | |
609 | * This operation is implemented in terms of zero or more calls to the stream's | |
610 | * read_some function. If the streambuf's get area already contains the | |
611 | * delimiter, the function returns immediately. | |
612 | * | |
613 | * @param s The stream from which the data is to be read. The type must support | |
614 | * the SyncReadStream concept. | |
615 | * | |
616 | * @param b A streambuf object into which the data will be read. | |
617 | * | |
618 | * @param delim The delimiter character. | |
619 | * | |
620 | * @param ec Set to indicate what error occurred, if any. | |
621 | * | |
622 | * @returns The number of bytes in the streambuf's get area up to and including | |
623 | * the delimiter. Returns 0 if an error occurred. | |
624 | * | |
625 | * @note After a successful read_until operation, the streambuf may contain | |
626 | * additional data beyond the delimiter. An application will typically leave | |
627 | * that data in the streambuf for a subsequent read_until operation to examine. | |
628 | */ | |
629 | template <typename SyncReadStream, typename Allocator> | |
630 | std::size_t read_until(SyncReadStream& s, | |
631 | boost::asio::basic_streambuf<Allocator>& b, char delim, | |
632 | boost::system::error_code& ec); | |
633 | ||
634 | /// Read data into a streambuf until it contains a specified delimiter. | |
635 | /** | |
636 | * This function is used to read data into the specified streambuf until the | |
637 | * streambuf's get area contains the specified delimiter. The call will block | |
638 | * until one of the following conditions is true: | |
639 | * | |
640 | * @li The get area of the streambuf contains the specified delimiter. | |
641 | * | |
642 | * @li An error occurred. | |
643 | * | |
644 | * This operation is implemented in terms of zero or more calls to the stream's | |
645 | * read_some function. If the streambuf's get area already contains the | |
646 | * delimiter, the function returns immediately. | |
647 | * | |
648 | * @param s The stream from which the data is to be read. The type must support | |
649 | * the SyncReadStream concept. | |
650 | * | |
651 | * @param b A streambuf object into which the data will be read. | |
652 | * | |
653 | * @param delim The delimiter string. | |
654 | * | |
655 | * @returns The number of bytes in the streambuf's get area up to and including | |
656 | * the delimiter. | |
657 | * | |
658 | * @throws boost::system::system_error Thrown on failure. | |
659 | * | |
660 | * @note After a successful read_until operation, the streambuf may contain | |
661 | * additional data beyond the delimiter. An application will typically leave | |
662 | * that data in the streambuf for a subsequent read_until operation to examine. | |
663 | * | |
664 | * @par Example | |
665 | * To read data into a streambuf until a newline is encountered: | |
666 | * @code boost::asio::streambuf b; | |
667 | * boost::asio::read_until(s, b, "\r\n"); | |
668 | * std::istream is(&b); | |
669 | * std::string line; | |
670 | * std::getline(is, line); @endcode | |
671 | * After the @c read_until operation completes successfully, the buffer @c b | |
672 | * contains the delimiter: | |
673 | * @code { 'a', 'b', ..., 'c', '\r', '\n', 'd', 'e', ... } @endcode | |
674 | * The call to @c std::getline then extracts the data up to and including the | |
675 | * newline (which is discarded), so that the string @c line contains: | |
676 | * @code { 'a', 'b', ..., 'c', '\r' } @endcode | |
677 | * The remaining data is left in the buffer @c b as follows: | |
678 | * @code { 'd', 'e', ... } @endcode | |
679 | * This data may be the start of a new line, to be extracted by a subsequent | |
680 | * @c read_until operation. | |
681 | */ | |
682 | template <typename SyncReadStream, typename Allocator> | |
683 | std::size_t read_until(SyncReadStream& s, | |
684 | boost::asio::basic_streambuf<Allocator>& b, | |
685 | BOOST_ASIO_STRING_VIEW_PARAM delim); | |
686 | ||
687 | /// Read data into a streambuf until it contains a specified delimiter. | |
688 | /** | |
689 | * This function is used to read data into the specified streambuf until the | |
690 | * streambuf's get area contains the specified delimiter. The call will block | |
691 | * until one of the following conditions is true: | |
692 | * | |
693 | * @li The get area of the streambuf contains the specified delimiter. | |
694 | * | |
695 | * @li An error occurred. | |
696 | * | |
697 | * This operation is implemented in terms of zero or more calls to the stream's | |
698 | * read_some function. If the streambuf's get area already contains the | |
699 | * delimiter, the function returns immediately. | |
700 | * | |
701 | * @param s The stream from which the data is to be read. The type must support | |
702 | * the SyncReadStream concept. | |
703 | * | |
704 | * @param b A streambuf object into which the data will be read. | |
705 | * | |
706 | * @param delim The delimiter string. | |
707 | * | |
708 | * @param ec Set to indicate what error occurred, if any. | |
709 | * | |
710 | * @returns The number of bytes in the streambuf's get area up to and including | |
711 | * the delimiter. Returns 0 if an error occurred. | |
712 | * | |
713 | * @note After a successful read_until operation, the streambuf may contain | |
714 | * additional data beyond the delimiter. An application will typically leave | |
715 | * that data in the streambuf for a subsequent read_until operation to examine. | |
716 | */ | |
717 | template <typename SyncReadStream, typename Allocator> | |
718 | std::size_t read_until(SyncReadStream& s, | |
719 | boost::asio::basic_streambuf<Allocator>& b, | |
720 | BOOST_ASIO_STRING_VIEW_PARAM delim, boost::system::error_code& ec); | |
721 | ||
722 | #if defined(BOOST_ASIO_HAS_BOOST_REGEX) \ | |
723 | || defined(GENERATING_DOCUMENTATION) | |
724 | ||
725 | /// Read data into a streambuf until some part of the data it contains matches | |
726 | /// a regular expression. | |
727 | /** | |
728 | * This function is used to read data into the specified streambuf until the | |
729 | * streambuf's get area contains some data that matches a regular expression. | |
730 | * The call will block until one of the following conditions is true: | |
731 | * | |
732 | * @li A substring of the streambuf's get area matches the regular expression. | |
733 | * | |
734 | * @li An error occurred. | |
735 | * | |
736 | * This operation is implemented in terms of zero or more calls to the stream's | |
737 | * read_some function. If the streambuf's get area already contains data that | |
738 | * matches the regular expression, the function returns immediately. | |
739 | * | |
740 | * @param s The stream from which the data is to be read. The type must support | |
741 | * the SyncReadStream concept. | |
742 | * | |
743 | * @param b A streambuf object into which the data will be read. | |
744 | * | |
745 | * @param expr The regular expression. | |
746 | * | |
747 | * @returns The number of bytes in the streambuf's get area up to and including | |
748 | * the substring that matches the regular expression. | |
749 | * | |
750 | * @throws boost::system::system_error Thrown on failure. | |
751 | * | |
752 | * @note After a successful read_until operation, the streambuf may contain | |
753 | * additional data beyond that which matched the regular expression. An | |
754 | * application will typically leave that data in the streambuf for a subsequent | |
755 | * read_until operation to examine. | |
756 | * | |
757 | * @par Example | |
758 | * To read data into a streambuf until a CR-LF sequence is encountered: | |
759 | * @code boost::asio::streambuf b; | |
760 | * boost::asio::read_until(s, b, boost::regex("\r\n")); | |
761 | * std::istream is(&b); | |
762 | * std::string line; | |
763 | * std::getline(is, line); @endcode | |
764 | * After the @c read_until operation completes successfully, the buffer @c b | |
765 | * contains the data which matched the regular expression: | |
766 | * @code { 'a', 'b', ..., 'c', '\r', '\n', 'd', 'e', ... } @endcode | |
767 | * The call to @c std::getline then extracts the data up to and including the | |
768 | * newline (which is discarded), so that the string @c line contains: | |
769 | * @code { 'a', 'b', ..., 'c', '\r' } @endcode | |
770 | * The remaining data is left in the buffer @c b as follows: | |
771 | * @code { 'd', 'e', ... } @endcode | |
772 | * This data may be the start of a new line, to be extracted by a subsequent | |
773 | * @c read_until operation. | |
774 | */ | |
775 | template <typename SyncReadStream, typename Allocator> | |
776 | std::size_t read_until(SyncReadStream& s, | |
777 | boost::asio::basic_streambuf<Allocator>& b, const boost::regex& expr); | |
778 | ||
779 | /// Read data into a streambuf until some part of the data it contains matches | |
780 | /// a regular expression. | |
781 | /** | |
782 | * This function is used to read data into the specified streambuf until the | |
783 | * streambuf's get area contains some data that matches a regular expression. | |
784 | * The call will block until one of the following conditions is true: | |
785 | * | |
786 | * @li A substring of the streambuf's get area matches the regular expression. | |
787 | * | |
788 | * @li An error occurred. | |
789 | * | |
790 | * This operation is implemented in terms of zero or more calls to the stream's | |
791 | * read_some function. If the streambuf's get area already contains data that | |
792 | * matches the regular expression, the function returns immediately. | |
793 | * | |
794 | * @param s The stream from which the data is to be read. The type must support | |
795 | * the SyncReadStream concept. | |
796 | * | |
797 | * @param b A streambuf object into which the data will be read. | |
798 | * | |
799 | * @param expr The regular expression. | |
800 | * | |
801 | * @param ec Set to indicate what error occurred, if any. | |
802 | * | |
803 | * @returns The number of bytes in the streambuf's get area up to and including | |
804 | * the substring that matches the regular expression. Returns 0 if an error | |
805 | * occurred. | |
806 | * | |
807 | * @note After a successful read_until operation, the streambuf may contain | |
808 | * additional data beyond that which matched the regular expression. An | |
809 | * application will typically leave that data in the streambuf for a subsequent | |
810 | * read_until operation to examine. | |
811 | */ | |
812 | template <typename SyncReadStream, typename Allocator> | |
813 | std::size_t read_until(SyncReadStream& s, | |
814 | boost::asio::basic_streambuf<Allocator>& b, const boost::regex& expr, | |
815 | boost::system::error_code& ec); | |
816 | ||
817 | #endif // defined(BOOST_ASIO_HAS_BOOST_REGEX) | |
818 | // || defined(GENERATING_DOCUMENTATION) | |
819 | ||
820 | /// Read data into a streambuf until a function object indicates a match. | |
821 | /** | |
822 | * This function is used to read data into the specified streambuf until a | |
823 | * user-defined match condition function object, when applied to the data | |
824 | * contained in the streambuf, indicates a successful match. The call will | |
825 | * block until one of the following conditions is true: | |
826 | * | |
827 | * @li The match condition function object returns a std::pair where the second | |
828 | * element evaluates to true. | |
829 | * | |
830 | * @li An error occurred. | |
831 | * | |
832 | * This operation is implemented in terms of zero or more calls to the stream's | |
833 | * read_some function. If the match condition function object already indicates | |
834 | * a match, the function returns immediately. | |
835 | * | |
836 | * @param s The stream from which the data is to be read. The type must support | |
837 | * the SyncReadStream concept. | |
838 | * | |
839 | * @param b A streambuf object into which the data will be read. | |
840 | * | |
841 | * @param match_condition The function object to be called to determine whether | |
842 | * a match exists. The signature of the function object must be: | |
843 | * @code pair<iterator, bool> match_condition(iterator begin, iterator end); | |
844 | * @endcode | |
845 | * where @c iterator represents the type: | |
846 | * @code buffers_iterator<basic_streambuf<Allocator>::const_buffers_type> | |
847 | * @endcode | |
848 | * The iterator parameters @c begin and @c end define the range of bytes to be | |
849 | * scanned to determine whether there is a match. The @c first member of the | |
850 | * return value is an iterator marking one-past-the-end of the bytes that have | |
851 | * been consumed by the match function. This iterator is used to calculate the | |
852 | * @c begin parameter for any subsequent invocation of the match condition. The | |
853 | * @c second member of the return value is true if a match has been found, false | |
854 | * otherwise. | |
855 | * | |
856 | * @returns The number of bytes in the streambuf's get area that have been fully | |
857 | * consumed by the match function. | |
858 | * | |
859 | * @throws boost::system::system_error Thrown on failure. | |
860 | * | |
861 | * @note After a successful read_until operation, the streambuf may contain | |
862 | * additional data beyond that which matched the function object. An application | |
863 | * will typically leave that data in the streambuf for a subsequent read_until | |
864 | * operation to examine. | |
865 | * | |
866 | * @note The default implementation of the @c is_match_condition type trait | |
867 | * evaluates to true for function pointers and function objects with a | |
868 | * @c result_type typedef. It must be specialised for other user-defined | |
869 | * function objects. | |
870 | * | |
871 | * @par Examples | |
872 | * To read data into a streambuf until whitespace is encountered: | |
873 | * @code typedef boost::asio::buffers_iterator< | |
874 | * boost::asio::streambuf::const_buffers_type> iterator; | |
875 | * | |
876 | * std::pair<iterator, bool> | |
877 | * match_whitespace(iterator begin, iterator end) | |
878 | * { | |
879 | * iterator i = begin; | |
880 | * while (i != end) | |
881 | * if (std::isspace(*i++)) | |
882 | * return std::make_pair(i, true); | |
883 | * return std::make_pair(i, false); | |
884 | * } | |
885 | * ... | |
886 | * boost::asio::streambuf b; | |
887 | * boost::asio::read_until(s, b, match_whitespace); | |
888 | * @endcode | |
889 | * | |
890 | * To read data into a streambuf until a matching character is found: | |
891 | * @code class match_char | |
892 | * { | |
893 | * public: | |
894 | * explicit match_char(char c) : c_(c) {} | |
895 | * | |
896 | * template <typename Iterator> | |
897 | * std::pair<Iterator, bool> operator()( | |
898 | * Iterator begin, Iterator end) const | |
899 | * { | |
900 | * Iterator i = begin; | |
901 | * while (i != end) | |
902 | * if (c_ == *i++) | |
903 | * return std::make_pair(i, true); | |
904 | * return std::make_pair(i, false); | |
905 | * } | |
906 | * | |
907 | * private: | |
908 | * char c_; | |
909 | * }; | |
910 | * | |
911 | * namespace asio { | |
912 | * template <> struct is_match_condition<match_char> | |
913 | * : public boost::true_type {}; | |
914 | * } // namespace asio | |
915 | * ... | |
916 | * boost::asio::streambuf b; | |
917 | * boost::asio::read_until(s, b, match_char('a')); | |
918 | * @endcode | |
919 | */ | |
920 | template <typename SyncReadStream, typename Allocator, typename MatchCondition> | |
921 | std::size_t read_until(SyncReadStream& s, | |
922 | boost::asio::basic_streambuf<Allocator>& b, MatchCondition match_condition, | |
923 | typename enable_if<is_match_condition<MatchCondition>::value>::type* = 0); | |
924 | ||
925 | /// Read data into a streambuf until a function object indicates a match. | |
926 | /** | |
927 | * This function is used to read data into the specified streambuf until a | |
928 | * user-defined match condition function object, when applied to the data | |
929 | * contained in the streambuf, indicates a successful match. The call will | |
930 | * block until one of the following conditions is true: | |
931 | * | |
932 | * @li The match condition function object returns a std::pair where the second | |
933 | * element evaluates to true. | |
934 | * | |
935 | * @li An error occurred. | |
936 | * | |
937 | * This operation is implemented in terms of zero or more calls to the stream's | |
938 | * read_some function. If the match condition function object already indicates | |
939 | * a match, the function returns immediately. | |
940 | * | |
941 | * @param s The stream from which the data is to be read. The type must support | |
942 | * the SyncReadStream concept. | |
943 | * | |
944 | * @param b A streambuf object into which the data will be read. | |
945 | * | |
946 | * @param match_condition The function object to be called to determine whether | |
947 | * a match exists. The signature of the function object must be: | |
948 | * @code pair<iterator, bool> match_condition(iterator begin, iterator end); | |
949 | * @endcode | |
950 | * where @c iterator represents the type: | |
951 | * @code buffers_iterator<basic_streambuf<Allocator>::const_buffers_type> | |
952 | * @endcode | |
953 | * The iterator parameters @c begin and @c end define the range of bytes to be | |
954 | * scanned to determine whether there is a match. The @c first member of the | |
955 | * return value is an iterator marking one-past-the-end of the bytes that have | |
956 | * been consumed by the match function. This iterator is used to calculate the | |
957 | * @c begin parameter for any subsequent invocation of the match condition. The | |
958 | * @c second member of the return value is true if a match has been found, false | |
959 | * otherwise. | |
960 | * | |
961 | * @param ec Set to indicate what error occurred, if any. | |
962 | * | |
963 | * @returns The number of bytes in the streambuf's get area that have been fully | |
964 | * consumed by the match function. Returns 0 if an error occurred. | |
965 | * | |
966 | * @note After a successful read_until operation, the streambuf may contain | |
967 | * additional data beyond that which matched the function object. An application | |
968 | * will typically leave that data in the streambuf for a subsequent read_until | |
969 | * operation to examine. | |
970 | * | |
971 | * @note The default implementation of the @c is_match_condition type trait | |
972 | * evaluates to true for function pointers and function objects with a | |
973 | * @c result_type typedef. It must be specialised for other user-defined | |
974 | * function objects. | |
975 | */ | |
976 | template <typename SyncReadStream, typename Allocator, typename MatchCondition> | |
977 | std::size_t read_until(SyncReadStream& s, | |
978 | boost::asio::basic_streambuf<Allocator>& b, | |
979 | MatchCondition match_condition, boost::system::error_code& ec, | |
980 | typename enable_if<is_match_condition<MatchCondition>::value>::type* = 0); | |
981 | ||
982 | #endif // !defined(BOOST_ASIO_NO_IOSTREAM) | |
983 | #endif // !defined(BOOST_ASIO_NO_EXTENSIONS) | |
984 | ||
985 | /*@}*/ | |
986 | /** | |
987 | * @defgroup async_read_until boost::asio::async_read_until | |
988 | * | |
989 | * @brief Start an asynchronous operation to read data into a dynamic buffer | |
990 | * sequence, or into a streambuf, until it contains a delimiter, matches a | |
991 | * regular expression, or a function object indicates a match. | |
992 | */ | |
993 | /*@{*/ | |
994 | ||
995 | /// Start an asynchronous operation to read data into a dynamic buffer sequence | |
996 | /// until it contains a specified delimiter. | |
997 | /** | |
998 | * This function is used to asynchronously read data into the specified dynamic | |
999 | * buffer sequence until the dynamic buffer sequence's get area contains the | |
1000 | * specified delimiter. The function call always returns immediately. The | |
1001 | * asynchronous operation will continue until one of the following conditions | |
1002 | * is true: | |
1003 | * | |
1004 | * @li The get area of the dynamic buffer sequence contains the specified | |
1005 | * delimiter. | |
1006 | * | |
1007 | * @li An error occurred. | |
1008 | * | |
1009 | * This operation is implemented in terms of zero or more calls to the stream's | |
1010 | * async_read_some function, and is known as a <em>composed operation</em>. If | |
1011 | * the dynamic buffer sequence's get area already contains the delimiter, this | |
1012 | * asynchronous operation completes immediately. The program must ensure that | |
1013 | * the stream performs no other read operations (such as async_read, | |
1014 | * async_read_until, the stream's async_read_some function, or any other | |
1015 | * composed operations that perform reads) until this operation completes. | |
1016 | * | |
1017 | * @param s The stream from which the data is to be read. The type must support | |
1018 | * the AsyncReadStream concept. | |
1019 | * | |
1020 | * @param buffers The dynamic buffer sequence into which the data will be read. | |
1021 | * Although the buffers object may be copied as necessary, ownership of the | |
1022 | * underlying memory blocks is retained by the caller, which must guarantee | |
1023 | * that they remain valid until the handler is called. | |
1024 | * | |
1025 | * @param delim The delimiter character. | |
1026 | * | |
1027 | * @param handler The handler to be called when the read operation completes. | |
1028 | * Copies will be made of the handler as required. The function signature of the | |
1029 | * handler must be: | |
1030 | * @code void handler( | |
1031 | * // Result of operation. | |
1032 | * const boost::system::error_code& error, | |
1033 | * | |
1034 | * // The number of bytes in the dynamic buffer sequence's | |
1035 | * // get area up to and including the delimiter. | |
1036 | * // 0 if an error occurred. | |
1037 | * std::size_t bytes_transferred | |
1038 | * ); @endcode | |
1039 | * Regardless of whether the asynchronous operation completes immediately or | |
1040 | * not, the handler will not be invoked from within this function. Invocation of | |
1041 | * the handler will be performed in a manner equivalent to using | |
1042 | * boost::asio::io_context::post(). | |
1043 | * | |
1044 | * @note After a successful async_read_until operation, the dynamic buffer | |
1045 | * sequence may contain additional data beyond the delimiter. An application | |
1046 | * will typically leave that data in the dynamic buffer sequence for a | |
1047 | * subsequent async_read_until operation to examine. | |
1048 | * | |
1049 | * @par Example | |
1050 | * To asynchronously read data into a @c std::string until a newline is | |
1051 | * encountered: | |
1052 | * @code std::string data; | |
1053 | * ... | |
1054 | * void handler(const boost::system::error_code& e, std::size_t size) | |
1055 | * { | |
1056 | * if (!e) | |
1057 | * { | |
1058 | * std::string line = data.substr(0, n); | |
1059 | * data.erase(0, n); | |
1060 | * ... | |
1061 | * } | |
1062 | * } | |
1063 | * ... | |
1064 | * boost::asio::async_read_until(s, data, '\n', handler); @endcode | |
1065 | * After the @c async_read_until operation completes successfully, the buffer | |
1066 | * @c data contains the delimiter: | |
1067 | * @code { 'a', 'b', ..., 'c', '\n', 'd', 'e', ... } @endcode | |
1068 | * The call to @c substr then extracts the data up to and including the | |
1069 | * delimiter, so that the string @c line contains: | |
1070 | * @code { 'a', 'b', ..., 'c', '\n' } @endcode | |
1071 | * After the call to @c erase, the remaining data is left in the buffer @c data | |
1072 | * as follows: | |
1073 | * @code { 'd', 'e', ... } @endcode | |
1074 | * This data may be the start of a new line, to be extracted by a subsequent | |
1075 | * @c async_read_until operation. | |
1076 | */ | |
1077 | template <typename AsyncReadStream, | |
1078 | typename DynamicBuffer, typename ReadHandler> | |
1079 | BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler, | |
1080 | void (boost::system::error_code, std::size_t)) | |
1081 | async_read_until(AsyncReadStream& s, | |
1082 | BOOST_ASIO_MOVE_ARG(DynamicBuffer) buffers, | |
1083 | char delim, BOOST_ASIO_MOVE_ARG(ReadHandler) handler); | |
1084 | ||
1085 | /// Start an asynchronous operation to read data into a dynamic buffer sequence | |
1086 | /// until it contains a specified delimiter. | |
1087 | /** | |
1088 | * This function is used to asynchronously read data into the specified dynamic | |
1089 | * buffer sequence until the dynamic buffer sequence's get area contains the | |
1090 | * specified delimiter. The function call always returns immediately. The | |
1091 | * asynchronous operation will continue until one of the following conditions | |
1092 | * is true: | |
1093 | * | |
1094 | * @li The get area of the dynamic buffer sequence contains the specified | |
1095 | * delimiter. | |
1096 | * | |
1097 | * @li An error occurred. | |
1098 | * | |
1099 | * This operation is implemented in terms of zero or more calls to the stream's | |
1100 | * async_read_some function, and is known as a <em>composed operation</em>. If | |
1101 | * the dynamic buffer sequence's get area already contains the delimiter, this | |
1102 | * asynchronous operation completes immediately. The program must ensure that | |
1103 | * the stream performs no other read operations (such as async_read, | |
1104 | * async_read_until, the stream's async_read_some function, or any other | |
1105 | * composed operations that perform reads) until this operation completes. | |
1106 | * | |
1107 | * @param s The stream from which the data is to be read. The type must support | |
1108 | * the AsyncReadStream concept. | |
1109 | * | |
1110 | * @param buffers The dynamic buffer sequence into which the data will be read. | |
1111 | * Although the buffers object may be copied as necessary, ownership of the | |
1112 | * underlying memory blocks is retained by the caller, which must guarantee | |
1113 | * that they remain valid until the handler is called. | |
1114 | * | |
1115 | * @param delim The delimiter string. | |
1116 | * | |
1117 | * @param handler The handler to be called when the read operation completes. | |
1118 | * Copies will be made of the handler as required. The function signature of the | |
1119 | * handler must be: | |
1120 | * @code void handler( | |
1121 | * // Result of operation. | |
1122 | * const boost::system::error_code& error, | |
1123 | * | |
1124 | * // The number of bytes in the dynamic buffer sequence's | |
1125 | * // get area up to and including the delimiter. | |
1126 | * // 0 if an error occurred. | |
1127 | * std::size_t bytes_transferred | |
1128 | * ); @endcode | |
1129 | * Regardless of whether the asynchronous operation completes immediately or | |
1130 | * not, the handler will not be invoked from within this function. Invocation of | |
1131 | * the handler will be performed in a manner equivalent to using | |
1132 | * boost::asio::io_context::post(). | |
1133 | * | |
1134 | * @note After a successful async_read_until operation, the dynamic buffer | |
1135 | * sequence may contain additional data beyond the delimiter. An application | |
1136 | * will typically leave that data in the dynamic buffer sequence for a | |
1137 | * subsequent async_read_until operation to examine. | |
1138 | * | |
1139 | * @par Example | |
1140 | * To asynchronously read data into a @c std::string until a CR-LF sequence is | |
1141 | * encountered: | |
1142 | * @code std::string data; | |
1143 | * ... | |
1144 | * void handler(const boost::system::error_code& e, std::size_t size) | |
1145 | * { | |
1146 | * if (!e) | |
1147 | * { | |
1148 | * std::string line = data.substr(0, n); | |
1149 | * data.erase(0, n); | |
1150 | * ... | |
1151 | * } | |
1152 | * } | |
1153 | * ... | |
1154 | * boost::asio::async_read_until(s, data, "\r\n", handler); @endcode | |
1155 | * After the @c async_read_until operation completes successfully, the string | |
1156 | * @c data contains the delimiter: | |
1157 | * @code { 'a', 'b', ..., 'c', '\r', '\n', 'd', 'e', ... } @endcode | |
1158 | * The call to @c substr then extracts the data up to and including the | |
1159 | * delimiter, so that the string @c line contains: | |
1160 | * @code { 'a', 'b', ..., 'c', '\r', '\n' } @endcode | |
1161 | * After the call to @c erase, the remaining data is left in the string @c data | |
1162 | * as follows: | |
1163 | * @code { 'd', 'e', ... } @endcode | |
1164 | * This data may be the start of a new line, to be extracted by a subsequent | |
1165 | * @c async_read_until operation. | |
1166 | */ | |
1167 | template <typename AsyncReadStream, | |
1168 | typename DynamicBuffer, typename ReadHandler> | |
1169 | BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler, | |
1170 | void (boost::system::error_code, std::size_t)) | |
1171 | async_read_until(AsyncReadStream& s, | |
1172 | BOOST_ASIO_MOVE_ARG(DynamicBuffer) buffers, | |
1173 | BOOST_ASIO_STRING_VIEW_PARAM delim, | |
1174 | BOOST_ASIO_MOVE_ARG(ReadHandler) handler); | |
1175 | ||
1176 | #if !defined(BOOST_ASIO_NO_EXTENSIONS) | |
1177 | #if defined(BOOST_ASIO_HAS_BOOST_REGEX) \ | |
1178 | || defined(GENERATING_DOCUMENTATION) | |
1179 | ||
1180 | /// Start an asynchronous operation to read data into a dynamic buffer sequence | |
1181 | /// until some part of its data matches a regular expression. | |
1182 | /** | |
1183 | * This function is used to asynchronously read data into the specified dynamic | |
1184 | * buffer sequence until the dynamic buffer sequence's get area contains some | |
1185 | * data that matches a regular expression. The function call always returns | |
1186 | * immediately. The asynchronous operation will continue until one of the | |
1187 | * following conditions is true: | |
1188 | * | |
1189 | * @li A substring of the dynamic buffer sequence's get area matches the regular | |
1190 | * expression. | |
1191 | * | |
1192 | * @li An error occurred. | |
1193 | * | |
1194 | * This operation is implemented in terms of zero or more calls to the stream's | |
1195 | * async_read_some function, and is known as a <em>composed operation</em>. If | |
1196 | * the dynamic buffer sequence's get area already contains data that matches | |
1197 | * the regular expression, this asynchronous operation completes immediately. | |
1198 | * The program must ensure that the stream performs no other read operations | |
1199 | * (such as async_read, async_read_until, the stream's async_read_some | |
1200 | * function, or any other composed operations that perform reads) until this | |
1201 | * operation completes. | |
1202 | * | |
1203 | * @param s The stream from which the data is to be read. The type must support | |
1204 | * the AsyncReadStream concept. | |
1205 | * | |
1206 | * @param buffers The dynamic buffer sequence into which the data will be read. | |
1207 | * Although the buffers object may be copied as necessary, ownership of the | |
1208 | * underlying memory blocks is retained by the caller, which must guarantee | |
1209 | * that they remain valid until the handler is called. | |
1210 | * | |
1211 | * @param expr The regular expression. | |
1212 | * | |
1213 | * @param handler The handler to be called when the read operation completes. | |
1214 | * Copies will be made of the handler as required. The function signature of the | |
1215 | * handler must be: | |
1216 | * @code void handler( | |
1217 | * // Result of operation. | |
1218 | * const boost::system::error_code& error, | |
1219 | * | |
1220 | * // The number of bytes in the dynamic buffer | |
1221 | * // sequence's get area up to and including the | |
1222 | * // substring that matches the regular expression. | |
1223 | * // 0 if an error occurred. | |
1224 | * std::size_t bytes_transferred | |
1225 | * ); @endcode | |
1226 | * Regardless of whether the asynchronous operation completes immediately or | |
1227 | * not, the handler will not be invoked from within this function. Invocation of | |
1228 | * the handler will be performed in a manner equivalent to using | |
1229 | * boost::asio::io_context::post(). | |
1230 | * | |
1231 | * @note After a successful async_read_until operation, the dynamic buffer | |
1232 | * sequence may contain additional data beyond that which matched the regular | |
1233 | * expression. An application will typically leave that data in the dynamic | |
1234 | * buffer sequence for a subsequent async_read_until operation to examine. | |
1235 | * | |
1236 | * @par Example | |
1237 | * To asynchronously read data into a @c std::string until a CR-LF sequence is | |
1238 | * encountered: | |
1239 | * @code std::string data; | |
1240 | * ... | |
1241 | * void handler(const boost::system::error_code& e, std::size_t size) | |
1242 | * { | |
1243 | * if (!e) | |
1244 | * { | |
1245 | * std::string line = data.substr(0, n); | |
1246 | * data.erase(0, n); | |
1247 | * ... | |
1248 | * } | |
1249 | * } | |
1250 | * ... | |
1251 | * boost::asio::async_read_until(s, data, | |
1252 | * boost::regex("\r\n"), handler); @endcode | |
1253 | * After the @c async_read_until operation completes successfully, the string | |
1254 | * @c data contains the data which matched the regular expression: | |
1255 | * @code { 'a', 'b', ..., 'c', '\r', '\n', 'd', 'e', ... } @endcode | |
1256 | * The call to @c substr then extracts the data up to and including the match, | |
1257 | * so that the string @c line contains: | |
1258 | * @code { 'a', 'b', ..., 'c', '\r', '\n' } @endcode | |
1259 | * After the call to @c erase, the remaining data is left in the string @c data | |
1260 | * as follows: | |
1261 | * @code { 'd', 'e', ... } @endcode | |
1262 | * This data may be the start of a new line, to be extracted by a subsequent | |
1263 | * @c async_read_until operation. | |
1264 | */ | |
1265 | template <typename AsyncReadStream, | |
1266 | typename DynamicBuffer, typename ReadHandler> | |
1267 | BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler, | |
1268 | void (boost::system::error_code, std::size_t)) | |
1269 | async_read_until(AsyncReadStream& s, | |
1270 | BOOST_ASIO_MOVE_ARG(DynamicBuffer) buffers, | |
1271 | const boost::regex& expr, | |
1272 | BOOST_ASIO_MOVE_ARG(ReadHandler) handler); | |
1273 | ||
1274 | #endif // defined(BOOST_ASIO_HAS_BOOST_REGEX) | |
1275 | // || defined(GENERATING_DOCUMENTATION) | |
1276 | ||
1277 | /// Start an asynchronous operation to read data into a dynamic buffer sequence | |
1278 | /// until a function object indicates a match. | |
1279 | /** | |
1280 | * This function is used to asynchronously read data into the specified dynamic | |
1281 | * buffer sequence until a user-defined match condition function object, when | |
1282 | * applied to the data contained in the dynamic buffer sequence, indicates a | |
1283 | * successful match. The function call always returns immediately. The | |
1284 | * asynchronous operation will continue until one of the following conditions | |
1285 | * is true: | |
1286 | * | |
1287 | * @li The match condition function object returns a std::pair where the second | |
1288 | * element evaluates to true. | |
1289 | * | |
1290 | * @li An error occurred. | |
1291 | * | |
1292 | * This operation is implemented in terms of zero or more calls to the stream's | |
1293 | * async_read_some function, and is known as a <em>composed operation</em>. If | |
1294 | * the match condition function object already indicates a match, this | |
1295 | * asynchronous operation completes immediately. The program must ensure that | |
1296 | * the stream performs no other read operations (such as async_read, | |
1297 | * async_read_until, the stream's async_read_some function, or any other | |
1298 | * composed operations that perform reads) until this operation completes. | |
1299 | * | |
1300 | * @param s The stream from which the data is to be read. The type must support | |
1301 | * the AsyncReadStream concept. | |
1302 | * | |
1303 | * @param buffers The dynamic buffer sequence into which the data will be read. | |
1304 | * Although the buffers object may be copied as necessary, ownership of the | |
1305 | * underlying memory blocks is retained by the caller, which must guarantee | |
1306 | * that they remain valid until the handler is called. | |
1307 | * | |
1308 | * @param match_condition The function object to be called to determine whether | |
1309 | * a match exists. The signature of the function object must be: | |
1310 | * @code pair<iterator, bool> match_condition(iterator begin, iterator end); | |
1311 | * @endcode | |
1312 | * where @c iterator represents the type: | |
1313 | * @code buffers_iterator<typename DynamicBuffer::const_buffers_type> | |
1314 | * @endcode | |
1315 | * The iterator parameters @c begin and @c end define the range of bytes to be | |
1316 | * scanned to determine whether there is a match. The @c first member of the | |
1317 | * return value is an iterator marking one-past-the-end of the bytes that have | |
1318 | * been consumed by the match function. This iterator is used to calculate the | |
1319 | * @c begin parameter for any subsequent invocation of the match condition. The | |
1320 | * @c second member of the return value is true if a match has been found, false | |
1321 | * otherwise. | |
1322 | * | |
1323 | * @param handler The handler to be called when the read operation completes. | |
1324 | * Copies will be made of the handler as required. The function signature of the | |
1325 | * handler must be: | |
1326 | * @code void handler( | |
1327 | * // Result of operation. | |
1328 | * const boost::system::error_code& error, | |
1329 | * | |
1330 | * // The number of bytes in the dynamic buffer sequence's | |
1331 | * // get area that have been fully consumed by the match | |
1332 | * // function. O if an error occurred. | |
1333 | * std::size_t bytes_transferred | |
1334 | * ); @endcode | |
1335 | * Regardless of whether the asynchronous operation completes immediately or | |
1336 | * not, the handler will not be invoked from within this function. Invocation of | |
1337 | * the handler will be performed in a manner equivalent to using | |
1338 | * boost::asio::io_context::post(). | |
1339 | * | |
1340 | * @note After a successful async_read_until operation, the dynamic buffer | |
1341 | * sequence may contain additional data beyond that which matched the function | |
1342 | * object. An application will typically leave that data in the dynamic buffer | |
1343 | * sequence for a subsequent async_read_until operation to examine. | |
1344 | * | |
1345 | * @note The default implementation of the @c is_match_condition type trait | |
1346 | * evaluates to true for function pointers and function objects with a | |
1347 | * @c result_type typedef. It must be specialised for other user-defined | |
1348 | * function objects. | |
1349 | * | |
1350 | * @par Examples | |
1351 | * To asynchronously read data into a @c std::string until whitespace is | |
1352 | * encountered: | |
1353 | * @code typedef boost::asio::buffers_iterator< | |
1354 | * boost::asio::const_buffers_1> iterator; | |
1355 | * | |
1356 | * std::pair<iterator, bool> | |
1357 | * match_whitespace(iterator begin, iterator end) | |
1358 | * { | |
1359 | * iterator i = begin; | |
1360 | * while (i != end) | |
1361 | * if (std::isspace(*i++)) | |
1362 | * return std::make_pair(i, true); | |
1363 | * return std::make_pair(i, false); | |
1364 | * } | |
1365 | * ... | |
1366 | * void handler(const boost::system::error_code& e, std::size_t size); | |
1367 | * ... | |
1368 | * std::string data; | |
1369 | * boost::asio::async_read_until(s, data, match_whitespace, handler); | |
1370 | * @endcode | |
1371 | * | |
1372 | * To asynchronously read data into a @c std::string until a matching character | |
1373 | * is found: | |
1374 | * @code class match_char | |
1375 | * { | |
1376 | * public: | |
1377 | * explicit match_char(char c) : c_(c) {} | |
1378 | * | |
1379 | * template <typename Iterator> | |
1380 | * std::pair<Iterator, bool> operator()( | |
1381 | * Iterator begin, Iterator end) const | |
1382 | * { | |
1383 | * Iterator i = begin; | |
1384 | * while (i != end) | |
1385 | * if (c_ == *i++) | |
1386 | * return std::make_pair(i, true); | |
1387 | * return std::make_pair(i, false); | |
1388 | * } | |
1389 | * | |
1390 | * private: | |
1391 | * char c_; | |
1392 | * }; | |
1393 | * | |
1394 | * namespace asio { | |
1395 | * template <> struct is_match_condition<match_char> | |
1396 | * : public boost::true_type {}; | |
1397 | * } // namespace asio | |
1398 | * ... | |
1399 | * void handler(const boost::system::error_code& e, std::size_t size); | |
1400 | * ... | |
1401 | * std::string data; | |
1402 | * boost::asio::async_read_until(s, data, match_char('a'), handler); | |
1403 | * @endcode | |
1404 | */ | |
1405 | template <typename AsyncReadStream, typename DynamicBuffer, | |
1406 | typename MatchCondition, typename ReadHandler> | |
1407 | BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler, | |
1408 | void (boost::system::error_code, std::size_t)) | |
1409 | async_read_until(AsyncReadStream& s, | |
1410 | BOOST_ASIO_MOVE_ARG(DynamicBuffer) buffers, | |
1411 | MatchCondition match_condition, BOOST_ASIO_MOVE_ARG(ReadHandler) handler, | |
1412 | typename enable_if<is_match_condition<MatchCondition>::value>::type* = 0); | |
1413 | ||
1414 | #if !defined(BOOST_ASIO_NO_IOSTREAM) | |
1415 | ||
1416 | /// Start an asynchronous operation to read data into a streambuf until it | |
1417 | /// contains a specified delimiter. | |
1418 | /** | |
1419 | * This function is used to asynchronously read data into the specified | |
1420 | * streambuf until the streambuf's get area contains the specified delimiter. | |
1421 | * The function call always returns immediately. The asynchronous operation | |
1422 | * will continue until one of the following conditions is true: | |
1423 | * | |
1424 | * @li The get area of the streambuf contains the specified delimiter. | |
1425 | * | |
1426 | * @li An error occurred. | |
1427 | * | |
1428 | * This operation is implemented in terms of zero or more calls to the stream's | |
1429 | * async_read_some function, and is known as a <em>composed operation</em>. If | |
1430 | * the streambuf's get area already contains the delimiter, this asynchronous | |
1431 | * operation completes immediately. The program must ensure that the stream | |
1432 | * performs no other read operations (such as async_read, async_read_until, the | |
1433 | * stream's async_read_some function, or any other composed operations that | |
1434 | * perform reads) until this operation completes. | |
1435 | * | |
1436 | * @param s The stream from which the data is to be read. The type must support | |
1437 | * the AsyncReadStream concept. | |
1438 | * | |
1439 | * @param b A streambuf object into which the data will be read. Ownership of | |
1440 | * the streambuf is retained by the caller, which must guarantee that it remains | |
1441 | * valid until the handler is called. | |
1442 | * | |
1443 | * @param delim The delimiter character. | |
1444 | * | |
1445 | * @param handler The handler to be called when the read operation completes. | |
1446 | * Copies will be made of the handler as required. The function signature of the | |
1447 | * handler must be: | |
1448 | * @code void handler( | |
1449 | * // Result of operation. | |
1450 | * const boost::system::error_code& error, | |
1451 | * | |
1452 | * // The number of bytes in the streambuf's get | |
1453 | * // area up to and including the delimiter. | |
1454 | * // 0 if an error occurred. | |
1455 | * std::size_t bytes_transferred | |
1456 | * ); @endcode | |
1457 | * Regardless of whether the asynchronous operation completes immediately or | |
1458 | * not, the handler will not be invoked from within this function. Invocation of | |
1459 | * the handler will be performed in a manner equivalent to using | |
1460 | * boost::asio::io_context::post(). | |
1461 | * | |
1462 | * @note After a successful async_read_until operation, the streambuf may | |
1463 | * contain additional data beyond the delimiter. An application will typically | |
1464 | * leave that data in the streambuf for a subsequent async_read_until operation | |
1465 | * to examine. | |
1466 | * | |
1467 | * @par Example | |
1468 | * To asynchronously read data into a streambuf until a newline is encountered: | |
1469 | * @code boost::asio::streambuf b; | |
1470 | * ... | |
1471 | * void handler(const boost::system::error_code& e, std::size_t size) | |
1472 | * { | |
1473 | * if (!e) | |
1474 | * { | |
1475 | * std::istream is(&b); | |
1476 | * std::string line; | |
1477 | * std::getline(is, line); | |
1478 | * ... | |
1479 | * } | |
1480 | * } | |
1481 | * ... | |
1482 | * boost::asio::async_read_until(s, b, '\n', handler); @endcode | |
1483 | * After the @c async_read_until operation completes successfully, the buffer | |
1484 | * @c b contains the delimiter: | |
1485 | * @code { 'a', 'b', ..., 'c', '\n', 'd', 'e', ... } @endcode | |
1486 | * The call to @c std::getline then extracts the data up to and including the | |
1487 | * newline (which is discarded), so that the string @c line contains: | |
1488 | * @code { 'a', 'b', ..., 'c' } @endcode | |
1489 | * The remaining data is left in the buffer @c b as follows: | |
1490 | * @code { 'd', 'e', ... } @endcode | |
1491 | * This data may be the start of a new line, to be extracted by a subsequent | |
1492 | * @c async_read_until operation. | |
1493 | */ | |
1494 | template <typename AsyncReadStream, typename Allocator, typename ReadHandler> | |
1495 | BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler, | |
1496 | void (boost::system::error_code, std::size_t)) | |
1497 | async_read_until(AsyncReadStream& s, | |
1498 | boost::asio::basic_streambuf<Allocator>& b, | |
1499 | char delim, BOOST_ASIO_MOVE_ARG(ReadHandler) handler); | |
1500 | ||
1501 | /// Start an asynchronous operation to read data into a streambuf until it | |
1502 | /// contains a specified delimiter. | |
1503 | /** | |
1504 | * This function is used to asynchronously read data into the specified | |
1505 | * streambuf until the streambuf's get area contains the specified delimiter. | |
1506 | * The function call always returns immediately. The asynchronous operation | |
1507 | * will continue until one of the following conditions is true: | |
1508 | * | |
1509 | * @li The get area of the streambuf contains the specified delimiter. | |
1510 | * | |
1511 | * @li An error occurred. | |
1512 | * | |
1513 | * This operation is implemented in terms of zero or more calls to the stream's | |
1514 | * async_read_some function, and is known as a <em>composed operation</em>. If | |
1515 | * the streambuf's get area already contains the delimiter, this asynchronous | |
1516 | * operation completes immediately. The program must ensure that the stream | |
1517 | * performs no other read operations (such as async_read, async_read_until, the | |
1518 | * stream's async_read_some function, or any other composed operations that | |
1519 | * perform reads) until this operation completes. | |
1520 | * | |
1521 | * @param s The stream from which the data is to be read. The type must support | |
1522 | * the AsyncReadStream concept. | |
1523 | * | |
1524 | * @param b A streambuf object into which the data will be read. Ownership of | |
1525 | * the streambuf is retained by the caller, which must guarantee that it remains | |
1526 | * valid until the handler is called. | |
1527 | * | |
1528 | * @param delim The delimiter string. | |
1529 | * | |
1530 | * @param handler The handler to be called when the read operation completes. | |
1531 | * Copies will be made of the handler as required. The function signature of the | |
1532 | * handler must be: | |
1533 | * @code void handler( | |
1534 | * // Result of operation. | |
1535 | * const boost::system::error_code& error, | |
1536 | * | |
1537 | * // The number of bytes in the streambuf's get | |
1538 | * // area up to and including the delimiter. | |
1539 | * // 0 if an error occurred. | |
1540 | * std::size_t bytes_transferred | |
1541 | * ); @endcode | |
1542 | * Regardless of whether the asynchronous operation completes immediately or | |
1543 | * not, the handler will not be invoked from within this function. Invocation of | |
1544 | * the handler will be performed in a manner equivalent to using | |
1545 | * boost::asio::io_context::post(). | |
1546 | * | |
1547 | * @note After a successful async_read_until operation, the streambuf may | |
1548 | * contain additional data beyond the delimiter. An application will typically | |
1549 | * leave that data in the streambuf for a subsequent async_read_until operation | |
1550 | * to examine. | |
1551 | * | |
1552 | * @par Example | |
1553 | * To asynchronously read data into a streambuf until a newline is encountered: | |
1554 | * @code boost::asio::streambuf b; | |
1555 | * ... | |
1556 | * void handler(const boost::system::error_code& e, std::size_t size) | |
1557 | * { | |
1558 | * if (!e) | |
1559 | * { | |
1560 | * std::istream is(&b); | |
1561 | * std::string line; | |
1562 | * std::getline(is, line); | |
1563 | * ... | |
1564 | * } | |
1565 | * } | |
1566 | * ... | |
1567 | * boost::asio::async_read_until(s, b, "\r\n", handler); @endcode | |
1568 | * After the @c async_read_until operation completes successfully, the buffer | |
1569 | * @c b contains the delimiter: | |
1570 | * @code { 'a', 'b', ..., 'c', '\r', '\n', 'd', 'e', ... } @endcode | |
1571 | * The call to @c std::getline then extracts the data up to and including the | |
1572 | * newline (which is discarded), so that the string @c line contains: | |
1573 | * @code { 'a', 'b', ..., 'c', '\r' } @endcode | |
1574 | * The remaining data is left in the buffer @c b as follows: | |
1575 | * @code { 'd', 'e', ... } @endcode | |
1576 | * This data may be the start of a new line, to be extracted by a subsequent | |
1577 | * @c async_read_until operation. | |
1578 | */ | |
1579 | template <typename AsyncReadStream, typename Allocator, typename ReadHandler> | |
1580 | BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler, | |
1581 | void (boost::system::error_code, std::size_t)) | |
1582 | async_read_until(AsyncReadStream& s, | |
1583 | boost::asio::basic_streambuf<Allocator>& b, | |
1584 | BOOST_ASIO_STRING_VIEW_PARAM delim, | |
1585 | BOOST_ASIO_MOVE_ARG(ReadHandler) handler); | |
1586 | ||
1587 | #if defined(BOOST_ASIO_HAS_BOOST_REGEX) \ | |
1588 | || defined(GENERATING_DOCUMENTATION) | |
1589 | ||
1590 | /// Start an asynchronous operation to read data into a streambuf until some | |
1591 | /// part of its data matches a regular expression. | |
1592 | /** | |
1593 | * This function is used to asynchronously read data into the specified | |
1594 | * streambuf until the streambuf's get area contains some data that matches a | |
1595 | * regular expression. The function call always returns immediately. The | |
1596 | * asynchronous operation will continue until one of the following conditions | |
1597 | * is true: | |
1598 | * | |
1599 | * @li A substring of the streambuf's get area matches the regular expression. | |
1600 | * | |
1601 | * @li An error occurred. | |
1602 | * | |
1603 | * This operation is implemented in terms of zero or more calls to the stream's | |
1604 | * async_read_some function, and is known as a <em>composed operation</em>. If | |
1605 | * the streambuf's get area already contains data that matches the regular | |
1606 | * expression, this asynchronous operation completes immediately. The program | |
1607 | * must ensure that the stream performs no other read operations (such as | |
1608 | * async_read, async_read_until, the stream's async_read_some function, or any | |
1609 | * other composed operations that perform reads) until this operation | |
1610 | * completes. | |
1611 | * | |
1612 | * @param s The stream from which the data is to be read. The type must support | |
1613 | * the AsyncReadStream concept. | |
1614 | * | |
1615 | * @param b A streambuf object into which the data will be read. Ownership of | |
1616 | * the streambuf is retained by the caller, which must guarantee that it remains | |
1617 | * valid until the handler is called. | |
1618 | * | |
1619 | * @param expr The regular expression. | |
1620 | * | |
1621 | * @param handler The handler to be called when the read operation completes. | |
1622 | * Copies will be made of the handler as required. The function signature of the | |
1623 | * handler must be: | |
1624 | * @code void handler( | |
1625 | * // Result of operation. | |
1626 | * const boost::system::error_code& error, | |
1627 | * | |
1628 | * // The number of bytes in the streambuf's get | |
1629 | * // area up to and including the substring | |
1630 | * // that matches the regular. expression. | |
1631 | * // 0 if an error occurred. | |
1632 | * std::size_t bytes_transferred | |
1633 | * ); @endcode | |
1634 | * Regardless of whether the asynchronous operation completes immediately or | |
1635 | * not, the handler will not be invoked from within this function. Invocation of | |
1636 | * the handler will be performed in a manner equivalent to using | |
1637 | * boost::asio::io_context::post(). | |
1638 | * | |
1639 | * @note After a successful async_read_until operation, the streambuf may | |
1640 | * contain additional data beyond that which matched the regular expression. An | |
1641 | * application will typically leave that data in the streambuf for a subsequent | |
1642 | * async_read_until operation to examine. | |
1643 | * | |
1644 | * @par Example | |
1645 | * To asynchronously read data into a streambuf until a CR-LF sequence is | |
1646 | * encountered: | |
1647 | * @code boost::asio::streambuf b; | |
1648 | * ... | |
1649 | * void handler(const boost::system::error_code& e, std::size_t size) | |
1650 | * { | |
1651 | * if (!e) | |
1652 | * { | |
1653 | * std::istream is(&b); | |
1654 | * std::string line; | |
1655 | * std::getline(is, line); | |
1656 | * ... | |
1657 | * } | |
1658 | * } | |
1659 | * ... | |
1660 | * boost::asio::async_read_until(s, b, boost::regex("\r\n"), handler); @endcode | |
1661 | * After the @c async_read_until operation completes successfully, the buffer | |
1662 | * @c b contains the data which matched the regular expression: | |
1663 | * @code { 'a', 'b', ..., 'c', '\r', '\n', 'd', 'e', ... } @endcode | |
1664 | * The call to @c std::getline then extracts the data up to and including the | |
1665 | * newline (which is discarded), so that the string @c line contains: | |
1666 | * @code { 'a', 'b', ..., 'c', '\r' } @endcode | |
1667 | * The remaining data is left in the buffer @c b as follows: | |
1668 | * @code { 'd', 'e', ... } @endcode | |
1669 | * This data may be the start of a new line, to be extracted by a subsequent | |
1670 | * @c async_read_until operation. | |
1671 | */ | |
1672 | template <typename AsyncReadStream, typename Allocator, typename ReadHandler> | |
1673 | BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler, | |
1674 | void (boost::system::error_code, std::size_t)) | |
1675 | async_read_until(AsyncReadStream& s, | |
1676 | boost::asio::basic_streambuf<Allocator>& b, const boost::regex& expr, | |
1677 | BOOST_ASIO_MOVE_ARG(ReadHandler) handler); | |
1678 | ||
1679 | #endif // defined(BOOST_ASIO_HAS_BOOST_REGEX) | |
1680 | // || defined(GENERATING_DOCUMENTATION) | |
1681 | ||
1682 | /// Start an asynchronous operation to read data into a streambuf until a | |
1683 | /// function object indicates a match. | |
1684 | /** | |
1685 | * This function is used to asynchronously read data into the specified | |
1686 | * streambuf until a user-defined match condition function object, when applied | |
1687 | * to the data contained in the streambuf, indicates a successful match. The | |
1688 | * function call always returns immediately. The asynchronous operation will | |
1689 | * continue until one of the following conditions is true: | |
1690 | * | |
1691 | * @li The match condition function object returns a std::pair where the second | |
1692 | * element evaluates to true. | |
1693 | * | |
1694 | * @li An error occurred. | |
1695 | * | |
1696 | * This operation is implemented in terms of zero or more calls to the stream's | |
1697 | * async_read_some function, and is known as a <em>composed operation</em>. If | |
1698 | * the match condition function object already indicates a match, this | |
1699 | * asynchronous operation completes immediately. The program must ensure that | |
1700 | * the stream performs no other read operations (such as async_read, | |
1701 | * async_read_until, the stream's async_read_some function, or any other | |
1702 | * composed operations that perform reads) until this operation completes. | |
1703 | * | |
1704 | * @param s The stream from which the data is to be read. The type must support | |
1705 | * the AsyncReadStream concept. | |
1706 | * | |
1707 | * @param b A streambuf object into which the data will be read. | |
1708 | * | |
1709 | * @param match_condition The function object to be called to determine whether | |
1710 | * a match exists. The signature of the function object must be: | |
1711 | * @code pair<iterator, bool> match_condition(iterator begin, iterator end); | |
1712 | * @endcode | |
1713 | * where @c iterator represents the type: | |
1714 | * @code buffers_iterator<basic_streambuf<Allocator>::const_buffers_type> | |
1715 | * @endcode | |
1716 | * The iterator parameters @c begin and @c end define the range of bytes to be | |
1717 | * scanned to determine whether there is a match. The @c first member of the | |
1718 | * return value is an iterator marking one-past-the-end of the bytes that have | |
1719 | * been consumed by the match function. This iterator is used to calculate the | |
1720 | * @c begin parameter for any subsequent invocation of the match condition. The | |
1721 | * @c second member of the return value is true if a match has been found, false | |
1722 | * otherwise. | |
1723 | * | |
1724 | * @param handler The handler to be called when the read operation completes. | |
1725 | * Copies will be made of the handler as required. The function signature of the | |
1726 | * handler must be: | |
1727 | * @code void handler( | |
1728 | * // Result of operation. | |
1729 | * const boost::system::error_code& error, | |
1730 | * | |
1731 | * // The number of bytes in the streambuf's get | |
1732 | * // area that have been fully consumed by the | |
1733 | * // match function. O if an error occurred. | |
1734 | * std::size_t bytes_transferred | |
1735 | * ); @endcode | |
1736 | * Regardless of whether the asynchronous operation completes immediately or | |
1737 | * not, the handler will not be invoked from within this function. Invocation of | |
1738 | * the handler will be performed in a manner equivalent to using | |
1739 | * boost::asio::io_context::post(). | |
1740 | * | |
1741 | * @note After a successful async_read_until operation, the streambuf may | |
1742 | * contain additional data beyond that which matched the function object. An | |
1743 | * application will typically leave that data in the streambuf for a subsequent | |
1744 | * async_read_until operation to examine. | |
1745 | * | |
1746 | * @note The default implementation of the @c is_match_condition type trait | |
1747 | * evaluates to true for function pointers and function objects with a | |
1748 | * @c result_type typedef. It must be specialised for other user-defined | |
1749 | * function objects. | |
1750 | * | |
1751 | * @par Examples | |
1752 | * To asynchronously read data into a streambuf until whitespace is encountered: | |
1753 | * @code typedef boost::asio::buffers_iterator< | |
1754 | * boost::asio::streambuf::const_buffers_type> iterator; | |
1755 | * | |
1756 | * std::pair<iterator, bool> | |
1757 | * match_whitespace(iterator begin, iterator end) | |
1758 | * { | |
1759 | * iterator i = begin; | |
1760 | * while (i != end) | |
1761 | * if (std::isspace(*i++)) | |
1762 | * return std::make_pair(i, true); | |
1763 | * return std::make_pair(i, false); | |
1764 | * } | |
1765 | * ... | |
1766 | * void handler(const boost::system::error_code& e, std::size_t size); | |
1767 | * ... | |
1768 | * boost::asio::streambuf b; | |
1769 | * boost::asio::async_read_until(s, b, match_whitespace, handler); | |
1770 | * @endcode | |
1771 | * | |
1772 | * To asynchronously read data into a streambuf until a matching character is | |
1773 | * found: | |
1774 | * @code class match_char | |
1775 | * { | |
1776 | * public: | |
1777 | * explicit match_char(char c) : c_(c) {} | |
1778 | * | |
1779 | * template <typename Iterator> | |
1780 | * std::pair<Iterator, bool> operator()( | |
1781 | * Iterator begin, Iterator end) const | |
1782 | * { | |
1783 | * Iterator i = begin; | |
1784 | * while (i != end) | |
1785 | * if (c_ == *i++) | |
1786 | * return std::make_pair(i, true); | |
1787 | * return std::make_pair(i, false); | |
1788 | * } | |
1789 | * | |
1790 | * private: | |
1791 | * char c_; | |
1792 | * }; | |
1793 | * | |
1794 | * namespace asio { | |
1795 | * template <> struct is_match_condition<match_char> | |
1796 | * : public boost::true_type {}; | |
1797 | * } // namespace asio | |
1798 | * ... | |
1799 | * void handler(const boost::system::error_code& e, std::size_t size); | |
1800 | * ... | |
1801 | * boost::asio::streambuf b; | |
1802 | * boost::asio::async_read_until(s, b, match_char('a'), handler); | |
1803 | * @endcode | |
1804 | */ | |
1805 | template <typename AsyncReadStream, typename Allocator, | |
1806 | typename MatchCondition, typename ReadHandler> | |
1807 | BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler, | |
1808 | void (boost::system::error_code, std::size_t)) | |
1809 | async_read_until(AsyncReadStream& s, | |
1810 | boost::asio::basic_streambuf<Allocator>& b, | |
1811 | MatchCondition match_condition, BOOST_ASIO_MOVE_ARG(ReadHandler) handler, | |
1812 | typename enable_if<is_match_condition<MatchCondition>::value>::type* = 0); | |
1813 | ||
1814 | #endif // !defined(BOOST_ASIO_NO_IOSTREAM) | |
1815 | #endif // !defined(BOOST_ASIO_NO_EXTENSIONS) | |
1816 | ||
1817 | /*@}*/ | |
1818 | ||
1819 | } // namespace asio | |
1820 | } // namespace boost | |
1821 | ||
1822 | #include <boost/asio/detail/pop_options.hpp> | |
1823 | ||
1824 | #include <boost/asio/impl/read_until.hpp> | |
1825 | ||
1826 | #endif // BOOST_ASIO_READ_UNTIL_HPP |