read_until.hpp 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826
  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 ASIO_READ_UNTIL_HPP
  11. #define ASIO_READ_UNTIL_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/config.hpp"
  16. #include <cstddef>
  17. #include <string>
  18. #include "asio/async_result.hpp"
  19. #include "asio/detail/regex_fwd.hpp"
  20. #include "asio/detail/string_view.hpp"
  21. #include "asio/detail/type_traits.hpp"
  22. #include "asio/error.hpp"
  23. #if !defined(ASIO_NO_EXTENSIONS)
  24. # include "asio/basic_streambuf_fwd.hpp"
  25. #endif // !defined(ASIO_NO_EXTENSIONS)
  26. #include "asio/detail/push_options.hpp"
  27. namespace asio {
  28. namespace detail
  29. {
  30. char (&has_result_type_helper(...))[2];
  31. template <typename T>
  32. char has_result_type_helper(T*, typename T::result_type* = 0);
  33. template <typename T>
  34. struct has_result_type
  35. {
  36. enum { value = (sizeof((has_result_type_helper)((T*)(0))) == 1) };
  37. };
  38. } // namespace detail
  39. /// Type trait used to determine whether a type can be used as a match condition
  40. /// function with read_until and async_read_until.
  41. template <typename T>
  42. struct is_match_condition
  43. {
  44. #if defined(GENERATING_DOCUMENTATION)
  45. /// The value member is true if the type may be used as a match condition.
  46. static const bool value;
  47. #else
  48. enum
  49. {
  50. value = asio::is_function<
  51. typename asio::remove_pointer<T>::type>::value
  52. || detail::has_result_type<T>::value
  53. };
  54. #endif
  55. };
  56. /**
  57. * @defgroup read_until asio::read_until
  58. *
  59. * @brief The @c read_until function is a composed operation that reads data
  60. * into a dynamic buffer sequence, or into a streambuf, until it contains a
  61. * delimiter, matches a regular expression, or a function object indicates a
  62. * 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 asio::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 = asio::read_until(s,
  103. * 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. 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. ASIO_MOVE_ARG(DynamicBuffer) buffers,
  158. char delim, asio::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 = asio::read_until(s,
  195. * 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. ASIO_MOVE_ARG(DynamicBuffer) buffers,
  213. 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. ASIO_MOVE_ARG(DynamicBuffer) buffers,
  251. ASIO_STRING_VIEW_PARAM delim,
  252. asio::error_code& ec);
  253. #if !defined(ASIO_NO_EXTENSIONS)
  254. #if defined(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 asio::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 = asio::read_until(s,
  295. * 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. 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. ASIO_MOVE_ARG(DynamicBuffer) buffers,
  353. const boost::regex& expr, asio::error_code& ec);
  354. #endif // defined(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 asio::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 asio::buffers_iterator<
  410. * 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. * 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. * 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. 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. ASIO_MOVE_ARG(DynamicBuffer) buffers,
  519. MatchCondition match_condition, asio::error_code& ec,
  520. typename enable_if<is_match_condition<MatchCondition>::value>::type* = 0);
  521. #if !defined(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 asio::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 asio::streambuf b;
  555. * 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. 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. asio::basic_streambuf<Allocator>& b, char delim,
  606. asio::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 asio::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 asio::streambuf b;
  640. * 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. asio::basic_streambuf<Allocator>& b,
  658. 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. asio::basic_streambuf<Allocator>& b,
  692. ASIO_STRING_VIEW_PARAM delim, asio::error_code& ec);
  693. #if defined(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 asio::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 asio::streambuf b;
  730. * 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. 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. asio::basic_streambuf<Allocator>& b, const boost::regex& expr,
  784. asio::error_code& ec);
  785. #endif // defined(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 asio::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 asio::buffers_iterator<
  841. * 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. * asio::streambuf b;
  854. * 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. * asio::streambuf b;
  884. * 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. 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. asio::basic_streambuf<Allocator>& b,
  945. MatchCondition match_condition, asio::error_code& ec,
  946. typename enable_if<is_match_condition<MatchCondition>::value>::type* = 0);
  947. #endif // !defined(ASIO_NO_IOSTREAM)
  948. #endif // !defined(ASIO_NO_EXTENSIONS)
  949. /*@}*/
  950. /**
  951. * @defgroup async_read_until asio::async_read_until
  952. *
  953. * @brief The @c async_read_until function is a composed asynchronous operation
  954. * that reads data into a dynamic buffer sequence, or into a streambuf, until
  955. * it contains a delimiter, matches a regular expression, or a function object
  956. * indicates a match.
  957. */
  958. /*@{*/
  959. /// Start an asynchronous operation to read data into a dynamic buffer sequence
  960. /// until it contains a specified delimiter.
  961. /**
  962. * This function is used to asynchronously read data into the specified dynamic
  963. * buffer sequence until the dynamic buffer sequence's get area contains the
  964. * specified delimiter. The function call always returns immediately. The
  965. * asynchronous operation will continue until one of the following conditions
  966. * is true:
  967. *
  968. * @li The get area of the dynamic buffer sequence contains the specified
  969. * delimiter.
  970. *
  971. * @li An error occurred.
  972. *
  973. * This operation is implemented in terms of zero or more calls to the stream's
  974. * async_read_some function, and is known as a <em>composed operation</em>. If
  975. * the dynamic buffer sequence's get area already contains the delimiter, this
  976. * asynchronous operation completes immediately. The program must ensure that
  977. * the stream performs no other read operations (such as async_read,
  978. * async_read_until, the stream's async_read_some function, or any other
  979. * composed operations that perform reads) until this operation completes.
  980. *
  981. * @param s The stream from which the data is to be read. The type must support
  982. * the AsyncReadStream concept.
  983. *
  984. * @param buffers The dynamic buffer sequence into which the data will be read.
  985. * Although the buffers object may be copied as necessary, ownership of the
  986. * underlying memory blocks is retained by the caller, which must guarantee
  987. * that they remain valid until the handler is called.
  988. *
  989. * @param delim The delimiter character.
  990. *
  991. * @param handler The handler to be called when the read operation completes.
  992. * Copies will be made of the handler as required. The function signature of the
  993. * handler must be:
  994. * @code void handler(
  995. * // Result of operation.
  996. * const asio::error_code& error,
  997. *
  998. * // The number of bytes in the dynamic buffer sequence's
  999. * // get area up to and including the delimiter.
  1000. * // 0 if an error occurred.
  1001. * std::size_t bytes_transferred
  1002. * ); @endcode
  1003. * Regardless of whether the asynchronous operation completes immediately or
  1004. * not, the handler will not be invoked from within this function. Invocation of
  1005. * the handler will be performed in a manner equivalent to using
  1006. * asio::io_context::post().
  1007. *
  1008. * @note After a successful async_read_until operation, the dynamic buffer
  1009. * sequence may contain additional data beyond the delimiter. An application
  1010. * will typically leave that data in the dynamic buffer sequence for a
  1011. * subsequent async_read_until operation to examine.
  1012. *
  1013. * @par Example
  1014. * To asynchronously read data into a @c std::string until a newline is
  1015. * encountered:
  1016. * @code std::string data;
  1017. * ...
  1018. * void handler(const asio::error_code& e, std::size_t size)
  1019. * {
  1020. * if (!e)
  1021. * {
  1022. * std::string line = data.substr(0, n);
  1023. * data.erase(0, n);
  1024. * ...
  1025. * }
  1026. * }
  1027. * ...
  1028. * asio::async_read_until(s, data, '\n', handler); @endcode
  1029. * After the @c async_read_until operation completes successfully, the buffer
  1030. * @c data contains the delimiter:
  1031. * @code { 'a', 'b', ..., 'c', '\n', 'd', 'e', ... } @endcode
  1032. * The call to @c substr then extracts the data up to and including the
  1033. * delimiter, so that the string @c line contains:
  1034. * @code { 'a', 'b', ..., 'c', '\n' } @endcode
  1035. * After the call to @c erase, the remaining data is left in the buffer @c data
  1036. * as follows:
  1037. * @code { 'd', 'e', ... } @endcode
  1038. * This data may be the start of a new line, to be extracted by a subsequent
  1039. * @c async_read_until operation.
  1040. */
  1041. template <typename AsyncReadStream,
  1042. typename DynamicBuffer, typename ReadHandler>
  1043. ASIO_INITFN_RESULT_TYPE(ReadHandler,
  1044. void (asio::error_code, std::size_t))
  1045. async_read_until(AsyncReadStream& s,
  1046. ASIO_MOVE_ARG(DynamicBuffer) buffers,
  1047. char delim, ASIO_MOVE_ARG(ReadHandler) handler);
  1048. /// Start an asynchronous operation to read data into a dynamic buffer sequence
  1049. /// until it contains a specified delimiter.
  1050. /**
  1051. * This function is used to asynchronously read data into the specified dynamic
  1052. * buffer sequence until the dynamic buffer sequence's get area contains the
  1053. * specified delimiter. The function call always returns immediately. The
  1054. * asynchronous operation will continue until one of the following conditions
  1055. * is true:
  1056. *
  1057. * @li The get area of the dynamic buffer sequence contains the specified
  1058. * delimiter.
  1059. *
  1060. * @li An error occurred.
  1061. *
  1062. * This operation is implemented in terms of zero or more calls to the stream's
  1063. * async_read_some function, and is known as a <em>composed operation</em>. If
  1064. * the dynamic buffer sequence's get area already contains the delimiter, this
  1065. * asynchronous operation completes immediately. The program must ensure that
  1066. * the stream performs no other read operations (such as async_read,
  1067. * async_read_until, the stream's async_read_some function, or any other
  1068. * composed operations that perform reads) until this operation completes.
  1069. *
  1070. * @param s The stream from which the data is to be read. The type must support
  1071. * the AsyncReadStream concept.
  1072. *
  1073. * @param buffers The dynamic buffer sequence into which the data will be read.
  1074. * Although the buffers object may be copied as necessary, ownership of the
  1075. * underlying memory blocks is retained by the caller, which must guarantee
  1076. * that they remain valid until the handler is called.
  1077. *
  1078. * @param delim The delimiter string.
  1079. *
  1080. * @param handler The handler to be called when the read operation completes.
  1081. * Copies will be made of the handler as required. The function signature of the
  1082. * handler must be:
  1083. * @code void handler(
  1084. * // Result of operation.
  1085. * const asio::error_code& error,
  1086. *
  1087. * // The number of bytes in the dynamic buffer sequence's
  1088. * // get area up to and including the delimiter.
  1089. * // 0 if an error occurred.
  1090. * std::size_t bytes_transferred
  1091. * ); @endcode
  1092. * Regardless of whether the asynchronous operation completes immediately or
  1093. * not, the handler will not be invoked from within this function. Invocation of
  1094. * the handler will be performed in a manner equivalent to using
  1095. * asio::io_context::post().
  1096. *
  1097. * @note After a successful async_read_until operation, the dynamic buffer
  1098. * sequence may contain additional data beyond the delimiter. An application
  1099. * will typically leave that data in the dynamic buffer sequence for a
  1100. * subsequent async_read_until operation to examine.
  1101. *
  1102. * @par Example
  1103. * To asynchronously read data into a @c std::string until a CR-LF sequence is
  1104. * encountered:
  1105. * @code std::string data;
  1106. * ...
  1107. * void handler(const asio::error_code& e, std::size_t size)
  1108. * {
  1109. * if (!e)
  1110. * {
  1111. * std::string line = data.substr(0, n);
  1112. * data.erase(0, n);
  1113. * ...
  1114. * }
  1115. * }
  1116. * ...
  1117. * asio::async_read_until(s, data, "\r\n", handler); @endcode
  1118. * After the @c async_read_until operation completes successfully, the string
  1119. * @c data contains the delimiter:
  1120. * @code { 'a', 'b', ..., 'c', '\r', '\n', 'd', 'e', ... } @endcode
  1121. * The call to @c substr then extracts the data up to and including the
  1122. * delimiter, so that the string @c line contains:
  1123. * @code { 'a', 'b', ..., 'c', '\r', '\n' } @endcode
  1124. * After the call to @c erase, the remaining data is left in the string @c data
  1125. * as follows:
  1126. * @code { 'd', 'e', ... } @endcode
  1127. * This data may be the start of a new line, to be extracted by a subsequent
  1128. * @c async_read_until operation.
  1129. */
  1130. template <typename AsyncReadStream,
  1131. typename DynamicBuffer, typename ReadHandler>
  1132. ASIO_INITFN_RESULT_TYPE(ReadHandler,
  1133. void (asio::error_code, std::size_t))
  1134. async_read_until(AsyncReadStream& s,
  1135. ASIO_MOVE_ARG(DynamicBuffer) buffers,
  1136. ASIO_STRING_VIEW_PARAM delim,
  1137. ASIO_MOVE_ARG(ReadHandler) handler);
  1138. #if !defined(ASIO_NO_EXTENSIONS)
  1139. #if defined(ASIO_HAS_BOOST_REGEX) \
  1140. || defined(GENERATING_DOCUMENTATION)
  1141. /// Start an asynchronous operation to read data into a dynamic buffer sequence
  1142. /// until some part of its data matches a regular expression.
  1143. /**
  1144. * This function is used to asynchronously read data into the specified dynamic
  1145. * buffer sequence until the dynamic buffer sequence's get area contains some
  1146. * data that matches a regular expression. The function call always returns
  1147. * immediately. The asynchronous operation will continue until one of the
  1148. * following conditions is true:
  1149. *
  1150. * @li A substring of the dynamic buffer sequence's get area matches the regular
  1151. * expression.
  1152. *
  1153. * @li An error occurred.
  1154. *
  1155. * This operation is implemented in terms of zero or more calls to the stream's
  1156. * async_read_some function, and is known as a <em>composed operation</em>. If
  1157. * the dynamic buffer sequence's get area already contains data that matches
  1158. * the regular expression, this asynchronous operation completes immediately.
  1159. * The program must ensure that the stream performs no other read operations
  1160. * (such as async_read, async_read_until, the stream's async_read_some
  1161. * function, or any other composed operations that perform reads) until this
  1162. * operation completes.
  1163. *
  1164. * @param s The stream from which the data is to be read. The type must support
  1165. * the AsyncReadStream concept.
  1166. *
  1167. * @param buffers The dynamic buffer sequence into which the data will be read.
  1168. * Although the buffers object may be copied as necessary, ownership of the
  1169. * underlying memory blocks is retained by the caller, which must guarantee
  1170. * that they remain valid until the handler is called.
  1171. *
  1172. * @param expr The regular expression.
  1173. *
  1174. * @param handler The handler to be called when the read operation completes.
  1175. * Copies will be made of the handler as required. The function signature of the
  1176. * handler must be:
  1177. * @code void handler(
  1178. * // Result of operation.
  1179. * const asio::error_code& error,
  1180. *
  1181. * // The number of bytes in the dynamic buffer
  1182. * // sequence's get area up to and including the
  1183. * // substring that matches the regular expression.
  1184. * // 0 if an error occurred.
  1185. * std::size_t bytes_transferred
  1186. * ); @endcode
  1187. * Regardless of whether the asynchronous operation completes immediately or
  1188. * not, the handler will not be invoked from within this function. Invocation of
  1189. * the handler will be performed in a manner equivalent to using
  1190. * asio::io_context::post().
  1191. *
  1192. * @note After a successful async_read_until operation, the dynamic buffer
  1193. * sequence may contain additional data beyond that which matched the regular
  1194. * expression. An application will typically leave that data in the dynamic
  1195. * buffer sequence for a subsequent async_read_until operation to examine.
  1196. *
  1197. * @par Example
  1198. * To asynchronously read data into a @c std::string until a CR-LF sequence is
  1199. * encountered:
  1200. * @code std::string data;
  1201. * ...
  1202. * void handler(const asio::error_code& e, std::size_t size)
  1203. * {
  1204. * if (!e)
  1205. * {
  1206. * std::string line = data.substr(0, n);
  1207. * data.erase(0, n);
  1208. * ...
  1209. * }
  1210. * }
  1211. * ...
  1212. * asio::async_read_until(s, data,
  1213. * boost::regex("\r\n"), handler); @endcode
  1214. * After the @c async_read_until operation completes successfully, the string
  1215. * @c data contains the data which matched the regular expression:
  1216. * @code { 'a', 'b', ..., 'c', '\r', '\n', 'd', 'e', ... } @endcode
  1217. * The call to @c substr then extracts the data up to and including the match,
  1218. * so that the string @c line contains:
  1219. * @code { 'a', 'b', ..., 'c', '\r', '\n' } @endcode
  1220. * After the call to @c erase, the remaining data is left in the string @c data
  1221. * as follows:
  1222. * @code { 'd', 'e', ... } @endcode
  1223. * This data may be the start of a new line, to be extracted by a subsequent
  1224. * @c async_read_until operation.
  1225. */
  1226. template <typename AsyncReadStream,
  1227. typename DynamicBuffer, typename ReadHandler>
  1228. ASIO_INITFN_RESULT_TYPE(ReadHandler,
  1229. void (asio::error_code, std::size_t))
  1230. async_read_until(AsyncReadStream& s,
  1231. ASIO_MOVE_ARG(DynamicBuffer) buffers,
  1232. const boost::regex& expr,
  1233. ASIO_MOVE_ARG(ReadHandler) handler);
  1234. #endif // defined(ASIO_HAS_BOOST_REGEX)
  1235. // || defined(GENERATING_DOCUMENTATION)
  1236. /// Start an asynchronous operation to read data into a dynamic buffer sequence
  1237. /// until a function object indicates a match.
  1238. /**
  1239. * This function is used to asynchronously read data into the specified dynamic
  1240. * buffer sequence until a user-defined match condition function object, when
  1241. * applied to the data contained in the dynamic buffer sequence, indicates a
  1242. * successful match. The function call always returns immediately. The
  1243. * asynchronous operation will continue until one of the following conditions
  1244. * is true:
  1245. *
  1246. * @li The match condition function object returns a std::pair where the second
  1247. * element evaluates to true.
  1248. *
  1249. * @li An error occurred.
  1250. *
  1251. * This operation is implemented in terms of zero or more calls to the stream's
  1252. * async_read_some function, and is known as a <em>composed operation</em>. If
  1253. * the match condition function object already indicates a match, this
  1254. * asynchronous operation completes immediately. The program must ensure that
  1255. * the stream performs no other read operations (such as async_read,
  1256. * async_read_until, the stream's async_read_some function, or any other
  1257. * composed operations that perform reads) until this operation completes.
  1258. *
  1259. * @param s The stream from which the data is to be read. The type must support
  1260. * the AsyncReadStream concept.
  1261. *
  1262. * @param buffers The dynamic buffer sequence into which the data will be read.
  1263. * Although the buffers object may be copied as necessary, ownership of the
  1264. * underlying memory blocks is retained by the caller, which must guarantee
  1265. * that they remain valid until the handler is called.
  1266. *
  1267. * @param match_condition The function object to be called to determine whether
  1268. * a match exists. The signature of the function object must be:
  1269. * @code pair<iterator, bool> match_condition(iterator begin, iterator end);
  1270. * @endcode
  1271. * where @c iterator represents the type:
  1272. * @code buffers_iterator<typename DynamicBuffer::const_buffers_type>
  1273. * @endcode
  1274. * The iterator parameters @c begin and @c end define the range of bytes to be
  1275. * scanned to determine whether there is a match. The @c first member of the
  1276. * return value is an iterator marking one-past-the-end of the bytes that have
  1277. * been consumed by the match function. This iterator is used to calculate the
  1278. * @c begin parameter for any subsequent invocation of the match condition. The
  1279. * @c second member of the return value is true if a match has been found, false
  1280. * otherwise.
  1281. *
  1282. * @param handler The handler to be called when the read operation completes.
  1283. * Copies will be made of the handler as required. The function signature of the
  1284. * handler must be:
  1285. * @code void handler(
  1286. * // Result of operation.
  1287. * const asio::error_code& error,
  1288. *
  1289. * // The number of bytes in the dynamic buffer sequence's
  1290. * // get area that have been fully consumed by the match
  1291. * // function. O if an error occurred.
  1292. * std::size_t bytes_transferred
  1293. * ); @endcode
  1294. * Regardless of whether the asynchronous operation completes immediately or
  1295. * not, the handler will not be invoked from within this function. Invocation of
  1296. * the handler will be performed in a manner equivalent to using
  1297. * asio::io_context::post().
  1298. *
  1299. * @note After a successful async_read_until operation, the dynamic buffer
  1300. * sequence may contain additional data beyond that which matched the function
  1301. * object. An application will typically leave that data in the dynamic buffer
  1302. * sequence for a subsequent async_read_until operation to examine.
  1303. *
  1304. * @note The default implementation of the @c is_match_condition type trait
  1305. * evaluates to true for function pointers and function objects with a
  1306. * @c result_type typedef. It must be specialised for other user-defined
  1307. * function objects.
  1308. *
  1309. * @par Examples
  1310. * To asynchronously read data into a @c std::string until whitespace is
  1311. * encountered:
  1312. * @code typedef asio::buffers_iterator<
  1313. * asio::const_buffers_1> iterator;
  1314. *
  1315. * std::pair<iterator, bool>
  1316. * match_whitespace(iterator begin, iterator end)
  1317. * {
  1318. * iterator i = begin;
  1319. * while (i != end)
  1320. * if (std::isspace(*i++))
  1321. * return std::make_pair(i, true);
  1322. * return std::make_pair(i, false);
  1323. * }
  1324. * ...
  1325. * void handler(const asio::error_code& e, std::size_t size);
  1326. * ...
  1327. * std::string data;
  1328. * asio::async_read_until(s, data, match_whitespace, handler);
  1329. * @endcode
  1330. *
  1331. * To asynchronously read data into a @c std::string until a matching character
  1332. * is found:
  1333. * @code class match_char
  1334. * {
  1335. * public:
  1336. * explicit match_char(char c) : c_(c) {}
  1337. *
  1338. * template <typename Iterator>
  1339. * std::pair<Iterator, bool> operator()(
  1340. * Iterator begin, Iterator end) const
  1341. * {
  1342. * Iterator i = begin;
  1343. * while (i != end)
  1344. * if (c_ == *i++)
  1345. * return std::make_pair(i, true);
  1346. * return std::make_pair(i, false);
  1347. * }
  1348. *
  1349. * private:
  1350. * char c_;
  1351. * };
  1352. *
  1353. * namespace asio {
  1354. * template <> struct is_match_condition<match_char>
  1355. * : public boost::true_type {};
  1356. * } // namespace asio
  1357. * ...
  1358. * void handler(const asio::error_code& e, std::size_t size);
  1359. * ...
  1360. * std::string data;
  1361. * asio::async_read_until(s, data, match_char('a'), handler);
  1362. * @endcode
  1363. */
  1364. template <typename AsyncReadStream, typename DynamicBuffer,
  1365. typename MatchCondition, typename ReadHandler>
  1366. ASIO_INITFN_RESULT_TYPE(ReadHandler,
  1367. void (asio::error_code, std::size_t))
  1368. async_read_until(AsyncReadStream& s,
  1369. ASIO_MOVE_ARG(DynamicBuffer) buffers,
  1370. MatchCondition match_condition, ASIO_MOVE_ARG(ReadHandler) handler,
  1371. typename enable_if<is_match_condition<MatchCondition>::value>::type* = 0);
  1372. #if !defined(ASIO_NO_IOSTREAM)
  1373. /// Start an asynchronous operation to read data into a streambuf until it
  1374. /// contains a specified delimiter.
  1375. /**
  1376. * This function is used to asynchronously read data into the specified
  1377. * streambuf until the streambuf's get area contains the specified delimiter.
  1378. * The function call always returns immediately. The asynchronous operation
  1379. * will continue until one of the following conditions is true:
  1380. *
  1381. * @li The get area of the streambuf contains the specified delimiter.
  1382. *
  1383. * @li An error occurred.
  1384. *
  1385. * This operation is implemented in terms of zero or more calls to the stream's
  1386. * async_read_some function, and is known as a <em>composed operation</em>. If
  1387. * the streambuf's get area already contains the delimiter, this asynchronous
  1388. * operation completes immediately. The program must ensure that the stream
  1389. * performs no other read operations (such as async_read, async_read_until, the
  1390. * stream's async_read_some function, or any other composed operations that
  1391. * perform reads) until this operation completes.
  1392. *
  1393. * @param s The stream from which the data is to be read. The type must support
  1394. * the AsyncReadStream concept.
  1395. *
  1396. * @param b A streambuf object into which the data will be read. Ownership of
  1397. * the streambuf is retained by the caller, which must guarantee that it remains
  1398. * valid until the handler is called.
  1399. *
  1400. * @param delim The delimiter character.
  1401. *
  1402. * @param handler The handler to be called when the read operation completes.
  1403. * Copies will be made of the handler as required. The function signature of the
  1404. * handler must be:
  1405. * @code void handler(
  1406. * // Result of operation.
  1407. * const asio::error_code& error,
  1408. *
  1409. * // The number of bytes in the streambuf's get
  1410. * // area up to and including the delimiter.
  1411. * // 0 if an error occurred.
  1412. * std::size_t bytes_transferred
  1413. * ); @endcode
  1414. * Regardless of whether the asynchronous operation completes immediately or
  1415. * not, the handler will not be invoked from within this function. Invocation of
  1416. * the handler will be performed in a manner equivalent to using
  1417. * asio::io_context::post().
  1418. *
  1419. * @note After a successful async_read_until operation, the streambuf may
  1420. * contain additional data beyond the delimiter. An application will typically
  1421. * leave that data in the streambuf for a subsequent async_read_until operation
  1422. * to examine.
  1423. *
  1424. * @par Example
  1425. * To asynchronously read data into a streambuf until a newline is encountered:
  1426. * @code asio::streambuf b;
  1427. * ...
  1428. * void handler(const asio::error_code& e, std::size_t size)
  1429. * {
  1430. * if (!e)
  1431. * {
  1432. * std::istream is(&b);
  1433. * std::string line;
  1434. * std::getline(is, line);
  1435. * ...
  1436. * }
  1437. * }
  1438. * ...
  1439. * asio::async_read_until(s, b, '\n', handler); @endcode
  1440. * After the @c async_read_until operation completes successfully, the buffer
  1441. * @c b contains the delimiter:
  1442. * @code { 'a', 'b', ..., 'c', '\n', 'd', 'e', ... } @endcode
  1443. * The call to @c std::getline then extracts the data up to and including the
  1444. * newline (which is discarded), so that the string @c line contains:
  1445. * @code { 'a', 'b', ..., 'c' } @endcode
  1446. * The remaining data is left in the buffer @c b as follows:
  1447. * @code { 'd', 'e', ... } @endcode
  1448. * This data may be the start of a new line, to be extracted by a subsequent
  1449. * @c async_read_until operation.
  1450. */
  1451. template <typename AsyncReadStream, typename Allocator, typename ReadHandler>
  1452. ASIO_INITFN_RESULT_TYPE(ReadHandler,
  1453. void (asio::error_code, std::size_t))
  1454. async_read_until(AsyncReadStream& s,
  1455. asio::basic_streambuf<Allocator>& b,
  1456. char delim, ASIO_MOVE_ARG(ReadHandler) handler);
  1457. /// Start an asynchronous operation to read data into a streambuf until it
  1458. /// contains a specified delimiter.
  1459. /**
  1460. * This function is used to asynchronously read data into the specified
  1461. * streambuf until the streambuf's get area contains the specified delimiter.
  1462. * The function call always returns immediately. The asynchronous operation
  1463. * will continue until one of the following conditions is true:
  1464. *
  1465. * @li The get area of the streambuf contains the specified delimiter.
  1466. *
  1467. * @li An error occurred.
  1468. *
  1469. * This operation is implemented in terms of zero or more calls to the stream's
  1470. * async_read_some function, and is known as a <em>composed operation</em>. If
  1471. * the streambuf's get area already contains the delimiter, this asynchronous
  1472. * operation completes immediately. The program must ensure that the stream
  1473. * performs no other read operations (such as async_read, async_read_until, the
  1474. * stream's async_read_some function, or any other composed operations that
  1475. * perform reads) until this operation completes.
  1476. *
  1477. * @param s The stream from which the data is to be read. The type must support
  1478. * the AsyncReadStream concept.
  1479. *
  1480. * @param b A streambuf object into which the data will be read. Ownership of
  1481. * the streambuf is retained by the caller, which must guarantee that it remains
  1482. * valid until the handler is called.
  1483. *
  1484. * @param delim The delimiter string.
  1485. *
  1486. * @param handler The handler to be called when the read operation completes.
  1487. * Copies will be made of the handler as required. The function signature of the
  1488. * handler must be:
  1489. * @code void handler(
  1490. * // Result of operation.
  1491. * const asio::error_code& error,
  1492. *
  1493. * // The number of bytes in the streambuf's get
  1494. * // area up to and including the delimiter.
  1495. * // 0 if an error occurred.
  1496. * std::size_t bytes_transferred
  1497. * ); @endcode
  1498. * Regardless of whether the asynchronous operation completes immediately or
  1499. * not, the handler will not be invoked from within this function. Invocation of
  1500. * the handler will be performed in a manner equivalent to using
  1501. * asio::io_context::post().
  1502. *
  1503. * @note After a successful async_read_until operation, the streambuf may
  1504. * contain additional data beyond the delimiter. An application will typically
  1505. * leave that data in the streambuf for a subsequent async_read_until operation
  1506. * to examine.
  1507. *
  1508. * @par Example
  1509. * To asynchronously read data into a streambuf until a newline is encountered:
  1510. * @code asio::streambuf b;
  1511. * ...
  1512. * void handler(const asio::error_code& e, std::size_t size)
  1513. * {
  1514. * if (!e)
  1515. * {
  1516. * std::istream is(&b);
  1517. * std::string line;
  1518. * std::getline(is, line);
  1519. * ...
  1520. * }
  1521. * }
  1522. * ...
  1523. * asio::async_read_until(s, b, "\r\n", handler); @endcode
  1524. * After the @c async_read_until operation completes successfully, the buffer
  1525. * @c b contains the delimiter:
  1526. * @code { 'a', 'b', ..., 'c', '\r', '\n', 'd', 'e', ... } @endcode
  1527. * The call to @c std::getline then extracts the data up to and including the
  1528. * newline (which is discarded), so that the string @c line contains:
  1529. * @code { 'a', 'b', ..., 'c', '\r' } @endcode
  1530. * The remaining data is left in the buffer @c b as follows:
  1531. * @code { 'd', 'e', ... } @endcode
  1532. * This data may be the start of a new line, to be extracted by a subsequent
  1533. * @c async_read_until operation.
  1534. */
  1535. template <typename AsyncReadStream, typename Allocator, typename ReadHandler>
  1536. ASIO_INITFN_RESULT_TYPE(ReadHandler,
  1537. void (asio::error_code, std::size_t))
  1538. async_read_until(AsyncReadStream& s,
  1539. asio::basic_streambuf<Allocator>& b,
  1540. ASIO_STRING_VIEW_PARAM delim,
  1541. ASIO_MOVE_ARG(ReadHandler) handler);
  1542. #if defined(ASIO_HAS_BOOST_REGEX) \
  1543. || defined(GENERATING_DOCUMENTATION)
  1544. /// Start an asynchronous operation to read data into a streambuf until some
  1545. /// part of its data matches a regular expression.
  1546. /**
  1547. * This function is used to asynchronously read data into the specified
  1548. * streambuf until the streambuf's get area contains some data that matches a
  1549. * regular expression. The function call always returns immediately. The
  1550. * asynchronous operation will continue until one of the following conditions
  1551. * is true:
  1552. *
  1553. * @li A substring of the streambuf's get area matches the regular expression.
  1554. *
  1555. * @li An error occurred.
  1556. *
  1557. * This operation is implemented in terms of zero or more calls to the stream's
  1558. * async_read_some function, and is known as a <em>composed operation</em>. If
  1559. * the streambuf's get area already contains data that matches the regular
  1560. * expression, this asynchronous operation completes immediately. The program
  1561. * must ensure that the stream performs no other read operations (such as
  1562. * async_read, async_read_until, the stream's async_read_some function, or any
  1563. * other composed operations that perform reads) until this operation
  1564. * completes.
  1565. *
  1566. * @param s The stream from which the data is to be read. The type must support
  1567. * the AsyncReadStream concept.
  1568. *
  1569. * @param b A streambuf object into which the data will be read. Ownership of
  1570. * the streambuf is retained by the caller, which must guarantee that it remains
  1571. * valid until the handler is called.
  1572. *
  1573. * @param expr The regular expression.
  1574. *
  1575. * @param handler The handler to be called when the read operation completes.
  1576. * Copies will be made of the handler as required. The function signature of the
  1577. * handler must be:
  1578. * @code void handler(
  1579. * // Result of operation.
  1580. * const asio::error_code& error,
  1581. *
  1582. * // The number of bytes in the streambuf's get
  1583. * // area up to and including the substring
  1584. * // that matches the regular. expression.
  1585. * // 0 if an error occurred.
  1586. * std::size_t bytes_transferred
  1587. * ); @endcode
  1588. * Regardless of whether the asynchronous operation completes immediately or
  1589. * not, the handler will not be invoked from within this function. Invocation of
  1590. * the handler will be performed in a manner equivalent to using
  1591. * asio::io_context::post().
  1592. *
  1593. * @note After a successful async_read_until operation, the streambuf may
  1594. * contain additional data beyond that which matched the regular expression. An
  1595. * application will typically leave that data in the streambuf for a subsequent
  1596. * async_read_until operation to examine.
  1597. *
  1598. * @par Example
  1599. * To asynchronously read data into a streambuf until a CR-LF sequence is
  1600. * encountered:
  1601. * @code asio::streambuf b;
  1602. * ...
  1603. * void handler(const asio::error_code& e, std::size_t size)
  1604. * {
  1605. * if (!e)
  1606. * {
  1607. * std::istream is(&b);
  1608. * std::string line;
  1609. * std::getline(is, line);
  1610. * ...
  1611. * }
  1612. * }
  1613. * ...
  1614. * asio::async_read_until(s, b, boost::regex("\r\n"), handler); @endcode
  1615. * After the @c async_read_until operation completes successfully, the buffer
  1616. * @c b contains the data which matched the regular expression:
  1617. * @code { 'a', 'b', ..., 'c', '\r', '\n', 'd', 'e', ... } @endcode
  1618. * The call to @c std::getline then extracts the data up to and including the
  1619. * newline (which is discarded), so that the string @c line contains:
  1620. * @code { 'a', 'b', ..., 'c', '\r' } @endcode
  1621. * The remaining data is left in the buffer @c b as follows:
  1622. * @code { 'd', 'e', ... } @endcode
  1623. * This data may be the start of a new line, to be extracted by a subsequent
  1624. * @c async_read_until operation.
  1625. */
  1626. template <typename AsyncReadStream, typename Allocator, typename ReadHandler>
  1627. ASIO_INITFN_RESULT_TYPE(ReadHandler,
  1628. void (asio::error_code, std::size_t))
  1629. async_read_until(AsyncReadStream& s,
  1630. asio::basic_streambuf<Allocator>& b, const boost::regex& expr,
  1631. ASIO_MOVE_ARG(ReadHandler) handler);
  1632. #endif // defined(ASIO_HAS_BOOST_REGEX)
  1633. // || defined(GENERATING_DOCUMENTATION)
  1634. /// Start an asynchronous operation to read data into a streambuf until a
  1635. /// function object indicates a match.
  1636. /**
  1637. * This function is used to asynchronously read data into the specified
  1638. * streambuf until a user-defined match condition function object, when applied
  1639. * to the data contained in the streambuf, indicates a successful match. The
  1640. * function call always returns immediately. The asynchronous operation will
  1641. * continue until one of the following conditions is true:
  1642. *
  1643. * @li The match condition function object returns a std::pair where the second
  1644. * element evaluates to true.
  1645. *
  1646. * @li An error occurred.
  1647. *
  1648. * This operation is implemented in terms of zero or more calls to the stream's
  1649. * async_read_some function, and is known as a <em>composed operation</em>. If
  1650. * the match condition function object already indicates a match, this
  1651. * asynchronous operation completes immediately. The program must ensure that
  1652. * the stream performs no other read operations (such as async_read,
  1653. * async_read_until, the stream's async_read_some function, or any other
  1654. * composed operations that perform reads) until this operation completes.
  1655. *
  1656. * @param s The stream from which the data is to be read. The type must support
  1657. * the AsyncReadStream concept.
  1658. *
  1659. * @param b A streambuf object into which the data will be read.
  1660. *
  1661. * @param match_condition The function object to be called to determine whether
  1662. * a match exists. The signature of the function object must be:
  1663. * @code pair<iterator, bool> match_condition(iterator begin, iterator end);
  1664. * @endcode
  1665. * where @c iterator represents the type:
  1666. * @code buffers_iterator<basic_streambuf<Allocator>::const_buffers_type>
  1667. * @endcode
  1668. * The iterator parameters @c begin and @c end define the range of bytes to be
  1669. * scanned to determine whether there is a match. The @c first member of the
  1670. * return value is an iterator marking one-past-the-end of the bytes that have
  1671. * been consumed by the match function. This iterator is used to calculate the
  1672. * @c begin parameter for any subsequent invocation of the match condition. The
  1673. * @c second member of the return value is true if a match has been found, false
  1674. * otherwise.
  1675. *
  1676. * @param handler The handler to be called when the read operation completes.
  1677. * Copies will be made of the handler as required. The function signature of the
  1678. * handler must be:
  1679. * @code void handler(
  1680. * // Result of operation.
  1681. * const asio::error_code& error,
  1682. *
  1683. * // The number of bytes in the streambuf's get
  1684. * // area that have been fully consumed by the
  1685. * // match function. O if an error occurred.
  1686. * std::size_t bytes_transferred
  1687. * ); @endcode
  1688. * Regardless of whether the asynchronous operation completes immediately or
  1689. * not, the handler will not be invoked from within this function. Invocation of
  1690. * the handler will be performed in a manner equivalent to using
  1691. * asio::io_context::post().
  1692. *
  1693. * @note After a successful async_read_until operation, the streambuf may
  1694. * contain additional data beyond that which matched the function object. An
  1695. * application will typically leave that data in the streambuf for a subsequent
  1696. * async_read_until operation to examine.
  1697. *
  1698. * @note The default implementation of the @c is_match_condition type trait
  1699. * evaluates to true for function pointers and function objects with a
  1700. * @c result_type typedef. It must be specialised for other user-defined
  1701. * function objects.
  1702. *
  1703. * @par Examples
  1704. * To asynchronously read data into a streambuf until whitespace is encountered:
  1705. * @code typedef asio::buffers_iterator<
  1706. * asio::streambuf::const_buffers_type> iterator;
  1707. *
  1708. * std::pair<iterator, bool>
  1709. * match_whitespace(iterator begin, iterator end)
  1710. * {
  1711. * iterator i = begin;
  1712. * while (i != end)
  1713. * if (std::isspace(*i++))
  1714. * return std::make_pair(i, true);
  1715. * return std::make_pair(i, false);
  1716. * }
  1717. * ...
  1718. * void handler(const asio::error_code& e, std::size_t size);
  1719. * ...
  1720. * asio::streambuf b;
  1721. * asio::async_read_until(s, b, match_whitespace, handler);
  1722. * @endcode
  1723. *
  1724. * To asynchronously read data into a streambuf until a matching character is
  1725. * found:
  1726. * @code class match_char
  1727. * {
  1728. * public:
  1729. * explicit match_char(char c) : c_(c) {}
  1730. *
  1731. * template <typename Iterator>
  1732. * std::pair<Iterator, bool> operator()(
  1733. * Iterator begin, Iterator end) const
  1734. * {
  1735. * Iterator i = begin;
  1736. * while (i != end)
  1737. * if (c_ == *i++)
  1738. * return std::make_pair(i, true);
  1739. * return std::make_pair(i, false);
  1740. * }
  1741. *
  1742. * private:
  1743. * char c_;
  1744. * };
  1745. *
  1746. * namespace asio {
  1747. * template <> struct is_match_condition<match_char>
  1748. * : public boost::true_type {};
  1749. * } // namespace asio
  1750. * ...
  1751. * void handler(const asio::error_code& e, std::size_t size);
  1752. * ...
  1753. * asio::streambuf b;
  1754. * asio::async_read_until(s, b, match_char('a'), handler);
  1755. * @endcode
  1756. */
  1757. template <typename AsyncReadStream, typename Allocator,
  1758. typename MatchCondition, typename ReadHandler>
  1759. ASIO_INITFN_RESULT_TYPE(ReadHandler,
  1760. void (asio::error_code, std::size_t))
  1761. async_read_until(AsyncReadStream& s,
  1762. asio::basic_streambuf<Allocator>& b,
  1763. MatchCondition match_condition, ASIO_MOVE_ARG(ReadHandler) handler,
  1764. typename enable_if<is_match_condition<MatchCondition>::value>::type* = 0);
  1765. #endif // !defined(ASIO_NO_IOSTREAM)
  1766. #endif // !defined(ASIO_NO_EXTENSIONS)
  1767. /*@}*/
  1768. } // namespace asio
  1769. #include "asio/detail/pop_options.hpp"
  1770. #include "asio/impl/read_until.hpp"
  1771. #endif // ASIO_READ_UNTIL_HPP