read_until.hpp 36 KB

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