1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>async_read_until (7 of 12 overloads)</title> 5<link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css"> 6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> 7<link rel="home" href="../../../boost_asio.html" title="Boost.Asio"> 8<link rel="up" href="../async_read_until.html" title="async_read_until"> 9<link rel="prev" href="overload6.html" title="async_read_until (6 of 12 overloads)"> 10<link rel="next" href="overload8.html" title="async_read_until (8 of 12 overloads)"> 11</head> 12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> 13<table cellpadding="2" width="100%"><tr> 14<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td> 15<td align="center"><a href="../../../../../index.html">Home</a></td> 16<td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td> 17<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> 18<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> 19<td align="center"><a href="../../../../../more/index.htm">More</a></td> 20</tr></table> 21<hr> 22<div class="spirit-nav"> 23<a accesskey="p" href="overload6.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../async_read_until.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../boost_asio.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="overload8.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> 24</div> 25<div class="section"> 26<div class="titlepage"><div><div><h4 class="title"> 27<a name="boost_asio.reference.async_read_until.overload7"></a><a class="link" href="overload7.html" title="async_read_until (7 of 12 overloads)">async_read_until 28 (7 of 12 overloads)</a> 29</h4></div></div></div> 30<p> 31 Start an asynchronous operation to read data into a streambuf until some 32 part of its data matches a regular expression. 33 </p> 34<pre class="programlisting">template< 35 typename <a class="link" href="../AsyncReadStream.html" title="Buffer-oriented asynchronous read stream requirements">AsyncReadStream</a>, 36 typename Allocator, 37 typename <a class="link" href="../ReadHandler.html" title="Read handler requirements">ReadHandler</a> = <a class="link" href="../asynchronous_operations.html#boost_asio.reference.asynchronous_operations.default_completion_tokens"><span class="emphasis"><em>DEFAULT</em></span></a>> 38<a class="link" href="../asynchronous_operations.html#boost_asio.reference.asynchronous_operations.automatic_deduction_of_initiating_function_return_type"><span class="emphasis"><em>DEDUCED</em></span></a> async_read_until( 39 AsyncReadStream & s, 40 boost::asio::basic_streambuf< Allocator > & b, 41 const boost::regex & expr, 42 ReadHandler && handler = <a class="link" href="../asynchronous_operations.html#boost_asio.reference.asynchronous_operations.default_completion_tokens"><span class="emphasis"><em>DEFAULT</em></span></a>); 43</pre> 44<p> 45 This function is used to asynchronously read data into the specified streambuf 46 until the streambuf's get area contains some data that matches a regular 47 expression. The function call always returns immediately. The asynchronous 48 operation will continue until one of the following conditions is true: 49 </p> 50<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> 51<li class="listitem"> 52 A substring of the streambuf's get area matches the regular expression. 53 </li> 54<li class="listitem"> 55 An error occurred. 56 </li> 57</ul></div> 58<p> 59 This operation is implemented in terms of zero or more calls to the stream's 60 async_read_some function, and is known as a <span class="emphasis"><em>composed operation</em></span>. 61 If the streambuf's get area already contains data that matches the regular 62 expression, this asynchronous operation completes immediately. The program 63 must ensure that the stream performs no other read operations (such as 64 async_read, async_read_until, the stream's async_read_some function, or 65 any other composed operations that perform reads) until this operation 66 completes. 67 </p> 68<h6> 69<a name="boost_asio.reference.async_read_until.overload7.h0"></a> 70 <span class="phrase"><a name="boost_asio.reference.async_read_until.overload7.parameters"></a></span><a class="link" href="overload7.html#boost_asio.reference.async_read_until.overload7.parameters">Parameters</a> 71 </h6> 72<div class="variablelist"> 73<p class="title"><b></b></p> 74<dl class="variablelist"> 75<dt><span class="term">s</span></dt> 76<dd><p> 77 The stream from which the data is to be read. The type must support 78 the AsyncReadStream concept. 79 </p></dd> 80<dt><span class="term">b</span></dt> 81<dd><p> 82 A streambuf object into which the data will be read. Ownership of 83 the streambuf is retained by the caller, which must guarantee that 84 it remains valid until the handler is called. 85 </p></dd> 86<dt><span class="term">expr</span></dt> 87<dd><p> 88 The regular expression. 89 </p></dd> 90<dt><span class="term">handler</span></dt> 91<dd> 92<p> 93 The handler to be called when the read operation completes. Copies 94 will be made of the handler as required. The function signature of 95 the handler must be: 96</p> 97<pre class="programlisting">void handler( 98 // Result of operation. 99 const boost::system::error_code& error, 100 101 // The number of bytes in the streambuf's get 102 // area up to and including the substring 103 // that matches the regular. expression. 104 // 0 if an error occurred. 105 std::size_t bytes_transferred 106); 107</pre> 108<p> 109 Regardless of whether the asynchronous operation completes immediately 110 or not, the handler will not be invoked from within this function. 111 On immediate completion, invocation of the handler will be performed 112 in a manner equivalent to using <a class="link" href="../post.html" title="post"><code class="computeroutput">post</code></a>. 113 </p> 114</dd> 115</dl> 116</div> 117<h6> 118<a name="boost_asio.reference.async_read_until.overload7.h1"></a> 119 <span class="phrase"><a name="boost_asio.reference.async_read_until.overload7.remarks"></a></span><a class="link" href="overload7.html#boost_asio.reference.async_read_until.overload7.remarks">Remarks</a> 120 </h6> 121<p> 122 After a successful async_read_until operation, the streambuf may contain 123 additional data beyond that which matched the regular expression. An application 124 will typically leave that data in the streambuf for a subsequent async_read_until 125 operation to examine. 126 </p> 127<h6> 128<a name="boost_asio.reference.async_read_until.overload7.h2"></a> 129 <span class="phrase"><a name="boost_asio.reference.async_read_until.overload7.example"></a></span><a class="link" href="overload7.html#boost_asio.reference.async_read_until.overload7.example">Example</a> 130 </h6> 131<p> 132 To asynchronously read data into a streambuf until a CR-LF sequence is 133 encountered: 134 </p> 135<pre class="programlisting">boost::asio::streambuf b; 136... 137void handler(const boost::system::error_code& e, std::size_t size) 138{ 139 if (!e) 140 { 141 std::istream is(&b); 142 std::string line; 143 std::getline(is, line); 144 ... 145 } 146} 147... 148boost::asio::async_read_until(s, b, boost::regex("\r\n"), handler); 149</pre> 150<p> 151 After the <code class="computeroutput">async_read_until</code> operation completes successfully, 152 the buffer <code class="computeroutput">b</code> contains the data which matched the regular expression: 153 </p> 154<pre class="programlisting">{ 'a', 'b', ..., 'c', '\r', '\n', 'd', 'e', ... } 155</pre> 156<p> 157 The call to <code class="computeroutput">std::getline</code> then extracts the data up to and 158 including the newline (which is discarded), so that the string <code class="computeroutput">line</code> 159 contains: 160 </p> 161<pre class="programlisting">{ 'a', 'b', ..., 'c', '\r' } 162</pre> 163<p> 164 The remaining data is left in the buffer <code class="computeroutput">b</code> as follows: 165 </p> 166<pre class="programlisting">{ 'd', 'e', ... } 167</pre> 168<p> 169 This data may be the start of a new line, to be extracted by a subsequent 170 <code class="computeroutput">async_read_until</code> operation. 171 </p> 172</div> 173<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 174<td align="left"></td> 175<td align="right"><div class="copyright-footer">Copyright © 2003-2020 Christopher M. 176 Kohlhoff<p> 177 Distributed under the Boost Software License, Version 1.0. (See accompanying 178 file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) 179 </p> 180</div></td> 181</tr></table> 182<hr> 183<div class="spirit-nav"> 184<a accesskey="p" href="overload6.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../async_read_until.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../boost_asio.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="overload8.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> 185</div> 186</body> 187</html> 188