Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

read_until.hpp 72KB

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