read_until.hpp 36 KB

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