read_until.hpp 36 KB

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