read_until.hpp 36 KB

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