read_until.hpp 113 KB

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