read_until.hpp 117 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901
  1. //
  2. // read_until.hpp
  3. // ~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2021 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. The function call always returns immediately. The
  1498. * asynchronous operation will continue until one of the following conditions
  1499. * 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 handler is called.
  1521. *
  1522. * @param delim The delimiter character.
  1523. *
  1524. * @param handler The handler to be called when the read operation completes.
  1525. * Copies will be made of the handler as required. The function signature of the
  1526. * handler must be:
  1527. * @code void handler(
  1528. * // Result of operation.
  1529. * const boost::system::error_code& error,
  1530. *
  1531. * // The number of bytes in the dynamic buffer sequence's
  1532. * // get area up to and including the delimiter.
  1533. * // 0 if an error occurred.
  1534. * std::size_t bytes_transferred
  1535. * ); @endcode
  1536. * Regardless of whether the asynchronous operation completes immediately or
  1537. * not, the handler will not be invoked from within this function. On
  1538. * immediate completion, invocation of the handler will be performed in a
  1539. * manner equivalent to using boost::asio::post().
  1540. *
  1541. * @note After a successful async_read_until operation, the dynamic buffer
  1542. * sequence may contain additional data beyond the delimiter. An application
  1543. * will typically leave that data in the dynamic buffer sequence for a
  1544. * subsequent async_read_until operation to examine.
  1545. *
  1546. * @par Example
  1547. * To asynchronously read data into a @c std::string until a newline is
  1548. * encountered:
  1549. * @code std::string data;
  1550. * ...
  1551. * void handler(const boost::system::error_code& e, std::size_t size)
  1552. * {
  1553. * if (!e)
  1554. * {
  1555. * std::string line = data.substr(0, n);
  1556. * data.erase(0, n);
  1557. * ...
  1558. * }
  1559. * }
  1560. * ...
  1561. * boost::asio::async_read_until(s, data, '\n', handler); @endcode
  1562. * After the @c async_read_until operation completes successfully, the buffer
  1563. * @c data contains the delimiter:
  1564. * @code { 'a', 'b', ..., 'c', '\n', 'd', 'e', ... } @endcode
  1565. * The call to @c substr then extracts the data up to and including the
  1566. * delimiter, so that the string @c line contains:
  1567. * @code { 'a', 'b', ..., 'c', '\n' } @endcode
  1568. * After the call to @c erase, the remaining data is left in the buffer @c data
  1569. * as follows:
  1570. * @code { 'd', 'e', ... } @endcode
  1571. * This data may be the start of a new line, to be extracted by a subsequent
  1572. * @c async_read_until operation.
  1573. */
  1574. template <typename AsyncReadStream, typename DynamicBuffer_v1,
  1575. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  1576. std::size_t)) ReadHandler
  1577. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(
  1578. typename AsyncReadStream::executor_type)>
  1579. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(ReadHandler,
  1580. void (boost::system::error_code, std::size_t))
  1581. async_read_until(AsyncReadStream& s,
  1582. BOOST_ASIO_MOVE_ARG(DynamicBuffer_v1) buffers, char delim,
  1583. BOOST_ASIO_MOVE_ARG(ReadHandler) handler
  1584. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(
  1585. typename AsyncReadStream::executor_type),
  1586. typename constraint<
  1587. is_dynamic_buffer_v1<typename decay<DynamicBuffer_v1>::type>::value
  1588. >::type = 0,
  1589. typename constraint<
  1590. !is_dynamic_buffer_v2<typename decay<DynamicBuffer_v1>::type>::value
  1591. >::type = 0);
  1592. /// Start an asynchronous operation to read data into a dynamic buffer sequence
  1593. /// until it contains a specified delimiter.
  1594. /**
  1595. * This function is used to asynchronously read data into the specified dynamic
  1596. * buffer sequence until the dynamic buffer sequence's get area contains the
  1597. * specified delimiter. The function call always returns immediately. The
  1598. * asynchronous operation will continue until one of the following conditions
  1599. * is true:
  1600. *
  1601. * @li The get area of the dynamic buffer sequence contains the specified
  1602. * delimiter.
  1603. *
  1604. * @li An error occurred.
  1605. *
  1606. * This operation is implemented in terms of zero or more calls to the stream's
  1607. * async_read_some function, and is known as a <em>composed operation</em>. If
  1608. * the dynamic buffer sequence's get area already contains the delimiter, this
  1609. * asynchronous operation completes immediately. The program must ensure that
  1610. * the stream performs no other read operations (such as async_read,
  1611. * async_read_until, the stream's async_read_some function, or any other
  1612. * composed operations that perform reads) until this operation completes.
  1613. *
  1614. * @param s The stream from which the data is to be read. The type must support
  1615. * the AsyncReadStream concept.
  1616. *
  1617. * @param buffers The dynamic buffer sequence into which the data will be read.
  1618. * Although the buffers object may be copied as necessary, ownership of the
  1619. * underlying memory blocks is retained by the caller, which must guarantee
  1620. * that they remain valid until the handler is called.
  1621. *
  1622. * @param delim The delimiter string.
  1623. *
  1624. * @param handler The handler to be called when the read operation completes.
  1625. * Copies will be made of the handler as required. The function signature of the
  1626. * handler must be:
  1627. * @code void handler(
  1628. * // Result of operation.
  1629. * const boost::system::error_code& error,
  1630. *
  1631. * // The number of bytes in the dynamic buffer sequence's
  1632. * // get area up to and including the delimiter.
  1633. * // 0 if an error occurred.
  1634. * std::size_t bytes_transferred
  1635. * ); @endcode
  1636. * Regardless of whether the asynchronous operation completes immediately or
  1637. * not, the handler will not be invoked from within this function. On
  1638. * immediate completion, invocation of the handler will be performed in a
  1639. * manner equivalent to using boost::asio::post().
  1640. *
  1641. * @note After a successful async_read_until operation, the dynamic buffer
  1642. * sequence may contain additional data beyond the delimiter. An application
  1643. * will typically leave that data in the dynamic buffer sequence for a
  1644. * subsequent async_read_until operation to examine.
  1645. *
  1646. * @par Example
  1647. * To asynchronously read data into a @c std::string until a CR-LF sequence is
  1648. * encountered:
  1649. * @code std::string data;
  1650. * ...
  1651. * void handler(const boost::system::error_code& e, std::size_t size)
  1652. * {
  1653. * if (!e)
  1654. * {
  1655. * std::string line = data.substr(0, n);
  1656. * data.erase(0, n);
  1657. * ...
  1658. * }
  1659. * }
  1660. * ...
  1661. * boost::asio::async_read_until(s, data, "\r\n", handler); @endcode
  1662. * After the @c async_read_until operation completes successfully, the string
  1663. * @c data contains the delimiter:
  1664. * @code { 'a', 'b', ..., 'c', '\r', '\n', 'd', 'e', ... } @endcode
  1665. * The call to @c substr then extracts the data up to and including the
  1666. * delimiter, so that the string @c line contains:
  1667. * @code { 'a', 'b', ..., 'c', '\r', '\n' } @endcode
  1668. * After the call to @c erase, the remaining data is left in the string @c data
  1669. * as follows:
  1670. * @code { 'd', 'e', ... } @endcode
  1671. * This data may be the start of a new line, to be extracted by a subsequent
  1672. * @c async_read_until operation.
  1673. */
  1674. template <typename AsyncReadStream, typename DynamicBuffer_v1,
  1675. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  1676. std::size_t)) ReadHandler
  1677. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(
  1678. typename AsyncReadStream::executor_type)>
  1679. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(ReadHandler,
  1680. void (boost::system::error_code, std::size_t))
  1681. async_read_until(AsyncReadStream& s,
  1682. BOOST_ASIO_MOVE_ARG(DynamicBuffer_v1) buffers,
  1683. BOOST_ASIO_STRING_VIEW_PARAM delim,
  1684. BOOST_ASIO_MOVE_ARG(ReadHandler) handler
  1685. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(
  1686. typename AsyncReadStream::executor_type),
  1687. typename constraint<
  1688. is_dynamic_buffer_v1<typename decay<DynamicBuffer_v1>::type>::value
  1689. >::type = 0,
  1690. typename constraint<
  1691. !is_dynamic_buffer_v2<typename decay<DynamicBuffer_v1>::type>::value
  1692. >::type = 0);
  1693. #if !defined(BOOST_ASIO_NO_EXTENSIONS)
  1694. #if defined(BOOST_ASIO_HAS_BOOST_REGEX) \
  1695. || defined(GENERATING_DOCUMENTATION)
  1696. /// Start an asynchronous operation to read data into a dynamic buffer sequence
  1697. /// until some part of its data matches a regular expression.
  1698. /**
  1699. * This function is used to asynchronously read data into the specified dynamic
  1700. * buffer sequence until the dynamic buffer sequence's get area contains some
  1701. * data that matches a regular expression. The function call always returns
  1702. * immediately. The asynchronous operation will continue until one of the
  1703. * following conditions is true:
  1704. *
  1705. * @li A substring of the dynamic buffer sequence's get area matches the regular
  1706. * expression.
  1707. *
  1708. * @li An error occurred.
  1709. *
  1710. * This operation is implemented in terms of zero or more calls to the stream's
  1711. * async_read_some function, and is known as a <em>composed operation</em>. If
  1712. * the dynamic buffer sequence's get area already contains data that matches
  1713. * the regular expression, this asynchronous operation completes immediately.
  1714. * The program must ensure that the stream performs no other read operations
  1715. * (such as async_read, async_read_until, the stream's async_read_some
  1716. * function, or any other composed operations that perform reads) until this
  1717. * operation completes.
  1718. *
  1719. * @param s The stream from which the data is to be read. The type must support
  1720. * the AsyncReadStream concept.
  1721. *
  1722. * @param buffers The dynamic buffer sequence into which the data will be read.
  1723. * Although the buffers object may be copied as necessary, ownership of the
  1724. * underlying memory blocks is retained by the caller, which must guarantee
  1725. * that they remain valid until the handler is called.
  1726. *
  1727. * @param expr The regular expression.
  1728. *
  1729. * @param handler The handler to be called when the read operation completes.
  1730. * Copies will be made of the handler as required. The function signature of the
  1731. * handler must be:
  1732. * @code void handler(
  1733. * // Result of operation.
  1734. * const boost::system::error_code& error,
  1735. *
  1736. * // The number of bytes in the dynamic buffer
  1737. * // sequence's get area up to and including the
  1738. * // substring that matches the regular expression.
  1739. * // 0 if an error occurred.
  1740. * std::size_t bytes_transferred
  1741. * ); @endcode
  1742. * Regardless of whether the asynchronous operation completes immediately or
  1743. * not, the handler will not be invoked from within this function. On
  1744. * immediate completion, invocation of the handler will be performed in a
  1745. * manner equivalent to using boost::asio::post().
  1746. *
  1747. * @note After a successful async_read_until operation, the dynamic buffer
  1748. * sequence may contain additional data beyond that which matched the regular
  1749. * expression. An application will typically leave that data in the dynamic
  1750. * buffer sequence for a subsequent async_read_until operation to examine.
  1751. *
  1752. * @par Example
  1753. * To asynchronously read data into a @c std::string until a CR-LF sequence is
  1754. * encountered:
  1755. * @code std::string data;
  1756. * ...
  1757. * void handler(const boost::system::error_code& e, std::size_t size)
  1758. * {
  1759. * if (!e)
  1760. * {
  1761. * std::string line = data.substr(0, n);
  1762. * data.erase(0, n);
  1763. * ...
  1764. * }
  1765. * }
  1766. * ...
  1767. * boost::asio::async_read_until(s, data,
  1768. * boost::regex("\r\n"), handler); @endcode
  1769. * After the @c async_read_until operation completes successfully, the string
  1770. * @c data contains the data which matched the regular expression:
  1771. * @code { 'a', 'b', ..., 'c', '\r', '\n', 'd', 'e', ... } @endcode
  1772. * The call to @c substr then extracts the data up to and including the match,
  1773. * so that the string @c line contains:
  1774. * @code { 'a', 'b', ..., 'c', '\r', '\n' } @endcode
  1775. * After the call to @c erase, the remaining data is left in the string @c data
  1776. * as follows:
  1777. * @code { 'd', 'e', ... } @endcode
  1778. * This data may be the start of a new line, to be extracted by a subsequent
  1779. * @c async_read_until operation.
  1780. */
  1781. template <typename AsyncReadStream, typename DynamicBuffer_v1,
  1782. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  1783. std::size_t)) ReadHandler
  1784. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(
  1785. typename AsyncReadStream::executor_type)>
  1786. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(ReadHandler,
  1787. void (boost::system::error_code, std::size_t))
  1788. async_read_until(AsyncReadStream& s,
  1789. BOOST_ASIO_MOVE_ARG(DynamicBuffer_v1) buffers,
  1790. const boost::regex& expr,
  1791. BOOST_ASIO_MOVE_ARG(ReadHandler) handler
  1792. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(
  1793. typename AsyncReadStream::executor_type),
  1794. typename constraint<
  1795. is_dynamic_buffer_v1<typename decay<DynamicBuffer_v1>::type>::value
  1796. >::type = 0,
  1797. typename constraint<
  1798. !is_dynamic_buffer_v2<typename decay<DynamicBuffer_v1>::type>::value
  1799. >::type = 0);
  1800. #endif // defined(BOOST_ASIO_HAS_BOOST_REGEX)
  1801. // || defined(GENERATING_DOCUMENTATION)
  1802. /// Start an asynchronous operation to read data into a dynamic buffer sequence
  1803. /// until a function object indicates a match.
  1804. /**
  1805. * This function is used to asynchronously read data into the specified dynamic
  1806. * buffer sequence until a user-defined match condition function object, when
  1807. * applied to the data contained in the dynamic buffer sequence, indicates a
  1808. * successful match. The function call always returns immediately. The
  1809. * asynchronous operation will continue until one of the following conditions
  1810. * is true:
  1811. *
  1812. * @li The match condition function object returns a std::pair where the second
  1813. * element evaluates to true.
  1814. *
  1815. * @li An error occurred.
  1816. *
  1817. * This operation is implemented in terms of zero or more calls to the stream's
  1818. * async_read_some function, and is known as a <em>composed operation</em>. If
  1819. * the match condition function object already indicates a match, this
  1820. * asynchronous operation completes immediately. The program must ensure that
  1821. * the stream performs no other read operations (such as async_read,
  1822. * async_read_until, the stream's async_read_some function, or any other
  1823. * composed operations that perform reads) until this operation completes.
  1824. *
  1825. * @param s The stream from which the data is to be read. The type must support
  1826. * the AsyncReadStream concept.
  1827. *
  1828. * @param buffers The dynamic buffer sequence into which the data will be read.
  1829. * Although the buffers object may be copied as necessary, ownership of the
  1830. * underlying memory blocks is retained by the caller, which must guarantee
  1831. * that they remain valid until the handler is called.
  1832. *
  1833. * @param match_condition The function object to be called to determine whether
  1834. * a match exists. The signature of the function object must be:
  1835. * @code pair<iterator, bool> match_condition(iterator begin, iterator end);
  1836. * @endcode
  1837. * where @c iterator represents the type:
  1838. * @code buffers_iterator<typename DynamicBuffer_v1::const_buffers_type>
  1839. * @endcode
  1840. * The iterator parameters @c begin and @c end define the range of bytes to be
  1841. * scanned to determine whether there is a match. The @c first member of the
  1842. * return value is an iterator marking one-past-the-end of the bytes that have
  1843. * been consumed by the match function. This iterator is used to calculate the
  1844. * @c begin parameter for any subsequent invocation of the match condition. The
  1845. * @c second member of the return value is true if a match has been found, false
  1846. * otherwise.
  1847. *
  1848. * @param handler The handler to be called when the read operation completes.
  1849. * Copies will be made of the handler as required. The function signature of the
  1850. * handler must be:
  1851. * @code void handler(
  1852. * // Result of operation.
  1853. * const boost::system::error_code& error,
  1854. *
  1855. * // The number of bytes in the dynamic buffer sequence's
  1856. * // get area that have been fully consumed by the match
  1857. * // function. O if an error occurred.
  1858. * std::size_t bytes_transferred
  1859. * ); @endcode
  1860. * Regardless of whether the asynchronous operation completes immediately or
  1861. * not, the handler will not be invoked from within this function. On
  1862. * immediate completion, invocation of the handler will be performed in a
  1863. * manner equivalent to using boost::asio::post().
  1864. *
  1865. * @note After a successful async_read_until operation, the dynamic buffer
  1866. * sequence may contain additional data beyond that which matched the function
  1867. * object. An application will typically leave that data in the dynamic buffer
  1868. * sequence for a subsequent async_read_until operation to examine.
  1869. *
  1870. * @note The default implementation of the @c is_match_condition type trait
  1871. * evaluates to true for function pointers and function objects with a
  1872. * @c result_type typedef. It must be specialised for other user-defined
  1873. * function objects.
  1874. *
  1875. * @par Examples
  1876. * To asynchronously read data into a @c std::string until whitespace is
  1877. * encountered:
  1878. * @code typedef boost::asio::buffers_iterator<
  1879. * boost::asio::const_buffers_1> iterator;
  1880. *
  1881. * std::pair<iterator, bool>
  1882. * match_whitespace(iterator begin, iterator end)
  1883. * {
  1884. * iterator i = begin;
  1885. * while (i != end)
  1886. * if (std::isspace(*i++))
  1887. * return std::make_pair(i, true);
  1888. * return std::make_pair(i, false);
  1889. * }
  1890. * ...
  1891. * void handler(const boost::system::error_code& e, std::size_t size);
  1892. * ...
  1893. * std::string data;
  1894. * boost::asio::async_read_until(s, data, match_whitespace, handler);
  1895. * @endcode
  1896. *
  1897. * To asynchronously read data into a @c std::string until a matching character
  1898. * is found:
  1899. * @code class match_char
  1900. * {
  1901. * public:
  1902. * explicit match_char(char c) : c_(c) {}
  1903. *
  1904. * template <typename Iterator>
  1905. * std::pair<Iterator, bool> operator()(
  1906. * Iterator begin, Iterator end) const
  1907. * {
  1908. * Iterator i = begin;
  1909. * while (i != end)
  1910. * if (c_ == *i++)
  1911. * return std::make_pair(i, true);
  1912. * return std::make_pair(i, false);
  1913. * }
  1914. *
  1915. * private:
  1916. * char c_;
  1917. * };
  1918. *
  1919. * namespace asio {
  1920. * template <> struct is_match_condition<match_char>
  1921. * : public boost::true_type {};
  1922. * } // namespace asio
  1923. * ...
  1924. * void handler(const boost::system::error_code& e, std::size_t size);
  1925. * ...
  1926. * std::string data;
  1927. * boost::asio::async_read_until(s, data, match_char('a'), handler);
  1928. * @endcode
  1929. */
  1930. template <typename AsyncReadStream,
  1931. typename DynamicBuffer_v1, typename MatchCondition,
  1932. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  1933. std::size_t)) ReadHandler
  1934. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(
  1935. typename AsyncReadStream::executor_type)>
  1936. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(ReadHandler,
  1937. void (boost::system::error_code, std::size_t))
  1938. async_read_until(AsyncReadStream& s,
  1939. BOOST_ASIO_MOVE_ARG(DynamicBuffer_v1) buffers,
  1940. MatchCondition match_condition,
  1941. BOOST_ASIO_MOVE_ARG(ReadHandler) handler
  1942. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(
  1943. typename AsyncReadStream::executor_type),
  1944. typename constraint<
  1945. is_match_condition<MatchCondition>::value
  1946. >::type = 0,
  1947. typename constraint<
  1948. is_dynamic_buffer_v1<typename decay<DynamicBuffer_v1>::type>::value
  1949. >::type = 0,
  1950. typename constraint<
  1951. !is_dynamic_buffer_v2<typename decay<DynamicBuffer_v1>::type>::value
  1952. >::type = 0);
  1953. #if !defined(BOOST_ASIO_NO_IOSTREAM)
  1954. /// Start an asynchronous operation to read data into a streambuf until it
  1955. /// contains a specified delimiter.
  1956. /**
  1957. * This function is used to asynchronously read data into the specified
  1958. * streambuf until the streambuf's get area contains the specified delimiter.
  1959. * The function call always returns immediately. The asynchronous operation
  1960. * will continue until one of the following conditions is true:
  1961. *
  1962. * @li The get area of the streambuf contains the specified delimiter.
  1963. *
  1964. * @li An error occurred.
  1965. *
  1966. * This operation is implemented in terms of zero or more calls to the stream's
  1967. * async_read_some function, and is known as a <em>composed operation</em>. If
  1968. * the streambuf's get area already contains the delimiter, this asynchronous
  1969. * operation completes immediately. The program must ensure that the stream
  1970. * performs no other read operations (such as async_read, async_read_until, the
  1971. * stream's async_read_some function, or any other composed operations that
  1972. * perform reads) until this operation completes.
  1973. *
  1974. * @param s The stream from which the data is to be read. The type must support
  1975. * the AsyncReadStream concept.
  1976. *
  1977. * @param b A streambuf object into which the data will be read. Ownership of
  1978. * the streambuf is retained by the caller, which must guarantee that it remains
  1979. * valid until the handler is called.
  1980. *
  1981. * @param delim The delimiter character.
  1982. *
  1983. * @param handler The handler to be called when the read operation completes.
  1984. * Copies will be made of the handler as required. The function signature of the
  1985. * handler must be:
  1986. * @code void handler(
  1987. * // Result of operation.
  1988. * const boost::system::error_code& error,
  1989. *
  1990. * // The number of bytes in the streambuf's get
  1991. * // area up to and including the delimiter.
  1992. * // 0 if an error occurred.
  1993. * std::size_t bytes_transferred
  1994. * ); @endcode
  1995. * Regardless of whether the asynchronous operation completes immediately or
  1996. * not, the handler will not be invoked from within this function. On
  1997. * immediate completion, invocation of the handler will be performed in a
  1998. * manner equivalent to using boost::asio::post().
  1999. *
  2000. * @note After a successful async_read_until operation, the streambuf may
  2001. * contain additional data beyond the delimiter. An application will typically
  2002. * leave that data in the streambuf for a subsequent async_read_until operation
  2003. * to examine.
  2004. *
  2005. * @par Example
  2006. * To asynchronously read data into a streambuf until a newline is encountered:
  2007. * @code boost::asio::streambuf b;
  2008. * ...
  2009. * void handler(const boost::system::error_code& e, std::size_t size)
  2010. * {
  2011. * if (!e)
  2012. * {
  2013. * std::istream is(&b);
  2014. * std::string line;
  2015. * std::getline(is, line);
  2016. * ...
  2017. * }
  2018. * }
  2019. * ...
  2020. * boost::asio::async_read_until(s, b, '\n', handler); @endcode
  2021. * After the @c async_read_until operation completes successfully, the buffer
  2022. * @c b contains the delimiter:
  2023. * @code { 'a', 'b', ..., 'c', '\n', 'd', 'e', ... } @endcode
  2024. * The call to @c std::getline then extracts the data up to and including the
  2025. * newline (which is discarded), so that the string @c line contains:
  2026. * @code { 'a', 'b', ..., 'c' } @endcode
  2027. * The remaining data is left in the buffer @c b as follows:
  2028. * @code { 'd', 'e', ... } @endcode
  2029. * This data may be the start of a new line, to be extracted by a subsequent
  2030. * @c async_read_until operation.
  2031. */
  2032. template <typename AsyncReadStream, typename Allocator,
  2033. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  2034. std::size_t)) ReadHandler
  2035. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(
  2036. typename AsyncReadStream::executor_type)>
  2037. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(ReadHandler,
  2038. void (boost::system::error_code, std::size_t))
  2039. async_read_until(AsyncReadStream& s,
  2040. boost::asio::basic_streambuf<Allocator>& b, char delim,
  2041. BOOST_ASIO_MOVE_ARG(ReadHandler) handler
  2042. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(
  2043. typename AsyncReadStream::executor_type));
  2044. /// Start an asynchronous operation to read data into a streambuf until it
  2045. /// contains a specified delimiter.
  2046. /**
  2047. * This function is used to asynchronously read data into the specified
  2048. * streambuf until the streambuf's get area contains the specified delimiter.
  2049. * The function call always returns immediately. The asynchronous operation
  2050. * will continue until one of the following conditions is true:
  2051. *
  2052. * @li The get area of the streambuf contains the specified delimiter.
  2053. *
  2054. * @li An error occurred.
  2055. *
  2056. * This operation is implemented in terms of zero or more calls to the stream's
  2057. * async_read_some function, and is known as a <em>composed operation</em>. If
  2058. * the streambuf's get area already contains the delimiter, this asynchronous
  2059. * operation completes immediately. The program must ensure that the stream
  2060. * performs no other read operations (such as async_read, async_read_until, the
  2061. * stream's async_read_some function, or any other composed operations that
  2062. * perform reads) until this operation completes.
  2063. *
  2064. * @param s The stream from which the data is to be read. The type must support
  2065. * the AsyncReadStream concept.
  2066. *
  2067. * @param b A streambuf object into which the data will be read. Ownership of
  2068. * the streambuf is retained by the caller, which must guarantee that it remains
  2069. * valid until the handler is called.
  2070. *
  2071. * @param delim The delimiter string.
  2072. *
  2073. * @param handler The handler to be called when the read operation completes.
  2074. * Copies will be made of the handler as required. The function signature of the
  2075. * handler must be:
  2076. * @code void handler(
  2077. * // Result of operation.
  2078. * const boost::system::error_code& error,
  2079. *
  2080. * // The number of bytes in the streambuf's get
  2081. * // area up to and including the delimiter.
  2082. * // 0 if an error occurred.
  2083. * std::size_t bytes_transferred
  2084. * ); @endcode
  2085. * Regardless of whether the asynchronous operation completes immediately or
  2086. * not, the handler will not be invoked from within this function. On
  2087. * immediate completion, invocation of the handler will be performed in a
  2088. * manner equivalent to using boost::asio::post().
  2089. *
  2090. * @note After a successful async_read_until operation, the streambuf may
  2091. * contain additional data beyond the delimiter. An application will typically
  2092. * leave that data in the streambuf for a subsequent async_read_until operation
  2093. * to examine.
  2094. *
  2095. * @par Example
  2096. * To asynchronously read data into a streambuf until a newline is encountered:
  2097. * @code boost::asio::streambuf b;
  2098. * ...
  2099. * void handler(const boost::system::error_code& e, std::size_t size)
  2100. * {
  2101. * if (!e)
  2102. * {
  2103. * std::istream is(&b);
  2104. * std::string line;
  2105. * std::getline(is, line);
  2106. * ...
  2107. * }
  2108. * }
  2109. * ...
  2110. * boost::asio::async_read_until(s, b, "\r\n", handler); @endcode
  2111. * After the @c async_read_until operation completes successfully, the buffer
  2112. * @c b contains the delimiter:
  2113. * @code { 'a', 'b', ..., 'c', '\r', '\n', 'd', 'e', ... } @endcode
  2114. * The call to @c std::getline then extracts the data up to and including the
  2115. * newline (which is discarded), so that the string @c line contains:
  2116. * @code { 'a', 'b', ..., 'c', '\r' } @endcode
  2117. * The remaining data is left in the buffer @c b as follows:
  2118. * @code { 'd', 'e', ... } @endcode
  2119. * This data may be the start of a new line, to be extracted by a subsequent
  2120. * @c async_read_until operation.
  2121. */
  2122. template <typename AsyncReadStream, typename Allocator,
  2123. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  2124. std::size_t)) ReadHandler
  2125. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(
  2126. typename AsyncReadStream::executor_type)>
  2127. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(ReadHandler,
  2128. void (boost::system::error_code, std::size_t))
  2129. async_read_until(AsyncReadStream& s,
  2130. boost::asio::basic_streambuf<Allocator>& b,
  2131. BOOST_ASIO_STRING_VIEW_PARAM delim,
  2132. BOOST_ASIO_MOVE_ARG(ReadHandler) handler
  2133. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(
  2134. typename AsyncReadStream::executor_type));
  2135. #if defined(BOOST_ASIO_HAS_BOOST_REGEX) \
  2136. || defined(GENERATING_DOCUMENTATION)
  2137. /// Start an asynchronous operation to read data into a streambuf until some
  2138. /// part of its data matches a regular expression.
  2139. /**
  2140. * This function is used to asynchronously read data into the specified
  2141. * streambuf until the streambuf's get area contains some data that matches a
  2142. * regular expression. The function call always returns immediately. The
  2143. * asynchronous operation will continue until one of the following conditions
  2144. * is true:
  2145. *
  2146. * @li A substring of the streambuf's get area matches the regular expression.
  2147. *
  2148. * @li An error occurred.
  2149. *
  2150. * This operation is implemented in terms of zero or more calls to the stream's
  2151. * async_read_some function, and is known as a <em>composed operation</em>. If
  2152. * the streambuf's get area already contains data that matches the regular
  2153. * expression, this asynchronous operation completes immediately. The program
  2154. * must ensure that the stream performs no other read operations (such as
  2155. * async_read, async_read_until, the stream's async_read_some function, or any
  2156. * other composed operations that perform reads) until this operation
  2157. * completes.
  2158. *
  2159. * @param s The stream from which the data is to be read. The type must support
  2160. * the AsyncReadStream concept.
  2161. *
  2162. * @param b A streambuf object into which the data will be read. Ownership of
  2163. * the streambuf is retained by the caller, which must guarantee that it remains
  2164. * valid until the handler is called.
  2165. *
  2166. * @param expr The regular expression.
  2167. *
  2168. * @param handler The handler to be called when the read operation completes.
  2169. * Copies will be made of the handler as required. The function signature of the
  2170. * handler must be:
  2171. * @code void handler(
  2172. * // Result of operation.
  2173. * const boost::system::error_code& error,
  2174. *
  2175. * // The number of bytes in the streambuf's get
  2176. * // area up to and including the substring
  2177. * // that matches the regular. expression.
  2178. * // 0 if an error occurred.
  2179. * std::size_t bytes_transferred
  2180. * ); @endcode
  2181. * Regardless of whether the asynchronous operation completes immediately or
  2182. * not, the handler will not be invoked from within this function. On
  2183. * immediate completion, invocation of the handler will be performed in a
  2184. * manner equivalent to using boost::asio::post().
  2185. *
  2186. * @note After a successful async_read_until operation, the streambuf may
  2187. * contain additional data beyond that which matched the regular expression. An
  2188. * application will typically leave that data in the streambuf for a subsequent
  2189. * async_read_until operation to examine.
  2190. *
  2191. * @par Example
  2192. * To asynchronously read data into a streambuf until a CR-LF sequence is
  2193. * encountered:
  2194. * @code boost::asio::streambuf b;
  2195. * ...
  2196. * void handler(const boost::system::error_code& e, std::size_t size)
  2197. * {
  2198. * if (!e)
  2199. * {
  2200. * std::istream is(&b);
  2201. * std::string line;
  2202. * std::getline(is, line);
  2203. * ...
  2204. * }
  2205. * }
  2206. * ...
  2207. * boost::asio::async_read_until(s, b, boost::regex("\r\n"), handler); @endcode
  2208. * After the @c async_read_until operation completes successfully, the buffer
  2209. * @c b contains the data which matched the regular expression:
  2210. * @code { 'a', 'b', ..., 'c', '\r', '\n', 'd', 'e', ... } @endcode
  2211. * The call to @c std::getline then extracts the data up to and including the
  2212. * newline (which is discarded), so that the string @c line contains:
  2213. * @code { 'a', 'b', ..., 'c', '\r' } @endcode
  2214. * The remaining data is left in the buffer @c b as follows:
  2215. * @code { 'd', 'e', ... } @endcode
  2216. * This data may be the start of a new line, to be extracted by a subsequent
  2217. * @c async_read_until operation.
  2218. */
  2219. template <typename AsyncReadStream, typename Allocator,
  2220. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  2221. std::size_t)) ReadHandler
  2222. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(
  2223. typename AsyncReadStream::executor_type)>
  2224. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(ReadHandler,
  2225. void (boost::system::error_code, std::size_t))
  2226. async_read_until(AsyncReadStream& s,
  2227. boost::asio::basic_streambuf<Allocator>& b, const boost::regex& expr,
  2228. BOOST_ASIO_MOVE_ARG(ReadHandler) handler
  2229. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(
  2230. typename AsyncReadStream::executor_type));
  2231. #endif // defined(BOOST_ASIO_HAS_BOOST_REGEX)
  2232. // || defined(GENERATING_DOCUMENTATION)
  2233. /// Start an asynchronous operation to read data into a streambuf until a
  2234. /// function object indicates a match.
  2235. /**
  2236. * This function is used to asynchronously read data into the specified
  2237. * streambuf until a user-defined match condition function object, when applied
  2238. * to the data contained in the streambuf, indicates a successful match. The
  2239. * function call always returns immediately. The asynchronous operation will
  2240. * continue until one of the following conditions is true:
  2241. *
  2242. * @li The match condition function object returns a std::pair where the second
  2243. * element evaluates to true.
  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 match condition function object already indicates a match, this
  2250. * asynchronous operation completes immediately. The program must ensure that
  2251. * the stream performs no other read operations (such as async_read,
  2252. * async_read_until, the stream's async_read_some function, or any other
  2253. * composed operations that perform reads) until this operation completes.
  2254. *
  2255. * @param s The stream from which the data is to be read. The type must support
  2256. * the AsyncReadStream concept.
  2257. *
  2258. * @param b A streambuf object into which the data will be read.
  2259. *
  2260. * @param match_condition The function object to be called to determine whether
  2261. * a match exists. The signature of the function object must be:
  2262. * @code pair<iterator, bool> match_condition(iterator begin, iterator end);
  2263. * @endcode
  2264. * where @c iterator represents the type:
  2265. * @code buffers_iterator<basic_streambuf<Allocator>::const_buffers_type>
  2266. * @endcode
  2267. * The iterator parameters @c begin and @c end define the range of bytes to be
  2268. * scanned to determine whether there is a match. The @c first member of the
  2269. * return value is an iterator marking one-past-the-end of the bytes that have
  2270. * been consumed by the match function. This iterator is used to calculate the
  2271. * @c begin parameter for any subsequent invocation of the match condition. The
  2272. * @c second member of the return value is true if a match has been found, false
  2273. * otherwise.
  2274. *
  2275. * @param handler The handler to be called when the read operation completes.
  2276. * Copies will be made of the handler as required. The function signature of the
  2277. * handler must be:
  2278. * @code void handler(
  2279. * // Result of operation.
  2280. * const boost::system::error_code& error,
  2281. *
  2282. * // The number of bytes in the streambuf's get
  2283. * // area that have been fully consumed by the
  2284. * // match function. O if an error occurred.
  2285. * std::size_t bytes_transferred
  2286. * ); @endcode
  2287. * Regardless of whether the asynchronous operation completes immediately or
  2288. * not, the handler will not be invoked from within this function. On
  2289. * immediate completion, invocation of the handler will be performed in a
  2290. * manner equivalent to using boost::asio::post().
  2291. *
  2292. * @note After a successful async_read_until operation, the streambuf may
  2293. * contain additional data beyond that which matched the function object. An
  2294. * application will typically leave that data in the streambuf for a subsequent
  2295. * async_read_until operation to examine.
  2296. *
  2297. * @note The default implementation of the @c is_match_condition type trait
  2298. * evaluates to true for function pointers and function objects with a
  2299. * @c result_type typedef. It must be specialised for other user-defined
  2300. * function objects.
  2301. *
  2302. * @par Examples
  2303. * To asynchronously read data into a streambuf until whitespace is encountered:
  2304. * @code typedef boost::asio::buffers_iterator<
  2305. * boost::asio::streambuf::const_buffers_type> iterator;
  2306. *
  2307. * std::pair<iterator, bool>
  2308. * match_whitespace(iterator begin, iterator end)
  2309. * {
  2310. * iterator i = begin;
  2311. * while (i != end)
  2312. * if (std::isspace(*i++))
  2313. * return std::make_pair(i, true);
  2314. * return std::make_pair(i, false);
  2315. * }
  2316. * ...
  2317. * void handler(const boost::system::error_code& e, std::size_t size);
  2318. * ...
  2319. * boost::asio::streambuf b;
  2320. * boost::asio::async_read_until(s, b, match_whitespace, handler);
  2321. * @endcode
  2322. *
  2323. * To asynchronously read data into a streambuf until a matching character is
  2324. * found:
  2325. * @code class match_char
  2326. * {
  2327. * public:
  2328. * explicit match_char(char c) : c_(c) {}
  2329. *
  2330. * template <typename Iterator>
  2331. * std::pair<Iterator, bool> operator()(
  2332. * Iterator begin, Iterator end) const
  2333. * {
  2334. * Iterator i = begin;
  2335. * while (i != end)
  2336. * if (c_ == *i++)
  2337. * return std::make_pair(i, true);
  2338. * return std::make_pair(i, false);
  2339. * }
  2340. *
  2341. * private:
  2342. * char c_;
  2343. * };
  2344. *
  2345. * namespace asio {
  2346. * template <> struct is_match_condition<match_char>
  2347. * : public boost::true_type {};
  2348. * } // namespace asio
  2349. * ...
  2350. * void handler(const boost::system::error_code& e, std::size_t size);
  2351. * ...
  2352. * boost::asio::streambuf b;
  2353. * boost::asio::async_read_until(s, b, match_char('a'), handler);
  2354. * @endcode
  2355. */
  2356. template <typename AsyncReadStream, typename Allocator, typename MatchCondition,
  2357. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  2358. std::size_t)) ReadHandler
  2359. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(
  2360. typename AsyncReadStream::executor_type)>
  2361. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(ReadHandler,
  2362. void (boost::system::error_code, std::size_t))
  2363. async_read_until(AsyncReadStream& s,
  2364. boost::asio::basic_streambuf<Allocator>& b,
  2365. MatchCondition match_condition,
  2366. BOOST_ASIO_MOVE_ARG(ReadHandler) handler
  2367. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(
  2368. typename AsyncReadStream::executor_type),
  2369. typename constraint<is_match_condition<MatchCondition>::value>::type = 0);
  2370. #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
  2371. #endif // !defined(BOOST_ASIO_NO_EXTENSIONS)
  2372. #endif // !defined(BOOST_ASIO_NO_DYNAMIC_BUFFER_V1)
  2373. /// Start an asynchronous operation to read data into a dynamic buffer sequence
  2374. /// until it contains a specified delimiter.
  2375. /**
  2376. * This function is used to asynchronously read data into the specified dynamic
  2377. * buffer sequence until the dynamic buffer sequence's get area contains the
  2378. * specified delimiter. The function call always returns immediately. The
  2379. * asynchronous operation will continue until one of the following conditions
  2380. * is true:
  2381. *
  2382. * @li The get area of the dynamic buffer sequence contains the specified
  2383. * delimiter.
  2384. *
  2385. * @li An error occurred.
  2386. *
  2387. * This operation is implemented in terms of zero or more calls to the stream's
  2388. * async_read_some function, and is known as a <em>composed operation</em>. If
  2389. * the dynamic buffer sequence's get area already contains the delimiter, this
  2390. * asynchronous operation completes immediately. The program must ensure that
  2391. * the stream performs no other read operations (such as async_read,
  2392. * async_read_until, the stream's async_read_some function, or any other
  2393. * composed operations that perform reads) until this operation completes.
  2394. *
  2395. * @param s The stream from which the data is to be read. The type must support
  2396. * the AsyncReadStream concept.
  2397. *
  2398. * @param buffers The dynamic buffer sequence into which the data will be read.
  2399. * Although the buffers object may be copied as necessary, ownership of the
  2400. * underlying memory blocks is retained by the caller, which must guarantee
  2401. * that they remain valid until the handler is called.
  2402. *
  2403. * @param delim The delimiter character.
  2404. *
  2405. * @param handler The handler to be called when the read operation completes.
  2406. * Copies will be made of the handler as required. The function signature of the
  2407. * handler must be:
  2408. * @code void handler(
  2409. * // Result of operation.
  2410. * const boost::system::error_code& error,
  2411. *
  2412. * // The number of bytes in the dynamic buffer sequence's
  2413. * // get area up to and including the delimiter.
  2414. * // 0 if an error occurred.
  2415. * std::size_t bytes_transferred
  2416. * ); @endcode
  2417. * Regardless of whether the asynchronous operation completes immediately or
  2418. * not, the handler will not be invoked from within this function. On
  2419. * immediate completion, invocation of the handler will be performed in a
  2420. * manner equivalent to using boost::asio::post().
  2421. *
  2422. * @note After a successful async_read_until operation, the dynamic buffer
  2423. * sequence may contain additional data beyond the delimiter. An application
  2424. * will typically leave that data in the dynamic buffer sequence for a
  2425. * subsequent async_read_until operation to examine.
  2426. *
  2427. * @par Example
  2428. * To asynchronously read data into a @c std::string until a newline is
  2429. * encountered:
  2430. * @code std::string data;
  2431. * ...
  2432. * void handler(const boost::system::error_code& e, std::size_t size)
  2433. * {
  2434. * if (!e)
  2435. * {
  2436. * std::string line = data.substr(0, n);
  2437. * data.erase(0, n);
  2438. * ...
  2439. * }
  2440. * }
  2441. * ...
  2442. * boost::asio::async_read_until(s, data, '\n', handler); @endcode
  2443. * After the @c async_read_until operation completes successfully, the buffer
  2444. * @c data contains the delimiter:
  2445. * @code { 'a', 'b', ..., 'c', '\n', 'd', 'e', ... } @endcode
  2446. * The call to @c substr then extracts the data up to and including the
  2447. * delimiter, so that the string @c line contains:
  2448. * @code { 'a', 'b', ..., 'c', '\n' } @endcode
  2449. * After the call to @c erase, the remaining data is left in the buffer @c data
  2450. * as follows:
  2451. * @code { 'd', 'e', ... } @endcode
  2452. * This data may be the start of a new line, to be extracted by a subsequent
  2453. * @c async_read_until operation.
  2454. */
  2455. template <typename AsyncReadStream, typename DynamicBuffer_v2,
  2456. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  2457. std::size_t)) ReadHandler
  2458. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(
  2459. typename AsyncReadStream::executor_type)>
  2460. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(ReadHandler,
  2461. void (boost::system::error_code, std::size_t))
  2462. async_read_until(AsyncReadStream& s, DynamicBuffer_v2 buffers, char delim,
  2463. BOOST_ASIO_MOVE_ARG(ReadHandler) handler
  2464. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(
  2465. typename AsyncReadStream::executor_type),
  2466. typename constraint<
  2467. is_dynamic_buffer_v2<DynamicBuffer_v2>::value
  2468. >::type = 0);
  2469. /// Start an asynchronous operation to read data into a dynamic buffer sequence
  2470. /// until it contains a specified delimiter.
  2471. /**
  2472. * This function is used to asynchronously read data into the specified dynamic
  2473. * buffer sequence until the dynamic buffer sequence's get area contains the
  2474. * specified delimiter. The function call always returns immediately. The
  2475. * asynchronous operation will continue until one of the following conditions
  2476. * is true:
  2477. *
  2478. * @li The get area of the dynamic buffer sequence contains the specified
  2479. * delimiter.
  2480. *
  2481. * @li An error occurred.
  2482. *
  2483. * This operation is implemented in terms of zero or more calls to the stream's
  2484. * async_read_some function, and is known as a <em>composed operation</em>. If
  2485. * the dynamic buffer sequence's get area already contains the delimiter, this
  2486. * asynchronous operation completes immediately. The program must ensure that
  2487. * the stream performs no other read operations (such as async_read,
  2488. * async_read_until, the stream's async_read_some function, or any other
  2489. * composed operations that perform reads) until this operation completes.
  2490. *
  2491. * @param s The stream from which the data is to be read. The type must support
  2492. * the AsyncReadStream concept.
  2493. *
  2494. * @param buffers The dynamic buffer sequence into which the data will be read.
  2495. * Although the buffers object may be copied as necessary, ownership of the
  2496. * underlying memory blocks is retained by the caller, which must guarantee
  2497. * that they remain valid until the handler is called.
  2498. *
  2499. * @param delim The delimiter string.
  2500. *
  2501. * @param handler The handler to be called when the read operation completes.
  2502. * Copies will be made of the handler as required. The function signature of the
  2503. * handler must be:
  2504. * @code void handler(
  2505. * // Result of operation.
  2506. * const boost::system::error_code& error,
  2507. *
  2508. * // The number of bytes in the dynamic buffer sequence's
  2509. * // get area up to and including the delimiter.
  2510. * // 0 if an error occurred.
  2511. * std::size_t bytes_transferred
  2512. * ); @endcode
  2513. * Regardless of whether the asynchronous operation completes immediately or
  2514. * not, the handler will not be invoked from within this function. On
  2515. * immediate completion, invocation of the handler will be performed in a
  2516. * manner equivalent to using boost::asio::post().
  2517. *
  2518. * @note After a successful async_read_until operation, the dynamic buffer
  2519. * sequence may contain additional data beyond the delimiter. An application
  2520. * will typically leave that data in the dynamic buffer sequence for a
  2521. * subsequent async_read_until operation to examine.
  2522. *
  2523. * @par Example
  2524. * To asynchronously read data into a @c std::string until a CR-LF sequence is
  2525. * encountered:
  2526. * @code std::string data;
  2527. * ...
  2528. * void handler(const boost::system::error_code& e, std::size_t size)
  2529. * {
  2530. * if (!e)
  2531. * {
  2532. * std::string line = data.substr(0, n);
  2533. * data.erase(0, n);
  2534. * ...
  2535. * }
  2536. * }
  2537. * ...
  2538. * boost::asio::async_read_until(s, data, "\r\n", handler); @endcode
  2539. * After the @c async_read_until operation completes successfully, the string
  2540. * @c data contains the delimiter:
  2541. * @code { 'a', 'b', ..., 'c', '\r', '\n', 'd', 'e', ... } @endcode
  2542. * The call to @c substr then extracts the data up to and including the
  2543. * delimiter, so that the string @c line contains:
  2544. * @code { 'a', 'b', ..., 'c', '\r', '\n' } @endcode
  2545. * After the call to @c erase, the remaining data is left in the string @c data
  2546. * as follows:
  2547. * @code { 'd', 'e', ... } @endcode
  2548. * This data may be the start of a new line, to be extracted by a subsequent
  2549. * @c async_read_until operation.
  2550. */
  2551. template <typename AsyncReadStream, typename DynamicBuffer_v2,
  2552. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  2553. std::size_t)) ReadHandler
  2554. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(
  2555. typename AsyncReadStream::executor_type)>
  2556. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(ReadHandler,
  2557. void (boost::system::error_code, std::size_t))
  2558. async_read_until(AsyncReadStream& s, DynamicBuffer_v2 buffers,
  2559. BOOST_ASIO_STRING_VIEW_PARAM delim,
  2560. BOOST_ASIO_MOVE_ARG(ReadHandler) handler
  2561. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(
  2562. typename AsyncReadStream::executor_type),
  2563. typename constraint<
  2564. is_dynamic_buffer_v2<DynamicBuffer_v2>::value
  2565. >::type = 0);
  2566. #if !defined(BOOST_ASIO_NO_EXTENSIONS)
  2567. #if defined(BOOST_ASIO_HAS_BOOST_REGEX) \
  2568. || defined(GENERATING_DOCUMENTATION)
  2569. /// Start an asynchronous operation to read data into a dynamic buffer sequence
  2570. /// until some part of its data matches a regular expression.
  2571. /**
  2572. * This function is used to asynchronously read data into the specified dynamic
  2573. * buffer sequence until the dynamic buffer sequence's get area contains some
  2574. * data that matches a regular expression. The function call always returns
  2575. * immediately. The asynchronous operation will continue until one of the
  2576. * following conditions is true:
  2577. *
  2578. * @li A substring of the dynamic buffer sequence's get area matches the regular
  2579. * expression.
  2580. *
  2581. * @li An error occurred.
  2582. *
  2583. * This operation is implemented in terms of zero or more calls to the stream's
  2584. * async_read_some function, and is known as a <em>composed operation</em>. If
  2585. * the dynamic buffer sequence's get area already contains data that matches
  2586. * the regular expression, this asynchronous operation completes immediately.
  2587. * The program must ensure that the stream performs no other read operations
  2588. * (such as async_read, async_read_until, the stream's async_read_some
  2589. * function, or any other composed operations that perform reads) until this
  2590. * operation completes.
  2591. *
  2592. * @param s The stream from which the data is to be read. The type must support
  2593. * the AsyncReadStream concept.
  2594. *
  2595. * @param buffers The dynamic buffer sequence into which the data will be read.
  2596. * Although the buffers object may be copied as necessary, ownership of the
  2597. * underlying memory blocks is retained by the caller, which must guarantee
  2598. * that they remain valid until the handler is called.
  2599. *
  2600. * @param expr The regular expression.
  2601. *
  2602. * @param handler The handler to be called when the read operation completes.
  2603. * Copies will be made of the handler as required. The function signature of the
  2604. * handler must be:
  2605. * @code void handler(
  2606. * // Result of operation.
  2607. * const boost::system::error_code& error,
  2608. *
  2609. * // The number of bytes in the dynamic buffer
  2610. * // sequence's get area up to and including the
  2611. * // substring that matches the regular expression.
  2612. * // 0 if an error occurred.
  2613. * std::size_t bytes_transferred
  2614. * ); @endcode
  2615. * Regardless of whether the asynchronous operation completes immediately or
  2616. * not, the handler will not be invoked from within this function. On
  2617. * immediate completion, invocation of the handler will be performed in a
  2618. * manner equivalent to using boost::asio::post().
  2619. *
  2620. * @note After a successful async_read_until operation, the dynamic buffer
  2621. * sequence may contain additional data beyond that which matched the regular
  2622. * expression. An application will typically leave that data in the dynamic
  2623. * buffer sequence for a subsequent async_read_until operation to examine.
  2624. *
  2625. * @par Example
  2626. * To asynchronously read data into a @c std::string until a CR-LF sequence is
  2627. * encountered:
  2628. * @code std::string data;
  2629. * ...
  2630. * void handler(const boost::system::error_code& e, std::size_t size)
  2631. * {
  2632. * if (!e)
  2633. * {
  2634. * std::string line = data.substr(0, n);
  2635. * data.erase(0, n);
  2636. * ...
  2637. * }
  2638. * }
  2639. * ...
  2640. * boost::asio::async_read_until(s, data,
  2641. * boost::regex("\r\n"), handler); @endcode
  2642. * After the @c async_read_until operation completes successfully, the string
  2643. * @c data contains the data which matched the regular expression:
  2644. * @code { 'a', 'b', ..., 'c', '\r', '\n', 'd', 'e', ... } @endcode
  2645. * The call to @c substr then extracts the data up to and including the match,
  2646. * so that the string @c line contains:
  2647. * @code { 'a', 'b', ..., 'c', '\r', '\n' } @endcode
  2648. * After the call to @c erase, the remaining data is left in the string @c data
  2649. * as follows:
  2650. * @code { 'd', 'e', ... } @endcode
  2651. * This data may be the start of a new line, to be extracted by a subsequent
  2652. * @c async_read_until operation.
  2653. */
  2654. template <typename AsyncReadStream, typename DynamicBuffer_v2,
  2655. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  2656. std::size_t)) ReadHandler
  2657. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(
  2658. typename AsyncReadStream::executor_type)>
  2659. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(ReadHandler,
  2660. void (boost::system::error_code, std::size_t))
  2661. async_read_until(AsyncReadStream& s, DynamicBuffer_v2 buffers,
  2662. const boost::regex& expr,
  2663. BOOST_ASIO_MOVE_ARG(ReadHandler) handler
  2664. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(
  2665. typename AsyncReadStream::executor_type),
  2666. typename constraint<
  2667. is_dynamic_buffer_v2<DynamicBuffer_v2>::value
  2668. >::type = 0);
  2669. #endif // defined(BOOST_ASIO_HAS_BOOST_REGEX)
  2670. // || defined(GENERATING_DOCUMENTATION)
  2671. /// Start an asynchronous operation to read data into a dynamic buffer sequence
  2672. /// until a function object indicates a match.
  2673. /**
  2674. * This function is used to asynchronously read data into the specified dynamic
  2675. * buffer sequence until a user-defined match condition function object, when
  2676. * applied to the data contained in the dynamic buffer sequence, indicates a
  2677. * successful match. The function call always returns immediately. The
  2678. * asynchronous operation will continue until one of the following conditions
  2679. * is true:
  2680. *
  2681. * @li The match condition function object returns a std::pair where the second
  2682. * element evaluates to true.
  2683. *
  2684. * @li An error occurred.
  2685. *
  2686. * This operation is implemented in terms of zero or more calls to the stream's
  2687. * async_read_some function, and is known as a <em>composed operation</em>. If
  2688. * the match condition function object already indicates a match, this
  2689. * asynchronous operation completes immediately. The program must ensure that
  2690. * the stream performs no other read operations (such as async_read,
  2691. * async_read_until, the stream's async_read_some function, or any other
  2692. * composed operations that perform reads) until this operation completes.
  2693. *
  2694. * @param s The stream from which the data is to be read. The type must support
  2695. * the AsyncReadStream concept.
  2696. *
  2697. * @param buffers The dynamic buffer sequence into which the data will be read.
  2698. * Although the buffers object may be copied as necessary, ownership of the
  2699. * underlying memory blocks is retained by the caller, which must guarantee
  2700. * that they remain valid until the handler is called.
  2701. *
  2702. * @param match_condition The function object to be called to determine whether
  2703. * a match exists. The signature of the function object must be:
  2704. * @code pair<iterator, bool> match_condition(iterator begin, iterator end);
  2705. * @endcode
  2706. * where @c iterator represents the type:
  2707. * @code buffers_iterator<typename DynamicBuffer_v2::const_buffers_type>
  2708. * @endcode
  2709. * The iterator parameters @c begin and @c end define the range of bytes to be
  2710. * scanned to determine whether there is a match. The @c first member of the
  2711. * return value is an iterator marking one-past-the-end of the bytes that have
  2712. * been consumed by the match function. This iterator is used to calculate the
  2713. * @c begin parameter for any subsequent invocation of the match condition. The
  2714. * @c second member of the return value is true if a match has been found, false
  2715. * otherwise.
  2716. *
  2717. * @param handler The handler to be called when the read operation completes.
  2718. * Copies will be made of the handler as required. The function signature of the
  2719. * handler must be:
  2720. * @code void handler(
  2721. * // Result of operation.
  2722. * const boost::system::error_code& error,
  2723. *
  2724. * // The number of bytes in the dynamic buffer sequence's
  2725. * // get area that have been fully consumed by the match
  2726. * // function. O if an error occurred.
  2727. * std::size_t bytes_transferred
  2728. * ); @endcode
  2729. * Regardless of whether the asynchronous operation completes immediately or
  2730. * not, the handler will not be invoked from within this function. On
  2731. * immediate completion, invocation of the handler will be performed in a
  2732. * manner equivalent to using boost::asio::post().
  2733. *
  2734. * @note After a successful async_read_until operation, the dynamic buffer
  2735. * sequence may contain additional data beyond that which matched the function
  2736. * object. An application will typically leave that data in the dynamic buffer
  2737. * sequence for a subsequent async_read_until operation to examine.
  2738. *
  2739. * @note The default implementation of the @c is_match_condition type trait
  2740. * evaluates to true for function pointers and function objects with a
  2741. * @c result_type typedef. It must be specialised for other user-defined
  2742. * function objects.
  2743. *
  2744. * @par Examples
  2745. * To asynchronously read data into a @c std::string until whitespace is
  2746. * encountered:
  2747. * @code typedef boost::asio::buffers_iterator<
  2748. * boost::asio::const_buffers_1> iterator;
  2749. *
  2750. * std::pair<iterator, bool>
  2751. * match_whitespace(iterator begin, iterator end)
  2752. * {
  2753. * iterator i = begin;
  2754. * while (i != end)
  2755. * if (std::isspace(*i++))
  2756. * return std::make_pair(i, true);
  2757. * return std::make_pair(i, false);
  2758. * }
  2759. * ...
  2760. * void handler(const boost::system::error_code& e, std::size_t size);
  2761. * ...
  2762. * std::string data;
  2763. * boost::asio::async_read_until(s, data, match_whitespace, handler);
  2764. * @endcode
  2765. *
  2766. * To asynchronously read data into a @c std::string until a matching character
  2767. * is found:
  2768. * @code class match_char
  2769. * {
  2770. * public:
  2771. * explicit match_char(char c) : c_(c) {}
  2772. *
  2773. * template <typename Iterator>
  2774. * std::pair<Iterator, bool> operator()(
  2775. * Iterator begin, Iterator end) const
  2776. * {
  2777. * Iterator i = begin;
  2778. * while (i != end)
  2779. * if (c_ == *i++)
  2780. * return std::make_pair(i, true);
  2781. * return std::make_pair(i, false);
  2782. * }
  2783. *
  2784. * private:
  2785. * char c_;
  2786. * };
  2787. *
  2788. * namespace asio {
  2789. * template <> struct is_match_condition<match_char>
  2790. * : public boost::true_type {};
  2791. * } // namespace asio
  2792. * ...
  2793. * void handler(const boost::system::error_code& e, std::size_t size);
  2794. * ...
  2795. * std::string data;
  2796. * boost::asio::async_read_until(s, data, match_char('a'), handler);
  2797. * @endcode
  2798. */
  2799. template <typename AsyncReadStream,
  2800. typename DynamicBuffer_v2, typename MatchCondition,
  2801. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  2802. std::size_t)) ReadHandler
  2803. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(
  2804. typename AsyncReadStream::executor_type)>
  2805. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(ReadHandler,
  2806. void (boost::system::error_code, std::size_t))
  2807. async_read_until(AsyncReadStream& s, DynamicBuffer_v2 buffers,
  2808. MatchCondition match_condition,
  2809. BOOST_ASIO_MOVE_ARG(ReadHandler) handler
  2810. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(
  2811. typename AsyncReadStream::executor_type),
  2812. typename constraint<
  2813. is_match_condition<MatchCondition>::value
  2814. >::type = 0,
  2815. typename constraint<
  2816. is_dynamic_buffer_v2<DynamicBuffer_v2>::value
  2817. >::type = 0);
  2818. #endif // !defined(BOOST_ASIO_NO_EXTENSIONS)
  2819. /*@}*/
  2820. } // namespace asio
  2821. } // namespace boost
  2822. #include <boost/asio/detail/pop_options.hpp>
  2823. #include <boost/asio/impl/read_until.hpp>
  2824. #endif // BOOST_ASIO_READ_UNTIL_HPP