Imported Upstream version 1.57.0
[platform/upstream/boost.git] / doc / html / boost_asio / reference / async_read_until / overload1.html
1 <html>
2 <head>
3 <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
4 <title>async_read_until (1 of 4 overloads)</title>
5 <link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css">
6 <meta name="generator" content="DocBook XSL Stylesheets V1.78.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="../async_read_until.html" title="async_read_until">
10 <link rel="next" href="overload2.html" title="async_read_until (2 of 4 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="../async_read_until.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="overload2.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.overload1"></a><a class="link" href="overload1.html" title="async_read_until (1 of 4 overloads)">async_read_until
28         (1 of 4 overloads)</a>
29 </h4></div></div></div>
30 <p>
31           Start an asynchronous operation to read data into a streambuf until it
32           contains a specified delimiter.
33         </p>
34 <pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span>
35     <span class="keyword">typename</span> <a class="link" href="../AsyncReadStream.html" title="Buffer-oriented asynchronous read stream requirements">AsyncReadStream</a><span class="special">,</span>
36     <span class="keyword">typename</span> <span class="identifier">Allocator</span><span class="special">,</span>
37     <span class="keyword">typename</span> <a class="link" href="../ReadHandler.html" title="Read handler requirements">ReadHandler</a><span class="special">&gt;</span>
38 <a class="link" href="../asynchronous_operations.html#boost_asio.reference.asynchronous_operations.return_type_of_an_initiating_function"><span class="emphasis"><em>void-or-deduced</em></span></a> <span class="identifier">async_read_until</span><span class="special">(</span>
39     <span class="identifier">AsyncReadStream</span> <span class="special">&amp;</span> <span class="identifier">s</span><span class="special">,</span>
40     <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">basic_streambuf</span><span class="special">&lt;</span> <span class="identifier">Allocator</span> <span class="special">&gt;</span> <span class="special">&amp;</span> <span class="identifier">b</span><span class="special">,</span>
41     <span class="keyword">char</span> <span class="identifier">delim</span><span class="special">,</span>
42     <span class="identifier">ReadHandler</span> <span class="identifier">handler</span><span class="special">);</span>
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 the specified delimiter. The function
47           call always returns immediately. The asynchronous operation will continue
48           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               The get area of the streambuf contains the specified delimiter.
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 the delimiter, this asynchronous
62           operation completes immediately. The program must ensure that the stream
63           performs no other read operations (such as async_read, async_read_until,
64           the stream's async_read_some function, or any other composed operations
65           that perform reads) until this operation completes.
66         </p>
67 <h6>
68 <a name="boost_asio.reference.async_read_until.overload1.h0"></a>
69           <span class="phrase"><a name="boost_asio.reference.async_read_until.overload1.parameters"></a></span><a class="link" href="overload1.html#boost_asio.reference.async_read_until.overload1.parameters">Parameters</a>
70         </h6>
71 <div class="variablelist">
72 <p class="title"><b></b></p>
73 <dl class="variablelist">
74 <dt><span class="term">s</span></dt>
75 <dd><p>
76                 The stream from which the data is to be read. The type must support
77                 the AsyncReadStream concept.
78               </p></dd>
79 <dt><span class="term">b</span></dt>
80 <dd><p>
81                 A streambuf object into which the data will be read. Ownership of
82                 the streambuf is retained by the caller, which must guarantee that
83                 it remains valid until the handler is called.
84               </p></dd>
85 <dt><span class="term">delim</span></dt>
86 <dd><p>
87                 The delimiter character.
88               </p></dd>
89 <dt><span class="term">handler</span></dt>
90 <dd>
91 <p>
92                 The handler to be called when the read operation completes. Copies
93                 will be made of the handler as required. The function signature of
94                 the handler must be:
95 </p>
96 <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">handler</span><span class="special">(</span>
97   <span class="comment">// Result of operation.</span>
98   <span class="keyword">const</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">system</span><span class="special">::</span><span class="identifier">error_code</span><span class="special">&amp;</span> <span class="identifier">error</span><span class="special">,</span>
99
100   <span class="comment">// The number of bytes in the streambuf's get</span>
101   <span class="comment">// area up to and including the delimiter.</span>
102   <span class="comment">// 0 if an error occurred.</span>
103   <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">bytes_transferred</span>
104 <span class="special">);</span>
105 </pre>
106 <p>
107                 Regardless of whether the asynchronous operation completes immediately
108                 or not, the handler will not be invoked from within this function.
109                 Invocation of the handler will be performed in a manner equivalent
110                 to using <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">io_service</span><span class="special">::</span><span class="identifier">post</span><span class="special">()</span></code>.
111               </p>
112 </dd>
113 </dl>
114 </div>
115 <h6>
116 <a name="boost_asio.reference.async_read_until.overload1.h1"></a>
117           <span class="phrase"><a name="boost_asio.reference.async_read_until.overload1.remarks"></a></span><a class="link" href="overload1.html#boost_asio.reference.async_read_until.overload1.remarks">Remarks</a>
118         </h6>
119 <p>
120           After a successful async_read_until operation, the streambuf may contain
121           additional data beyond the delimiter. An application will typically leave
122           that data in the streambuf for a subsequent async_read_until operation
123           to examine.
124         </p>
125 <h6>
126 <a name="boost_asio.reference.async_read_until.overload1.h2"></a>
127           <span class="phrase"><a name="boost_asio.reference.async_read_until.overload1.example"></a></span><a class="link" href="overload1.html#boost_asio.reference.async_read_until.overload1.example">Example</a>
128         </h6>
129 <p>
130           To asynchronously read data into a streambuf until a newline is encountered:
131         </p>
132 <pre class="programlisting"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">streambuf</span> <span class="identifier">b</span><span class="special">;</span>
133 <span class="special">...</span>
134 <span class="keyword">void</span> <span class="identifier">handler</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">system</span><span class="special">::</span><span class="identifier">error_code</span><span class="special">&amp;</span> <span class="identifier">e</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">size</span><span class="special">)</span>
135 <span class="special">{</span>
136   <span class="keyword">if</span> <span class="special">(!</span><span class="identifier">e</span><span class="special">)</span>
137   <span class="special">{</span>
138     <span class="identifier">std</span><span class="special">::</span><span class="identifier">istream</span> <span class="identifier">is</span><span class="special">(&amp;</span><span class="identifier">b</span><span class="special">);</span>
139     <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">line</span><span class="special">;</span>
140     <span class="identifier">std</span><span class="special">::</span><span class="identifier">getline</span><span class="special">(</span><span class="identifier">is</span><span class="special">,</span> <span class="identifier">line</span><span class="special">);</span>
141     <span class="special">...</span>
142   <span class="special">}</span>
143 <span class="special">}</span>
144 <span class="special">...</span>
145 <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">async_read_until</span><span class="special">(</span><span class="identifier">s</span><span class="special">,</span> <span class="identifier">b</span><span class="special">,</span> <span class="char">'\n'</span><span class="special">,</span> <span class="identifier">handler</span><span class="special">);</span>
146 </pre>
147 <p>
148           After the <code class="computeroutput"><span class="identifier">async_read_until</span></code>
149           operation completes successfully, the buffer <code class="computeroutput"><span class="identifier">b</span></code>
150           contains the delimiter:
151         </p>
152 <pre class="programlisting"><span class="special">{</span> <span class="char">'a'</span><span class="special">,</span> <span class="char">'b'</span><span class="special">,</span> <span class="special">...,</span> <span class="char">'c'</span><span class="special">,</span> <span class="char">'\n'</span><span class="special">,</span> <span class="char">'d'</span><span class="special">,</span> <span class="char">'e'</span><span class="special">,</span> <span class="special">...</span> <span class="special">}</span>
153 </pre>
154 <p>
155           The call to <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">getline</span></code> then extracts the data up to
156           and including the delimiter, so that the string <code class="computeroutput"><span class="identifier">line</span></code>
157           contains:
158         </p>
159 <pre class="programlisting"><span class="special">{</span> <span class="char">'a'</span><span class="special">,</span> <span class="char">'b'</span><span class="special">,</span> <span class="special">...,</span> <span class="char">'c'</span><span class="special">,</span> <span class="char">'\n'</span> <span class="special">}</span>
160 </pre>
161 <p>
162           The remaining data is left in the buffer <code class="computeroutput"><span class="identifier">b</span></code>
163           as follows:
164         </p>
165 <pre class="programlisting"><span class="special">{</span> <span class="char">'d'</span><span class="special">,</span> <span class="char">'e'</span><span class="special">,</span> <span class="special">...</span> <span class="special">}</span>
166 </pre>
167 <p>
168           This data may be the start of a new line, to be extracted by a subsequent
169           <code class="computeroutput"><span class="identifier">async_read_until</span></code> operation.
170         </p>
171 </div>
172 <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
173 <td align="left"></td>
174 <td align="right"><div class="copyright-footer">Copyright &#169; 2003-2014 Christopher M. Kohlhoff<p>
175         Distributed under the Boost Software License, Version 1.0. (See accompanying
176         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>)
177       </p>
178 </div></td>
179 </tr></table>
180 <hr>
181 <div class="spirit-nav">
182 <a accesskey="p" href="../async_read_until.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="overload2.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
183 </div>
184 </body>
185 </html>