read_until.hpp 73 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 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