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 112KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795
  1. //
  2. // read_until.hpp
  3. // ~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 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/buffer.hpp"
  20. #include "asio/detail/regex_fwd.hpp"
  21. #include "asio/detail/string_view.hpp"
  22. #include "asio/detail/type_traits.hpp"
  23. #include "asio/error.hpp"
  24. #if !defined(ASIO_NO_EXTENSIONS)
  25. # include "asio/basic_streambuf_fwd.hpp"
  26. #endif // !defined(ASIO_NO_EXTENSIONS)
  27. #include "asio/detail/push_options.hpp"
  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 = asio::is_function<
  52. typename asio::remove_pointer<T>::type>::value
  53. || detail::has_result_type<T>::value
  54. };
  55. #endif
  56. };
  57. /**
  58. * @defgroup read_until asio::read_until
  59. *
  60. * @brief The @c read_until function is a composed operation that reads data
  61. * into a dynamic buffer sequence, or into a streambuf, until it contains a
  62. * delimiter, matches a regular expression, or a function object indicates a
  63. * match.
  64. */
  65. /*@{*/
  66. #if !defined(ASIO_NO_DYNAMIC_BUFFER_V1)
  67. /// Read data into a dynamic buffer sequence until it contains a specified
  68. /// delimiter.
  69. /**
  70. * This function is used to read data into the specified dynamic buffer
  71. * sequence until the dynamic buffer sequence's get area contains the specified
  72. * delimiter. The call will block until one of the following conditions is
  73. * true:
  74. *
  75. * @li The get area of the dynamic buffer sequence contains the specified
  76. * delimiter.
  77. *
  78. * @li An error occurred.
  79. *
  80. * This operation is implemented in terms of zero or more calls to the stream's
  81. * read_some function. If the dynamic buffer sequence's get area already
  82. * contains the delimiter, the function returns immediately.
  83. *
  84. * @param s The stream from which the data is to be read. The type must support
  85. * the SyncReadStream concept.
  86. *
  87. * @param buffers The dynamic buffer sequence into which the data will be read.
  88. *
  89. * @param delim The delimiter character.
  90. *
  91. * @returns The number of bytes in the dynamic buffer sequence's get area up to
  92. * and including the delimiter.
  93. *
  94. * @throws asio::system_error Thrown on failure.
  95. *
  96. * @note After a successful read_until operation, the dynamic buffer sequence
  97. * may contain additional data beyond the delimiter. An application will
  98. * typically leave that data in the dynamic buffer sequence for a subsequent
  99. * read_until operation to examine.
  100. *
  101. * @par Example
  102. * To read data into a @c std::string until a newline is encountered:
  103. * @code std::string data;
  104. * std::string n = asio::read_until(s,
  105. * asio::dynamic_buffer(data), '\n');
  106. * std::string line = data.substr(0, n);
  107. * data.erase(0, n); @endcode
  108. * After the @c read_until operation completes successfully, the string @c data
  109. * contains the delimiter:
  110. * @code { 'a', 'b', ..., 'c', '\n', 'd', 'e', ... } @endcode
  111. * The call to @c substr then extracts the data up to and including the
  112. * delimiter, so that the string @c line contains:
  113. * @code { 'a', 'b', ..., 'c', '\n' } @endcode
  114. * After the call to @c erase, the remaining data is left in the buffer @c b as
  115. * follows:
  116. * @code { 'd', 'e', ... } @endcode
  117. * This data may be the start of a new line, to be extracted by a subsequent
  118. * @c read_until operation.
  119. */
  120. template <typename SyncReadStream, typename DynamicBuffer_v1>
  121. std::size_t read_until(SyncReadStream& s,
  122. ASIO_MOVE_ARG(DynamicBuffer_v1) buffers, char delim,
  123. typename enable_if<
  124. is_dynamic_buffer_v1<typename decay<DynamicBuffer_v1>::type>::value
  125. && !is_dynamic_buffer_v2<typename decay<DynamicBuffer_v1>::type>::value
  126. >::type* = 0);
  127. /// Read data into a dynamic buffer sequence until it contains a specified
  128. /// delimiter.
  129. /**
  130. * This function is used to read data into the specified dynamic buffer
  131. * sequence until the dynamic buffer sequence's get area contains the specified
  132. * delimiter. The call will block until one of the following conditions is
  133. * true:
  134. *
  135. * @li The get area of the dynamic buffer sequence contains the specified
  136. * delimiter.
  137. *
  138. * @li An error occurred.
  139. *
  140. * This operation is implemented in terms of zero or more calls to the stream's
  141. * read_some function. If the dynamic buffer sequence's get area already
  142. * contains the delimiter, the function returns immediately.
  143. *
  144. * @param s The stream from which the data is to be read. The type must support
  145. * the SyncReadStream concept.
  146. *
  147. * @param buffers The dynamic buffer sequence into which the data will be read.
  148. *
  149. * @param delim The delimiter character.
  150. *
  151. * @param ec Set to indicate what error occurred, if any.
  152. *
  153. * @returns The number of bytes in the dynamic buffer sequence's get area up to
  154. * and including the delimiter. Returns 0 if an error occurred.
  155. *
  156. * @note After a successful read_until operation, the dynamic buffer sequence
  157. * may contain additional data beyond the delimiter. An application will
  158. * typically leave that data in the dynamic buffer sequence for a subsequent
  159. * read_until operation to examine.
  160. */
  161. template <typename SyncReadStream, typename DynamicBuffer_v1>
  162. std::size_t read_until(SyncReadStream& s,
  163. ASIO_MOVE_ARG(DynamicBuffer_v1) buffers,
  164. char delim, asio::error_code& ec,
  165. typename enable_if<
  166. is_dynamic_buffer_v1<typename decay<DynamicBuffer_v1>::type>::value
  167. && !is_dynamic_buffer_v2<typename decay<DynamicBuffer_v1>::type>::value
  168. >::type* = 0);
  169. /// Read data into a dynamic buffer sequence until it contains a specified
  170. /// delimiter.
  171. /**
  172. * This function is used to read data into the specified dynamic buffer
  173. * sequence until the dynamic buffer sequence's get area contains the specified
  174. * delimiter. The call will block until one of the following conditions is
  175. * true:
  176. *
  177. * @li The get area of the dynamic buffer sequence contains the specified
  178. * delimiter.
  179. *
  180. * @li An error occurred.
  181. *
  182. * This operation is implemented in terms of zero or more calls to the stream's
  183. * read_some function. If the dynamic buffer sequence's get area already
  184. * contains the delimiter, the function returns immediately.
  185. *
  186. * @param s The stream from which the data is to be read. The type must support
  187. * the SyncReadStream concept.
  188. *
  189. * @param buffers The dynamic buffer sequence into which the data will be read.
  190. *
  191. * @param delim The delimiter string.
  192. *
  193. * @returns The number of bytes in the dynamic buffer sequence's get area up to
  194. * and including the delimiter.
  195. *
  196. * @note After a successful read_until operation, the dynamic buffer sequence
  197. * may contain additional data beyond the delimiter. An application will
  198. * typically leave that data in the dynamic buffer sequence for a subsequent
  199. * read_until operation to examine.
  200. *
  201. * @par Example
  202. * To read data into a @c std::string until a CR-LF sequence is encountered:
  203. * @code std::string data;
  204. * std::string n = asio::read_until(s,
  205. * asio::dynamic_buffer(data), "\r\n");
  206. * std::string line = data.substr(0, n);
  207. * data.erase(0, n); @endcode
  208. * After the @c read_until operation completes successfully, the string @c data
  209. * contains the delimiter:
  210. * @code { 'a', 'b', ..., 'c', '\r', '\n', 'd', 'e', ... } @endcode
  211. * The call to @c substr then extracts the data up to and including the
  212. * delimiter, so that the string @c line contains:
  213. * @code { 'a', 'b', ..., 'c', '\r', '\n' } @endcode
  214. * After the call to @c erase, the remaining data is left in the buffer @c b as
  215. * follows:
  216. * @code { 'd', 'e', ... } @endcode
  217. * This data may be the start of a new line, to be extracted by a subsequent
  218. * @c read_until operation.
  219. */
  220. template <typename SyncReadStream, typename DynamicBuffer_v1>
  221. std::size_t read_until(SyncReadStream& s,
  222. ASIO_MOVE_ARG(DynamicBuffer_v1) buffers,
  223. ASIO_STRING_VIEW_PARAM delim,
  224. typename enable_if<
  225. is_dynamic_buffer_v1<typename decay<DynamicBuffer_v1>::type>::value
  226. && !is_dynamic_buffer_v2<typename decay<DynamicBuffer_v1>::type>::value
  227. >::type* = 0);
  228. /// Read data into a dynamic buffer sequence until it contains a specified
  229. /// delimiter.
  230. /**
  231. * This function is used to read data into the specified dynamic buffer
  232. * sequence until the dynamic buffer sequence's get area contains the specified
  233. * delimiter. The call will block until one of the following conditions is
  234. * true:
  235. *
  236. * @li The get area of the dynamic buffer sequence contains the specified
  237. * delimiter.
  238. *
  239. * @li An error occurred.
  240. *
  241. * This operation is implemented in terms of zero or more calls to the stream's
  242. * read_some function. If the dynamic buffer sequence's get area already
  243. * contains the delimiter, the function returns immediately.
  244. *
  245. * @param s The stream from which the data is to be read. The type must support
  246. * the SyncReadStream concept.
  247. *
  248. * @param buffers The dynamic buffer sequence into which the data will be read.
  249. *
  250. * @param delim The delimiter string.
  251. *
  252. * @param ec Set to indicate what error occurred, if any.
  253. *
  254. * @returns The number of bytes in the dynamic buffer sequence's get area up to
  255. * and including the delimiter. Returns 0 if an error occurred.
  256. *
  257. * @note After a successful read_until operation, the dynamic buffer sequence
  258. * may contain additional data beyond the delimiter. An application will
  259. * typically leave that data in the dynamic buffer sequence for a subsequent
  260. * read_until operation to examine.
  261. */
  262. template <typename SyncReadStream, typename DynamicBuffer_v1>
  263. std::size_t read_until(SyncReadStream& s,
  264. ASIO_MOVE_ARG(DynamicBuffer_v1) buffers,
  265. ASIO_STRING_VIEW_PARAM delim,
  266. asio::error_code& ec,
  267. typename enable_if<
  268. is_dynamic_buffer_v1<typename decay<DynamicBuffer_v1>::type>::value
  269. && !is_dynamic_buffer_v2<typename decay<DynamicBuffer_v1>::type>::value
  270. >::type* = 0);
  271. #if !defined(ASIO_NO_EXTENSIONS)
  272. #if defined(ASIO_HAS_BOOST_REGEX) \
  273. || defined(GENERATING_DOCUMENTATION)
  274. /// Read data into a dynamic buffer sequence until some part of the data it
  275. /// contains matches a regular expression.
  276. /**
  277. * This function is used to read data into the specified dynamic buffer
  278. * sequence until the dynamic buffer sequence's get area contains some data
  279. * that matches a regular expression. The call will block until one of the
  280. * following conditions is true:
  281. *
  282. * @li A substring of the dynamic buffer sequence's get area matches the
  283. * regular expression.
  284. *
  285. * @li An error occurred.
  286. *
  287. * This operation is implemented in terms of zero or more calls to the stream's
  288. * read_some function. If the dynamic buffer sequence's get area already
  289. * contains data that matches the regular expression, the function returns
  290. * immediately.
  291. *
  292. * @param s The stream from which the data is to be read. The type must support
  293. * the SyncReadStream concept.
  294. *
  295. * @param buffers A dynamic buffer sequence into which the data will be read.
  296. *
  297. * @param expr The regular expression.
  298. *
  299. * @returns The number of bytes in the dynamic buffer sequence's get area up to
  300. * and including the substring that matches the regular expression.
  301. *
  302. * @throws asio::system_error Thrown on failure.
  303. *
  304. * @note After a successful read_until operation, the dynamic buffer sequence
  305. * may contain additional data beyond that which matched the regular
  306. * expression. An application will typically leave that data in the dynamic
  307. * buffer sequence for a subsequent read_until operation to examine.
  308. *
  309. * @par Example
  310. * To read data into a @c std::string until a CR-LF sequence is encountered:
  311. * @code std::string data;
  312. * std::string n = asio::read_until(s,
  313. * asio::dynamic_buffer(data), boost::regex("\r\n"));
  314. * std::string line = data.substr(0, n);
  315. * data.erase(0, n); @endcode
  316. * After the @c read_until operation completes successfully, the string @c data
  317. * contains the delimiter:
  318. * @code { 'a', 'b', ..., 'c', '\r', '\n', 'd', 'e', ... } @endcode
  319. * The call to @c substr then extracts the data up to and including the
  320. * delimiter, so that the string @c line contains:
  321. * @code { 'a', 'b', ..., 'c', '\r', '\n' } @endcode
  322. * After the call to @c erase, the remaining data is left in the buffer @c b as
  323. * follows:
  324. * @code { 'd', 'e', ... } @endcode
  325. * This data may be the start of a new line, to be extracted by a subsequent
  326. * @c read_until operation.
  327. */
  328. template <typename SyncReadStream, typename DynamicBuffer_v1>
  329. std::size_t read_until(SyncReadStream& s,
  330. ASIO_MOVE_ARG(DynamicBuffer_v1) buffers,
  331. const boost::regex& expr,
  332. typename enable_if<
  333. is_dynamic_buffer_v1<typename decay<DynamicBuffer_v1>::type>::value
  334. && !is_dynamic_buffer_v2<typename decay<DynamicBuffer_v1>::type>::value
  335. >::type* = 0);
  336. /// Read data into a dynamic buffer sequence until some part of the data it
  337. /// contains matches a regular expression.
  338. /**
  339. * This function is used to read data into the specified dynamic buffer
  340. * sequence until the dynamic buffer sequence's get area contains some data
  341. * that matches a regular expression. The call will block until one of the
  342. * following conditions is true:
  343. *
  344. * @li A substring of the dynamic buffer sequence's get area matches the
  345. * regular expression.
  346. *
  347. * @li An error occurred.
  348. *
  349. * This operation is implemented in terms of zero or more calls to the stream's
  350. * read_some function. If the dynamic buffer sequence's get area already
  351. * contains data that matches the regular expression, the function returns
  352. * immediately.
  353. *
  354. * @param s The stream from which the data is to be read. The type must support
  355. * the SyncReadStream concept.
  356. *
  357. * @param buffers A dynamic buffer sequence into which the data will be read.
  358. *
  359. * @param expr The regular expression.
  360. *
  361. * @param ec Set to indicate what error occurred, if any.
  362. *
  363. * @returns The number of bytes in the dynamic buffer sequence's get area up to
  364. * and including the substring that matches the regular expression. Returns 0
  365. * if an error occurred.
  366. *
  367. * @note After a successful read_until operation, the dynamic buffer sequence
  368. * may contain additional data beyond that which matched the regular
  369. * expression. An application will typically leave that data in the dynamic
  370. * buffer sequence for a subsequent read_until operation to examine.
  371. */
  372. template <typename SyncReadStream, typename DynamicBuffer_v1>
  373. std::size_t read_until(SyncReadStream& s,
  374. ASIO_MOVE_ARG(DynamicBuffer_v1) buffers,
  375. const boost::regex& expr, asio::error_code& ec,
  376. typename enable_if<
  377. is_dynamic_buffer_v1<typename decay<DynamicBuffer_v1>::type>::value
  378. && !is_dynamic_buffer_v2<typename decay<DynamicBuffer_v1>::type>::value
  379. >::type* = 0);
  380. #endif // defined(ASIO_HAS_BOOST_REGEX)
  381. // || defined(GENERATING_DOCUMENTATION)
  382. /// Read data into a dynamic buffer sequence until a function object indicates a
  383. /// match.
  384. /**
  385. * This function is used to read data into the specified dynamic buffer
  386. * sequence until a user-defined match condition function object, when applied
  387. * to the data contained in the dynamic buffer sequence, indicates a successful
  388. * match. The call will block until one of the following conditions is true:
  389. *
  390. * @li The match condition function object returns a std::pair where the second
  391. * element evaluates to true.
  392. *
  393. * @li An error occurred.
  394. *
  395. * This operation is implemented in terms of zero or more calls to the stream's
  396. * read_some function. If the match condition function object already indicates
  397. * a match, the function returns immediately.
  398. *
  399. * @param s The stream from which the data is to be read. The type must support
  400. * the SyncReadStream concept.
  401. *
  402. * @param buffers A dynamic buffer sequence into which the data will be read.
  403. *
  404. * @param match_condition The function object to be called to determine whether
  405. * a match exists. The signature of the function object must be:
  406. * @code pair<iterator, bool> match_condition(iterator begin, iterator end);
  407. * @endcode
  408. * where @c iterator represents the type:
  409. * @code buffers_iterator<typename DynamicBuffer_v1::const_buffers_type>
  410. * @endcode
  411. * The iterator parameters @c begin and @c end define the range of bytes to be
  412. * scanned to determine whether there is a match. The @c first member of the
  413. * return value is an iterator marking one-past-the-end of the bytes that have
  414. * been consumed by the match function. This iterator is used to calculate the
  415. * @c begin parameter for any subsequent invocation of the match condition. The
  416. * @c second member of the return value is true if a match has been found, false
  417. * otherwise.
  418. *
  419. * @returns The number of bytes in the dynamic_buffer's get area that
  420. * have been fully consumed by the match function.
  421. *
  422. * @throws asio::system_error Thrown on failure.
  423. *
  424. * @note After a successful read_until operation, the dynamic buffer sequence
  425. * may contain additional data beyond that which matched the function object.
  426. * An application will typically leave that data in the dynamic buffer sequence
  427. * for a subsequent read_until operation to examine.
  428. * @note The default implementation of the @c is_match_condition type trait
  429. * evaluates to true for function pointers and function objects with a
  430. * @c result_type typedef. It must be specialised for other user-defined
  431. * function objects.
  432. *
  433. * @par Examples
  434. * To read data into a dynamic buffer sequence until whitespace is encountered:
  435. * @code typedef asio::buffers_iterator<
  436. * asio::const_buffers_1> iterator;
  437. *
  438. * std::pair<iterator, bool>
  439. * match_whitespace(iterator begin, iterator end)
  440. * {
  441. * iterator i = begin;
  442. * while (i != end)
  443. * if (std::isspace(*i++))
  444. * return std::make_pair(i, true);
  445. * return std::make_pair(i, false);
  446. * }
  447. * ...
  448. * std::string data;
  449. * asio::read_until(s, data, match_whitespace);
  450. * @endcode
  451. *
  452. * To read data into a @c std::string until a matching character is found:
  453. * @code class match_char
  454. * {
  455. * public:
  456. * explicit match_char(char c) : c_(c) {}
  457. *
  458. * template <typename Iterator>
  459. * std::pair<Iterator, bool> operator()(
  460. * Iterator begin, Iterator end) const
  461. * {
  462. * Iterator i = begin;
  463. * while (i != end)
  464. * if (c_ == *i++)
  465. * return std::make_pair(i, true);
  466. * return std::make_pair(i, false);
  467. * }
  468. *
  469. * private:
  470. * char c_;
  471. * };
  472. *
  473. * namespace asio {
  474. * template <> struct is_match_condition<match_char>
  475. * : public boost::true_type {};
  476. * } // namespace asio
  477. * ...
  478. * std::string data;
  479. * asio::read_until(s, data, match_char('a'));
  480. * @endcode
  481. */
  482. template <typename SyncReadStream,
  483. typename DynamicBuffer_v1, typename MatchCondition>
  484. std::size_t read_until(SyncReadStream& s,
  485. ASIO_MOVE_ARG(DynamicBuffer_v1) buffers,
  486. MatchCondition match_condition,
  487. typename enable_if<
  488. is_match_condition<MatchCondition>::value
  489. && is_dynamic_buffer_v1<typename decay<DynamicBuffer_v1>::type>::value
  490. && !is_dynamic_buffer_v2<typename decay<DynamicBuffer_v1>::type>::value
  491. >::type* = 0);
  492. /// Read data into a dynamic buffer sequence until a function object indicates a
  493. /// match.
  494. /**
  495. * This function is used to read data into the specified dynamic buffer
  496. * sequence until a user-defined match condition function object, when applied
  497. * to the data contained in the dynamic buffer sequence, indicates a successful
  498. * match. The call will block until one of the following conditions is true:
  499. *
  500. * @li The match condition function object returns a std::pair where the second
  501. * element evaluates to true.
  502. *
  503. * @li An error occurred.
  504. *
  505. * This operation is implemented in terms of zero or more calls to the stream's
  506. * read_some function. If the match condition function object already indicates
  507. * a match, the function returns immediately.
  508. *
  509. * @param s The stream from which the data is to be read. The type must support
  510. * the SyncReadStream concept.
  511. *
  512. * @param buffers A dynamic buffer sequence into which the data will be read.
  513. *
  514. * @param match_condition The function object to be called to determine whether
  515. * a match exists. The signature of the function object must be:
  516. * @code pair<iterator, bool> match_condition(iterator begin, iterator end);
  517. * @endcode
  518. * where @c iterator represents the type:
  519. * @code buffers_iterator<DynamicBuffer_v1::const_buffers_type>
  520. * @endcode
  521. * The iterator parameters @c begin and @c end define the range of bytes to be
  522. * scanned to determine whether there is a match. The @c first member of the
  523. * return value is an iterator marking one-past-the-end of the bytes that have
  524. * been consumed by the match function. This iterator is used to calculate the
  525. * @c begin parameter for any subsequent invocation of the match condition. The
  526. * @c second member of the return value is true if a match has been found, false
  527. * otherwise.
  528. *
  529. * @param ec Set to indicate what error occurred, if any.
  530. *
  531. * @returns The number of bytes in the dynamic buffer sequence's get area that
  532. * have been fully consumed by the match function. Returns 0 if an error
  533. * occurred.
  534. *
  535. * @note After a successful read_until operation, the dynamic buffer sequence
  536. * may contain additional data beyond that which matched the function object.
  537. * An application will typically leave that data in the dynamic buffer sequence
  538. * for a subsequent read_until operation to examine.
  539. *
  540. * @note The default implementation of the @c is_match_condition type trait
  541. * evaluates to true for function pointers and function objects with a
  542. * @c result_type typedef. It must be specialised for other user-defined
  543. * function objects.
  544. */
  545. template <typename SyncReadStream,
  546. typename DynamicBuffer_v1, typename MatchCondition>
  547. std::size_t read_until(SyncReadStream& s,
  548. ASIO_MOVE_ARG(DynamicBuffer_v1) buffers,
  549. MatchCondition match_condition, asio::error_code& ec,
  550. typename enable_if<
  551. is_match_condition<MatchCondition>::value
  552. && is_dynamic_buffer_v1<typename decay<DynamicBuffer_v1>::type>::value
  553. && !is_dynamic_buffer_v2<typename decay<DynamicBuffer_v1>::type>::value
  554. >::type* = 0);
  555. #if !defined(ASIO_NO_IOSTREAM)
  556. /// Read data into a streambuf until it contains a specified delimiter.
  557. /**
  558. * This function is used to read data into the specified streambuf until the
  559. * streambuf's get area contains the specified delimiter. The call will block
  560. * until one of the following conditions is true:
  561. *
  562. * @li The get area of the streambuf contains the specified delimiter.
  563. *
  564. * @li An error occurred.
  565. *
  566. * This operation is implemented in terms of zero or more calls to the stream's
  567. * read_some function. If the streambuf's get area already contains the
  568. * delimiter, the function returns immediately.
  569. *
  570. * @param s The stream from which the data is to be read. The type must support
  571. * the SyncReadStream concept.
  572. *
  573. * @param b A streambuf object into which the data will be read.
  574. *
  575. * @param delim The delimiter character.
  576. *
  577. * @returns The number of bytes in the streambuf's get area up to and including
  578. * the delimiter.
  579. *
  580. * @throws asio::system_error Thrown on failure.
  581. *
  582. * @note After a successful read_until operation, the streambuf may contain
  583. * additional data beyond the delimiter. An application will typically leave
  584. * that data in the streambuf for a subsequent read_until operation to examine.
  585. *
  586. * @par Example
  587. * To read data into a streambuf until a newline is encountered:
  588. * @code asio::streambuf b;
  589. * asio::read_until(s, b, '\n');
  590. * std::istream is(&b);
  591. * std::string line;
  592. * std::getline(is, line); @endcode
  593. * After the @c read_until operation completes successfully, the buffer @c b
  594. * contains the delimiter:
  595. * @code { 'a', 'b', ..., 'c', '\n', 'd', 'e', ... } @endcode
  596. * The call to @c std::getline then extracts the data up to and including the
  597. * newline (which is discarded), so that the string @c line contains:
  598. * @code { 'a', 'b', ..., 'c' } @endcode
  599. * The remaining data is left in the buffer @c b as follows:
  600. * @code { 'd', 'e', ... } @endcode
  601. * This data may be the start of a new line, to be extracted by a subsequent
  602. * @c read_until operation.
  603. */
  604. template <typename SyncReadStream, typename Allocator>
  605. std::size_t read_until(SyncReadStream& s,
  606. asio::basic_streambuf<Allocator>& b, char delim);
  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 character.
  627. *
  628. * @param ec Set to indicate what error occurred, if any.
  629. *
  630. * @returns The number of bytes in the streambuf's get area up to and including
  631. * the delimiter. Returns 0 if an error occurred.
  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. template <typename SyncReadStream, typename Allocator>
  638. std::size_t read_until(SyncReadStream& s,
  639. asio::basic_streambuf<Allocator>& b, char delim,
  640. asio::error_code& ec);
  641. /// Read data into a streambuf until it contains a specified delimiter.
  642. /**
  643. * This function is used to read data into the specified streambuf until the
  644. * streambuf's get area contains the specified delimiter. The call will block
  645. * until one of the following conditions is true:
  646. *
  647. * @li The get area of the streambuf contains the specified delimiter.
  648. *
  649. * @li An error occurred.
  650. *
  651. * This operation is implemented in terms of zero or more calls to the stream's
  652. * read_some function. If the streambuf's get area already contains the
  653. * delimiter, the function returns immediately.
  654. *
  655. * @param s The stream from which the data is to be read. The type must support
  656. * the SyncReadStream concept.
  657. *
  658. * @param b A streambuf object into which the data will be read.
  659. *
  660. * @param delim The delimiter string.
  661. *
  662. * @returns The number of bytes in the streambuf's get area up to and including
  663. * the delimiter.
  664. *
  665. * @throws asio::system_error Thrown on failure.
  666. *
  667. * @note After a successful read_until operation, the streambuf may contain
  668. * additional data beyond the delimiter. An application will typically leave
  669. * that data in the streambuf for a subsequent read_until operation to examine.
  670. *
  671. * @par Example
  672. * To read data into a streambuf until a newline is encountered:
  673. * @code asio::streambuf b;
  674. * asio::read_until(s, b, "\r\n");
  675. * std::istream is(&b);
  676. * std::string line;
  677. * std::getline(is, line); @endcode
  678. * After the @c read_until operation completes successfully, the buffer @c b
  679. * contains the delimiter:
  680. * @code { 'a', 'b', ..., 'c', '\r', '\n', 'd', 'e', ... } @endcode
  681. * The call to @c std::getline then extracts the data up to and including the
  682. * newline (which is discarded), so that the string @c line contains:
  683. * @code { 'a', 'b', ..., 'c', '\r' } @endcode
  684. * The remaining data is left in the buffer @c b as follows:
  685. * @code { 'd', 'e', ... } @endcode
  686. * This data may be the start of a new line, to be extracted by a subsequent
  687. * @c read_until operation.
  688. */
  689. template <typename SyncReadStream, typename Allocator>
  690. std::size_t read_until(SyncReadStream& s,
  691. asio::basic_streambuf<Allocator>& b,
  692. ASIO_STRING_VIEW_PARAM delim);
  693. /// Read data into a streambuf until it contains a specified delimiter.
  694. /**
  695. * This function is used to read data into the specified streambuf until the
  696. * streambuf's get area contains the specified delimiter. The call will block
  697. * until one of the following conditions is true:
  698. *
  699. * @li The get area of the streambuf contains the specified delimiter.
  700. *
  701. * @li An error occurred.
  702. *
  703. * This operation is implemented in terms of zero or more calls to the stream's
  704. * read_some function. If the streambuf's get area already contains the
  705. * delimiter, the function returns immediately.
  706. *
  707. * @param s The stream from which the data is to be read. The type must support
  708. * the SyncReadStream concept.
  709. *
  710. * @param b A streambuf object into which the data will be read.
  711. *
  712. * @param delim The delimiter string.
  713. *
  714. * @param ec Set to indicate what error occurred, if any.
  715. *
  716. * @returns The number of bytes in the streambuf's get area up to and including
  717. * the delimiter. Returns 0 if an error occurred.
  718. *
  719. * @note After a successful read_until operation, the streambuf may contain
  720. * additional data beyond the delimiter. An application will typically leave
  721. * that data in the streambuf for a subsequent read_until operation to examine.
  722. */
  723. template <typename SyncReadStream, typename Allocator>
  724. std::size_t read_until(SyncReadStream& s,
  725. asio::basic_streambuf<Allocator>& b,
  726. ASIO_STRING_VIEW_PARAM delim, asio::error_code& ec);
  727. #if defined(ASIO_HAS_BOOST_REGEX) \
  728. || defined(GENERATING_DOCUMENTATION)
  729. /// Read data into a streambuf until some part of the data it contains matches
  730. /// a regular expression.
  731. /**
  732. * This function is used to read data into the specified streambuf until the
  733. * streambuf's get area contains some data that matches a regular expression.
  734. * The call will block until one of the following conditions is true:
  735. *
  736. * @li A substring of the streambuf's get area matches the regular expression.
  737. *
  738. * @li An error occurred.
  739. *
  740. * This operation is implemented in terms of zero or more calls to the stream's
  741. * read_some function. If the streambuf's get area already contains data that
  742. * matches the regular expression, the function returns immediately.
  743. *
  744. * @param s The stream from which the data is to be read. The type must support
  745. * the SyncReadStream concept.
  746. *
  747. * @param b A streambuf object into which the data will be read.
  748. *
  749. * @param expr The regular expression.
  750. *
  751. * @returns The number of bytes in the streambuf's get area up to and including
  752. * the substring that matches the regular expression.
  753. *
  754. * @throws asio::system_error Thrown on failure.
  755. *
  756. * @note After a successful read_until operation, the streambuf may contain
  757. * additional data beyond that which matched the regular expression. An
  758. * application will typically leave that data in the streambuf for a subsequent
  759. * read_until operation to examine.
  760. *
  761. * @par Example
  762. * To read data into a streambuf until a CR-LF sequence is encountered:
  763. * @code asio::streambuf b;
  764. * asio::read_until(s, b, boost::regex("\r\n"));
  765. * std::istream is(&b);
  766. * std::string line;
  767. * std::getline(is, line); @endcode
  768. * After the @c read_until operation completes successfully, the buffer @c b
  769. * contains the data which matched the regular expression:
  770. * @code { 'a', 'b', ..., 'c', '\r', '\n', 'd', 'e', ... } @endcode
  771. * The call to @c std::getline then extracts the data up to and including the
  772. * newline (which is discarded), so that the string @c line contains:
  773. * @code { 'a', 'b', ..., 'c', '\r' } @endcode
  774. * The remaining data is left in the buffer @c b as follows:
  775. * @code { 'd', 'e', ... } @endcode
  776. * This data may be the start of a new line, to be extracted by a subsequent
  777. * @c read_until operation.
  778. */
  779. template <typename SyncReadStream, typename Allocator>
  780. std::size_t read_until(SyncReadStream& s,
  781. asio::basic_streambuf<Allocator>& b, const boost::regex& expr);
  782. /// Read data into a streambuf until some part of the data it contains matches
  783. /// a regular expression.
  784. /**
  785. * This function is used to read data into the specified streambuf until the
  786. * streambuf's get area contains some data that matches a regular expression.
  787. * The call will block until one of the following conditions is true:
  788. *
  789. * @li A substring of the streambuf's get area matches the regular expression.
  790. *
  791. * @li An error occurred.
  792. *
  793. * This operation is implemented in terms of zero or more calls to the stream's
  794. * read_some function. If the streambuf's get area already contains data that
  795. * matches the regular expression, the function returns immediately.
  796. *
  797. * @param s The stream from which the data is to be read. The type must support
  798. * the SyncReadStream concept.
  799. *
  800. * @param b A streambuf object into which the data will be read.
  801. *
  802. * @param expr The regular expression.
  803. *
  804. * @param ec Set to indicate what error occurred, if any.
  805. *
  806. * @returns The number of bytes in the streambuf's get area up to and including
  807. * the substring that matches the regular expression. Returns 0 if an error
  808. * occurred.
  809. *
  810. * @note After a successful read_until operation, the streambuf may contain
  811. * additional data beyond that which matched the regular expression. An
  812. * application will typically leave that data in the streambuf for a subsequent
  813. * read_until operation to examine.
  814. */
  815. template <typename SyncReadStream, typename Allocator>
  816. std::size_t read_until(SyncReadStream& s,
  817. asio::basic_streambuf<Allocator>& b, const boost::regex& expr,
  818. asio::error_code& ec);
  819. #endif // defined(ASIO_HAS_BOOST_REGEX)
  820. // || defined(GENERATING_DOCUMENTATION)
  821. /// Read data into a streambuf until a function object indicates a match.
  822. /**
  823. * This function is used to read data into the specified streambuf until a
  824. * user-defined match condition function object, when applied to the data
  825. * contained in the streambuf, indicates a successful match. The call will
  826. * block until one of the following conditions is true:
  827. *
  828. * @li The match condition function object returns a std::pair where the second
  829. * element evaluates to true.
  830. *
  831. * @li An error occurred.
  832. *
  833. * This operation is implemented in terms of zero or more calls to the stream's
  834. * read_some function. If the match condition function object already indicates
  835. * a match, the function returns immediately.
  836. *
  837. * @param s The stream from which the data is to be read. The type must support
  838. * the SyncReadStream concept.
  839. *
  840. * @param b A streambuf object into which the data will be read.
  841. *
  842. * @param match_condition The function object to be called to determine whether
  843. * a match exists. The signature of the function object must be:
  844. * @code pair<iterator, bool> match_condition(iterator begin, iterator end);
  845. * @endcode
  846. * where @c iterator represents the type:
  847. * @code buffers_iterator<basic_streambuf<Allocator>::const_buffers_type>
  848. * @endcode
  849. * The iterator parameters @c begin and @c end define the range of bytes to be
  850. * scanned to determine whether there is a match. The @c first member of the
  851. * return value is an iterator marking one-past-the-end of the bytes that have
  852. * been consumed by the match function. This iterator is used to calculate the
  853. * @c begin parameter for any subsequent invocation of the match condition. The
  854. * @c second member of the return value is true if a match has been found, false
  855. * otherwise.
  856. *
  857. * @returns The number of bytes in the streambuf's get area that have been fully
  858. * consumed by the match function.
  859. *
  860. * @throws asio::system_error Thrown on failure.
  861. *
  862. * @note After a successful read_until operation, the streambuf may contain
  863. * additional data beyond that which matched the function object. An application
  864. * will typically leave that data in the streambuf for a subsequent read_until
  865. * operation to examine.
  866. *
  867. * @note The default implementation of the @c is_match_condition type trait
  868. * evaluates to true for function pointers and function objects with a
  869. * @c result_type typedef. It must be specialised for other user-defined
  870. * function objects.
  871. *
  872. * @par Examples
  873. * To read data into a streambuf until whitespace is encountered:
  874. * @code typedef asio::buffers_iterator<
  875. * asio::streambuf::const_buffers_type> iterator;
  876. *
  877. * std::pair<iterator, bool>
  878. * match_whitespace(iterator begin, iterator end)
  879. * {
  880. * iterator i = begin;
  881. * while (i != end)
  882. * if (std::isspace(*i++))
  883. * return std::make_pair(i, true);
  884. * return std::make_pair(i, false);
  885. * }
  886. * ...
  887. * asio::streambuf b;
  888. * asio::read_until(s, b, match_whitespace);
  889. * @endcode
  890. *
  891. * To read data into a streambuf until a matching character is found:
  892. * @code class match_char
  893. * {
  894. * public:
  895. * explicit match_char(char c) : c_(c) {}
  896. *
  897. * template <typename Iterator>
  898. * std::pair<Iterator, bool> operator()(
  899. * Iterator begin, Iterator end) const
  900. * {
  901. * Iterator i = begin;
  902. * while (i != end)
  903. * if (c_ == *i++)
  904. * return std::make_pair(i, true);
  905. * return std::make_pair(i, false);
  906. * }
  907. *
  908. * private:
  909. * char c_;
  910. * };
  911. *
  912. * namespace asio {
  913. * template <> struct is_match_condition<match_char>
  914. * : public boost::true_type {};
  915. * } // namespace asio
  916. * ...
  917. * asio::streambuf b;
  918. * asio::read_until(s, b, match_char('a'));
  919. * @endcode
  920. */
  921. template <typename SyncReadStream, typename Allocator, typename MatchCondition>
  922. std::size_t read_until(SyncReadStream& s,
  923. asio::basic_streambuf<Allocator>& b, MatchCondition match_condition,
  924. typename enable_if<is_match_condition<MatchCondition>::value>::type* = 0);
  925. /// Read data into a streambuf until a function object indicates a match.
  926. /**
  927. * This function is used to read data into the specified streambuf until a
  928. * user-defined match condition function object, when applied to the data
  929. * contained in the streambuf, indicates a successful match. The call will
  930. * block until one of the following conditions is true:
  931. *
  932. * @li The match condition function object returns a std::pair where the second
  933. * element evaluates to true.
  934. *
  935. * @li An error occurred.
  936. *
  937. * This operation is implemented in terms of zero or more calls to the stream's
  938. * read_some function. If the match condition function object already indicates
  939. * a match, the function returns immediately.
  940. *
  941. * @param s The stream from which the data is to be read. The type must support
  942. * the SyncReadStream concept.
  943. *
  944. * @param b A streambuf object into which the data will be read.
  945. *
  946. * @param match_condition The function object to be called to determine whether
  947. * a match exists. The signature of the function object must be:
  948. * @code pair<iterator, bool> match_condition(iterator begin, iterator end);
  949. * @endcode
  950. * where @c iterator represents the type:
  951. * @code buffers_iterator<basic_streambuf<Allocator>::const_buffers_type>
  952. * @endcode
  953. * The iterator parameters @c begin and @c end define the range of bytes to be
  954. * scanned to determine whether there is a match. The @c first member of the
  955. * return value is an iterator marking one-past-the-end of the bytes that have
  956. * been consumed by the match function. This iterator is used to calculate the
  957. * @c begin parameter for any subsequent invocation of the match condition. The
  958. * @c second member of the return value is true if a match has been found, false
  959. * otherwise.
  960. *
  961. * @param ec Set to indicate what error occurred, if any.
  962. *
  963. * @returns The number of bytes in the streambuf's get area that have been fully
  964. * consumed by the match function. Returns 0 if an error occurred.
  965. *
  966. * @note After a successful read_until operation, the streambuf may contain
  967. * additional data beyond that which matched the function object. An application
  968. * will typically leave that data in the streambuf for a subsequent read_until
  969. * operation to examine.
  970. *
  971. * @note The default implementation of the @c is_match_condition type trait
  972. * evaluates to true for function pointers and function objects with a
  973. * @c result_type typedef. It must be specialised for other user-defined
  974. * function objects.
  975. */
  976. template <typename SyncReadStream, typename Allocator, typename MatchCondition>
  977. std::size_t read_until(SyncReadStream& s,
  978. asio::basic_streambuf<Allocator>& b,
  979. MatchCondition match_condition, asio::error_code& ec,
  980. typename enable_if<is_match_condition<MatchCondition>::value>::type* = 0);
  981. #endif // !defined(ASIO_NO_IOSTREAM)
  982. #endif // !defined(ASIO_NO_EXTENSIONS)
  983. #endif // !defined(ASIO_NO_DYNAMIC_BUFFER_V1)
  984. /// Read data into a dynamic buffer sequence until it contains a specified
  985. /// delimiter.
  986. /**
  987. * This function is used to read data into the specified dynamic buffer
  988. * sequence until the dynamic buffer sequence's get area contains the specified
  989. * delimiter. The call will block until one of the following conditions is
  990. * true:
  991. *
  992. * @li The get area of the dynamic buffer sequence contains the specified
  993. * delimiter.
  994. *
  995. * @li An error occurred.
  996. *
  997. * This operation is implemented in terms of zero or more calls to the stream's
  998. * read_some function. If the dynamic buffer sequence's get area already
  999. * contains the delimiter, the function returns immediately.
  1000. *
  1001. * @param s The stream from which the data is to be read. The type must support
  1002. * the SyncReadStream concept.
  1003. *
  1004. * @param buffers The dynamic buffer sequence into which the data will be read.
  1005. *
  1006. * @param delim The delimiter character.
  1007. *
  1008. * @returns The number of bytes in the dynamic buffer sequence's get area up to
  1009. * and including the delimiter.
  1010. *
  1011. * @throws asio::system_error Thrown on failure.
  1012. *
  1013. * @note After a successful read_until operation, the dynamic buffer sequence
  1014. * may contain additional data beyond the delimiter. An application will
  1015. * typically leave that data in the dynamic buffer sequence for a subsequent
  1016. * read_until operation to examine.
  1017. *
  1018. * @par Example
  1019. * To read data into a @c std::string until a newline is encountered:
  1020. * @code std::string data;
  1021. * std::string n = asio::read_until(s,
  1022. * asio::dynamic_buffer(data), '\n');
  1023. * std::string line = data.substr(0, n);
  1024. * data.erase(0, n); @endcode
  1025. * After the @c read_until operation completes successfully, the string @c data
  1026. * contains the delimiter:
  1027. * @code { 'a', 'b', ..., 'c', '\n', 'd', 'e', ... } @endcode
  1028. * The call to @c substr then extracts the data up to and including the
  1029. * delimiter, so that the string @c line contains:
  1030. * @code { 'a', 'b', ..., 'c', '\n' } @endcode
  1031. * After the call to @c erase, the remaining data is left in the buffer @c b as
  1032. * follows:
  1033. * @code { 'd', 'e', ... } @endcode
  1034. * This data may be the start of a new line, to be extracted by a subsequent
  1035. * @c read_until operation.
  1036. */
  1037. template <typename SyncReadStream, typename DynamicBuffer_v2>
  1038. std::size_t read_until(SyncReadStream& s, DynamicBuffer_v2 buffers, char delim,
  1039. typename enable_if<
  1040. is_dynamic_buffer_v2<DynamicBuffer_v2>::value
  1041. >::type* = 0);
  1042. /// Read data into a dynamic buffer sequence until it contains a specified
  1043. /// delimiter.
  1044. /**
  1045. * This function is used to read data into the specified dynamic buffer
  1046. * sequence until the dynamic buffer sequence's get area contains the specified
  1047. * delimiter. The call will block until one of the following conditions is
  1048. * true:
  1049. *
  1050. * @li The get area of the dynamic buffer sequence contains the specified
  1051. * delimiter.
  1052. *
  1053. * @li An error occurred.
  1054. *
  1055. * This operation is implemented in terms of zero or more calls to the stream's
  1056. * read_some function. If the dynamic buffer sequence's get area already
  1057. * contains the delimiter, the function returns immediately.
  1058. *
  1059. * @param s The stream from which the data is to be read. The type must support
  1060. * the SyncReadStream concept.
  1061. *
  1062. * @param buffers The dynamic buffer sequence into which the data will be read.
  1063. *
  1064. * @param delim The delimiter character.
  1065. *
  1066. * @param ec Set to indicate what error occurred, if any.
  1067. *
  1068. * @returns The number of bytes in the dynamic buffer sequence's get area up to
  1069. * and including the delimiter. Returns 0 if an error occurred.
  1070. *
  1071. * @note After a successful read_until operation, the dynamic buffer sequence
  1072. * may contain additional data beyond the delimiter. An application will
  1073. * typically leave that data in the dynamic buffer sequence for a subsequent
  1074. * read_until operation to examine.
  1075. */
  1076. template <typename SyncReadStream, typename DynamicBuffer_v2>
  1077. std::size_t read_until(SyncReadStream& s, DynamicBuffer_v2 buffers,
  1078. char delim, asio::error_code& ec,
  1079. typename enable_if<
  1080. is_dynamic_buffer_v2<DynamicBuffer_v2>::value
  1081. >::type* = 0);
  1082. /// Read data into a dynamic buffer sequence until it contains a specified
  1083. /// delimiter.
  1084. /**
  1085. * This function is used to read data into the specified dynamic buffer
  1086. * sequence until the dynamic buffer sequence's get area contains the specified
  1087. * delimiter. The call will block until one of the following conditions is
  1088. * true:
  1089. *
  1090. * @li The get area of the dynamic buffer sequence contains the specified
  1091. * delimiter.
  1092. *
  1093. * @li An error occurred.
  1094. *
  1095. * This operation is implemented in terms of zero or more calls to the stream's
  1096. * read_some function. If the dynamic buffer sequence's get area already
  1097. * contains the delimiter, the function returns immediately.
  1098. *
  1099. * @param s The stream from which the data is to be read. The type must support
  1100. * the SyncReadStream concept.
  1101. *
  1102. * @param buffers The dynamic buffer sequence into which the data will be read.
  1103. *
  1104. * @param delim The delimiter string.
  1105. *
  1106. * @returns The number of bytes in the dynamic buffer sequence's get area up to
  1107. * and including the delimiter.
  1108. *
  1109. * @note After a successful read_until operation, the dynamic buffer sequence
  1110. * may contain additional data beyond the delimiter. An application will
  1111. * typically leave that data in the dynamic buffer sequence for a subsequent
  1112. * read_until operation to examine.
  1113. *
  1114. * @par Example
  1115. * To read data into a @c std::string until a CR-LF sequence is encountered:
  1116. * @code std::string data;
  1117. * std::string n = asio::read_until(s,
  1118. * asio::dynamic_buffer(data), "\r\n");
  1119. * std::string line = data.substr(0, n);
  1120. * data.erase(0, n); @endcode
  1121. * After the @c read_until operation completes successfully, the string @c data
  1122. * contains the delimiter:
  1123. * @code { 'a', 'b', ..., 'c', '\r', '\n', 'd', 'e', ... } @endcode
  1124. * The call to @c substr then extracts the data up to and including the
  1125. * delimiter, so that the string @c line contains:
  1126. * @code { 'a', 'b', ..., 'c', '\r', '\n' } @endcode
  1127. * After the call to @c erase, the remaining data is left in the buffer @c b as
  1128. * follows:
  1129. * @code { 'd', 'e', ... } @endcode
  1130. * This data may be the start of a new line, to be extracted by a subsequent
  1131. * @c read_until operation.
  1132. */
  1133. template <typename SyncReadStream, typename DynamicBuffer_v2>
  1134. std::size_t read_until(SyncReadStream& s, DynamicBuffer_v2 buffers,
  1135. ASIO_STRING_VIEW_PARAM delim,
  1136. typename enable_if<
  1137. is_dynamic_buffer_v2<DynamicBuffer_v2>::value
  1138. >::type* = 0);
  1139. /// Read data into a dynamic buffer sequence until it contains a specified
  1140. /// delimiter.
  1141. /**
  1142. * This function is used to read data into the specified dynamic buffer
  1143. * sequence until the dynamic buffer sequence's get area contains the specified
  1144. * delimiter. The call will block until one of the following conditions is
  1145. * true:
  1146. *
  1147. * @li The get area of the dynamic buffer sequence contains the specified
  1148. * delimiter.
  1149. *
  1150. * @li An error occurred.
  1151. *
  1152. * This operation is implemented in terms of zero or more calls to the stream's
  1153. * read_some function. If the dynamic buffer sequence's get area already
  1154. * contains the delimiter, the function returns immediately.
  1155. *
  1156. * @param s The stream from which the data is to be read. The type must support
  1157. * the SyncReadStream concept.
  1158. *
  1159. * @param buffers The dynamic buffer sequence into which the data will be read.
  1160. *
  1161. * @param delim The delimiter string.
  1162. *
  1163. * @param ec Set to indicate what error occurred, if any.
  1164. *
  1165. * @returns The number of bytes in the dynamic buffer sequence's get area up to
  1166. * and including the delimiter. Returns 0 if an error occurred.
  1167. *
  1168. * @note After a successful read_until operation, the dynamic buffer sequence
  1169. * may contain additional data beyond the delimiter. An application will
  1170. * typically leave that data in the dynamic buffer sequence for a subsequent
  1171. * read_until operation to examine.
  1172. */
  1173. template <typename SyncReadStream, typename DynamicBuffer_v2>
  1174. std::size_t read_until(SyncReadStream& s, DynamicBuffer_v2 buffers,
  1175. ASIO_STRING_VIEW_PARAM delim, asio::error_code& ec,
  1176. typename enable_if<
  1177. is_dynamic_buffer_v2<DynamicBuffer_v2>::value
  1178. >::type* = 0);
  1179. #if !defined(ASIO_NO_EXTENSIONS)
  1180. #if defined(ASIO_HAS_BOOST_REGEX) \
  1181. || defined(GENERATING_DOCUMENTATION)
  1182. /// Read data into a dynamic buffer sequence until some part of the data it
  1183. /// contains matches a regular expression.
  1184. /**
  1185. * This function is used to read data into the specified dynamic buffer
  1186. * sequence until the dynamic buffer sequence's get area contains some data
  1187. * that matches a regular expression. The call will block until one of the
  1188. * following conditions is true:
  1189. *
  1190. * @li A substring of the dynamic buffer sequence's get area matches the
  1191. * regular expression.
  1192. *
  1193. * @li An error occurred.
  1194. *
  1195. * This operation is implemented in terms of zero or more calls to the stream's
  1196. * read_some function. If the dynamic buffer sequence's get area already
  1197. * contains data that matches the regular expression, the function returns
  1198. * immediately.
  1199. *
  1200. * @param s The stream from which the data is to be read. The type must support
  1201. * the SyncReadStream concept.
  1202. *
  1203. * @param buffers A dynamic buffer sequence into which the data will be read.
  1204. *
  1205. * @param expr The regular expression.
  1206. *
  1207. * @returns The number of bytes in the dynamic buffer sequence's get area up to
  1208. * and including the substring that matches the regular expression.
  1209. *
  1210. * @throws asio::system_error Thrown on failure.
  1211. *
  1212. * @note After a successful read_until operation, the dynamic buffer sequence
  1213. * may contain additional data beyond that which matched the regular
  1214. * expression. An application will typically leave that data in the dynamic
  1215. * buffer sequence for a subsequent read_until operation to examine.
  1216. *
  1217. * @par Example
  1218. * To read data into a @c std::string until a CR-LF sequence is encountered:
  1219. * @code std::string data;
  1220. * std::string n = asio::read_until(s,
  1221. * asio::dynamic_buffer(data), boost::regex("\r\n"));
  1222. * std::string line = data.substr(0, n);
  1223. * data.erase(0, n); @endcode
  1224. * After the @c read_until operation completes successfully, the string @c data
  1225. * contains the delimiter:
  1226. * @code { 'a', 'b', ..., 'c', '\r', '\n', 'd', 'e', ... } @endcode
  1227. * The call to @c substr then extracts the data up to and including the
  1228. * delimiter, so that the string @c line contains:
  1229. * @code { 'a', 'b', ..., 'c', '\r', '\n' } @endcode
  1230. * After the call to @c erase, the remaining data is left in the buffer @c b as
  1231. * follows:
  1232. * @code { 'd', 'e', ... } @endcode
  1233. * This data may be the start of a new line, to be extracted by a subsequent
  1234. * @c read_until operation.
  1235. */
  1236. template <typename SyncReadStream, typename DynamicBuffer_v2>
  1237. std::size_t read_until(SyncReadStream& s, DynamicBuffer_v2 buffers,
  1238. const boost::regex& expr,
  1239. typename enable_if<
  1240. is_dynamic_buffer_v2<DynamicBuffer_v2>::value
  1241. >::type* = 0);
  1242. /// Read data into a dynamic buffer sequence until some part of the data it
  1243. /// contains matches a regular expression.
  1244. /**
  1245. * This function is used to read data into the specified dynamic buffer
  1246. * sequence until the dynamic buffer sequence's get area contains some data
  1247. * that matches a regular expression. The call will block until one of the
  1248. * following conditions is true:
  1249. *
  1250. * @li A substring of the dynamic buffer sequence's get area matches the
  1251. * regular expression.
  1252. *
  1253. * @li An error occurred.
  1254. *
  1255. * This operation is implemented in terms of zero or more calls to the stream's
  1256. * read_some function. If the dynamic buffer sequence's get area already
  1257. * contains data that matches the regular expression, the function returns
  1258. * immediately.
  1259. *
  1260. * @param s The stream from which the data is to be read. The type must support
  1261. * the SyncReadStream concept.
  1262. *
  1263. * @param buffers A dynamic buffer sequence into which the data will be read.
  1264. *
  1265. * @param expr The regular expression.
  1266. *
  1267. * @param ec Set to indicate what error occurred, if any.
  1268. *
  1269. * @returns The number of bytes in the dynamic buffer sequence's get area up to
  1270. * and including the substring that matches the regular expression. Returns 0
  1271. * if an error occurred.
  1272. *
  1273. * @note After a successful read_until operation, the dynamic buffer sequence
  1274. * may contain additional data beyond that which matched the regular
  1275. * expression. An application will typically leave that data in the dynamic
  1276. * buffer sequence for a subsequent read_until operation to examine.
  1277. */
  1278. template <typename SyncReadStream, typename DynamicBuffer_v2>
  1279. std::size_t read_until(SyncReadStream& s, DynamicBuffer_v2 buffers,
  1280. const boost::regex& expr, asio::error_code& ec,
  1281. typename enable_if<
  1282. is_dynamic_buffer_v2<DynamicBuffer_v2>::value
  1283. >::type* = 0);
  1284. #endif // defined(ASIO_HAS_BOOST_REGEX)
  1285. // || defined(GENERATING_DOCUMENTATION)
  1286. /// Read data into a dynamic buffer sequence until a function object indicates a
  1287. /// match.
  1288. /**
  1289. * This function is used to read data into the specified dynamic buffer
  1290. * sequence until a user-defined match condition function object, when applied
  1291. * to the data contained in the dynamic buffer sequence, indicates a successful
  1292. * match. The call will block until one of the following conditions is true:
  1293. *
  1294. * @li The match condition function object returns a std::pair where the second
  1295. * element evaluates to true.
  1296. *
  1297. * @li An error occurred.
  1298. *
  1299. * This operation is implemented in terms of zero or more calls to the stream's
  1300. * read_some function. If the match condition function object already indicates
  1301. * a match, the function returns immediately.
  1302. *
  1303. * @param s The stream from which the data is to be read. The type must support
  1304. * the SyncReadStream concept.
  1305. *
  1306. * @param buffers A dynamic buffer sequence into which the data will be read.
  1307. *
  1308. * @param match_condition The function object to be called to determine whether
  1309. * a match exists. The signature of the function object must be:
  1310. * @code pair<iterator, bool> match_condition(iterator begin, iterator end);
  1311. * @endcode
  1312. * where @c iterator represents the type:
  1313. * @code buffers_iterator<typename DynamicBuffer_v2::const_buffers_type>
  1314. * @endcode
  1315. * The iterator parameters @c begin and @c end define the range of bytes to be
  1316. * scanned to determine whether there is a match. The @c first member of the
  1317. * return value is an iterator marking one-past-the-end of the bytes that have
  1318. * been consumed by the match function. This iterator is used to calculate the
  1319. * @c begin parameter for any subsequent invocation of the match condition. The
  1320. * @c second member of the return value is true if a match has been found, false
  1321. * otherwise.
  1322. *
  1323. * @returns The number of bytes in the dynamic_buffer's get area that
  1324. * have been fully consumed by the match function.
  1325. *
  1326. * @throws asio::system_error Thrown on failure.
  1327. *
  1328. * @note After a successful read_until operation, the dynamic buffer sequence
  1329. * may contain additional data beyond that which matched the function object.
  1330. * An application will typically leave that data in the dynamic buffer sequence
  1331. * for a subsequent read_until operation to examine.
  1332. * @note The default implementation of the @c is_match_condition type trait
  1333. * evaluates to true for function pointers and function objects with a
  1334. * @c result_type typedef. It must be specialised for other user-defined
  1335. * function objects.
  1336. *
  1337. * @par Examples
  1338. * To read data into a dynamic buffer sequence until whitespace is encountered:
  1339. * @code typedef asio::buffers_iterator<
  1340. * asio::const_buffers_1> iterator;
  1341. *
  1342. * std::pair<iterator, bool>
  1343. * match_whitespace(iterator begin, iterator end)
  1344. * {
  1345. * iterator i = begin;
  1346. * while (i != end)
  1347. * if (std::isspace(*i++))
  1348. * return std::make_pair(i, true);
  1349. * return std::make_pair(i, false);
  1350. * }
  1351. * ...
  1352. * std::string data;
  1353. * asio::read_until(s, data, match_whitespace);
  1354. * @endcode
  1355. *
  1356. * To read data into a @c std::string until a matching character is found:
  1357. * @code class match_char
  1358. * {
  1359. * public:
  1360. * explicit match_char(char c) : c_(c) {}
  1361. *
  1362. * template <typename Iterator>
  1363. * std::pair<Iterator, bool> operator()(
  1364. * Iterator begin, Iterator end) const
  1365. * {
  1366. * Iterator i = begin;
  1367. * while (i != end)
  1368. * if (c_ == *i++)
  1369. * return std::make_pair(i, true);
  1370. * return std::make_pair(i, false);
  1371. * }
  1372. *
  1373. * private:
  1374. * char c_;
  1375. * };
  1376. *
  1377. * namespace asio {
  1378. * template <> struct is_match_condition<match_char>
  1379. * : public boost::true_type {};
  1380. * } // namespace asio
  1381. * ...
  1382. * std::string data;
  1383. * asio::read_until(s, data, match_char('a'));
  1384. * @endcode
  1385. */
  1386. template <typename SyncReadStream,
  1387. typename DynamicBuffer_v2, typename MatchCondition>
  1388. std::size_t read_until(SyncReadStream& s, DynamicBuffer_v2 buffers,
  1389. MatchCondition match_condition,
  1390. typename enable_if<
  1391. is_match_condition<MatchCondition>::value
  1392. && is_dynamic_buffer_v2<DynamicBuffer_v2>::value
  1393. >::type* = 0);
  1394. /// Read data into a dynamic buffer sequence until a function object indicates a
  1395. /// match.
  1396. /**
  1397. * This function is used to read data into the specified dynamic buffer
  1398. * sequence until a user-defined match condition function object, when applied
  1399. * to the data contained in the dynamic buffer sequence, indicates a successful
  1400. * match. The call will block until one of the following conditions is true:
  1401. *
  1402. * @li The match condition function object returns a std::pair where the second
  1403. * element evaluates to true.
  1404. *
  1405. * @li An error occurred.
  1406. *
  1407. * This operation is implemented in terms of zero or more calls to the stream's
  1408. * read_some function. If the match condition function object already indicates
  1409. * a match, the function returns immediately.
  1410. *
  1411. * @param s The stream from which the data is to be read. The type must support
  1412. * the SyncReadStream concept.
  1413. *
  1414. * @param buffers A dynamic buffer sequence into which the data will be read.
  1415. *
  1416. * @param match_condition The function object to be called to determine whether
  1417. * a match exists. The signature of the function object must be:
  1418. * @code pair<iterator, bool> match_condition(iterator begin, iterator end);
  1419. * @endcode
  1420. * where @c iterator represents the type:
  1421. * @code buffers_iterator<DynamicBuffer_v2::const_buffers_type>
  1422. * @endcode
  1423. * The iterator parameters @c begin and @c end define the range of bytes to be
  1424. * scanned to determine whether there is a match. The @c first member of the
  1425. * return value is an iterator marking one-past-the-end of the bytes that have
  1426. * been consumed by the match function. This iterator is used to calculate the
  1427. * @c begin parameter for any subsequent invocation of the match condition. The
  1428. * @c second member of the return value is true if a match has been found, false
  1429. * otherwise.
  1430. *
  1431. * @param ec Set to indicate what error occurred, if any.
  1432. *
  1433. * @returns The number of bytes in the dynamic buffer sequence's get area that
  1434. * have been fully consumed by the match function. Returns 0 if an error
  1435. * occurred.
  1436. *
  1437. * @note After a successful read_until operation, the dynamic buffer sequence
  1438. * may contain additional data beyond that which matched the function object.
  1439. * An application will typically leave that data in the dynamic buffer sequence
  1440. * for a subsequent read_until operation to examine.
  1441. *
  1442. * @note The default implementation of the @c is_match_condition type trait
  1443. * evaluates to true for function pointers and function objects with a
  1444. * @c result_type typedef. It must be specialised for other user-defined
  1445. * function objects.
  1446. */
  1447. template <typename SyncReadStream,
  1448. typename DynamicBuffer_v2, typename MatchCondition>
  1449. std::size_t read_until(SyncReadStream& s, DynamicBuffer_v2 buffers,
  1450. MatchCondition match_condition, asio::error_code& ec,
  1451. typename enable_if<
  1452. is_match_condition<MatchCondition>::value
  1453. && is_dynamic_buffer_v2<DynamicBuffer_v2>::value
  1454. >::type* = 0);
  1455. #endif // !defined(ASIO_NO_EXTENSIONS)
  1456. /*@}*/
  1457. /**
  1458. * @defgroup async_read_until asio::async_read_until
  1459. *
  1460. * @brief The @c async_read_until function is a composed asynchronous operation
  1461. * that reads data into a dynamic buffer sequence, or into a streambuf, until
  1462. * it contains a delimiter, matches a regular expression, or a function object
  1463. * indicates a match.
  1464. */
  1465. /*@{*/
  1466. #if !defined(ASIO_NO_DYNAMIC_BUFFER_V1)
  1467. /// Start an asynchronous operation to read data into a dynamic buffer sequence
  1468. /// until it contains a specified delimiter.
  1469. /**
  1470. * This function is used to asynchronously read data into the specified dynamic
  1471. * buffer sequence until the dynamic buffer sequence's get area contains the
  1472. * specified delimiter. The function call always returns immediately. The
  1473. * asynchronous operation will continue until one of the following conditions
  1474. * is true:
  1475. *
  1476. * @li The get area of the dynamic buffer sequence contains the specified
  1477. * delimiter.
  1478. *
  1479. * @li An error occurred.
  1480. *
  1481. * This operation is implemented in terms of zero or more calls to the stream's
  1482. * async_read_some function, and is known as a <em>composed operation</em>. If
  1483. * the dynamic buffer sequence's get area already contains the delimiter, this
  1484. * asynchronous operation completes immediately. The program must ensure that
  1485. * the stream performs no other read operations (such as async_read,
  1486. * async_read_until, the stream's async_read_some function, or any other
  1487. * composed operations that perform reads) until this operation completes.
  1488. *
  1489. * @param s The stream from which the data is to be read. The type must support
  1490. * the AsyncReadStream concept.
  1491. *
  1492. * @param buffers The dynamic buffer sequence into which the data will be read.
  1493. * Although the buffers object may be copied as necessary, ownership of the
  1494. * underlying memory blocks is retained by the caller, which must guarantee
  1495. * that they remain valid until the handler is called.
  1496. *
  1497. * @param delim The delimiter character.
  1498. *
  1499. * @param handler The handler to be called when the read operation completes.
  1500. * Copies will be made of the handler as required. The function signature of the
  1501. * handler must be:
  1502. * @code void handler(
  1503. * // Result of operation.
  1504. * const asio::error_code& error,
  1505. *
  1506. * // The number of bytes in the dynamic buffer sequence's
  1507. * // get area up to and including the delimiter.
  1508. * // 0 if an error occurred.
  1509. * std::size_t bytes_transferred
  1510. * ); @endcode
  1511. * Regardless of whether the asynchronous operation completes immediately or
  1512. * not, the handler will not be invoked from within this function. On
  1513. * immediate completion, invocation of the handler will be performed in a
  1514. * manner equivalent to using asio::post().
  1515. *
  1516. * @note After a successful async_read_until operation, the dynamic buffer
  1517. * sequence may contain additional data beyond the delimiter. An application
  1518. * will typically leave that data in the dynamic buffer sequence for a
  1519. * subsequent async_read_until operation to examine.
  1520. *
  1521. * @par Example
  1522. * To asynchronously read data into a @c std::string until a newline is
  1523. * encountered:
  1524. * @code std::string data;
  1525. * ...
  1526. * void handler(const asio::error_code& e, std::size_t size)
  1527. * {
  1528. * if (!e)
  1529. * {
  1530. * std::string line = data.substr(0, n);
  1531. * data.erase(0, n);
  1532. * ...
  1533. * }
  1534. * }
  1535. * ...
  1536. * asio::async_read_until(s, data, '\n', handler); @endcode
  1537. * After the @c async_read_until operation completes successfully, the buffer
  1538. * @c data contains the delimiter:
  1539. * @code { 'a', 'b', ..., 'c', '\n', 'd', 'e', ... } @endcode
  1540. * The call to @c substr then extracts the data up to and including the
  1541. * delimiter, so that the string @c line contains:
  1542. * @code { 'a', 'b', ..., 'c', '\n' } @endcode
  1543. * After the call to @c erase, the remaining data is left in the buffer @c data
  1544. * as follows:
  1545. * @code { 'd', 'e', ... } @endcode
  1546. * This data may be the start of a new line, to be extracted by a subsequent
  1547. * @c async_read_until operation.
  1548. */
  1549. template <typename AsyncReadStream,
  1550. typename DynamicBuffer_v1, typename ReadHandler>
  1551. ASIO_INITFN_RESULT_TYPE(ReadHandler,
  1552. void (asio::error_code, std::size_t))
  1553. async_read_until(AsyncReadStream& s,
  1554. ASIO_MOVE_ARG(DynamicBuffer_v1) buffers,
  1555. char delim, ASIO_MOVE_ARG(ReadHandler) handler,
  1556. typename enable_if<
  1557. is_dynamic_buffer_v1<typename decay<DynamicBuffer_v1>::type>::value
  1558. && !is_dynamic_buffer_v2<typename decay<DynamicBuffer_v1>::type>::value
  1559. >::type* = 0);
  1560. /// Start an asynchronous operation to read data into a dynamic buffer sequence
  1561. /// until it contains a specified delimiter.
  1562. /**
  1563. * This function is used to asynchronously read data into the specified dynamic
  1564. * buffer sequence until the dynamic buffer sequence's get area contains the
  1565. * specified delimiter. The function call always returns immediately. The
  1566. * asynchronous operation will continue until one of the following conditions
  1567. * is true:
  1568. *
  1569. * @li The get area of the dynamic buffer sequence contains the specified
  1570. * delimiter.
  1571. *
  1572. * @li An error occurred.
  1573. *
  1574. * This operation is implemented in terms of zero or more calls to the stream's
  1575. * async_read_some function, and is known as a <em>composed operation</em>. If
  1576. * the dynamic buffer sequence's get area already contains the delimiter, this
  1577. * asynchronous operation completes immediately. The program must ensure that
  1578. * the stream performs no other read operations (such as async_read,
  1579. * async_read_until, the stream's async_read_some function, or any other
  1580. * composed operations that perform reads) until this operation completes.
  1581. *
  1582. * @param s The stream from which the data is to be read. The type must support
  1583. * the AsyncReadStream concept.
  1584. *
  1585. * @param buffers The dynamic buffer sequence into which the data will be read.
  1586. * Although the buffers object may be copied as necessary, ownership of the
  1587. * underlying memory blocks is retained by the caller, which must guarantee
  1588. * that they remain valid until the handler is called.
  1589. *
  1590. * @param delim The delimiter string.
  1591. *
  1592. * @param handler The handler to be called when the read operation completes.
  1593. * Copies will be made of the handler as required. The function signature of the
  1594. * handler must be:
  1595. * @code void handler(
  1596. * // Result of operation.
  1597. * const asio::error_code& error,
  1598. *
  1599. * // The number of bytes in the dynamic buffer sequence's
  1600. * // get area up to and including the delimiter.
  1601. * // 0 if an error occurred.
  1602. * std::size_t bytes_transferred
  1603. * ); @endcode
  1604. * Regardless of whether the asynchronous operation completes immediately or
  1605. * not, the handler will not be invoked from within this function. On
  1606. * immediate completion, invocation of the handler will be performed in a
  1607. * manner equivalent to using asio::post().
  1608. *
  1609. * @note After a successful async_read_until operation, the dynamic buffer
  1610. * sequence may contain additional data beyond the delimiter. An application
  1611. * will typically leave that data in the dynamic buffer sequence for a
  1612. * subsequent async_read_until operation to examine.
  1613. *
  1614. * @par Example
  1615. * To asynchronously read data into a @c std::string until a CR-LF sequence is
  1616. * encountered:
  1617. * @code std::string data;
  1618. * ...
  1619. * void handler(const asio::error_code& e, std::size_t size)
  1620. * {
  1621. * if (!e)
  1622. * {
  1623. * std::string line = data.substr(0, n);
  1624. * data.erase(0, n);
  1625. * ...
  1626. * }
  1627. * }
  1628. * ...
  1629. * asio::async_read_until(s, data, "\r\n", handler); @endcode
  1630. * After the @c async_read_until operation completes successfully, the string
  1631. * @c data contains the delimiter:
  1632. * @code { 'a', 'b', ..., 'c', '\r', '\n', 'd', 'e', ... } @endcode
  1633. * The call to @c substr then extracts the data up to and including the
  1634. * delimiter, so that the string @c line contains:
  1635. * @code { 'a', 'b', ..., 'c', '\r', '\n' } @endcode
  1636. * After the call to @c erase, the remaining data is left in the string @c data
  1637. * as follows:
  1638. * @code { 'd', 'e', ... } @endcode
  1639. * This data may be the start of a new line, to be extracted by a subsequent
  1640. * @c async_read_until operation.
  1641. */
  1642. template <typename AsyncReadStream,
  1643. typename DynamicBuffer_v1, typename ReadHandler>
  1644. ASIO_INITFN_RESULT_TYPE(ReadHandler,
  1645. void (asio::error_code, std::size_t))
  1646. async_read_until(AsyncReadStream& s,
  1647. ASIO_MOVE_ARG(DynamicBuffer_v1) buffers,
  1648. ASIO_STRING_VIEW_PARAM delim,
  1649. ASIO_MOVE_ARG(ReadHandler) handler,
  1650. typename enable_if<
  1651. is_dynamic_buffer_v1<typename decay<DynamicBuffer_v1>::type>::value
  1652. && !is_dynamic_buffer_v2<typename decay<DynamicBuffer_v1>::type>::value
  1653. >::type* = 0);
  1654. #if !defined(ASIO_NO_EXTENSIONS)
  1655. #if defined(ASIO_HAS_BOOST_REGEX) \
  1656. || defined(GENERATING_DOCUMENTATION)
  1657. /// Start an asynchronous operation to read data into a dynamic buffer sequence
  1658. /// until some part of its data matches a regular expression.
  1659. /**
  1660. * This function is used to asynchronously read data into the specified dynamic
  1661. * buffer sequence until the dynamic buffer sequence's get area contains some
  1662. * data that matches a regular expression. The function call always returns
  1663. * immediately. The asynchronous operation will continue until one of the
  1664. * following conditions is true:
  1665. *
  1666. * @li A substring of the dynamic buffer sequence's get area matches the regular
  1667. * expression.
  1668. *
  1669. * @li An error occurred.
  1670. *
  1671. * This operation is implemented in terms of zero or more calls to the stream's
  1672. * async_read_some function, and is known as a <em>composed operation</em>. If
  1673. * the dynamic buffer sequence's get area already contains data that matches
  1674. * the regular expression, this asynchronous operation completes immediately.
  1675. * The program must ensure that the stream performs no other read operations
  1676. * (such as async_read, async_read_until, the stream's async_read_some
  1677. * function, or any other composed operations that perform reads) until this
  1678. * operation completes.
  1679. *
  1680. * @param s The stream from which the data is to be read. The type must support
  1681. * the AsyncReadStream concept.
  1682. *
  1683. * @param buffers The dynamic buffer sequence into which the data will be read.
  1684. * Although the buffers object may be copied as necessary, ownership of the
  1685. * underlying memory blocks is retained by the caller, which must guarantee
  1686. * that they remain valid until the handler is called.
  1687. *
  1688. * @param expr The regular expression.
  1689. *
  1690. * @param handler The handler to be called when the read operation completes.
  1691. * Copies will be made of the handler as required. The function signature of the
  1692. * handler must be:
  1693. * @code void handler(
  1694. * // Result of operation.
  1695. * const asio::error_code& error,
  1696. *
  1697. * // The number of bytes in the dynamic buffer
  1698. * // sequence's get area up to and including the
  1699. * // substring that matches the regular expression.
  1700. * // 0 if an error occurred.
  1701. * std::size_t bytes_transferred
  1702. * ); @endcode
  1703. * Regardless of whether the asynchronous operation completes immediately or
  1704. * not, the handler will not be invoked from within this function. On
  1705. * immediate completion, invocation of the handler will be performed in a
  1706. * manner equivalent to using asio::post().
  1707. *
  1708. * @note After a successful async_read_until operation, the dynamic buffer
  1709. * sequence may contain additional data beyond that which matched the regular
  1710. * expression. An application will typically leave that data in the dynamic
  1711. * buffer sequence for a subsequent async_read_until operation to examine.
  1712. *
  1713. * @par Example
  1714. * To asynchronously read data into a @c std::string until a CR-LF sequence is
  1715. * encountered:
  1716. * @code std::string data;
  1717. * ...
  1718. * void handler(const asio::error_code& e, std::size_t size)
  1719. * {
  1720. * if (!e)
  1721. * {
  1722. * std::string line = data.substr(0, n);
  1723. * data.erase(0, n);
  1724. * ...
  1725. * }
  1726. * }
  1727. * ...
  1728. * asio::async_read_until(s, data,
  1729. * boost::regex("\r\n"), handler); @endcode
  1730. * After the @c async_read_until operation completes successfully, the string
  1731. * @c data contains the data which matched the regular expression:
  1732. * @code { 'a', 'b', ..., 'c', '\r', '\n', 'd', 'e', ... } @endcode
  1733. * The call to @c substr then extracts the data up to and including the match,
  1734. * so that the string @c line contains:
  1735. * @code { 'a', 'b', ..., 'c', '\r', '\n' } @endcode
  1736. * After the call to @c erase, the remaining data is left in the string @c data
  1737. * as follows:
  1738. * @code { 'd', 'e', ... } @endcode
  1739. * This data may be the start of a new line, to be extracted by a subsequent
  1740. * @c async_read_until operation.
  1741. */
  1742. template <typename AsyncReadStream,
  1743. typename DynamicBuffer_v1, typename ReadHandler>
  1744. ASIO_INITFN_RESULT_TYPE(ReadHandler,
  1745. void (asio::error_code, std::size_t))
  1746. async_read_until(AsyncReadStream& s,
  1747. ASIO_MOVE_ARG(DynamicBuffer_v1) buffers,
  1748. const boost::regex& expr,
  1749. ASIO_MOVE_ARG(ReadHandler) handler,
  1750. typename enable_if<
  1751. is_dynamic_buffer_v1<typename decay<DynamicBuffer_v1>::type>::value
  1752. && !is_dynamic_buffer_v2<typename decay<DynamicBuffer_v1>::type>::value
  1753. >::type* = 0);
  1754. #endif // defined(ASIO_HAS_BOOST_REGEX)
  1755. // || defined(GENERATING_DOCUMENTATION)
  1756. /// Start an asynchronous operation to read data into a dynamic buffer sequence
  1757. /// until a function object indicates a match.
  1758. /**
  1759. * This function is used to asynchronously read data into the specified dynamic
  1760. * buffer sequence until a user-defined match condition function object, when
  1761. * applied to the data contained in the dynamic buffer sequence, indicates a
  1762. * successful match. The function call always returns immediately. The
  1763. * asynchronous operation will continue until one of the following conditions
  1764. * is true:
  1765. *
  1766. * @li The match condition function object returns a std::pair where the second
  1767. * element evaluates to true.
  1768. *
  1769. * @li An error occurred.
  1770. *
  1771. * This operation is implemented in terms of zero or more calls to the stream's
  1772. * async_read_some function, and is known as a <em>composed operation</em>. If
  1773. * the match condition function object already indicates a match, this
  1774. * asynchronous operation completes immediately. The program must ensure that
  1775. * the stream performs no other read operations (such as async_read,
  1776. * async_read_until, the stream's async_read_some function, or any other
  1777. * composed operations that perform reads) until this operation completes.
  1778. *
  1779. * @param s The stream from which the data is to be read. The type must support
  1780. * the AsyncReadStream concept.
  1781. *
  1782. * @param buffers The dynamic buffer sequence into which the data will be read.
  1783. * Although the buffers object may be copied as necessary, ownership of the
  1784. * underlying memory blocks is retained by the caller, which must guarantee
  1785. * that they remain valid until the handler is called.
  1786. *
  1787. * @param match_condition The function object to be called to determine whether
  1788. * a match exists. The signature of the function object must be:
  1789. * @code pair<iterator, bool> match_condition(iterator begin, iterator end);
  1790. * @endcode
  1791. * where @c iterator represents the type:
  1792. * @code buffers_iterator<typename DynamicBuffer_v1::const_buffers_type>
  1793. * @endcode
  1794. * The iterator parameters @c begin and @c end define the range of bytes to be
  1795. * scanned to determine whether there is a match. The @c first member of the
  1796. * return value is an iterator marking one-past-the-end of the bytes that have
  1797. * been consumed by the match function. This iterator is used to calculate the
  1798. * @c begin parameter for any subsequent invocation of the match condition. The
  1799. * @c second member of the return value is true if a match has been found, false
  1800. * otherwise.
  1801. *
  1802. * @param handler The handler to be called when the read operation completes.
  1803. * Copies will be made of the handler as required. The function signature of the
  1804. * handler must be:
  1805. * @code void handler(
  1806. * // Result of operation.
  1807. * const asio::error_code& error,
  1808. *
  1809. * // The number of bytes in the dynamic buffer sequence's
  1810. * // get area that have been fully consumed by the match
  1811. * // function. O if an error occurred.
  1812. * std::size_t bytes_transferred
  1813. * ); @endcode
  1814. * Regardless of whether the asynchronous operation completes immediately or
  1815. * not, the handler will not be invoked from within this function. On
  1816. * immediate completion, invocation of the handler will be performed in a
  1817. * manner equivalent to using asio::post().
  1818. *
  1819. * @note After a successful async_read_until operation, the dynamic buffer
  1820. * sequence may contain additional data beyond that which matched the function
  1821. * object. An application will typically leave that data in the dynamic buffer
  1822. * sequence for a subsequent async_read_until operation to examine.
  1823. *
  1824. * @note The default implementation of the @c is_match_condition type trait
  1825. * evaluates to true for function pointers and function objects with a
  1826. * @c result_type typedef. It must be specialised for other user-defined
  1827. * function objects.
  1828. *
  1829. * @par Examples
  1830. * To asynchronously read data into a @c std::string until whitespace is
  1831. * encountered:
  1832. * @code typedef asio::buffers_iterator<
  1833. * asio::const_buffers_1> iterator;
  1834. *
  1835. * std::pair<iterator, bool>
  1836. * match_whitespace(iterator begin, iterator end)
  1837. * {
  1838. * iterator i = begin;
  1839. * while (i != end)
  1840. * if (std::isspace(*i++))
  1841. * return std::make_pair(i, true);
  1842. * return std::make_pair(i, false);
  1843. * }
  1844. * ...
  1845. * void handler(const asio::error_code& e, std::size_t size);
  1846. * ...
  1847. * std::string data;
  1848. * asio::async_read_until(s, data, match_whitespace, handler);
  1849. * @endcode
  1850. *
  1851. * To asynchronously read data into a @c std::string until a matching character
  1852. * is found:
  1853. * @code class match_char
  1854. * {
  1855. * public:
  1856. * explicit match_char(char c) : c_(c) {}
  1857. *
  1858. * template <typename Iterator>
  1859. * std::pair<Iterator, bool> operator()(
  1860. * Iterator begin, Iterator end) const
  1861. * {
  1862. * Iterator i = begin;
  1863. * while (i != end)
  1864. * if (c_ == *i++)
  1865. * return std::make_pair(i, true);
  1866. * return std::make_pair(i, false);
  1867. * }
  1868. *
  1869. * private:
  1870. * char c_;
  1871. * };
  1872. *
  1873. * namespace asio {
  1874. * template <> struct is_match_condition<match_char>
  1875. * : public boost::true_type {};
  1876. * } // namespace asio
  1877. * ...
  1878. * void handler(const asio::error_code& e, std::size_t size);
  1879. * ...
  1880. * std::string data;
  1881. * asio::async_read_until(s, data, match_char('a'), handler);
  1882. * @endcode
  1883. */
  1884. template <typename AsyncReadStream, typename DynamicBuffer_v1,
  1885. typename MatchCondition, typename ReadHandler>
  1886. ASIO_INITFN_RESULT_TYPE(ReadHandler,
  1887. void (asio::error_code, std::size_t))
  1888. async_read_until(AsyncReadStream& s,
  1889. ASIO_MOVE_ARG(DynamicBuffer_v1) buffers,
  1890. MatchCondition match_condition, ASIO_MOVE_ARG(ReadHandler) handler,
  1891. typename enable_if<
  1892. is_match_condition<MatchCondition>::value
  1893. && is_dynamic_buffer_v1<typename decay<DynamicBuffer_v1>::type>::value
  1894. && !is_dynamic_buffer_v2<typename decay<DynamicBuffer_v1>::type>::value
  1895. >::type* = 0);
  1896. #if !defined(ASIO_NO_IOSTREAM)
  1897. /// Start an asynchronous operation to read data into a streambuf until it
  1898. /// contains a specified delimiter.
  1899. /**
  1900. * This function is used to asynchronously read data into the specified
  1901. * streambuf until the streambuf's get area contains the specified delimiter.
  1902. * The function call always returns immediately. The asynchronous operation
  1903. * will continue until one of the following conditions is true:
  1904. *
  1905. * @li The get area of the streambuf contains the specified delimiter.
  1906. *
  1907. * @li An error occurred.
  1908. *
  1909. * This operation is implemented in terms of zero or more calls to the stream's
  1910. * async_read_some function, and is known as a <em>composed operation</em>. If
  1911. * the streambuf's get area already contains the delimiter, this asynchronous
  1912. * operation completes immediately. The program must ensure that the stream
  1913. * performs no other read operations (such as async_read, async_read_until, the
  1914. * stream's async_read_some function, or any other composed operations that
  1915. * perform reads) until this operation completes.
  1916. *
  1917. * @param s The stream from which the data is to be read. The type must support
  1918. * the AsyncReadStream concept.
  1919. *
  1920. * @param b A streambuf object into which the data will be read. Ownership of
  1921. * the streambuf is retained by the caller, which must guarantee that it remains
  1922. * valid until the handler is called.
  1923. *
  1924. * @param delim The delimiter character.
  1925. *
  1926. * @param handler The handler to be called when the read operation completes.
  1927. * Copies will be made of the handler as required. The function signature of the
  1928. * handler must be:
  1929. * @code void handler(
  1930. * // Result of operation.
  1931. * const asio::error_code& error,
  1932. *
  1933. * // The number of bytes in the streambuf's get
  1934. * // area up to and including the delimiter.
  1935. * // 0 if an error occurred.
  1936. * std::size_t bytes_transferred
  1937. * ); @endcode
  1938. * Regardless of whether the asynchronous operation completes immediately or
  1939. * not, the handler will not be invoked from within this function. On
  1940. * immediate completion, invocation of the handler will be performed in a
  1941. * manner equivalent to using asio::post().
  1942. *
  1943. * @note After a successful async_read_until operation, the streambuf may
  1944. * contain additional data beyond the delimiter. An application will typically
  1945. * leave that data in the streambuf for a subsequent async_read_until operation
  1946. * to examine.
  1947. *
  1948. * @par Example
  1949. * To asynchronously read data into a streambuf until a newline is encountered:
  1950. * @code asio::streambuf b;
  1951. * ...
  1952. * void handler(const asio::error_code& e, std::size_t size)
  1953. * {
  1954. * if (!e)
  1955. * {
  1956. * std::istream is(&b);
  1957. * std::string line;
  1958. * std::getline(is, line);
  1959. * ...
  1960. * }
  1961. * }
  1962. * ...
  1963. * asio::async_read_until(s, b, '\n', handler); @endcode
  1964. * After the @c async_read_until operation completes successfully, the buffer
  1965. * @c b contains the delimiter:
  1966. * @code { 'a', 'b', ..., 'c', '\n', 'd', 'e', ... } @endcode
  1967. * The call to @c std::getline then extracts the data up to and including the
  1968. * newline (which is discarded), so that the string @c line contains:
  1969. * @code { 'a', 'b', ..., 'c' } @endcode
  1970. * The remaining data is left in the buffer @c b as follows:
  1971. * @code { 'd', 'e', ... } @endcode
  1972. * This data may be the start of a new line, to be extracted by a subsequent
  1973. * @c async_read_until operation.
  1974. */
  1975. template <typename AsyncReadStream, typename Allocator, typename ReadHandler>
  1976. ASIO_INITFN_RESULT_TYPE(ReadHandler,
  1977. void (asio::error_code, std::size_t))
  1978. async_read_until(AsyncReadStream& s,
  1979. asio::basic_streambuf<Allocator>& b,
  1980. char delim, ASIO_MOVE_ARG(ReadHandler) handler);
  1981. /// Start an asynchronous operation to read data into a streambuf until it
  1982. /// contains a specified delimiter.
  1983. /**
  1984. * This function is used to asynchronously read data into the specified
  1985. * streambuf until the streambuf's get area contains the specified delimiter.
  1986. * The function call always returns immediately. The asynchronous operation
  1987. * will continue until one of the following conditions is true:
  1988. *
  1989. * @li The get area of the streambuf contains the specified delimiter.
  1990. *
  1991. * @li An error occurred.
  1992. *
  1993. * This operation is implemented in terms of zero or more calls to the stream's
  1994. * async_read_some function, and is known as a <em>composed operation</em>. If
  1995. * the streambuf's get area already contains the delimiter, this asynchronous
  1996. * operation completes immediately. The program must ensure that the stream
  1997. * performs no other read operations (such as async_read, async_read_until, the
  1998. * stream's async_read_some function, or any other composed operations that
  1999. * perform reads) until this operation completes.
  2000. *
  2001. * @param s The stream from which the data is to be read. The type must support
  2002. * the AsyncReadStream concept.
  2003. *
  2004. * @param b A streambuf object into which the data will be read. Ownership of
  2005. * the streambuf is retained by the caller, which must guarantee that it remains
  2006. * valid until the handler is called.
  2007. *
  2008. * @param delim The delimiter string.
  2009. *
  2010. * @param handler The handler to be called when the read operation completes.
  2011. * Copies will be made of the handler as required. The function signature of the
  2012. * handler must be:
  2013. * @code void handler(
  2014. * // Result of operation.
  2015. * const asio::error_code& error,
  2016. *
  2017. * // The number of bytes in the streambuf's get
  2018. * // area up to and including the delimiter.
  2019. * // 0 if an error occurred.
  2020. * std::size_t bytes_transferred
  2021. * ); @endcode
  2022. * Regardless of whether the asynchronous operation completes immediately or
  2023. * not, the handler will not be invoked from within this function. On
  2024. * immediate completion, invocation of the handler will be performed in a
  2025. * manner equivalent to using asio::post().
  2026. *
  2027. * @note After a successful async_read_until operation, the streambuf may
  2028. * contain additional data beyond the delimiter. An application will typically
  2029. * leave that data in the streambuf for a subsequent async_read_until operation
  2030. * to examine.
  2031. *
  2032. * @par Example
  2033. * To asynchronously read data into a streambuf until a newline is encountered:
  2034. * @code asio::streambuf b;
  2035. * ...
  2036. * void handler(const asio::error_code& e, std::size_t size)
  2037. * {
  2038. * if (!e)
  2039. * {
  2040. * std::istream is(&b);
  2041. * std::string line;
  2042. * std::getline(is, line);
  2043. * ...
  2044. * }
  2045. * }
  2046. * ...
  2047. * asio::async_read_until(s, b, "\r\n", handler); @endcode
  2048. * After the @c async_read_until operation completes successfully, the buffer
  2049. * @c b contains the delimiter:
  2050. * @code { 'a', 'b', ..., 'c', '\r', '\n', 'd', 'e', ... } @endcode
  2051. * The call to @c std::getline then extracts the data up to and including the
  2052. * newline (which is discarded), so that the string @c line contains:
  2053. * @code { 'a', 'b', ..., 'c', '\r' } @endcode
  2054. * The remaining data is left in the buffer @c b as follows:
  2055. * @code { 'd', 'e', ... } @endcode
  2056. * This data may be the start of a new line, to be extracted by a subsequent
  2057. * @c async_read_until operation.
  2058. */
  2059. template <typename AsyncReadStream, typename Allocator, typename ReadHandler>
  2060. ASIO_INITFN_RESULT_TYPE(ReadHandler,
  2061. void (asio::error_code, std::size_t))
  2062. async_read_until(AsyncReadStream& s,
  2063. asio::basic_streambuf<Allocator>& b,
  2064. ASIO_STRING_VIEW_PARAM delim,
  2065. ASIO_MOVE_ARG(ReadHandler) handler);
  2066. #if defined(ASIO_HAS_BOOST_REGEX) \
  2067. || defined(GENERATING_DOCUMENTATION)
  2068. /// Start an asynchronous operation to read data into a streambuf until some
  2069. /// part of its data matches a regular expression.
  2070. /**
  2071. * This function is used to asynchronously read data into the specified
  2072. * streambuf until the streambuf's get area contains some data that matches a
  2073. * regular expression. The function call always returns immediately. The
  2074. * asynchronous operation will continue until one of the following conditions
  2075. * is true:
  2076. *
  2077. * @li A substring of the streambuf's get area matches the regular expression.
  2078. *
  2079. * @li An error occurred.
  2080. *
  2081. * This operation is implemented in terms of zero or more calls to the stream's
  2082. * async_read_some function, and is known as a <em>composed operation</em>. If
  2083. * the streambuf's get area already contains data that matches the regular
  2084. * expression, this asynchronous operation completes immediately. The program
  2085. * must ensure that the stream performs no other read operations (such as
  2086. * async_read, async_read_until, the stream's async_read_some function, or any
  2087. * other composed operations that perform reads) until this operation
  2088. * completes.
  2089. *
  2090. * @param s The stream from which the data is to be read. The type must support
  2091. * the AsyncReadStream concept.
  2092. *
  2093. * @param b A streambuf object into which the data will be read. Ownership of
  2094. * the streambuf is retained by the caller, which must guarantee that it remains
  2095. * valid until the handler is called.
  2096. *
  2097. * @param expr The regular expression.
  2098. *
  2099. * @param handler The handler to be called when the read operation completes.
  2100. * Copies will be made of the handler as required. The function signature of the
  2101. * handler must be:
  2102. * @code void handler(
  2103. * // Result of operation.
  2104. * const asio::error_code& error,
  2105. *
  2106. * // The number of bytes in the streambuf's get
  2107. * // area up to and including the substring
  2108. * // that matches the regular. expression.
  2109. * // 0 if an error occurred.
  2110. * std::size_t bytes_transferred
  2111. * ); @endcode
  2112. * Regardless of whether the asynchronous operation completes immediately or
  2113. * not, the handler will not be invoked from within this function. On
  2114. * immediate completion, invocation of the handler will be performed in a
  2115. * manner equivalent to using asio::post().
  2116. *
  2117. * @note After a successful async_read_until operation, the streambuf may
  2118. * contain additional data beyond that which matched the regular expression. An
  2119. * application will typically leave that data in the streambuf for a subsequent
  2120. * async_read_until operation to examine.
  2121. *
  2122. * @par Example
  2123. * To asynchronously read data into a streambuf until a CR-LF sequence is
  2124. * encountered:
  2125. * @code asio::streambuf b;
  2126. * ...
  2127. * void handler(const asio::error_code& e, std::size_t size)
  2128. * {
  2129. * if (!e)
  2130. * {
  2131. * std::istream is(&b);
  2132. * std::string line;
  2133. * std::getline(is, line);
  2134. * ...
  2135. * }
  2136. * }
  2137. * ...
  2138. * asio::async_read_until(s, b, boost::regex("\r\n"), handler); @endcode
  2139. * After the @c async_read_until operation completes successfully, the buffer
  2140. * @c b contains the data which matched the regular expression:
  2141. * @code { 'a', 'b', ..., 'c', '\r', '\n', 'd', 'e', ... } @endcode
  2142. * The call to @c std::getline then extracts the data up to and including the
  2143. * newline (which is discarded), so that the string @c line contains:
  2144. * @code { 'a', 'b', ..., 'c', '\r' } @endcode
  2145. * The remaining data is left in the buffer @c b as follows:
  2146. * @code { 'd', 'e', ... } @endcode
  2147. * This data may be the start of a new line, to be extracted by a subsequent
  2148. * @c async_read_until operation.
  2149. */
  2150. template <typename AsyncReadStream, typename Allocator, typename ReadHandler>
  2151. ASIO_INITFN_RESULT_TYPE(ReadHandler,
  2152. void (asio::error_code, std::size_t))
  2153. async_read_until(AsyncReadStream& s,
  2154. asio::basic_streambuf<Allocator>& b, const boost::regex& expr,
  2155. ASIO_MOVE_ARG(ReadHandler) handler);
  2156. #endif // defined(ASIO_HAS_BOOST_REGEX)
  2157. // || defined(GENERATING_DOCUMENTATION)
  2158. /// Start an asynchronous operation to read data into a streambuf until a
  2159. /// function object indicates a match.
  2160. /**
  2161. * This function is used to asynchronously read data into the specified
  2162. * streambuf until a user-defined match condition function object, when applied
  2163. * to the data contained in the streambuf, indicates a successful match. The
  2164. * function call always returns immediately. The asynchronous operation will
  2165. * continue until one of the following conditions is true:
  2166. *
  2167. * @li The match condition function object returns a std::pair where the second
  2168. * element evaluates to true.
  2169. *
  2170. * @li An error occurred.
  2171. *
  2172. * This operation is implemented in terms of zero or more calls to the stream's
  2173. * async_read_some function, and is known as a <em>composed operation</em>. If
  2174. * the match condition function object already indicates a match, this
  2175. * asynchronous operation completes immediately. The program must ensure that
  2176. * the stream performs no other read operations (such as async_read,
  2177. * async_read_until, the stream's async_read_some function, or any other
  2178. * composed operations that perform reads) until this operation completes.
  2179. *
  2180. * @param s The stream from which the data is to be read. The type must support
  2181. * the AsyncReadStream concept.
  2182. *
  2183. * @param b A streambuf object into which the data will be read.
  2184. *
  2185. * @param match_condition The function object to be called to determine whether
  2186. * a match exists. The signature of the function object must be:
  2187. * @code pair<iterator, bool> match_condition(iterator begin, iterator end);
  2188. * @endcode
  2189. * where @c iterator represents the type:
  2190. * @code buffers_iterator<basic_streambuf<Allocator>::const_buffers_type>
  2191. * @endcode
  2192. * The iterator parameters @c begin and @c end define the range of bytes to be
  2193. * scanned to determine whether there is a match. The @c first member of the
  2194. * return value is an iterator marking one-past-the-end of the bytes that have
  2195. * been consumed by the match function. This iterator is used to calculate the
  2196. * @c begin parameter for any subsequent invocation of the match condition. The
  2197. * @c second member of the return value is true if a match has been found, false
  2198. * otherwise.
  2199. *
  2200. * @param handler The handler to be called when the read operation completes.
  2201. * Copies will be made of the handler as required. The function signature of the
  2202. * handler must be:
  2203. * @code void handler(
  2204. * // Result of operation.
  2205. * const asio::error_code& error,
  2206. *
  2207. * // The number of bytes in the streambuf's get
  2208. * // area that have been fully consumed by the
  2209. * // match function. O if an error occurred.
  2210. * std::size_t bytes_transferred
  2211. * ); @endcode
  2212. * Regardless of whether the asynchronous operation completes immediately or
  2213. * not, the handler will not be invoked from within this function. On
  2214. * immediate completion, invocation of the handler will be performed in a
  2215. * manner equivalent to using asio::post().
  2216. *
  2217. * @note After a successful async_read_until operation, the streambuf may
  2218. * contain additional data beyond that which matched the function object. An
  2219. * application will typically leave that data in the streambuf for a subsequent
  2220. * async_read_until operation to examine.
  2221. *
  2222. * @note The default implementation of the @c is_match_condition type trait
  2223. * evaluates to true for function pointers and function objects with a
  2224. * @c result_type typedef. It must be specialised for other user-defined
  2225. * function objects.
  2226. *
  2227. * @par Examples
  2228. * To asynchronously read data into a streambuf until whitespace is encountered:
  2229. * @code typedef asio::buffers_iterator<
  2230. * asio::streambuf::const_buffers_type> iterator;
  2231. *
  2232. * std::pair<iterator, bool>
  2233. * match_whitespace(iterator begin, iterator end)
  2234. * {
  2235. * iterator i = begin;
  2236. * while (i != end)
  2237. * if (std::isspace(*i++))
  2238. * return std::make_pair(i, true);
  2239. * return std::make_pair(i, false);
  2240. * }
  2241. * ...
  2242. * void handler(const asio::error_code& e, std::size_t size);
  2243. * ...
  2244. * asio::streambuf b;
  2245. * asio::async_read_until(s, b, match_whitespace, handler);
  2246. * @endcode
  2247. *
  2248. * To asynchronously read data into a streambuf until a matching character is
  2249. * found:
  2250. * @code class match_char
  2251. * {
  2252. * public:
  2253. * explicit match_char(char c) : c_(c) {}
  2254. *
  2255. * template <typename Iterator>
  2256. * std::pair<Iterator, bool> operator()(
  2257. * Iterator begin, Iterator end) const
  2258. * {
  2259. * Iterator i = begin;
  2260. * while (i != end)
  2261. * if (c_ == *i++)
  2262. * return std::make_pair(i, true);
  2263. * return std::make_pair(i, false);
  2264. * }
  2265. *
  2266. * private:
  2267. * char c_;
  2268. * };
  2269. *
  2270. * namespace asio {
  2271. * template <> struct is_match_condition<match_char>
  2272. * : public boost::true_type {};
  2273. * } // namespace asio
  2274. * ...
  2275. * void handler(const asio::error_code& e, std::size_t size);
  2276. * ...
  2277. * asio::streambuf b;
  2278. * asio::async_read_until(s, b, match_char('a'), handler);
  2279. * @endcode
  2280. */
  2281. template <typename AsyncReadStream, typename Allocator,
  2282. typename MatchCondition, typename ReadHandler>
  2283. ASIO_INITFN_RESULT_TYPE(ReadHandler,
  2284. void (asio::error_code, std::size_t))
  2285. async_read_until(AsyncReadStream& s,
  2286. asio::basic_streambuf<Allocator>& b,
  2287. MatchCondition match_condition, ASIO_MOVE_ARG(ReadHandler) handler,
  2288. typename enable_if<is_match_condition<MatchCondition>::value>::type* = 0);
  2289. #endif // !defined(ASIO_NO_IOSTREAM)
  2290. #endif // !defined(ASIO_NO_EXTENSIONS)
  2291. #endif // !defined(ASIO_NO_DYNAMIC_BUFFER_V1)
  2292. /// Start an asynchronous operation to read data into a dynamic buffer sequence
  2293. /// until it contains a specified delimiter.
  2294. /**
  2295. * This function is used to asynchronously read data into the specified dynamic
  2296. * buffer sequence until the dynamic buffer sequence's get area contains the
  2297. * specified delimiter. The function call always returns immediately. The
  2298. * asynchronous operation will continue until one of the following conditions
  2299. * is true:
  2300. *
  2301. * @li The get area of the dynamic buffer sequence contains the specified
  2302. * delimiter.
  2303. *
  2304. * @li An error occurred.
  2305. *
  2306. * This operation is implemented in terms of zero or more calls to the stream's
  2307. * async_read_some function, and is known as a <em>composed operation</em>. If
  2308. * the dynamic buffer sequence's get area already contains the delimiter, this
  2309. * asynchronous operation completes immediately. The program must ensure that
  2310. * the stream performs no other read operations (such as async_read,
  2311. * async_read_until, the stream's async_read_some function, or any other
  2312. * composed operations that perform reads) until this operation completes.
  2313. *
  2314. * @param s The stream from which the data is to be read. The type must support
  2315. * the AsyncReadStream concept.
  2316. *
  2317. * @param buffers The dynamic buffer sequence into which the data will be read.
  2318. * Although the buffers object may be copied as necessary, ownership of the
  2319. * underlying memory blocks is retained by the caller, which must guarantee
  2320. * that they remain valid until the handler is called.
  2321. *
  2322. * @param delim The delimiter character.
  2323. *
  2324. * @param handler The handler to be called when the read operation completes.
  2325. * Copies will be made of the handler as required. The function signature of the
  2326. * handler must be:
  2327. * @code void handler(
  2328. * // Result of operation.
  2329. * const asio::error_code& error,
  2330. *
  2331. * // The number of bytes in the dynamic buffer sequence's
  2332. * // get area up to and including the delimiter.
  2333. * // 0 if an error occurred.
  2334. * std::size_t bytes_transferred
  2335. * ); @endcode
  2336. * Regardless of whether the asynchronous operation completes immediately or
  2337. * not, the handler will not be invoked from within this function. On
  2338. * immediate completion, invocation of the handler will be performed in a
  2339. * manner equivalent to using asio::post().
  2340. *
  2341. * @note After a successful async_read_until operation, the dynamic buffer
  2342. * sequence may contain additional data beyond the delimiter. An application
  2343. * will typically leave that data in the dynamic buffer sequence for a
  2344. * subsequent async_read_until operation to examine.
  2345. *
  2346. * @par Example
  2347. * To asynchronously read data into a @c std::string until a newline is
  2348. * encountered:
  2349. * @code std::string data;
  2350. * ...
  2351. * void handler(const asio::error_code& e, std::size_t size)
  2352. * {
  2353. * if (!e)
  2354. * {
  2355. * std::string line = data.substr(0, n);
  2356. * data.erase(0, n);
  2357. * ...
  2358. * }
  2359. * }
  2360. * ...
  2361. * asio::async_read_until(s, data, '\n', handler); @endcode
  2362. * After the @c async_read_until operation completes successfully, the buffer
  2363. * @c data contains the delimiter:
  2364. * @code { 'a', 'b', ..., 'c', '\n', 'd', 'e', ... } @endcode
  2365. * The call to @c substr then extracts the data up to and including the
  2366. * delimiter, so that the string @c line contains:
  2367. * @code { 'a', 'b', ..., 'c', '\n' } @endcode
  2368. * After the call to @c erase, the remaining data is left in the buffer @c data
  2369. * as follows:
  2370. * @code { 'd', 'e', ... } @endcode
  2371. * This data may be the start of a new line, to be extracted by a subsequent
  2372. * @c async_read_until operation.
  2373. */
  2374. template <typename AsyncReadStream,
  2375. typename DynamicBuffer_v2, typename ReadHandler>
  2376. ASIO_INITFN_RESULT_TYPE(ReadHandler,
  2377. void (asio::error_code, std::size_t))
  2378. async_read_until(AsyncReadStream& s, DynamicBuffer_v2 buffers,
  2379. char delim, ASIO_MOVE_ARG(ReadHandler) handler,
  2380. typename enable_if<
  2381. is_dynamic_buffer_v2<DynamicBuffer_v2>::value
  2382. >::type* = 0);
  2383. /// Start an asynchronous operation to read data into a dynamic buffer sequence
  2384. /// until it contains a specified delimiter.
  2385. /**
  2386. * This function is used to asynchronously read data into the specified dynamic
  2387. * buffer sequence until the dynamic buffer sequence's get area contains the
  2388. * specified delimiter. The function call always returns immediately. The
  2389. * asynchronous operation will continue until one of the following conditions
  2390. * is true:
  2391. *
  2392. * @li The get area of the dynamic buffer sequence contains the specified
  2393. * delimiter.
  2394. *
  2395. * @li An error occurred.
  2396. *
  2397. * This operation is implemented in terms of zero or more calls to the stream's
  2398. * async_read_some function, and is known as a <em>composed operation</em>. If
  2399. * the dynamic buffer sequence's get area already contains the delimiter, this
  2400. * asynchronous operation completes immediately. The program must ensure that
  2401. * the stream performs no other read operations (such as async_read,
  2402. * async_read_until, the stream's async_read_some function, or any other
  2403. * composed operations that perform reads) until this operation completes.
  2404. *
  2405. * @param s The stream from which the data is to be read. The type must support
  2406. * the AsyncReadStream concept.
  2407. *
  2408. * @param buffers The dynamic buffer sequence into which the data will be read.
  2409. * Although the buffers object may be copied as necessary, ownership of the
  2410. * underlying memory blocks is retained by the caller, which must guarantee
  2411. * that they remain valid until the handler is called.
  2412. *
  2413. * @param delim The delimiter string.
  2414. *
  2415. * @param handler The handler to be called when the read operation completes.
  2416. * Copies will be made of the handler as required. The function signature of the
  2417. * handler must be:
  2418. * @code void handler(
  2419. * // Result of operation.
  2420. * const asio::error_code& error,
  2421. *
  2422. * // The number of bytes in the dynamic buffer sequence's
  2423. * // get area up to and including the delimiter.
  2424. * // 0 if an error occurred.
  2425. * std::size_t bytes_transferred
  2426. * ); @endcode
  2427. * Regardless of whether the asynchronous operation completes immediately or
  2428. * not, the handler will not be invoked from within this function. On
  2429. * immediate completion, invocation of the handler will be performed in a
  2430. * manner equivalent to using asio::post().
  2431. *
  2432. * @note After a successful async_read_until operation, the dynamic buffer
  2433. * sequence may contain additional data beyond the delimiter. An application
  2434. * will typically leave that data in the dynamic buffer sequence for a
  2435. * subsequent async_read_until operation to examine.
  2436. *
  2437. * @par Example
  2438. * To asynchronously read data into a @c std::string until a CR-LF sequence is
  2439. * encountered:
  2440. * @code std::string data;
  2441. * ...
  2442. * void handler(const asio::error_code& e, std::size_t size)
  2443. * {
  2444. * if (!e)
  2445. * {
  2446. * std::string line = data.substr(0, n);
  2447. * data.erase(0, n);
  2448. * ...
  2449. * }
  2450. * }
  2451. * ...
  2452. * asio::async_read_until(s, data, "\r\n", handler); @endcode
  2453. * After the @c async_read_until operation completes successfully, the string
  2454. * @c data contains the delimiter:
  2455. * @code { 'a', 'b', ..., 'c', '\r', '\n', 'd', 'e', ... } @endcode
  2456. * The call to @c substr then extracts the data up to and including the
  2457. * delimiter, so that the string @c line contains:
  2458. * @code { 'a', 'b', ..., 'c', '\r', '\n' } @endcode
  2459. * After the call to @c erase, the remaining data is left in the string @c data
  2460. * as follows:
  2461. * @code { 'd', 'e', ... } @endcode
  2462. * This data may be the start of a new line, to be extracted by a subsequent
  2463. * @c async_read_until operation.
  2464. */
  2465. template <typename AsyncReadStream,
  2466. typename DynamicBuffer_v2, typename ReadHandler>
  2467. ASIO_INITFN_RESULT_TYPE(ReadHandler,
  2468. void (asio::error_code, std::size_t))
  2469. async_read_until(AsyncReadStream& s, DynamicBuffer_v2 buffers,
  2470. ASIO_STRING_VIEW_PARAM delim,
  2471. ASIO_MOVE_ARG(ReadHandler) handler,
  2472. typename enable_if<
  2473. is_dynamic_buffer_v2<DynamicBuffer_v2>::value
  2474. >::type* = 0);
  2475. #if !defined(ASIO_NO_EXTENSIONS)
  2476. #if defined(ASIO_HAS_BOOST_REGEX) \
  2477. || defined(GENERATING_DOCUMENTATION)
  2478. /// Start an asynchronous operation to read data into a dynamic buffer sequence
  2479. /// until some part of its data matches a regular expression.
  2480. /**
  2481. * This function is used to asynchronously read data into the specified dynamic
  2482. * buffer sequence until the dynamic buffer sequence's get area contains some
  2483. * data that matches a regular expression. The function call always returns
  2484. * immediately. The asynchronous operation will continue until one of the
  2485. * following conditions is true:
  2486. *
  2487. * @li A substring of the dynamic buffer sequence's get area matches the regular
  2488. * expression.
  2489. *
  2490. * @li An error occurred.
  2491. *
  2492. * This operation is implemented in terms of zero or more calls to the stream's
  2493. * async_read_some function, and is known as a <em>composed operation</em>. If
  2494. * the dynamic buffer sequence's get area already contains data that matches
  2495. * the regular expression, this asynchronous operation completes immediately.
  2496. * The program must ensure that the stream performs no other read operations
  2497. * (such as async_read, async_read_until, the stream's async_read_some
  2498. * function, or any other composed operations that perform reads) until this
  2499. * operation completes.
  2500. *
  2501. * @param s The stream from which the data is to be read. The type must support
  2502. * the AsyncReadStream concept.
  2503. *
  2504. * @param buffers The dynamic buffer sequence into which the data will be read.
  2505. * Although the buffers object may be copied as necessary, ownership of the
  2506. * underlying memory blocks is retained by the caller, which must guarantee
  2507. * that they remain valid until the handler is called.
  2508. *
  2509. * @param expr The regular expression.
  2510. *
  2511. * @param handler The handler to be called when the read operation completes.
  2512. * Copies will be made of the handler as required. The function signature of the
  2513. * handler must be:
  2514. * @code void handler(
  2515. * // Result of operation.
  2516. * const asio::error_code& error,
  2517. *
  2518. * // The number of bytes in the dynamic buffer
  2519. * // sequence's get area up to and including the
  2520. * // substring that matches the regular expression.
  2521. * // 0 if an error occurred.
  2522. * std::size_t bytes_transferred
  2523. * ); @endcode
  2524. * Regardless of whether the asynchronous operation completes immediately or
  2525. * not, the handler will not be invoked from within this function. On
  2526. * immediate completion, invocation of the handler will be performed in a
  2527. * manner equivalent to using asio::post().
  2528. *
  2529. * @note After a successful async_read_until operation, the dynamic buffer
  2530. * sequence may contain additional data beyond that which matched the regular
  2531. * expression. An application will typically leave that data in the dynamic
  2532. * buffer sequence for a subsequent async_read_until operation to examine.
  2533. *
  2534. * @par Example
  2535. * To asynchronously read data into a @c std::string until a CR-LF sequence is
  2536. * encountered:
  2537. * @code std::string data;
  2538. * ...
  2539. * void handler(const asio::error_code& e, std::size_t size)
  2540. * {
  2541. * if (!e)
  2542. * {
  2543. * std::string line = data.substr(0, n);
  2544. * data.erase(0, n);
  2545. * ...
  2546. * }
  2547. * }
  2548. * ...
  2549. * asio::async_read_until(s, data,
  2550. * boost::regex("\r\n"), handler); @endcode
  2551. * After the @c async_read_until operation completes successfully, the string
  2552. * @c data contains the data which matched the regular expression:
  2553. * @code { 'a', 'b', ..., 'c', '\r', '\n', 'd', 'e', ... } @endcode
  2554. * The call to @c substr then extracts the data up to and including the match,
  2555. * so that the string @c line contains:
  2556. * @code { 'a', 'b', ..., 'c', '\r', '\n' } @endcode
  2557. * After the call to @c erase, the remaining data is left in the string @c data
  2558. * as follows:
  2559. * @code { 'd', 'e', ... } @endcode
  2560. * This data may be the start of a new line, to be extracted by a subsequent
  2561. * @c async_read_until operation.
  2562. */
  2563. template <typename AsyncReadStream,
  2564. typename DynamicBuffer_v2, typename ReadHandler>
  2565. ASIO_INITFN_RESULT_TYPE(ReadHandler,
  2566. void (asio::error_code, std::size_t))
  2567. async_read_until(AsyncReadStream& s, DynamicBuffer_v2 buffers,
  2568. const boost::regex& expr,
  2569. ASIO_MOVE_ARG(ReadHandler) handler,
  2570. typename enable_if<
  2571. is_dynamic_buffer_v2<DynamicBuffer_v2>::value
  2572. >::type* = 0);
  2573. #endif // defined(ASIO_HAS_BOOST_REGEX)
  2574. // || defined(GENERATING_DOCUMENTATION)
  2575. /// Start an asynchronous operation to read data into a dynamic buffer sequence
  2576. /// until a function object indicates a match.
  2577. /**
  2578. * This function is used to asynchronously read data into the specified dynamic
  2579. * buffer sequence until a user-defined match condition function object, when
  2580. * applied to the data contained in the dynamic buffer sequence, indicates a
  2581. * successful match. The function call always returns immediately. The
  2582. * asynchronous operation will continue until one of the following conditions
  2583. * is true:
  2584. *
  2585. * @li The match condition function object returns a std::pair where the second
  2586. * element evaluates to true.
  2587. *
  2588. * @li An error occurred.
  2589. *
  2590. * This operation is implemented in terms of zero or more calls to the stream's
  2591. * async_read_some function, and is known as a <em>composed operation</em>. If
  2592. * the match condition function object already indicates a match, this
  2593. * asynchronous operation completes immediately. The program must ensure that
  2594. * the stream performs no other read operations (such as async_read,
  2595. * async_read_until, the stream's async_read_some function, or any other
  2596. * composed operations that perform reads) until this operation completes.
  2597. *
  2598. * @param s The stream from which the data is to be read. The type must support
  2599. * the AsyncReadStream concept.
  2600. *
  2601. * @param buffers The dynamic buffer sequence into which the data will be read.
  2602. * Although the buffers object may be copied as necessary, ownership of the
  2603. * underlying memory blocks is retained by the caller, which must guarantee
  2604. * that they remain valid until the handler is called.
  2605. *
  2606. * @param match_condition The function object to be called to determine whether
  2607. * a match exists. The signature of the function object must be:
  2608. * @code pair<iterator, bool> match_condition(iterator begin, iterator end);
  2609. * @endcode
  2610. * where @c iterator represents the type:
  2611. * @code buffers_iterator<typename DynamicBuffer_v2::const_buffers_type>
  2612. * @endcode
  2613. * The iterator parameters @c begin and @c end define the range of bytes to be
  2614. * scanned to determine whether there is a match. The @c first member of the
  2615. * return value is an iterator marking one-past-the-end of the bytes that have
  2616. * been consumed by the match function. This iterator is used to calculate the
  2617. * @c begin parameter for any subsequent invocation of the match condition. The
  2618. * @c second member of the return value is true if a match has been found, false
  2619. * otherwise.
  2620. *
  2621. * @param handler The handler to be called when the read operation completes.
  2622. * Copies will be made of the handler as required. The function signature of the
  2623. * handler must be:
  2624. * @code void handler(
  2625. * // Result of operation.
  2626. * const asio::error_code& error,
  2627. *
  2628. * // The number of bytes in the dynamic buffer sequence's
  2629. * // get area that have been fully consumed by the match
  2630. * // function. O if an error occurred.
  2631. * std::size_t bytes_transferred
  2632. * ); @endcode
  2633. * Regardless of whether the asynchronous operation completes immediately or
  2634. * not, the handler will not be invoked from within this function. On
  2635. * immediate completion, invocation of the handler will be performed in a
  2636. * manner equivalent to using asio::post().
  2637. *
  2638. * @note After a successful async_read_until operation, the dynamic buffer
  2639. * sequence may contain additional data beyond that which matched the function
  2640. * object. An application will typically leave that data in the dynamic buffer
  2641. * sequence for a subsequent async_read_until operation to examine.
  2642. *
  2643. * @note The default implementation of the @c is_match_condition type trait
  2644. * evaluates to true for function pointers and function objects with a
  2645. * @c result_type typedef. It must be specialised for other user-defined
  2646. * function objects.
  2647. *
  2648. * @par Examples
  2649. * To asynchronously read data into a @c std::string until whitespace is
  2650. * encountered:
  2651. * @code typedef asio::buffers_iterator<
  2652. * asio::const_buffers_1> iterator;
  2653. *
  2654. * std::pair<iterator, bool>
  2655. * match_whitespace(iterator begin, iterator end)
  2656. * {
  2657. * iterator i = begin;
  2658. * while (i != end)
  2659. * if (std::isspace(*i++))
  2660. * return std::make_pair(i, true);
  2661. * return std::make_pair(i, false);
  2662. * }
  2663. * ...
  2664. * void handler(const asio::error_code& e, std::size_t size);
  2665. * ...
  2666. * std::string data;
  2667. * asio::async_read_until(s, data, match_whitespace, handler);
  2668. * @endcode
  2669. *
  2670. * To asynchronously read data into a @c std::string until a matching character
  2671. * is found:
  2672. * @code class match_char
  2673. * {
  2674. * public:
  2675. * explicit match_char(char c) : c_(c) {}
  2676. *
  2677. * template <typename Iterator>
  2678. * std::pair<Iterator, bool> operator()(
  2679. * Iterator begin, Iterator end) const
  2680. * {
  2681. * Iterator i = begin;
  2682. * while (i != end)
  2683. * if (c_ == *i++)
  2684. * return std::make_pair(i, true);
  2685. * return std::make_pair(i, false);
  2686. * }
  2687. *
  2688. * private:
  2689. * char c_;
  2690. * };
  2691. *
  2692. * namespace asio {
  2693. * template <> struct is_match_condition<match_char>
  2694. * : public boost::true_type {};
  2695. * } // namespace asio
  2696. * ...
  2697. * void handler(const asio::error_code& e, std::size_t size);
  2698. * ...
  2699. * std::string data;
  2700. * asio::async_read_until(s, data, match_char('a'), handler);
  2701. * @endcode
  2702. */
  2703. template <typename AsyncReadStream, typename DynamicBuffer_v2,
  2704. typename MatchCondition, typename ReadHandler>
  2705. ASIO_INITFN_RESULT_TYPE(ReadHandler,
  2706. void (asio::error_code, std::size_t))
  2707. async_read_until(AsyncReadStream& s, DynamicBuffer_v2 buffers,
  2708. MatchCondition match_condition, ASIO_MOVE_ARG(ReadHandler) handler,
  2709. typename enable_if<
  2710. is_match_condition<MatchCondition>::value
  2711. && is_dynamic_buffer_v2<DynamicBuffer_v2>::value
  2712. >::type* = 0);
  2713. #endif // !defined(ASIO_NO_EXTENSIONS)
  2714. /*@}*/
  2715. } // namespace asio
  2716. #include "asio/detail/pop_options.hpp"
  2717. #include "asio/impl/read_until.hpp"
  2718. #endif // ASIO_READ_UNTIL_HPP