read_until.hpp 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827
  1. //
  2. // read_until.hpp
  3. // ~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2018 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. #include <cstddef>
  17. #include <string>
  18. #include <boost/asio/async_result.hpp>
  19. #include <boost/asio/detail/regex_fwd.hpp>
  20. #include <boost/asio/detail/string_view.hpp>
  21. #include <boost/asio/detail/type_traits.hpp>
  22. #include <boost/asio/error.hpp>
  23. #if !defined(BOOST_ASIO_NO_EXTENSIONS)
  24. # include <boost/asio/basic_streambuf_fwd.hpp>
  25. #endif // !defined(BOOST_ASIO_NO_EXTENSIONS)
  26. #include <boost/asio/detail/push_options.hpp>
  27. namespace boost {
  28. namespace asio {
  29. namespace detail
  30. {
  31. char (&has_result_type_helper(...))[2];
  32. template <typename T>
  33. char has_result_type_helper(T*, typename T::result_type* = 0);
  34. template <typename T>
  35. struct has_result_type
  36. {
  37. enum { value = (sizeof((has_result_type_helper)((T*)(0))) == 1) };
  38. };
  39. } // namespace detail
  40. /// Type trait used to determine whether a type can be used as a match condition
  41. /// function with read_until and async_read_until.
  42. template <typename T>
  43. struct is_match_condition
  44. {
  45. #if defined(GENERATING_DOCUMENTATION)
  46. /// The value member is true if the type may be used as a match condition.
  47. static const bool value;
  48. #else
  49. enum
  50. {
  51. value = boost::asio::is_function<
  52. typename boost::asio::remove_pointer<T>::type>::value
  53. || detail::has_result_type<T>::value
  54. };
  55. #endif
  56. };
  57. /**
  58. * @defgroup read_until boost::asio::read_until
  59. *
  60. * @brief Read data into a dynamic buffer sequence, or into a streambuf, until
  61. * it contains a delimiter, matches a regular expression, or a function object
  62. * indicates a match.
  63. */
  64. /*@{*/
  65. /// Read data into a dynamic buffer sequence until it contains a specified
  66. /// delimiter.
  67. /**
  68. * This function is used to read data into the specified dynamic buffer
  69. * sequence until the dynamic buffer sequence's get area contains the specified
  70. * delimiter. The call will block until one of the following conditions is
  71. * true:
  72. *
  73. * @li The get area of the dynamic buffer sequence contains the specified
  74. * delimiter.
  75. *
  76. * @li An error occurred.
  77. *
  78. * This operation is implemented in terms of zero or more calls to the stream's
  79. * read_some function. If the dynamic buffer sequence's get area already
  80. * contains the delimiter, the function returns immediately.
  81. *
  82. * @param s The stream from which the data is to be read. The type must support
  83. * the SyncReadStream concept.
  84. *
  85. * @param buffers The dynamic buffer sequence into which the data will be read.
  86. *
  87. * @param delim The delimiter character.
  88. *
  89. * @returns The number of bytes in the dynamic buffer sequence's get area up to
  90. * and including the delimiter.
  91. *
  92. * @throws boost::system::system_error Thrown on failure.
  93. *
  94. * @note After a successful read_until operation, the dynamic buffer sequence
  95. * may contain additional data beyond the delimiter. An application will
  96. * typically leave that data in the dynamic buffer sequence for a subsequent
  97. * read_until operation to examine.
  98. *
  99. * @par Example
  100. * To read data into a @c std::string until a newline is encountered:
  101. * @code std::string data;
  102. * std::string n = boost::asio::read_until(s,
  103. * boost::asio::dynamic_buffer(data), '\n');
  104. * std::string line = data.substr(0, n);
  105. * data.erase(0, n); @endcode
  106. * After the @c read_until operation completes successfully, the string @c data
  107. * contains the delimiter:
  108. * @code { 'a', 'b', ..., 'c', '\n', 'd', 'e', ... } @endcode
  109. * The call to @c substr then extracts the data up to and including the
  110. * delimiter, so that the string @c line contains:
  111. * @code { 'a', 'b', ..., 'c', '\n' } @endcode
  112. * After the call to @c erase, the remaining data is left in the buffer @c b as
  113. * follows:
  114. * @code { 'd', 'e', ... } @endcode
  115. * This data may be the start of a new line, to be extracted by a subsequent
  116. * @c read_until operation.
  117. */
  118. template <typename SyncReadStream, typename DynamicBuffer>
  119. std::size_t read_until(SyncReadStream& s,
  120. BOOST_ASIO_MOVE_ARG(DynamicBuffer) buffers, char delim);
  121. /// Read data into a dynamic buffer sequence until it contains a specified
  122. /// delimiter.
  123. /**
  124. * This function is used to read data into the specified dynamic buffer
  125. * sequence until the dynamic buffer sequence's get area contains the specified
  126. * delimiter. The call will block until one of the following conditions is
  127. * true:
  128. *
  129. * @li The get area of the dynamic buffer sequence contains the specified
  130. * delimiter.
  131. *
  132. * @li An error occurred.
  133. *
  134. * This operation is implemented in terms of zero or more calls to the stream's
  135. * read_some function. If the dynamic buffer sequence's get area already
  136. * contains the delimiter, the function returns immediately.
  137. *
  138. * @param s The stream from which the data is to be read. The type must support
  139. * the SyncReadStream concept.
  140. *
  141. * @param buffers The dynamic buffer sequence into which the data will be read.
  142. *
  143. * @param delim The delimiter character.
  144. *
  145. * @param ec Set to indicate what error occurred, if any.
  146. *
  147. * @returns The number of bytes in the dynamic buffer sequence's get area up to
  148. * and including the delimiter. Returns 0 if an error occurred.
  149. *
  150. * @note After a successful read_until operation, the dynamic buffer sequence
  151. * may contain additional data beyond the delimiter. An application will
  152. * typically leave that data in the dynamic buffer sequence for a subsequent
  153. * read_until operation to examine.
  154. */
  155. template <typename SyncReadStream, typename DynamicBuffer>
  156. std::size_t read_until(SyncReadStream& s,
  157. BOOST_ASIO_MOVE_ARG(DynamicBuffer) buffers,
  158. char delim, boost::system::error_code& ec);
  159. /// Read data into a dynamic buffer sequence until it contains a specified
  160. /// delimiter.
  161. /**
  162. * This function is used to read data into the specified dynamic buffer
  163. * sequence until the dynamic buffer sequence's get area contains the specified
  164. * delimiter. The call will block until one of the following conditions is
  165. * true:
  166. *
  167. * @li The get area of the dynamic buffer sequence contains the specified
  168. * delimiter.
  169. *
  170. * @li An error occurred.
  171. *
  172. * This operation is implemented in terms of zero or more calls to the stream's
  173. * read_some function. If the dynamic buffer sequence's get area already
  174. * contains the delimiter, the function returns immediately.
  175. *
  176. * @param s The stream from which the data is to be read. The type must support
  177. * the SyncReadStream concept.
  178. *
  179. * @param buffers The dynamic buffer sequence into which the data will be read.
  180. *
  181. * @param delim The delimiter string.
  182. *
  183. * @returns The number of bytes in the dynamic buffer sequence's get area up to
  184. * and including the delimiter.
  185. *
  186. * @note After a successful read_until operation, the dynamic buffer sequence
  187. * may contain additional data beyond the delimiter. An application will
  188. * typically leave that data in the dynamic buffer sequence for a subsequent
  189. * read_until operation to examine.
  190. *
  191. * @par Example
  192. * To read data into a @c std::string until a CR-LF sequence is encountered:
  193. * @code std::string data;
  194. * std::string n = boost::asio::read_until(s,
  195. * boost::asio::dynamic_buffer(data), "\r\n");
  196. * std::string line = data.substr(0, n);
  197. * data.erase(0, n); @endcode
  198. * After the @c read_until operation completes successfully, the string @c data
  199. * contains the delimiter:
  200. * @code { 'a', 'b', ..., 'c', '\r', '\n', 'd', 'e', ... } @endcode
  201. * The call to @c substr then extracts the data up to and including the
  202. * delimiter, so that the string @c line contains:
  203. * @code { 'a', 'b', ..., 'c', '\r', '\n' } @endcode
  204. * After the call to @c erase, the remaining data is left in the buffer @c b as
  205. * follows:
  206. * @code { 'd', 'e', ... } @endcode
  207. * This data may be the start of a new line, to be extracted by a subsequent
  208. * @c read_until operation.
  209. */
  210. template <typename SyncReadStream, typename DynamicBuffer>
  211. std::size_t read_until(SyncReadStream& s,
  212. BOOST_ASIO_MOVE_ARG(DynamicBuffer) buffers,
  213. BOOST_ASIO_STRING_VIEW_PARAM delim);
  214. /// Read data into a dynamic buffer sequence until it contains a specified
  215. /// delimiter.
  216. /**
  217. * This function is used to read data into the specified dynamic buffer
  218. * sequence until the dynamic buffer sequence's get area contains the specified
  219. * delimiter. The call will block until one of the following conditions is
  220. * true:
  221. *
  222. * @li The get area of the dynamic buffer sequence contains the specified
  223. * delimiter.
  224. *
  225. * @li An error occurred.
  226. *
  227. * This operation is implemented in terms of zero or more calls to the stream's
  228. * read_some function. If the dynamic buffer sequence's get area already
  229. * contains the delimiter, the function returns immediately.
  230. *
  231. * @param s The stream from which the data is to be read. The type must support
  232. * the SyncReadStream concept.
  233. *
  234. * @param buffers The dynamic buffer sequence into which the data will be read.
  235. *
  236. * @param delim The delimiter string.
  237. *
  238. * @param ec Set to indicate what error occurred, if any.
  239. *
  240. * @returns The number of bytes in the dynamic buffer sequence's get area up to
  241. * and including the delimiter. Returns 0 if an error occurred.
  242. *
  243. * @note After a successful read_until operation, the dynamic buffer sequence
  244. * may contain additional data beyond the delimiter. An application will
  245. * typically leave that data in the dynamic buffer sequence for a subsequent
  246. * read_until operation to examine.
  247. */
  248. template <typename SyncReadStream, typename DynamicBuffer>
  249. std::size_t read_until(SyncReadStream& s,
  250. BOOST_ASIO_MOVE_ARG(DynamicBuffer) buffers,
  251. BOOST_ASIO_STRING_VIEW_PARAM delim,
  252. boost::system::error_code& ec);
  253. #if !defined(BOOST_ASIO_NO_EXTENSIONS)
  254. #if defined(BOOST_ASIO_HAS_BOOST_REGEX) \
  255. || defined(GENERATING_DOCUMENTATION)
  256. /// Read data into a dynamic buffer sequence until some part of the data it
  257. /// contains matches a regular expression.
  258. /**
  259. * This function is used to read data into the specified dynamic buffer
  260. * sequence until the dynamic buffer sequence's get area contains some data
  261. * that matches a regular expression. The call will block until one of the
  262. * following conditions is true:
  263. *
  264. * @li A substring of the dynamic buffer sequence's get area matches the
  265. * regular expression.
  266. *
  267. * @li An error occurred.
  268. *
  269. * This operation is implemented in terms of zero or more calls to the stream's
  270. * read_some function. If the dynamic buffer sequence's get area already
  271. * contains data that matches the regular expression, the function returns
  272. * immediately.
  273. *
  274. * @param s The stream from which the data is to be read. The type must support
  275. * the SyncReadStream concept.
  276. *
  277. * @param buffers A dynamic buffer sequence into which the data will be read.
  278. *
  279. * @param expr The regular expression.
  280. *
  281. * @returns The number of bytes in the dynamic buffer sequence's get area up to
  282. * and including the substring that matches the regular expression.
  283. *
  284. * @throws boost::system::system_error Thrown on failure.
  285. *
  286. * @note After a successful read_until operation, the dynamic buffer sequence
  287. * may contain additional data beyond that which matched the regular
  288. * expression. An application will typically leave that data in the dynamic
  289. * buffer sequence for a subsequent read_until operation to examine.
  290. *
  291. * @par Example
  292. * To read data into a @c std::string until a CR-LF sequence is encountered:
  293. * @code std::string data;
  294. * std::string n = boost::asio::read_until(s,
  295. * boost::asio::dynamic_buffer(data), boost::regex("\r\n"));
  296. * std::string line = data.substr(0, n);
  297. * data.erase(0, n); @endcode
  298. * After the @c read_until operation completes successfully, the string @c data
  299. * contains the delimiter:
  300. * @code { 'a', 'b', ..., 'c', '\r', '\n', 'd', 'e', ... } @endcode
  301. * The call to @c substr then extracts the data up to and including the
  302. * delimiter, so that the string @c line contains:
  303. * @code { 'a', 'b', ..., 'c', '\r', '\n' } @endcode
  304. * After the call to @c erase, the remaining data is left in the buffer @c b as
  305. * follows:
  306. * @code { 'd', 'e', ... } @endcode
  307. * This data may be the start of a new line, to be extracted by a subsequent
  308. * @c read_until operation.
  309. */
  310. template <typename SyncReadStream, typename DynamicBuffer>
  311. std::size_t read_until(SyncReadStream& s,
  312. BOOST_ASIO_MOVE_ARG(DynamicBuffer) buffers,
  313. const boost::regex& expr);
  314. /// Read data into a dynamic buffer sequence until some part of the data it
  315. /// contains matches a regular expression.
  316. /**
  317. * This function is used to read data into the specified dynamic buffer
  318. * sequence until the dynamic buffer sequence's get area contains some data
  319. * that matches a regular expression. The call will block until one of the
  320. * following conditions is true:
  321. *
  322. * @li A substring of the dynamic buffer sequence's get area matches the
  323. * regular expression.
  324. *
  325. * @li An error occurred.
  326. *
  327. * This operation is implemented in terms of zero or more calls to the stream's
  328. * read_some function. If the dynamic buffer sequence's get area already
  329. * contains data that matches the regular expression, the function returns
  330. * immediately.
  331. *
  332. * @param s The stream from which the data is to be read. The type must support
  333. * the SyncReadStream concept.
  334. *
  335. * @param buffers A dynamic buffer sequence into which the data will be read.
  336. *
  337. * @param expr The regular expression.
  338. *
  339. * @param ec Set to indicate what error occurred, if any.
  340. *
  341. * @returns The number of bytes in the dynamic buffer sequence's get area up to
  342. * and including the substring that matches the regular expression. Returns 0
  343. * if an error occurred.
  344. *
  345. * @note After a successful read_until operation, the dynamic buffer sequence
  346. * may contain additional data beyond that which matched the regular
  347. * expression. An application will typically leave that data in the dynamic
  348. * buffer sequence for a subsequent read_until operation to examine.
  349. */
  350. template <typename SyncReadStream, typename DynamicBuffer>
  351. std::size_t read_until(SyncReadStream& s,
  352. BOOST_ASIO_MOVE_ARG(DynamicBuffer) buffers,
  353. const boost::regex& expr, boost::system::error_code& ec);
  354. #endif // defined(BOOST_ASIO_HAS_BOOST_REGEX)
  355. // || defined(GENERATING_DOCUMENTATION)
  356. /// Read data into a dynamic buffer sequence until a function object indicates a
  357. /// match.
  358. /**
  359. * This function is used to read data into the specified dynamic buffer
  360. * sequence until a user-defined match condition function object, when applied
  361. * to the data contained in the dynamic buffer sequence, indicates a successful
  362. * match. The call will block until one of the following conditions is true:
  363. *
  364. * @li The match condition function object returns a std::pair where the second
  365. * element evaluates to true.
  366. *
  367. * @li An error occurred.
  368. *
  369. * This operation is implemented in terms of zero or more calls to the stream's
  370. * read_some function. If the match condition function object already indicates
  371. * a match, the function returns immediately.
  372. *
  373. * @param s The stream from which the data is to be read. The type must support
  374. * the SyncReadStream concept.
  375. *
  376. * @param buffers A dynamic buffer sequence into which the data will be read.
  377. *
  378. * @param match_condition The function object to be called to determine whether
  379. * a match exists. The signature of the function object must be:
  380. * @code pair<iterator, bool> match_condition(iterator begin, iterator end);
  381. * @endcode
  382. * where @c iterator represents the type:
  383. * @code buffers_iterator<typename DynamicBuffer::const_buffers_type>
  384. * @endcode
  385. * The iterator parameters @c begin and @c end define the range of bytes to be
  386. * scanned to determine whether there is a match. The @c first member of the
  387. * return value is an iterator marking one-past-the-end of the bytes that have
  388. * been consumed by the match function. This iterator is used to calculate the
  389. * @c begin parameter for any subsequent invocation of the match condition. The
  390. * @c second member of the return value is true if a match has been found, false
  391. * otherwise.
  392. *
  393. * @returns The number of bytes in the dynamic_buffer's get area that
  394. * have been fully consumed by the match function.
  395. *
  396. * @throws boost::system::system_error Thrown on failure.
  397. *
  398. * @note After a successful read_until operation, the dynamic buffer sequence
  399. * may contain additional data beyond that which matched the function object.
  400. * An application will typically leave that data in the dynamic buffer sequence
  401. * for a subsequent read_until operation to examine.
  402. * @note The default implementation of the @c is_match_condition type trait
  403. * evaluates to true for function pointers and function objects with a
  404. * @c result_type typedef. It must be specialised for other user-defined
  405. * function objects.
  406. *
  407. * @par Examples
  408. * To read data into a dynamic buffer sequence until whitespace is encountered:
  409. * @code typedef boost::asio::buffers_iterator<
  410. * boost::asio::const_buffers_1> iterator;
  411. *
  412. * std::pair<iterator, bool>
  413. * match_whitespace(iterator begin, iterator end)
  414. * {
  415. * iterator i = begin;
  416. * while (i != end)
  417. * if (std::isspace(*i++))
  418. * return std::make_pair(i, true);
  419. * return std::make_pair(i, false);
  420. * }
  421. * ...
  422. * std::string data;
  423. * boost::asio::read_until(s, data, match_whitespace);
  424. * @endcode
  425. *
  426. * To read data into a @c std::string until a matching character is found:
  427. * @code class match_char
  428. * {
  429. * public:
  430. * explicit match_char(char c) : c_(c) {}
  431. *
  432. * template <typename Iterator>
  433. * std::pair<Iterator, bool> operator()(
  434. * Iterator begin, Iterator end) const
  435. * {
  436. * Iterator i = begin;
  437. * while (i != end)
  438. * if (c_ == *i++)
  439. * return std::make_pair(i, true);
  440. * return std::make_pair(i, false);
  441. * }
  442. *
  443. * private:
  444. * char c_;
  445. * };
  446. *
  447. * namespace asio {
  448. * template <> struct is_match_condition<match_char>
  449. * : public boost::true_type {};
  450. * } // namespace asio
  451. * ...
  452. * std::string data;
  453. * boost::asio::read_until(s, data, match_char('a'));
  454. * @endcode
  455. */
  456. template <typename SyncReadStream,
  457. typename DynamicBuffer, typename MatchCondition>
  458. std::size_t read_until(SyncReadStream& s,
  459. BOOST_ASIO_MOVE_ARG(DynamicBuffer) buffers,
  460. MatchCondition match_condition,
  461. typename enable_if<is_match_condition<MatchCondition>::value>::type* = 0);
  462. /// Read data into a dynamic buffer sequence until a function object indicates a
  463. /// match.
  464. /**
  465. * This function is used to read data into the specified dynamic buffer
  466. * sequence until a user-defined match condition function object, when applied
  467. * to the data contained in the dynamic buffer sequence, indicates a successful
  468. * match. The call will block until one of the following conditions is true:
  469. *
  470. * @li The match condition function object returns a std::pair where the second
  471. * element evaluates to true.
  472. *
  473. * @li An error occurred.
  474. *
  475. * This operation is implemented in terms of zero or more calls to the stream's
  476. * read_some function. If the match condition function object already indicates
  477. * a match, the function returns immediately.
  478. *
  479. * @param s The stream from which the data is to be read. The type must support
  480. * the SyncReadStream concept.
  481. *
  482. * @param buffers A dynamic buffer sequence into which the data will be read.
  483. *
  484. * @param match_condition The function object to be called to determine whether
  485. * a match exists. The signature of the function object must be:
  486. * @code pair<iterator, bool> match_condition(iterator begin, iterator end);
  487. * @endcode
  488. * where @c iterator represents the type:
  489. * @code buffers_iterator<DynamicBuffer::const_buffers_type>
  490. * @endcode
  491. * The iterator parameters @c begin and @c end define the range of bytes to be
  492. * scanned to determine whether there is a match. The @c first member of the
  493. * return value is an iterator marking one-past-the-end of the bytes that have
  494. * been consumed by the match function. This iterator is used to calculate the
  495. * @c begin parameter for any subsequent invocation of the match condition. The
  496. * @c second member of the return value is true if a match has been found, false
  497. * otherwise.
  498. *
  499. * @param ec Set to indicate what error occurred, if any.
  500. *
  501. * @returns The number of bytes in the dynamic buffer sequence's get area that
  502. * have been fully consumed by the match function. Returns 0 if an error
  503. * occurred.
  504. *
  505. * @note After a successful read_until operation, the dynamic buffer sequence
  506. * may contain additional data beyond that which matched the function object.
  507. * An application will typically leave that data in the dynamic buffer sequence
  508. * for a subsequent read_until operation to examine.
  509. *
  510. * @note The default implementation of the @c is_match_condition type trait
  511. * evaluates to true for function pointers and function objects with a
  512. * @c result_type typedef. It must be specialised for other user-defined
  513. * function objects.
  514. */
  515. template <typename SyncReadStream,
  516. typename DynamicBuffer, typename MatchCondition>
  517. std::size_t read_until(SyncReadStream& s,
  518. BOOST_ASIO_MOVE_ARG(DynamicBuffer) buffers,
  519. MatchCondition match_condition, boost::system::error_code& ec,
  520. typename enable_if<is_match_condition<MatchCondition>::value>::type* = 0);
  521. #if !defined(BOOST_ASIO_NO_IOSTREAM)
  522. /// Read data into a streambuf until it contains a specified delimiter.
  523. /**
  524. * This function is used to read data into the specified streambuf until the
  525. * streambuf's get area contains the specified delimiter. The call will block
  526. * until one of the following conditions is true:
  527. *
  528. * @li The get area of the streambuf contains the specified delimiter.
  529. *
  530. * @li An error occurred.
  531. *
  532. * This operation is implemented in terms of zero or more calls to the stream's
  533. * read_some function. If the streambuf's get area already contains the
  534. * delimiter, the function returns immediately.
  535. *
  536. * @param s The stream from which the data is to be read. The type must support
  537. * the SyncReadStream concept.
  538. *
  539. * @param b A streambuf object into which the data will be read.
  540. *
  541. * @param delim The delimiter character.
  542. *
  543. * @returns The number of bytes in the streambuf's get area up to and including
  544. * the delimiter.
  545. *
  546. * @throws boost::system::system_error Thrown on failure.
  547. *
  548. * @note After a successful read_until operation, the streambuf may contain
  549. * additional data beyond the delimiter. An application will typically leave
  550. * that data in the streambuf for a subsequent read_until operation to examine.
  551. *
  552. * @par Example
  553. * To read data into a streambuf until a newline is encountered:
  554. * @code boost::asio::streambuf b;
  555. * boost::asio::read_until(s, b, '\n');
  556. * std::istream is(&b);
  557. * std::string line;
  558. * std::getline(is, line); @endcode
  559. * After the @c read_until operation completes successfully, the buffer @c b
  560. * contains the delimiter:
  561. * @code { 'a', 'b', ..., 'c', '\n', 'd', 'e', ... } @endcode
  562. * The call to @c std::getline then extracts the data up to and including the
  563. * newline (which is discarded), so that the string @c line contains:
  564. * @code { 'a', 'b', ..., 'c' } @endcode
  565. * The remaining data is left in the buffer @c b as follows:
  566. * @code { 'd', 'e', ... } @endcode
  567. * This data may be the start of a new line, to be extracted by a subsequent
  568. * @c read_until operation.
  569. */
  570. template <typename SyncReadStream, typename Allocator>
  571. std::size_t read_until(SyncReadStream& s,
  572. boost::asio::basic_streambuf<Allocator>& b, char delim);
  573. /// Read data into a streambuf until it contains a specified delimiter.
  574. /**
  575. * This function is used to read data into the specified streambuf until the
  576. * streambuf's get area contains the specified delimiter. The call will block
  577. * until one of the following conditions is true:
  578. *
  579. * @li The get area of the streambuf contains the specified delimiter.
  580. *
  581. * @li An error occurred.
  582. *
  583. * This operation is implemented in terms of zero or more calls to the stream's
  584. * read_some function. If the streambuf's get area already contains the
  585. * delimiter, the function returns immediately.
  586. *
  587. * @param s The stream from which the data is to be read. The type must support
  588. * the SyncReadStream concept.
  589. *
  590. * @param b A streambuf object into which the data will be read.
  591. *
  592. * @param delim The delimiter character.
  593. *
  594. * @param ec Set to indicate what error occurred, if any.
  595. *
  596. * @returns The number of bytes in the streambuf's get area up to and including
  597. * the delimiter. Returns 0 if an error occurred.
  598. *
  599. * @note After a successful read_until operation, the streambuf may contain
  600. * additional data beyond the delimiter. An application will typically leave
  601. * that data in the streambuf for a subsequent read_until operation to examine.
  602. */
  603. template <typename SyncReadStream, typename Allocator>
  604. std::size_t read_until(SyncReadStream& s,
  605. boost::asio::basic_streambuf<Allocator>& b, char delim,
  606. boost::system::error_code& ec);
  607. /// Read data into a streambuf until it contains a specified delimiter.
  608. /**
  609. * This function is used to read data into the specified streambuf until the
  610. * streambuf's get area contains the specified delimiter. The call will block
  611. * until one of the following conditions is true:
  612. *
  613. * @li The get area of the streambuf contains the specified delimiter.
  614. *
  615. * @li An error occurred.
  616. *
  617. * This operation is implemented in terms of zero or more calls to the stream's
  618. * read_some function. If the streambuf's get area already contains the
  619. * delimiter, the function returns immediately.
  620. *
  621. * @param s The stream from which the data is to be read. The type must support
  622. * the SyncReadStream concept.
  623. *
  624. * @param b A streambuf object into which the data will be read.
  625. *
  626. * @param delim The delimiter string.
  627. *
  628. * @returns The number of bytes in the streambuf's get area up to and including
  629. * the delimiter.
  630. *
  631. * @throws boost::system::system_error Thrown on failure.
  632. *
  633. * @note After a successful read_until operation, the streambuf may contain
  634. * additional data beyond the delimiter. An application will typically leave
  635. * that data in the streambuf for a subsequent read_until operation to examine.
  636. *
  637. * @par Example
  638. * To read data into a streambuf until a newline is encountered:
  639. * @code boost::asio::streambuf b;
  640. * boost::asio::read_until(s, b, "\r\n");
  641. * std::istream is(&b);
  642. * std::string line;
  643. * std::getline(is, line); @endcode
  644. * After the @c read_until operation completes successfully, the buffer @c b
  645. * 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. * newline (which is discarded), so that the string @c line contains:
  649. * @code { 'a', 'b', ..., 'c', '\r' } @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 read_until operation.
  654. */
  655. template <typename SyncReadStream, typename Allocator>
  656. std::size_t read_until(SyncReadStream& s,
  657. boost::asio::basic_streambuf<Allocator>& b,
  658. BOOST_ASIO_STRING_VIEW_PARAM delim);
  659. /// Read data into a streambuf until it contains a specified delimiter.
  660. /**
  661. * This function is used to read data into the specified streambuf until the
  662. * streambuf's get area contains the specified delimiter. The call will block
  663. * until one of the following conditions is true:
  664. *
  665. * @li The get area of the streambuf contains the specified delimiter.
  666. *
  667. * @li An error occurred.
  668. *
  669. * This operation is implemented in terms of zero or more calls to the stream's
  670. * read_some function. If the streambuf's get area already contains the
  671. * delimiter, the function returns immediately.
  672. *
  673. * @param s The stream from which the data is to be read. The type must support
  674. * the SyncReadStream concept.
  675. *
  676. * @param b A streambuf object into which the data will be read.
  677. *
  678. * @param delim The delimiter string.
  679. *
  680. * @param ec Set to indicate what error occurred, if any.
  681. *
  682. * @returns The number of bytes in the streambuf's get area up to and including
  683. * the delimiter. Returns 0 if an error occurred.
  684. *
  685. * @note After a successful read_until operation, the streambuf may contain
  686. * additional data beyond the delimiter. An application will typically leave
  687. * that data in the streambuf for a subsequent read_until operation to examine.
  688. */
  689. template <typename SyncReadStream, typename Allocator>
  690. std::size_t read_until(SyncReadStream& s,
  691. boost::asio::basic_streambuf<Allocator>& b,
  692. BOOST_ASIO_STRING_VIEW_PARAM delim, boost::system::error_code& ec);
  693. #if defined(BOOST_ASIO_HAS_BOOST_REGEX) \
  694. || defined(GENERATING_DOCUMENTATION)
  695. /// Read data into a streambuf until some part of the data it contains matches
  696. /// a regular expression.
  697. /**
  698. * This function is used to read data into the specified streambuf until the
  699. * streambuf's get area contains some data that matches a regular expression.
  700. * The call will block until one of the following conditions is true:
  701. *
  702. * @li A substring of the streambuf's get area matches the regular expression.
  703. *
  704. * @li An error occurred.
  705. *
  706. * This operation is implemented in terms of zero or more calls to the stream's
  707. * read_some function. If the streambuf's get area already contains data that
  708. * matches the regular expression, the function returns immediately.
  709. *
  710. * @param s The stream from which the data is to be read. The type must support
  711. * the SyncReadStream concept.
  712. *
  713. * @param b A streambuf object into which the data will be read.
  714. *
  715. * @param expr The regular expression.
  716. *
  717. * @returns The number of bytes in the streambuf's get area up to and including
  718. * the substring that matches the regular expression.
  719. *
  720. * @throws boost::system::system_error Thrown on failure.
  721. *
  722. * @note After a successful read_until operation, the streambuf may contain
  723. * additional data beyond that which matched the regular expression. An
  724. * application will typically leave that data in the streambuf for a subsequent
  725. * read_until operation to examine.
  726. *
  727. * @par Example
  728. * To read data into a streambuf until a CR-LF sequence is encountered:
  729. * @code boost::asio::streambuf b;
  730. * boost::asio::read_until(s, b, boost::regex("\r\n"));
  731. * std::istream is(&b);
  732. * std::string line;
  733. * std::getline(is, line); @endcode
  734. * After the @c read_until operation completes successfully, the buffer @c b
  735. * 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. * newline (which is discarded), so that the string @c line contains:
  739. * @code { 'a', 'b', ..., 'c', '\r' } @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 read_until operation.
  744. */
  745. template <typename SyncReadStream, typename Allocator>
  746. std::size_t read_until(SyncReadStream& s,
  747. boost::asio::basic_streambuf<Allocator>& b, const boost::regex& expr);
  748. /// Read data into a streambuf until some part of the data it contains matches
  749. /// a regular expression.
  750. /**
  751. * This function is used to read data into the specified streambuf until the
  752. * streambuf's get area contains some data that matches a regular expression.
  753. * The call will block until one of the following conditions is true:
  754. *
  755. * @li A substring of the streambuf's get area matches the regular expression.
  756. *
  757. * @li An error occurred.
  758. *
  759. * This operation is implemented in terms of zero or more calls to the stream's
  760. * read_some function. If the streambuf's get area already contains data that
  761. * matches the regular expression, the function returns immediately.
  762. *
  763. * @param s The stream from which the data is to be read. The type must support
  764. * the SyncReadStream concept.
  765. *
  766. * @param b A streambuf object into which the data will be read.
  767. *
  768. * @param expr The regular expression.
  769. *
  770. * @param ec Set to indicate what error occurred, if any.
  771. *
  772. * @returns The number of bytes in the streambuf's get area up to and including
  773. * the substring that matches the regular expression. Returns 0 if an error
  774. * occurred.
  775. *
  776. * @note After a successful read_until operation, the streambuf may contain
  777. * additional data beyond that which matched the regular expression. An
  778. * application will typically leave that data in the streambuf for a subsequent
  779. * read_until operation to examine.
  780. */
  781. template <typename SyncReadStream, typename Allocator>
  782. std::size_t read_until(SyncReadStream& s,
  783. boost::asio::basic_streambuf<Allocator>& b, const boost::regex& expr,
  784. boost::system::error_code& ec);
  785. #endif // defined(BOOST_ASIO_HAS_BOOST_REGEX)
  786. // || defined(GENERATING_DOCUMENTATION)
  787. /// Read data into a streambuf until a function object indicates a match.
  788. /**
  789. * This function is used to read data into the specified streambuf until a
  790. * user-defined match condition function object, when applied to the data
  791. * contained in the streambuf, indicates a successful match. The call will
  792. * block until one of the following conditions is true:
  793. *
  794. * @li The match condition function object returns a std::pair where the second
  795. * element evaluates to true.
  796. *
  797. * @li An error occurred.
  798. *
  799. * This operation is implemented in terms of zero or more calls to the stream's
  800. * read_some function. If the match condition function object already indicates
  801. * a match, the function returns immediately.
  802. *
  803. * @param s The stream from which the data is to be read. The type must support
  804. * the SyncReadStream concept.
  805. *
  806. * @param b A streambuf object into which the data will be read.
  807. *
  808. * @param match_condition The function object to be called to determine whether
  809. * a match exists. The signature of the function object must be:
  810. * @code pair<iterator, bool> match_condition(iterator begin, iterator end);
  811. * @endcode
  812. * where @c iterator represents the type:
  813. * @code buffers_iterator<basic_streambuf<Allocator>::const_buffers_type>
  814. * @endcode
  815. * The iterator parameters @c begin and @c end define the range of bytes to be
  816. * scanned to determine whether there is a match. The @c first member of the
  817. * return value is an iterator marking one-past-the-end of the bytes that have
  818. * been consumed by the match function. This iterator is used to calculate the
  819. * @c begin parameter for any subsequent invocation of the match condition. The
  820. * @c second member of the return value is true if a match has been found, false
  821. * otherwise.
  822. *
  823. * @returns The number of bytes in the streambuf's get area that have been fully
  824. * consumed by the match function.
  825. *
  826. * @throws boost::system::system_error Thrown on failure.
  827. *
  828. * @note After a successful read_until operation, the streambuf may contain
  829. * additional data beyond that which matched the function object. An application
  830. * will typically leave that data in the streambuf for a subsequent read_until
  831. * operation to examine.
  832. *
  833. * @note The default implementation of the @c is_match_condition type trait
  834. * evaluates to true for function pointers and function objects with a
  835. * @c result_type typedef. It must be specialised for other user-defined
  836. * function objects.
  837. *
  838. * @par Examples
  839. * To read data into a streambuf until whitespace is encountered:
  840. * @code typedef boost::asio::buffers_iterator<
  841. * boost::asio::streambuf::const_buffers_type> iterator;
  842. *
  843. * std::pair<iterator, bool>
  844. * match_whitespace(iterator begin, iterator end)
  845. * {
  846. * iterator i = begin;
  847. * while (i != end)
  848. * if (std::isspace(*i++))
  849. * return std::make_pair(i, true);
  850. * return std::make_pair(i, false);
  851. * }
  852. * ...
  853. * boost::asio::streambuf b;
  854. * boost::asio::read_until(s, b, match_whitespace);
  855. * @endcode
  856. *
  857. * To read data into a streambuf until a matching character is found:
  858. * @code class match_char
  859. * {
  860. * public:
  861. * explicit match_char(char c) : c_(c) {}
  862. *
  863. * template <typename Iterator>
  864. * std::pair<Iterator, bool> operator()(
  865. * Iterator begin, Iterator end) const
  866. * {
  867. * Iterator i = begin;
  868. * while (i != end)
  869. * if (c_ == *i++)
  870. * return std::make_pair(i, true);
  871. * return std::make_pair(i, false);
  872. * }
  873. *
  874. * private:
  875. * char c_;
  876. * };
  877. *
  878. * namespace asio {
  879. * template <> struct is_match_condition<match_char>
  880. * : public boost::true_type {};
  881. * } // namespace asio
  882. * ...
  883. * boost::asio::streambuf b;
  884. * boost::asio::read_until(s, b, match_char('a'));
  885. * @endcode
  886. */
  887. template <typename SyncReadStream, typename Allocator, typename MatchCondition>
  888. std::size_t read_until(SyncReadStream& s,
  889. boost::asio::basic_streambuf<Allocator>& b, MatchCondition match_condition,
  890. typename enable_if<is_match_condition<MatchCondition>::value>::type* = 0);
  891. /// Read data into a streambuf until a function object indicates a match.
  892. /**
  893. * This function is used to read data into the specified streambuf until a
  894. * user-defined match condition function object, when applied to the data
  895. * contained in the streambuf, indicates a successful match. The call will
  896. * block until one of the following conditions is true:
  897. *
  898. * @li The match condition function object returns a std::pair where the second
  899. * element evaluates to true.
  900. *
  901. * @li An error occurred.
  902. *
  903. * This operation is implemented in terms of zero or more calls to the stream's
  904. * read_some function. If the match condition function object already indicates
  905. * a match, the function returns immediately.
  906. *
  907. * @param s The stream from which the data is to be read. The type must support
  908. * the SyncReadStream concept.
  909. *
  910. * @param b A streambuf object into which the data will be read.
  911. *
  912. * @param match_condition The function object to be called to determine whether
  913. * a match exists. The signature of the function object must be:
  914. * @code pair<iterator, bool> match_condition(iterator begin, iterator end);
  915. * @endcode
  916. * where @c iterator represents the type:
  917. * @code buffers_iterator<basic_streambuf<Allocator>::const_buffers_type>
  918. * @endcode
  919. * The iterator parameters @c begin and @c end define the range of bytes to be
  920. * scanned to determine whether there is a match. The @c first member of the
  921. * return value is an iterator marking one-past-the-end of the bytes that have
  922. * been consumed by the match function. This iterator is used to calculate the
  923. * @c begin parameter for any subsequent invocation of the match condition. The
  924. * @c second member of the return value is true if a match has been found, false
  925. * otherwise.
  926. *
  927. * @param ec Set to indicate what error occurred, if any.
  928. *
  929. * @returns The number of bytes in the streambuf's get area that have been fully
  930. * consumed by the match function. Returns 0 if an error occurred.
  931. *
  932. * @note After a successful read_until operation, the streambuf may contain
  933. * additional data beyond that which matched the function object. An application
  934. * will typically leave that data in the streambuf for a subsequent read_until
  935. * operation to examine.
  936. *
  937. * @note The default implementation of the @c is_match_condition type trait
  938. * evaluates to true for function pointers and function objects with a
  939. * @c result_type typedef. It must be specialised for other user-defined
  940. * function objects.
  941. */
  942. template <typename SyncReadStream, typename Allocator, typename MatchCondition>
  943. std::size_t read_until(SyncReadStream& s,
  944. boost::asio::basic_streambuf<Allocator>& b,
  945. MatchCondition match_condition, boost::system::error_code& ec,
  946. typename enable_if<is_match_condition<MatchCondition>::value>::type* = 0);
  947. #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
  948. #endif // !defined(BOOST_ASIO_NO_EXTENSIONS)
  949. /*@}*/
  950. /**
  951. * @defgroup async_read_until boost::asio::async_read_until
  952. *
  953. * @brief Start an asynchronous operation to read data into a dynamic buffer
  954. * sequence, or into a streambuf, until it contains a delimiter, matches a
  955. * regular expression, or a function object indicates a match.
  956. */
  957. /*@{*/
  958. /// Start an asynchronous operation to read data into a dynamic buffer sequence
  959. /// until it contains a specified delimiter.
  960. /**
  961. * This function is used to asynchronously read data into the specified dynamic
  962. * buffer sequence until the dynamic buffer sequence's get area contains the
  963. * specified delimiter. The function call always returns immediately. The
  964. * asynchronous operation will continue until one of the following conditions
  965. * is true:
  966. *
  967. * @li The get area of the dynamic buffer sequence contains the specified
  968. * delimiter.
  969. *
  970. * @li An error occurred.
  971. *
  972. * This operation is implemented in terms of zero or more calls to the stream's
  973. * async_read_some function, and is known as a <em>composed operation</em>. If
  974. * the dynamic buffer sequence's get area already contains the delimiter, this
  975. * asynchronous operation completes immediately. The program must ensure that
  976. * the stream performs no other read operations (such as async_read,
  977. * async_read_until, the stream's async_read_some function, or any other
  978. * composed operations that perform reads) until this operation completes.
  979. *
  980. * @param s The stream from which the data is to be read. The type must support
  981. * the AsyncReadStream concept.
  982. *
  983. * @param buffers The dynamic buffer sequence into which the data will be read.
  984. * Although the buffers object may be copied as necessary, ownership of the
  985. * underlying memory blocks is retained by the caller, which must guarantee
  986. * that they remain valid until the handler is called.
  987. *
  988. * @param delim The delimiter character.
  989. *
  990. * @param handler The handler to be called when the read operation completes.
  991. * Copies will be made of the handler as required. The function signature of the
  992. * handler must be:
  993. * @code void handler(
  994. * // Result of operation.
  995. * const boost::system::error_code& error,
  996. *
  997. * // The number of bytes in the dynamic buffer sequence's
  998. * // get area up to and including the delimiter.
  999. * // 0 if an error occurred.
  1000. * std::size_t bytes_transferred
  1001. * ); @endcode
  1002. * Regardless of whether the asynchronous operation completes immediately or
  1003. * not, the handler will not be invoked from within this function. Invocation of
  1004. * the handler will be performed in a manner equivalent to using
  1005. * boost::asio::io_context::post().
  1006. *
  1007. * @note After a successful async_read_until operation, the dynamic buffer
  1008. * sequence may contain additional data beyond the delimiter. An application
  1009. * will typically leave that data in the dynamic buffer sequence for a
  1010. * subsequent async_read_until operation to examine.
  1011. *
  1012. * @par Example
  1013. * To asynchronously read data into a @c std::string until a newline is
  1014. * encountered:
  1015. * @code std::string data;
  1016. * ...
  1017. * void handler(const boost::system::error_code& e, std::size_t size)
  1018. * {
  1019. * if (!e)
  1020. * {
  1021. * std::string line = data.substr(0, n);
  1022. * data.erase(0, n);
  1023. * ...
  1024. * }
  1025. * }
  1026. * ...
  1027. * boost::asio::async_read_until(s, data, '\n', handler); @endcode
  1028. * After the @c async_read_until operation completes successfully, the buffer
  1029. * @c data contains the delimiter:
  1030. * @code { 'a', 'b', ..., 'c', '\n', 'd', 'e', ... } @endcode
  1031. * The call to @c substr then extracts the data up to and including the
  1032. * delimiter, so that the string @c line contains:
  1033. * @code { 'a', 'b', ..., 'c', '\n' } @endcode
  1034. * After the call to @c erase, the remaining data is left in the buffer @c data
  1035. * as follows:
  1036. * @code { 'd', 'e', ... } @endcode
  1037. * This data may be the start of a new line, to be extracted by a subsequent
  1038. * @c async_read_until operation.
  1039. */
  1040. template <typename AsyncReadStream,
  1041. typename DynamicBuffer, typename ReadHandler>
  1042. BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
  1043. void (boost::system::error_code, std::size_t))
  1044. async_read_until(AsyncReadStream& s,
  1045. BOOST_ASIO_MOVE_ARG(DynamicBuffer) buffers,
  1046. char delim, BOOST_ASIO_MOVE_ARG(ReadHandler) handler);
  1047. /// Start an asynchronous operation to read data into a dynamic buffer sequence
  1048. /// until it contains a specified delimiter.
  1049. /**
  1050. * This function is used to asynchronously read data into the specified dynamic
  1051. * buffer sequence until the dynamic buffer sequence's get area contains the
  1052. * specified delimiter. The function call always returns immediately. The
  1053. * asynchronous operation will continue until one of the following conditions
  1054. * is true:
  1055. *
  1056. * @li The get area of the dynamic buffer sequence contains the specified
  1057. * delimiter.
  1058. *
  1059. * @li An error occurred.
  1060. *
  1061. * This operation is implemented in terms of zero or more calls to the stream's
  1062. * async_read_some function, and is known as a <em>composed operation</em>. If
  1063. * the dynamic buffer sequence's get area already contains the delimiter, this
  1064. * asynchronous operation completes immediately. The program must ensure that
  1065. * the stream performs no other read operations (such as async_read,
  1066. * async_read_until, the stream's async_read_some function, or any other
  1067. * composed operations that perform reads) until this operation completes.
  1068. *
  1069. * @param s The stream from which the data is to be read. The type must support
  1070. * the AsyncReadStream concept.
  1071. *
  1072. * @param buffers The dynamic buffer sequence into which the data will be read.
  1073. * Although the buffers object may be copied as necessary, ownership of the
  1074. * underlying memory blocks is retained by the caller, which must guarantee
  1075. * that they remain valid until the handler is called.
  1076. *
  1077. * @param delim The delimiter string.
  1078. *
  1079. * @param handler The handler to be called when the read operation completes.
  1080. * Copies will be made of the handler as required. The function signature of the
  1081. * handler must be:
  1082. * @code void handler(
  1083. * // Result of operation.
  1084. * const boost::system::error_code& error,
  1085. *
  1086. * // The number of bytes in the dynamic buffer sequence's
  1087. * // get area up to and including the delimiter.
  1088. * // 0 if an error occurred.
  1089. * std::size_t bytes_transferred
  1090. * ); @endcode
  1091. * Regardless of whether the asynchronous operation completes immediately or
  1092. * not, the handler will not be invoked from within this function. Invocation of
  1093. * the handler will be performed in a manner equivalent to using
  1094. * boost::asio::io_context::post().
  1095. *
  1096. * @note After a successful async_read_until operation, the dynamic buffer
  1097. * sequence may contain additional data beyond the delimiter. An application
  1098. * will typically leave that data in the dynamic buffer sequence for a
  1099. * subsequent async_read_until operation to examine.
  1100. *
  1101. * @par Example
  1102. * To asynchronously read data into a @c std::string until a CR-LF sequence is
  1103. * encountered:
  1104. * @code std::string data;
  1105. * ...
  1106. * void handler(const boost::system::error_code& e, std::size_t size)
  1107. * {
  1108. * if (!e)
  1109. * {
  1110. * std::string line = data.substr(0, n);
  1111. * data.erase(0, n);
  1112. * ...
  1113. * }
  1114. * }
  1115. * ...
  1116. * boost::asio::async_read_until(s, data, "\r\n", handler); @endcode
  1117. * After the @c async_read_until operation completes successfully, the string
  1118. * @c data contains the delimiter:
  1119. * @code { 'a', 'b', ..., 'c', '\r', '\n', 'd', 'e', ... } @endcode
  1120. * The call to @c substr then extracts the data up to and including the
  1121. * delimiter, so that the string @c line contains:
  1122. * @code { 'a', 'b', ..., 'c', '\r', '\n' } @endcode
  1123. * After the call to @c erase, the remaining data is left in the string @c data
  1124. * as follows:
  1125. * @code { 'd', 'e', ... } @endcode
  1126. * This data may be the start of a new line, to be extracted by a subsequent
  1127. * @c async_read_until operation.
  1128. */
  1129. template <typename AsyncReadStream,
  1130. typename DynamicBuffer, typename ReadHandler>
  1131. BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
  1132. void (boost::system::error_code, std::size_t))
  1133. async_read_until(AsyncReadStream& s,
  1134. BOOST_ASIO_MOVE_ARG(DynamicBuffer) buffers,
  1135. BOOST_ASIO_STRING_VIEW_PARAM delim,
  1136. BOOST_ASIO_MOVE_ARG(ReadHandler) handler);
  1137. #if !defined(BOOST_ASIO_NO_EXTENSIONS)
  1138. #if defined(BOOST_ASIO_HAS_BOOST_REGEX) \
  1139. || defined(GENERATING_DOCUMENTATION)
  1140. /// Start an asynchronous operation to read data into a dynamic buffer sequence
  1141. /// until some part of its data matches a regular expression.
  1142. /**
  1143. * This function is used to asynchronously read data into the specified dynamic
  1144. * buffer sequence until the dynamic buffer sequence's get area contains some
  1145. * data that matches a regular expression. The function call always returns
  1146. * immediately. The asynchronous operation will continue until one of the
  1147. * following conditions is true:
  1148. *
  1149. * @li A substring of the dynamic buffer sequence's get area matches the regular
  1150. * expression.
  1151. *
  1152. * @li An error occurred.
  1153. *
  1154. * This operation is implemented in terms of zero or more calls to the stream's
  1155. * async_read_some function, and is known as a <em>composed operation</em>. If
  1156. * the dynamic buffer sequence's get area already contains data that matches
  1157. * the regular expression, this asynchronous operation completes immediately.
  1158. * The program must ensure that the stream performs no other read operations
  1159. * (such as async_read, async_read_until, the stream's async_read_some
  1160. * function, or any other composed operations that perform reads) until this
  1161. * operation completes.
  1162. *
  1163. * @param s The stream from which the data is to be read. The type must support
  1164. * the AsyncReadStream concept.
  1165. *
  1166. * @param buffers The dynamic buffer sequence into which the data will be read.
  1167. * Although the buffers object may be copied as necessary, ownership of the
  1168. * underlying memory blocks is retained by the caller, which must guarantee
  1169. * that they remain valid until the handler is called.
  1170. *
  1171. * @param expr The regular expression.
  1172. *
  1173. * @param handler The handler to be called when the read operation completes.
  1174. * Copies will be made of the handler as required. The function signature of the
  1175. * handler must be:
  1176. * @code void handler(
  1177. * // Result of operation.
  1178. * const boost::system::error_code& error,
  1179. *
  1180. * // The number of bytes in the dynamic buffer
  1181. * // sequence's get area up to and including the
  1182. * // substring that matches the regular expression.
  1183. * // 0 if an error occurred.
  1184. * std::size_t bytes_transferred
  1185. * ); @endcode
  1186. * Regardless of whether the asynchronous operation completes immediately or
  1187. * not, the handler will not be invoked from within this function. Invocation of
  1188. * the handler will be performed in a manner equivalent to using
  1189. * boost::asio::io_context::post().
  1190. *
  1191. * @note After a successful async_read_until operation, the dynamic buffer
  1192. * sequence may contain additional data beyond that which matched the regular
  1193. * expression. An application will typically leave that data in the dynamic
  1194. * buffer sequence for a subsequent async_read_until operation to examine.
  1195. *
  1196. * @par Example
  1197. * To asynchronously read data into a @c std::string until a CR-LF sequence is
  1198. * encountered:
  1199. * @code std::string data;
  1200. * ...
  1201. * void handler(const boost::system::error_code& e, std::size_t size)
  1202. * {
  1203. * if (!e)
  1204. * {
  1205. * std::string line = data.substr(0, n);
  1206. * data.erase(0, n);
  1207. * ...
  1208. * }
  1209. * }
  1210. * ...
  1211. * boost::asio::async_read_until(s, data,
  1212. * boost::regex("\r\n"), handler); @endcode
  1213. * After the @c async_read_until operation completes successfully, the string
  1214. * @c data contains the data which matched the regular expression:
  1215. * @code { 'a', 'b', ..., 'c', '\r', '\n', 'd', 'e', ... } @endcode
  1216. * The call to @c substr then extracts the data up to and including the match,
  1217. * so that the string @c line contains:
  1218. * @code { 'a', 'b', ..., 'c', '\r', '\n' } @endcode
  1219. * After the call to @c erase, the remaining data is left in the string @c data
  1220. * as follows:
  1221. * @code { 'd', 'e', ... } @endcode
  1222. * This data may be the start of a new line, to be extracted by a subsequent
  1223. * @c async_read_until operation.
  1224. */
  1225. template <typename AsyncReadStream,
  1226. typename DynamicBuffer, typename ReadHandler>
  1227. BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
  1228. void (boost::system::error_code, std::size_t))
  1229. async_read_until(AsyncReadStream& s,
  1230. BOOST_ASIO_MOVE_ARG(DynamicBuffer) buffers,
  1231. const boost::regex& expr,
  1232. BOOST_ASIO_MOVE_ARG(ReadHandler) handler);
  1233. #endif // defined(BOOST_ASIO_HAS_BOOST_REGEX)
  1234. // || defined(GENERATING_DOCUMENTATION)
  1235. /// Start an asynchronous operation to read data into a dynamic buffer sequence
  1236. /// until a function object indicates a match.
  1237. /**
  1238. * This function is used to asynchronously read data into the specified dynamic
  1239. * buffer sequence until a user-defined match condition function object, when
  1240. * applied to the data contained in the dynamic buffer sequence, indicates a
  1241. * successful match. The function call always returns immediately. The
  1242. * asynchronous operation will continue until one of the following conditions
  1243. * is true:
  1244. *
  1245. * @li The match condition function object returns a std::pair where the second
  1246. * element evaluates to true.
  1247. *
  1248. * @li An error occurred.
  1249. *
  1250. * This operation is implemented in terms of zero or more calls to the stream's
  1251. * async_read_some function, and is known as a <em>composed operation</em>. If
  1252. * the match condition function object already indicates a match, this
  1253. * asynchronous operation completes immediately. The program must ensure that
  1254. * the stream performs no other read operations (such as async_read,
  1255. * async_read_until, the stream's async_read_some function, or any other
  1256. * composed operations that perform reads) until this operation completes.
  1257. *
  1258. * @param s The stream from which the data is to be read. The type must support
  1259. * the AsyncReadStream concept.
  1260. *
  1261. * @param buffers The dynamic buffer sequence into which the data will be read.
  1262. * Although the buffers object may be copied as necessary, ownership of the
  1263. * underlying memory blocks is retained by the caller, which must guarantee
  1264. * that they remain valid until the handler is called.
  1265. *
  1266. * @param match_condition The function object to be called to determine whether
  1267. * a match exists. The signature of the function object must be:
  1268. * @code pair<iterator, bool> match_condition(iterator begin, iterator end);
  1269. * @endcode
  1270. * where @c iterator represents the type:
  1271. * @code buffers_iterator<typename DynamicBuffer::const_buffers_type>
  1272. * @endcode
  1273. * The iterator parameters @c begin and @c end define the range of bytes to be
  1274. * scanned to determine whether there is a match. The @c first member of the
  1275. * return value is an iterator marking one-past-the-end of the bytes that have
  1276. * been consumed by the match function. This iterator is used to calculate the
  1277. * @c begin parameter for any subsequent invocation of the match condition. The
  1278. * @c second member of the return value is true if a match has been found, false
  1279. * otherwise.
  1280. *
  1281. * @param handler The handler to be called when the read operation completes.
  1282. * Copies will be made of the handler as required. The function signature of the
  1283. * handler must be:
  1284. * @code void handler(
  1285. * // Result of operation.
  1286. * const boost::system::error_code& error,
  1287. *
  1288. * // The number of bytes in the dynamic buffer sequence's
  1289. * // get area that have been fully consumed by the match
  1290. * // function. O if an error occurred.
  1291. * std::size_t bytes_transferred
  1292. * ); @endcode
  1293. * Regardless of whether the asynchronous operation completes immediately or
  1294. * not, the handler will not be invoked from within this function. Invocation of
  1295. * the handler will be performed in a manner equivalent to using
  1296. * boost::asio::io_context::post().
  1297. *
  1298. * @note After a successful async_read_until operation, the dynamic buffer
  1299. * sequence may contain additional data beyond that which matched the function
  1300. * object. An application will typically leave that data in the dynamic buffer
  1301. * sequence for a subsequent async_read_until operation to examine.
  1302. *
  1303. * @note The default implementation of the @c is_match_condition type trait
  1304. * evaluates to true for function pointers and function objects with a
  1305. * @c result_type typedef. It must be specialised for other user-defined
  1306. * function objects.
  1307. *
  1308. * @par Examples
  1309. * To asynchronously read data into a @c std::string until whitespace is
  1310. * encountered:
  1311. * @code typedef boost::asio::buffers_iterator<
  1312. * boost::asio::const_buffers_1> iterator;
  1313. *
  1314. * std::pair<iterator, bool>
  1315. * match_whitespace(iterator begin, iterator end)
  1316. * {
  1317. * iterator i = begin;
  1318. * while (i != end)
  1319. * if (std::isspace(*i++))
  1320. * return std::make_pair(i, true);
  1321. * return std::make_pair(i, false);
  1322. * }
  1323. * ...
  1324. * void handler(const boost::system::error_code& e, std::size_t size);
  1325. * ...
  1326. * std::string data;
  1327. * boost::asio::async_read_until(s, data, match_whitespace, handler);
  1328. * @endcode
  1329. *
  1330. * To asynchronously read data into a @c std::string until a matching character
  1331. * is found:
  1332. * @code class match_char
  1333. * {
  1334. * public:
  1335. * explicit match_char(char c) : c_(c) {}
  1336. *
  1337. * template <typename Iterator>
  1338. * std::pair<Iterator, bool> operator()(
  1339. * Iterator begin, Iterator end) const
  1340. * {
  1341. * Iterator i = begin;
  1342. * while (i != end)
  1343. * if (c_ == *i++)
  1344. * return std::make_pair(i, true);
  1345. * return std::make_pair(i, false);
  1346. * }
  1347. *
  1348. * private:
  1349. * char c_;
  1350. * };
  1351. *
  1352. * namespace asio {
  1353. * template <> struct is_match_condition<match_char>
  1354. * : public boost::true_type {};
  1355. * } // namespace asio
  1356. * ...
  1357. * void handler(const boost::system::error_code& e, std::size_t size);
  1358. * ...
  1359. * std::string data;
  1360. * boost::asio::async_read_until(s, data, match_char('a'), handler);
  1361. * @endcode
  1362. */
  1363. template <typename AsyncReadStream, typename DynamicBuffer,
  1364. typename MatchCondition, typename ReadHandler>
  1365. BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
  1366. void (boost::system::error_code, std::size_t))
  1367. async_read_until(AsyncReadStream& s,
  1368. BOOST_ASIO_MOVE_ARG(DynamicBuffer) buffers,
  1369. MatchCondition match_condition, BOOST_ASIO_MOVE_ARG(ReadHandler) handler,
  1370. typename enable_if<is_match_condition<MatchCondition>::value>::type* = 0);
  1371. #if !defined(BOOST_ASIO_NO_IOSTREAM)
  1372. /// Start an asynchronous operation to read data into a streambuf until it
  1373. /// contains a specified delimiter.
  1374. /**
  1375. * This function is used to asynchronously read data into the specified
  1376. * streambuf until the streambuf's get area contains the specified delimiter.
  1377. * The function call always returns immediately. The asynchronous operation
  1378. * will continue until one of the following conditions is true:
  1379. *
  1380. * @li The get area of the streambuf contains the specified delimiter.
  1381. *
  1382. * @li An error occurred.
  1383. *
  1384. * This operation is implemented in terms of zero or more calls to the stream's
  1385. * async_read_some function, and is known as a <em>composed operation</em>. If
  1386. * the streambuf's get area already contains the delimiter, this asynchronous
  1387. * operation completes immediately. The program must ensure that the stream
  1388. * performs no other read operations (such as async_read, async_read_until, the
  1389. * stream's async_read_some function, or any other composed operations that
  1390. * perform reads) until this operation completes.
  1391. *
  1392. * @param s The stream from which the data is to be read. The type must support
  1393. * the AsyncReadStream concept.
  1394. *
  1395. * @param b A streambuf object into which the data will be read. Ownership of
  1396. * the streambuf is retained by the caller, which must guarantee that it remains
  1397. * valid until the handler is called.
  1398. *
  1399. * @param delim The delimiter character.
  1400. *
  1401. * @param handler The handler to be called when the read operation completes.
  1402. * Copies will be made of the handler as required. The function signature of the
  1403. * handler must be:
  1404. * @code void handler(
  1405. * // Result of operation.
  1406. * const boost::system::error_code& error,
  1407. *
  1408. * // The number of bytes in the streambuf's get
  1409. * // area up to and including the delimiter.
  1410. * // 0 if an error occurred.
  1411. * std::size_t bytes_transferred
  1412. * ); @endcode
  1413. * Regardless of whether the asynchronous operation completes immediately or
  1414. * not, the handler will not be invoked from within this function. Invocation of
  1415. * the handler will be performed in a manner equivalent to using
  1416. * boost::asio::io_context::post().
  1417. *
  1418. * @note After a successful async_read_until operation, the streambuf may
  1419. * contain additional data beyond the delimiter. An application will typically
  1420. * leave that data in the streambuf for a subsequent async_read_until operation
  1421. * to examine.
  1422. *
  1423. * @par Example
  1424. * To asynchronously read data into a streambuf until a newline is encountered:
  1425. * @code boost::asio::streambuf b;
  1426. * ...
  1427. * void handler(const boost::system::error_code& e, std::size_t size)
  1428. * {
  1429. * if (!e)
  1430. * {
  1431. * std::istream is(&b);
  1432. * std::string line;
  1433. * std::getline(is, line);
  1434. * ...
  1435. * }
  1436. * }
  1437. * ...
  1438. * boost::asio::async_read_until(s, b, '\n', handler); @endcode
  1439. * After the @c async_read_until operation completes successfully, the buffer
  1440. * @c b contains the delimiter:
  1441. * @code { 'a', 'b', ..., 'c', '\n', 'd', 'e', ... } @endcode
  1442. * The call to @c std::getline then extracts the data up to and including the
  1443. * newline (which is discarded), so that the string @c line contains:
  1444. * @code { 'a', 'b', ..., 'c' } @endcode
  1445. * The remaining data is left in the buffer @c b as follows:
  1446. * @code { 'd', 'e', ... } @endcode
  1447. * This data may be the start of a new line, to be extracted by a subsequent
  1448. * @c async_read_until operation.
  1449. */
  1450. template <typename AsyncReadStream, typename Allocator, typename ReadHandler>
  1451. BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
  1452. void (boost::system::error_code, std::size_t))
  1453. async_read_until(AsyncReadStream& s,
  1454. boost::asio::basic_streambuf<Allocator>& b,
  1455. char delim, BOOST_ASIO_MOVE_ARG(ReadHandler) handler);
  1456. /// Start an asynchronous operation to read data into a streambuf until it
  1457. /// contains a specified delimiter.
  1458. /**
  1459. * This function is used to asynchronously read data into the specified
  1460. * streambuf until the streambuf's get area contains the specified delimiter.
  1461. * The function call always returns immediately. The asynchronous operation
  1462. * will continue until one of the following conditions is true:
  1463. *
  1464. * @li The get area of the streambuf contains the specified delimiter.
  1465. *
  1466. * @li An error occurred.
  1467. *
  1468. * This operation is implemented in terms of zero or more calls to the stream's
  1469. * async_read_some function, and is known as a <em>composed operation</em>. If
  1470. * the streambuf's get area already contains the delimiter, this asynchronous
  1471. * operation completes immediately. The program must ensure that the stream
  1472. * performs no other read operations (such as async_read, async_read_until, the
  1473. * stream's async_read_some function, or any other composed operations that
  1474. * perform reads) until this operation completes.
  1475. *
  1476. * @param s The stream from which the data is to be read. The type must support
  1477. * the AsyncReadStream concept.
  1478. *
  1479. * @param b A streambuf object into which the data will be read. Ownership of
  1480. * the streambuf is retained by the caller, which must guarantee that it remains
  1481. * valid until the handler is called.
  1482. *
  1483. * @param delim The delimiter string.
  1484. *
  1485. * @param handler The handler to be called when the read operation completes.
  1486. * Copies will be made of the handler as required. The function signature of the
  1487. * handler must be:
  1488. * @code void handler(
  1489. * // Result of operation.
  1490. * const boost::system::error_code& error,
  1491. *
  1492. * // The number of bytes in the streambuf's get
  1493. * // area up to and including the delimiter.
  1494. * // 0 if an error occurred.
  1495. * std::size_t bytes_transferred
  1496. * ); @endcode
  1497. * Regardless of whether the asynchronous operation completes immediately or
  1498. * not, the handler will not be invoked from within this function. Invocation of
  1499. * the handler will be performed in a manner equivalent to using
  1500. * boost::asio::io_context::post().
  1501. *
  1502. * @note After a successful async_read_until operation, the streambuf may
  1503. * contain additional data beyond the delimiter. An application will typically
  1504. * leave that data in the streambuf for a subsequent async_read_until operation
  1505. * to examine.
  1506. *
  1507. * @par Example
  1508. * To asynchronously read data into a streambuf until a newline is encountered:
  1509. * @code boost::asio::streambuf b;
  1510. * ...
  1511. * void handler(const boost::system::error_code& e, std::size_t size)
  1512. * {
  1513. * if (!e)
  1514. * {
  1515. * std::istream is(&b);
  1516. * std::string line;
  1517. * std::getline(is, line);
  1518. * ...
  1519. * }
  1520. * }
  1521. * ...
  1522. * boost::asio::async_read_until(s, b, "\r\n", handler); @endcode
  1523. * After the @c async_read_until operation completes successfully, the buffer
  1524. * @c b contains the delimiter:
  1525. * @code { 'a', 'b', ..., 'c', '\r', '\n', 'd', 'e', ... } @endcode
  1526. * The call to @c std::getline then extracts the data up to and including the
  1527. * newline (which is discarded), so that the string @c line contains:
  1528. * @code { 'a', 'b', ..., 'c', '\r' } @endcode
  1529. * The remaining data is left in the buffer @c b as follows:
  1530. * @code { 'd', 'e', ... } @endcode
  1531. * This data may be the start of a new line, to be extracted by a subsequent
  1532. * @c async_read_until operation.
  1533. */
  1534. template <typename AsyncReadStream, typename Allocator, typename ReadHandler>
  1535. BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
  1536. void (boost::system::error_code, std::size_t))
  1537. async_read_until(AsyncReadStream& s,
  1538. boost::asio::basic_streambuf<Allocator>& b,
  1539. BOOST_ASIO_STRING_VIEW_PARAM delim,
  1540. BOOST_ASIO_MOVE_ARG(ReadHandler) handler);
  1541. #if defined(BOOST_ASIO_HAS_BOOST_REGEX) \
  1542. || defined(GENERATING_DOCUMENTATION)
  1543. /// Start an asynchronous operation to read data into a streambuf until some
  1544. /// part of its data matches a regular expression.
  1545. /**
  1546. * This function is used to asynchronously read data into the specified
  1547. * streambuf until the streambuf's get area contains some data that matches a
  1548. * regular expression. The function call always returns immediately. The
  1549. * asynchronous operation will continue until one of the following conditions
  1550. * is true:
  1551. *
  1552. * @li A substring of the streambuf's get area matches the regular expression.
  1553. *
  1554. * @li An error occurred.
  1555. *
  1556. * This operation is implemented in terms of zero or more calls to the stream's
  1557. * async_read_some function, and is known as a <em>composed operation</em>. If
  1558. * the streambuf's get area already contains data that matches the regular
  1559. * expression, this asynchronous operation completes immediately. The program
  1560. * must ensure that the stream performs no other read operations (such as
  1561. * async_read, async_read_until, the stream's async_read_some function, or any
  1562. * other composed operations that perform reads) until this operation
  1563. * completes.
  1564. *
  1565. * @param s The stream from which the data is to be read. The type must support
  1566. * the AsyncReadStream concept.
  1567. *
  1568. * @param b A streambuf object into which the data will be read. Ownership of
  1569. * the streambuf is retained by the caller, which must guarantee that it remains
  1570. * valid until the handler is called.
  1571. *
  1572. * @param expr The regular expression.
  1573. *
  1574. * @param handler The handler to be called when the read operation completes.
  1575. * Copies will be made of the handler as required. The function signature of the
  1576. * handler must be:
  1577. * @code void handler(
  1578. * // Result of operation.
  1579. * const boost::system::error_code& error,
  1580. *
  1581. * // The number of bytes in the streambuf's get
  1582. * // area up to and including the substring
  1583. * // that matches the regular. expression.
  1584. * // 0 if an error occurred.
  1585. * std::size_t bytes_transferred
  1586. * ); @endcode
  1587. * Regardless of whether the asynchronous operation completes immediately or
  1588. * not, the handler will not be invoked from within this function. Invocation of
  1589. * the handler will be performed in a manner equivalent to using
  1590. * boost::asio::io_context::post().
  1591. *
  1592. * @note After a successful async_read_until operation, the streambuf may
  1593. * contain additional data beyond that which matched the regular expression. An
  1594. * application will typically leave that data in the streambuf for a subsequent
  1595. * async_read_until operation to examine.
  1596. *
  1597. * @par Example
  1598. * To asynchronously read data into a streambuf until a CR-LF sequence is
  1599. * encountered:
  1600. * @code boost::asio::streambuf b;
  1601. * ...
  1602. * void handler(const boost::system::error_code& e, std::size_t size)
  1603. * {
  1604. * if (!e)
  1605. * {
  1606. * std::istream is(&b);
  1607. * std::string line;
  1608. * std::getline(is, line);
  1609. * ...
  1610. * }
  1611. * }
  1612. * ...
  1613. * boost::asio::async_read_until(s, b, boost::regex("\r\n"), handler); @endcode
  1614. * After the @c async_read_until operation completes successfully, the buffer
  1615. * @c b contains the data which matched the regular expression:
  1616. * @code { 'a', 'b', ..., 'c', '\r', '\n', 'd', 'e', ... } @endcode
  1617. * The call to @c std::getline then extracts the data up to and including the
  1618. * newline (which is discarded), so that the string @c line contains:
  1619. * @code { 'a', 'b', ..., 'c', '\r' } @endcode
  1620. * The remaining data is left in the buffer @c b as follows:
  1621. * @code { 'd', 'e', ... } @endcode
  1622. * This data may be the start of a new line, to be extracted by a subsequent
  1623. * @c async_read_until operation.
  1624. */
  1625. template <typename AsyncReadStream, typename Allocator, typename ReadHandler>
  1626. BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
  1627. void (boost::system::error_code, std::size_t))
  1628. async_read_until(AsyncReadStream& s,
  1629. boost::asio::basic_streambuf<Allocator>& b, const boost::regex& expr,
  1630. BOOST_ASIO_MOVE_ARG(ReadHandler) handler);
  1631. #endif // defined(BOOST_ASIO_HAS_BOOST_REGEX)
  1632. // || defined(GENERATING_DOCUMENTATION)
  1633. /// Start an asynchronous operation to read data into a streambuf until a
  1634. /// function object indicates a match.
  1635. /**
  1636. * This function is used to asynchronously read data into the specified
  1637. * streambuf until a user-defined match condition function object, when applied
  1638. * to the data contained in the streambuf, indicates a successful match. The
  1639. * function call always returns immediately. The asynchronous operation will
  1640. * continue until one of the following conditions is true:
  1641. *
  1642. * @li The match condition function object returns a std::pair where the second
  1643. * element evaluates to true.
  1644. *
  1645. * @li An error occurred.
  1646. *
  1647. * This operation is implemented in terms of zero or more calls to the stream's
  1648. * async_read_some function, and is known as a <em>composed operation</em>. If
  1649. * the match condition function object already indicates a match, this
  1650. * asynchronous operation completes immediately. The program must ensure that
  1651. * the stream performs no other read operations (such as async_read,
  1652. * async_read_until, the stream's async_read_some function, or any other
  1653. * composed operations that perform reads) until this operation completes.
  1654. *
  1655. * @param s The stream from which the data is to be read. The type must support
  1656. * the AsyncReadStream concept.
  1657. *
  1658. * @param b A streambuf object into which the data will be read.
  1659. *
  1660. * @param match_condition The function object to be called to determine whether
  1661. * a match exists. The signature of the function object must be:
  1662. * @code pair<iterator, bool> match_condition(iterator begin, iterator end);
  1663. * @endcode
  1664. * where @c iterator represents the type:
  1665. * @code buffers_iterator<basic_streambuf<Allocator>::const_buffers_type>
  1666. * @endcode
  1667. * The iterator parameters @c begin and @c end define the range of bytes to be
  1668. * scanned to determine whether there is a match. The @c first member of the
  1669. * return value is an iterator marking one-past-the-end of the bytes that have
  1670. * been consumed by the match function. This iterator is used to calculate the
  1671. * @c begin parameter for any subsequent invocation of the match condition. The
  1672. * @c second member of the return value is true if a match has been found, false
  1673. * otherwise.
  1674. *
  1675. * @param handler The handler to be called when the read operation completes.
  1676. * Copies will be made of the handler as required. The function signature of the
  1677. * handler must be:
  1678. * @code void handler(
  1679. * // Result of operation.
  1680. * const boost::system::error_code& error,
  1681. *
  1682. * // The number of bytes in the streambuf's get
  1683. * // area that have been fully consumed by the
  1684. * // match function. O if an error occurred.
  1685. * std::size_t bytes_transferred
  1686. * ); @endcode
  1687. * Regardless of whether the asynchronous operation completes immediately or
  1688. * not, the handler will not be invoked from within this function. Invocation of
  1689. * the handler will be performed in a manner equivalent to using
  1690. * boost::asio::io_context::post().
  1691. *
  1692. * @note After a successful async_read_until operation, the streambuf may
  1693. * contain additional data beyond that which matched the function object. An
  1694. * application will typically leave that data in the streambuf for a subsequent
  1695. * async_read_until operation to examine.
  1696. *
  1697. * @note The default implementation of the @c is_match_condition type trait
  1698. * evaluates to true for function pointers and function objects with a
  1699. * @c result_type typedef. It must be specialised for other user-defined
  1700. * function objects.
  1701. *
  1702. * @par Examples
  1703. * To asynchronously read data into a streambuf until whitespace is encountered:
  1704. * @code typedef boost::asio::buffers_iterator<
  1705. * boost::asio::streambuf::const_buffers_type> iterator;
  1706. *
  1707. * std::pair<iterator, bool>
  1708. * match_whitespace(iterator begin, iterator end)
  1709. * {
  1710. * iterator i = begin;
  1711. * while (i != end)
  1712. * if (std::isspace(*i++))
  1713. * return std::make_pair(i, true);
  1714. * return std::make_pair(i, false);
  1715. * }
  1716. * ...
  1717. * void handler(const boost::system::error_code& e, std::size_t size);
  1718. * ...
  1719. * boost::asio::streambuf b;
  1720. * boost::asio::async_read_until(s, b, match_whitespace, handler);
  1721. * @endcode
  1722. *
  1723. * To asynchronously read data into a streambuf until a matching character is
  1724. * found:
  1725. * @code class match_char
  1726. * {
  1727. * public:
  1728. * explicit match_char(char c) : c_(c) {}
  1729. *
  1730. * template <typename Iterator>
  1731. * std::pair<Iterator, bool> operator()(
  1732. * Iterator begin, Iterator end) const
  1733. * {
  1734. * Iterator i = begin;
  1735. * while (i != end)
  1736. * if (c_ == *i++)
  1737. * return std::make_pair(i, true);
  1738. * return std::make_pair(i, false);
  1739. * }
  1740. *
  1741. * private:
  1742. * char c_;
  1743. * };
  1744. *
  1745. * namespace asio {
  1746. * template <> struct is_match_condition<match_char>
  1747. * : public boost::true_type {};
  1748. * } // namespace asio
  1749. * ...
  1750. * void handler(const boost::system::error_code& e, std::size_t size);
  1751. * ...
  1752. * boost::asio::streambuf b;
  1753. * boost::asio::async_read_until(s, b, match_char('a'), handler);
  1754. * @endcode
  1755. */
  1756. template <typename AsyncReadStream, typename Allocator,
  1757. typename MatchCondition, typename ReadHandler>
  1758. BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
  1759. void (boost::system::error_code, std::size_t))
  1760. async_read_until(AsyncReadStream& s,
  1761. boost::asio::basic_streambuf<Allocator>& b,
  1762. MatchCondition match_condition, BOOST_ASIO_MOVE_ARG(ReadHandler) handler,
  1763. typename enable_if<is_match_condition<MatchCondition>::value>::type* = 0);
  1764. #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
  1765. #endif // !defined(BOOST_ASIO_NO_EXTENSIONS)
  1766. /*@}*/
  1767. } // namespace asio
  1768. } // namespace boost
  1769. #include <boost/asio/detail/pop_options.hpp>
  1770. #include <boost/asio/impl/read_until.hpp>
  1771. #endif // BOOST_ASIO_READ_UNTIL_HPP