read_until.hpp 117 KB

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