read_until.hpp 123 KB

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