read_until.hpp 32 KB

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