read_until.hpp 125 KB

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