istream 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP_ISTREAM
  10. #define _LIBCPP_ISTREAM
  11. /*
  12. istream synopsis
  13. template <class charT, class traits = char_traits<charT> >
  14. class basic_istream
  15. : virtual public basic_ios<charT,traits>
  16. {
  17. public:
  18. // types (inherited from basic_ios (27.5.4)):
  19. typedef charT char_type;
  20. typedef traits traits_type;
  21. typedef typename traits_type::int_type int_type;
  22. typedef typename traits_type::pos_type pos_type;
  23. typedef typename traits_type::off_type off_type;
  24. // 27.7.1.1.1 Constructor/destructor:
  25. explicit basic_istream(basic_streambuf<char_type, traits_type>* sb);
  26. basic_istream(basic_istream&& rhs);
  27. virtual ~basic_istream();
  28. // 27.7.1.1.2 Assign/swap:
  29. basic_istream& operator=(basic_istream&& rhs);
  30. void swap(basic_istream& rhs);
  31. // 27.7.1.1.3 Prefix/suffix:
  32. class sentry;
  33. // 27.7.1.2 Formatted input:
  34. basic_istream& operator>>(basic_istream& (*pf)(basic_istream&));
  35. basic_istream& operator>>(basic_ios<char_type, traits_type>&
  36. (*pf)(basic_ios<char_type, traits_type>&));
  37. basic_istream& operator>>(ios_base& (*pf)(ios_base&));
  38. basic_istream& operator>>(basic_streambuf<char_type, traits_type>* sb);
  39. basic_istream& operator>>(bool& n);
  40. basic_istream& operator>>(short& n);
  41. basic_istream& operator>>(unsigned short& n);
  42. basic_istream& operator>>(int& n);
  43. basic_istream& operator>>(unsigned int& n);
  44. basic_istream& operator>>(long& n);
  45. basic_istream& operator>>(unsigned long& n);
  46. basic_istream& operator>>(long long& n);
  47. basic_istream& operator>>(unsigned long long& n);
  48. basic_istream& operator>>(float& f);
  49. basic_istream& operator>>(double& f);
  50. basic_istream& operator>>(long double& f);
  51. basic_istream& operator>>(void*& p);
  52. // 27.7.1.3 Unformatted input:
  53. streamsize gcount() const;
  54. int_type get();
  55. basic_istream& get(char_type& c);
  56. basic_istream& get(char_type* s, streamsize n);
  57. basic_istream& get(char_type* s, streamsize n, char_type delim);
  58. basic_istream& get(basic_streambuf<char_type,traits_type>& sb);
  59. basic_istream& get(basic_streambuf<char_type,traits_type>& sb, char_type delim);
  60. basic_istream& getline(char_type* s, streamsize n);
  61. basic_istream& getline(char_type* s, streamsize n, char_type delim);
  62. basic_istream& ignore(streamsize n = 1, int_type delim = traits_type::eof());
  63. int_type peek();
  64. basic_istream& read (char_type* s, streamsize n);
  65. streamsize readsome(char_type* s, streamsize n);
  66. basic_istream& putback(char_type c);
  67. basic_istream& unget();
  68. int sync();
  69. pos_type tellg();
  70. basic_istream& seekg(pos_type);
  71. basic_istream& seekg(off_type, ios_base::seekdir);
  72. protected:
  73. basic_istream(const basic_istream& rhs) = delete;
  74. basic_istream(basic_istream&& rhs);
  75. // 27.7.2.1.2 Assign/swap:
  76. basic_istream& operator=(const basic_istream& rhs) = delete;
  77. basic_istream& operator=(basic_istream&& rhs);
  78. void swap(basic_istream& rhs);
  79. };
  80. // 27.7.1.2.3 character extraction templates:
  81. template<class charT, class traits>
  82. basic_istream<charT,traits>& operator>>(basic_istream<charT,traits>&, charT&);
  83. template<class traits>
  84. basic_istream<char,traits>& operator>>(basic_istream<char,traits>&, unsigned char&);
  85. template<class traits>
  86. basic_istream<char,traits>& operator>>(basic_istream<char,traits>&, signed char&);
  87. template<class charT, class traits>
  88. basic_istream<charT,traits>& operator>>(basic_istream<charT,traits>&, charT*);
  89. template<class traits>
  90. basic_istream<char,traits>& operator>>(basic_istream<char,traits>&, unsigned char*);
  91. template<class traits>
  92. basic_istream<char,traits>& operator>>(basic_istream<char,traits>&, signed char*);
  93. template <class charT, class traits>
  94. void
  95. swap(basic_istream<charT, traits>& x, basic_istream<charT, traits>& y);
  96. typedef basic_istream<char> istream;
  97. typedef basic_istream<wchar_t> wistream;
  98. template <class charT, class traits = char_traits<charT> >
  99. class basic_iostream :
  100. public basic_istream<charT,traits>,
  101. public basic_ostream<charT,traits>
  102. {
  103. public:
  104. // types:
  105. typedef charT char_type;
  106. typedef traits traits_type;
  107. typedef typename traits_type::int_type int_type;
  108. typedef typename traits_type::pos_type pos_type;
  109. typedef typename traits_type::off_type off_type;
  110. // constructor/destructor
  111. explicit basic_iostream(basic_streambuf<char_type, traits_type>* sb);
  112. basic_iostream(basic_iostream&& rhs);
  113. virtual ~basic_iostream();
  114. // assign/swap
  115. basic_iostream& operator=(basic_iostream&& rhs);
  116. void swap(basic_iostream& rhs);
  117. };
  118. template <class charT, class traits>
  119. void
  120. swap(basic_iostream<charT, traits>& x, basic_iostream<charT, traits>& y);
  121. typedef basic_iostream<char> iostream;
  122. typedef basic_iostream<wchar_t> wiostream;
  123. template <class charT, class traits>
  124. basic_istream<charT,traits>&
  125. ws(basic_istream<charT,traits>& is);
  126. // rvalue stream extraction
  127. template <class Stream, class T>
  128. Stream&& operator>>(Stream&& is, T&& x);
  129. } // std
  130. */
  131. #include <__assert> // all public C++ headers provide the assertion handler
  132. #include <__config>
  133. #include <__iterator/istreambuf_iterator.h>
  134. #include <__utility/forward.h>
  135. #include <ostream>
  136. #include <version>
  137. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  138. # pragma GCC system_header
  139. #endif
  140. _LIBCPP_PUSH_MACROS
  141. #include <__undef_macros>
  142. _LIBCPP_BEGIN_NAMESPACE_STD
  143. template <class _CharT, class _Traits>
  144. class _LIBCPP_TEMPLATE_VIS basic_istream
  145. : virtual public basic_ios<_CharT, _Traits>
  146. {
  147. streamsize __gc_;
  148. public:
  149. // types (inherited from basic_ios (27.5.4)):
  150. typedef _CharT char_type;
  151. typedef _Traits traits_type;
  152. typedef typename traits_type::int_type int_type;
  153. typedef typename traits_type::pos_type pos_type;
  154. typedef typename traits_type::off_type off_type;
  155. // 27.7.1.1.1 Constructor/destructor:
  156. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
  157. explicit basic_istream(basic_streambuf<char_type, traits_type>* __sb) : __gc_(0)
  158. { this->init(__sb); }
  159. virtual ~basic_istream();
  160. protected:
  161. inline _LIBCPP_INLINE_VISIBILITY
  162. basic_istream(basic_istream&& __rhs);
  163. // 27.7.1.1.2 Assign/swap:
  164. inline _LIBCPP_INLINE_VISIBILITY
  165. basic_istream& operator=(basic_istream&& __rhs);
  166. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
  167. void swap(basic_istream& __rhs) {
  168. _VSTD::swap(__gc_, __rhs.__gc_);
  169. basic_ios<char_type, traits_type>::swap(__rhs);
  170. }
  171. basic_istream (const basic_istream& __rhs) = delete;
  172. basic_istream& operator=(const basic_istream& __rhs) = delete;
  173. public:
  174. // 27.7.1.1.3 Prefix/suffix:
  175. class _LIBCPP_TEMPLATE_VIS sentry;
  176. // 27.7.1.2 Formatted input:
  177. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
  178. basic_istream& operator>>(basic_istream& (*__pf)(basic_istream&))
  179. { return __pf(*this); }
  180. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
  181. basic_istream& operator>>(basic_ios<char_type, traits_type>&
  182. (*__pf)(basic_ios<char_type, traits_type>&))
  183. { __pf(*this); return *this; }
  184. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
  185. basic_istream& operator>>(ios_base& (*__pf)(ios_base&))
  186. { __pf(*this); return *this; }
  187. basic_istream& operator>>(basic_streambuf<char_type, traits_type>* __sb);
  188. basic_istream& operator>>(bool& __n);
  189. basic_istream& operator>>(short& __n);
  190. basic_istream& operator>>(unsigned short& __n);
  191. basic_istream& operator>>(int& __n);
  192. basic_istream& operator>>(unsigned int& __n);
  193. basic_istream& operator>>(long& __n);
  194. basic_istream& operator>>(unsigned long& __n);
  195. basic_istream& operator>>(long long& __n);
  196. basic_istream& operator>>(unsigned long long& __n);
  197. basic_istream& operator>>(float& __f);
  198. basic_istream& operator>>(double& __f);
  199. basic_istream& operator>>(long double& __f);
  200. basic_istream& operator>>(void*& __p);
  201. // 27.7.1.3 Unformatted input:
  202. _LIBCPP_INLINE_VISIBILITY
  203. streamsize gcount() const {return __gc_;}
  204. int_type get();
  205. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
  206. basic_istream& get(char_type& __c) {
  207. int_type __ch = get();
  208. if (__ch != traits_type::eof())
  209. __c = traits_type::to_char_type(__ch);
  210. return *this;
  211. }
  212. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
  213. basic_istream& get(char_type* __s, streamsize __n)
  214. { return get(__s, __n, this->widen('\n')); }
  215. basic_istream& get(char_type* __s, streamsize __n, char_type __dlm);
  216. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
  217. basic_istream& get(basic_streambuf<char_type, traits_type>& __sb)
  218. { return get(__sb, this->widen('\n')); }
  219. basic_istream& get(basic_streambuf<char_type, traits_type>& __sb, char_type __dlm);
  220. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
  221. basic_istream& getline(char_type* __s, streamsize __n)
  222. { return getline(__s, __n, this->widen('\n')); }
  223. basic_istream& getline(char_type* __s, streamsize __n, char_type __dlm);
  224. basic_istream& ignore(streamsize __n = 1, int_type __dlm = traits_type::eof());
  225. int_type peek();
  226. basic_istream& read (char_type* __s, streamsize __n);
  227. streamsize readsome(char_type* __s, streamsize __n);
  228. basic_istream& putback(char_type __c);
  229. basic_istream& unget();
  230. int sync();
  231. pos_type tellg();
  232. basic_istream& seekg(pos_type __pos);
  233. basic_istream& seekg(off_type __off, ios_base::seekdir __dir);
  234. };
  235. template <class _CharT, class _Traits>
  236. class _LIBCPP_TEMPLATE_VIS basic_istream<_CharT, _Traits>::sentry
  237. {
  238. bool __ok_;
  239. public:
  240. explicit sentry(basic_istream<_CharT, _Traits>& __is, bool __noskipws = false);
  241. // ~sentry() = default;
  242. _LIBCPP_INLINE_VISIBILITY
  243. explicit operator bool() const {return __ok_;}
  244. sentry(const sentry&) = delete;
  245. sentry& operator=(const sentry&) = delete;
  246. };
  247. template <class _CharT, class _Traits>
  248. basic_istream<_CharT, _Traits>::sentry::sentry(basic_istream<_CharT, _Traits>& __is,
  249. bool __noskipws)
  250. : __ok_(false)
  251. {
  252. if (__is.good())
  253. {
  254. if (__is.tie())
  255. __is.tie()->flush();
  256. if (!__noskipws && (__is.flags() & ios_base::skipws))
  257. {
  258. typedef istreambuf_iterator<_CharT, _Traits> _Ip;
  259. const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__is.getloc());
  260. _Ip __i(__is);
  261. _Ip __eof;
  262. for (; __i != __eof; ++__i)
  263. if (!__ct.is(__ct.space, *__i))
  264. break;
  265. if (__i == __eof)
  266. __is.setstate(ios_base::failbit | ios_base::eofbit);
  267. }
  268. __ok_ = __is.good();
  269. }
  270. else
  271. __is.setstate(ios_base::failbit);
  272. }
  273. template <class _CharT, class _Traits>
  274. basic_istream<_CharT, _Traits>::basic_istream(basic_istream&& __rhs)
  275. : __gc_(__rhs.__gc_)
  276. {
  277. __rhs.__gc_ = 0;
  278. this->move(__rhs);
  279. }
  280. template <class _CharT, class _Traits>
  281. basic_istream<_CharT, _Traits>&
  282. basic_istream<_CharT, _Traits>::operator=(basic_istream&& __rhs)
  283. {
  284. swap(__rhs);
  285. return *this;
  286. }
  287. template <class _CharT, class _Traits>
  288. basic_istream<_CharT, _Traits>::~basic_istream()
  289. {
  290. }
  291. template <class _Tp, class _CharT, class _Traits>
  292. _LIBCPP_INLINE_VISIBILITY
  293. basic_istream<_CharT, _Traits>&
  294. __input_arithmetic(basic_istream<_CharT, _Traits>& __is, _Tp& __n) {
  295. ios_base::iostate __state = ios_base::goodbit;
  296. typename basic_istream<_CharT, _Traits>::sentry __s(__is);
  297. if (__s)
  298. {
  299. #ifndef _LIBCPP_NO_EXCEPTIONS
  300. try
  301. {
  302. #endif // _LIBCPP_NO_EXCEPTIONS
  303. typedef istreambuf_iterator<_CharT, _Traits> _Ip;
  304. typedef num_get<_CharT, _Ip> _Fp;
  305. use_facet<_Fp>(__is.getloc()).get(_Ip(__is), _Ip(), __is, __state, __n);
  306. #ifndef _LIBCPP_NO_EXCEPTIONS
  307. }
  308. catch (...)
  309. {
  310. __state |= ios_base::badbit;
  311. __is.__setstate_nothrow(__state);
  312. if (__is.exceptions() & ios_base::badbit)
  313. {
  314. throw;
  315. }
  316. }
  317. #endif
  318. __is.setstate(__state);
  319. }
  320. return __is;
  321. }
  322. template <class _CharT, class _Traits>
  323. basic_istream<_CharT, _Traits>&
  324. basic_istream<_CharT, _Traits>::operator>>(unsigned short& __n)
  325. {
  326. return _VSTD::__input_arithmetic<unsigned short>(*this, __n);
  327. }
  328. template <class _CharT, class _Traits>
  329. basic_istream<_CharT, _Traits>&
  330. basic_istream<_CharT, _Traits>::operator>>(unsigned int& __n)
  331. {
  332. return _VSTD::__input_arithmetic<unsigned int>(*this, __n);
  333. }
  334. template <class _CharT, class _Traits>
  335. basic_istream<_CharT, _Traits>&
  336. basic_istream<_CharT, _Traits>::operator>>(long& __n)
  337. {
  338. return _VSTD::__input_arithmetic<long>(*this, __n);
  339. }
  340. template <class _CharT, class _Traits>
  341. basic_istream<_CharT, _Traits>&
  342. basic_istream<_CharT, _Traits>::operator>>(unsigned long& __n)
  343. {
  344. return _VSTD::__input_arithmetic<unsigned long>(*this, __n);
  345. }
  346. template <class _CharT, class _Traits>
  347. basic_istream<_CharT, _Traits>&
  348. basic_istream<_CharT, _Traits>::operator>>(long long& __n)
  349. {
  350. return _VSTD::__input_arithmetic<long long>(*this, __n);
  351. }
  352. template <class _CharT, class _Traits>
  353. basic_istream<_CharT, _Traits>&
  354. basic_istream<_CharT, _Traits>::operator>>(unsigned long long& __n)
  355. {
  356. return _VSTD::__input_arithmetic<unsigned long long>(*this, __n);
  357. }
  358. template <class _CharT, class _Traits>
  359. basic_istream<_CharT, _Traits>&
  360. basic_istream<_CharT, _Traits>::operator>>(float& __n)
  361. {
  362. return _VSTD::__input_arithmetic<float>(*this, __n);
  363. }
  364. template <class _CharT, class _Traits>
  365. basic_istream<_CharT, _Traits>&
  366. basic_istream<_CharT, _Traits>::operator>>(double& __n)
  367. {
  368. return _VSTD::__input_arithmetic<double>(*this, __n);
  369. }
  370. template <class _CharT, class _Traits>
  371. basic_istream<_CharT, _Traits>&
  372. basic_istream<_CharT, _Traits>::operator>>(long double& __n)
  373. {
  374. return _VSTD::__input_arithmetic<long double>(*this, __n);
  375. }
  376. template <class _CharT, class _Traits>
  377. basic_istream<_CharT, _Traits>&
  378. basic_istream<_CharT, _Traits>::operator>>(bool& __n)
  379. {
  380. return _VSTD::__input_arithmetic<bool>(*this, __n);
  381. }
  382. template <class _CharT, class _Traits>
  383. basic_istream<_CharT, _Traits>&
  384. basic_istream<_CharT, _Traits>::operator>>(void*& __n)
  385. {
  386. return _VSTD::__input_arithmetic<void*>(*this, __n);
  387. }
  388. template <class _Tp, class _CharT, class _Traits>
  389. _LIBCPP_INLINE_VISIBILITY
  390. basic_istream<_CharT, _Traits>&
  391. __input_arithmetic_with_numeric_limits(basic_istream<_CharT, _Traits>& __is, _Tp& __n) {
  392. ios_base::iostate __state = ios_base::goodbit;
  393. typename basic_istream<_CharT, _Traits>::sentry __s(__is);
  394. if (__s)
  395. {
  396. #ifndef _LIBCPP_NO_EXCEPTIONS
  397. try
  398. {
  399. #endif // _LIBCPP_NO_EXCEPTIONS
  400. typedef istreambuf_iterator<_CharT, _Traits> _Ip;
  401. typedef num_get<_CharT, _Ip> _Fp;
  402. long __temp;
  403. use_facet<_Fp>(__is.getloc()).get(_Ip(__is), _Ip(), __is, __state, __temp);
  404. if (__temp < numeric_limits<_Tp>::min())
  405. {
  406. __state |= ios_base::failbit;
  407. __n = numeric_limits<_Tp>::min();
  408. }
  409. else if (__temp > numeric_limits<_Tp>::max())
  410. {
  411. __state |= ios_base::failbit;
  412. __n = numeric_limits<_Tp>::max();
  413. }
  414. else
  415. {
  416. __n = static_cast<_Tp>(__temp);
  417. }
  418. #ifndef _LIBCPP_NO_EXCEPTIONS
  419. }
  420. catch (...)
  421. {
  422. __state |= ios_base::badbit;
  423. __is.__setstate_nothrow(__state);
  424. if (__is.exceptions() & ios_base::badbit)
  425. {
  426. throw;
  427. }
  428. }
  429. #endif // _LIBCPP_NO_EXCEPTIONS
  430. __is.setstate(__state);
  431. }
  432. return __is;
  433. }
  434. template <class _CharT, class _Traits>
  435. basic_istream<_CharT, _Traits>&
  436. basic_istream<_CharT, _Traits>::operator>>(short& __n)
  437. {
  438. return _VSTD::__input_arithmetic_with_numeric_limits<short>(*this, __n);
  439. }
  440. template <class _CharT, class _Traits>
  441. basic_istream<_CharT, _Traits>&
  442. basic_istream<_CharT, _Traits>::operator>>(int& __n)
  443. {
  444. return _VSTD::__input_arithmetic_with_numeric_limits<int>(*this, __n);
  445. }
  446. template<class _CharT, class _Traits>
  447. _LIBCPP_INLINE_VISIBILITY
  448. basic_istream<_CharT, _Traits>&
  449. __input_c_string(basic_istream<_CharT, _Traits>& __is, _CharT* __p, size_t __n)
  450. {
  451. ios_base::iostate __state = ios_base::goodbit;
  452. typename basic_istream<_CharT, _Traits>::sentry __sen(__is);
  453. if (__sen)
  454. {
  455. #ifndef _LIBCPP_NO_EXCEPTIONS
  456. try
  457. {
  458. #endif
  459. _CharT* __s = __p;
  460. const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__is.getloc());
  461. while (__s != __p + (__n-1))
  462. {
  463. typename _Traits::int_type __i = __is.rdbuf()->sgetc();
  464. if (_Traits::eq_int_type(__i, _Traits::eof()))
  465. {
  466. __state |= ios_base::eofbit;
  467. break;
  468. }
  469. _CharT __ch = _Traits::to_char_type(__i);
  470. if (__ct.is(__ct.space, __ch))
  471. break;
  472. *__s++ = __ch;
  473. __is.rdbuf()->sbumpc();
  474. }
  475. *__s = _CharT();
  476. __is.width(0);
  477. if (__s == __p)
  478. __state |= ios_base::failbit;
  479. #ifndef _LIBCPP_NO_EXCEPTIONS
  480. }
  481. catch (...)
  482. {
  483. __state |= ios_base::badbit;
  484. __is.__setstate_nothrow(__state);
  485. if (__is.exceptions() & ios_base::badbit)
  486. {
  487. throw;
  488. }
  489. }
  490. #endif
  491. __is.setstate(__state);
  492. }
  493. return __is;
  494. }
  495. #if _LIBCPP_STD_VER > 17
  496. template<class _CharT, class _Traits, size_t _Np>
  497. inline _LIBCPP_INLINE_VISIBILITY
  498. basic_istream<_CharT, _Traits>&
  499. operator>>(basic_istream<_CharT, _Traits>& __is, _CharT (&__buf)[_Np])
  500. {
  501. size_t __n = _Np;
  502. if (__is.width() > 0)
  503. __n = _VSTD::min(size_t(__is.width()), _Np);
  504. return _VSTD::__input_c_string(__is, __buf, __n);
  505. }
  506. template<class _Traits, size_t _Np>
  507. inline _LIBCPP_INLINE_VISIBILITY
  508. basic_istream<char, _Traits>&
  509. operator>>(basic_istream<char, _Traits>& __is, unsigned char (&__buf)[_Np])
  510. {
  511. return __is >> (char(&)[_Np])__buf;
  512. }
  513. template<class _Traits, size_t _Np>
  514. inline _LIBCPP_INLINE_VISIBILITY
  515. basic_istream<char, _Traits>&
  516. operator>>(basic_istream<char, _Traits>& __is, signed char (&__buf)[_Np])
  517. {
  518. return __is >> (char(&)[_Np])__buf;
  519. }
  520. #else
  521. template<class _CharT, class _Traits>
  522. inline _LIBCPP_INLINE_VISIBILITY
  523. basic_istream<_CharT, _Traits>&
  524. operator>>(basic_istream<_CharT, _Traits>& __is, _CharT* __s)
  525. {
  526. streamsize __n = __is.width();
  527. if (__n <= 0)
  528. __n = numeric_limits<streamsize>::max() / sizeof(_CharT) - 1;
  529. return _VSTD::__input_c_string(__is, __s, size_t(__n));
  530. }
  531. template<class _Traits>
  532. inline _LIBCPP_INLINE_VISIBILITY
  533. basic_istream<char, _Traits>&
  534. operator>>(basic_istream<char, _Traits>& __is, unsigned char* __s)
  535. {
  536. return __is >> (char*)__s;
  537. }
  538. template<class _Traits>
  539. inline _LIBCPP_INLINE_VISIBILITY
  540. basic_istream<char, _Traits>&
  541. operator>>(basic_istream<char, _Traits>& __is, signed char* __s)
  542. {
  543. return __is >> (char*)__s;
  544. }
  545. #endif // _LIBCPP_STD_VER > 17
  546. template<class _CharT, class _Traits>
  547. basic_istream<_CharT, _Traits>&
  548. operator>>(basic_istream<_CharT, _Traits>& __is, _CharT& __c)
  549. {
  550. ios_base::iostate __state = ios_base::goodbit;
  551. typename basic_istream<_CharT, _Traits>::sentry __sen(__is);
  552. if (__sen)
  553. {
  554. #ifndef _LIBCPP_NO_EXCEPTIONS
  555. try
  556. {
  557. #endif
  558. typename _Traits::int_type __i = __is.rdbuf()->sbumpc();
  559. if (_Traits::eq_int_type(__i, _Traits::eof()))
  560. __state |= ios_base::eofbit | ios_base::failbit;
  561. else
  562. __c = _Traits::to_char_type(__i);
  563. #ifndef _LIBCPP_NO_EXCEPTIONS
  564. }
  565. catch (...)
  566. {
  567. __state |= ios_base::badbit;
  568. __is.__setstate_nothrow(__state);
  569. if (__is.exceptions() & ios_base::badbit)
  570. {
  571. throw;
  572. }
  573. }
  574. #endif
  575. __is.setstate(__state);
  576. }
  577. return __is;
  578. }
  579. template<class _Traits>
  580. inline _LIBCPP_INLINE_VISIBILITY
  581. basic_istream<char, _Traits>&
  582. operator>>(basic_istream<char, _Traits>& __is, unsigned char& __c)
  583. {
  584. return __is >> (char&)__c;
  585. }
  586. template<class _Traits>
  587. inline _LIBCPP_INLINE_VISIBILITY
  588. basic_istream<char, _Traits>&
  589. operator>>(basic_istream<char, _Traits>& __is, signed char& __c)
  590. {
  591. return __is >> (char&)__c;
  592. }
  593. template<class _CharT, class _Traits>
  594. basic_istream<_CharT, _Traits>&
  595. basic_istream<_CharT, _Traits>::operator>>(basic_streambuf<char_type, traits_type>* __sb)
  596. {
  597. ios_base::iostate __state = ios_base::goodbit;
  598. __gc_ = 0;
  599. sentry __s(*this, true);
  600. if (__s)
  601. {
  602. if (__sb)
  603. {
  604. #ifndef _LIBCPP_NO_EXCEPTIONS
  605. try
  606. {
  607. #endif // _LIBCPP_NO_EXCEPTIONS
  608. while (true)
  609. {
  610. typename traits_type::int_type __i = this->rdbuf()->sgetc();
  611. if (traits_type::eq_int_type(__i, _Traits::eof()))
  612. {
  613. __state |= ios_base::eofbit;
  614. break;
  615. }
  616. if (traits_type::eq_int_type(
  617. __sb->sputc(traits_type::to_char_type(__i)),
  618. traits_type::eof()))
  619. break;
  620. ++__gc_;
  621. this->rdbuf()->sbumpc();
  622. }
  623. if (__gc_ == 0)
  624. __state |= ios_base::failbit;
  625. #ifndef _LIBCPP_NO_EXCEPTIONS
  626. }
  627. catch (...)
  628. {
  629. __state |= ios_base::badbit;
  630. if (__gc_ == 0)
  631. __state |= ios_base::failbit;
  632. this->__setstate_nothrow(__state);
  633. if (this->exceptions() & ios_base::failbit || this->exceptions() & ios_base::badbit)
  634. {
  635. throw;
  636. }
  637. }
  638. #endif // _LIBCPP_NO_EXCEPTIONS
  639. }
  640. else
  641. {
  642. __state |= ios_base::failbit;
  643. }
  644. this->setstate(__state);
  645. }
  646. return *this;
  647. }
  648. template<class _CharT, class _Traits>
  649. typename basic_istream<_CharT, _Traits>::int_type
  650. basic_istream<_CharT, _Traits>::get()
  651. {
  652. ios_base::iostate __state = ios_base::goodbit;
  653. __gc_ = 0;
  654. int_type __r = traits_type::eof();
  655. sentry __s(*this, true);
  656. if (__s)
  657. {
  658. #ifndef _LIBCPP_NO_EXCEPTIONS
  659. try
  660. {
  661. #endif
  662. __r = this->rdbuf()->sbumpc();
  663. if (traits_type::eq_int_type(__r, traits_type::eof()))
  664. __state |= ios_base::failbit | ios_base::eofbit;
  665. else
  666. __gc_ = 1;
  667. #ifndef _LIBCPP_NO_EXCEPTIONS
  668. }
  669. catch (...)
  670. {
  671. this->__setstate_nothrow(this->rdstate() | ios_base::badbit);
  672. if (this->exceptions() & ios_base::badbit)
  673. {
  674. throw;
  675. }
  676. }
  677. #endif
  678. this->setstate(__state);
  679. }
  680. return __r;
  681. }
  682. template<class _CharT, class _Traits>
  683. basic_istream<_CharT, _Traits>&
  684. basic_istream<_CharT, _Traits>::get(char_type* __s, streamsize __n, char_type __dlm)
  685. {
  686. ios_base::iostate __state = ios_base::goodbit;
  687. __gc_ = 0;
  688. sentry __sen(*this, true);
  689. if (__sen)
  690. {
  691. if (__n > 0)
  692. {
  693. #ifndef _LIBCPP_NO_EXCEPTIONS
  694. try
  695. {
  696. #endif
  697. while (__gc_ < __n-1)
  698. {
  699. int_type __i = this->rdbuf()->sgetc();
  700. if (traits_type::eq_int_type(__i, traits_type::eof()))
  701. {
  702. __state |= ios_base::eofbit;
  703. break;
  704. }
  705. char_type __ch = traits_type::to_char_type(__i);
  706. if (traits_type::eq(__ch, __dlm))
  707. break;
  708. *__s++ = __ch;
  709. ++__gc_;
  710. this->rdbuf()->sbumpc();
  711. }
  712. if (__gc_ == 0)
  713. __state |= ios_base::failbit;
  714. #ifndef _LIBCPP_NO_EXCEPTIONS
  715. }
  716. catch (...)
  717. {
  718. __state |= ios_base::badbit;
  719. this->__setstate_nothrow(__state);
  720. if (this->exceptions() & ios_base::badbit)
  721. {
  722. if (__n > 0)
  723. *__s = char_type();
  724. throw;
  725. }
  726. }
  727. #endif
  728. }
  729. else
  730. {
  731. __state |= ios_base::failbit;
  732. }
  733. if (__n > 0)
  734. *__s = char_type();
  735. this->setstate(__state);
  736. }
  737. if (__n > 0)
  738. *__s = char_type();
  739. return *this;
  740. }
  741. template<class _CharT, class _Traits>
  742. basic_istream<_CharT, _Traits>&
  743. basic_istream<_CharT, _Traits>::get(basic_streambuf<char_type, traits_type>& __sb,
  744. char_type __dlm)
  745. {
  746. ios_base::iostate __state = ios_base::goodbit;
  747. __gc_ = 0;
  748. sentry __sen(*this, true);
  749. if (__sen)
  750. {
  751. #ifndef _LIBCPP_NO_EXCEPTIONS
  752. try
  753. {
  754. #endif // _LIBCPP_NO_EXCEPTIONS
  755. while (true)
  756. {
  757. typename traits_type::int_type __i = this->rdbuf()->sgetc();
  758. if (traits_type::eq_int_type(__i, traits_type::eof()))
  759. {
  760. __state |= ios_base::eofbit;
  761. break;
  762. }
  763. char_type __ch = traits_type::to_char_type(__i);
  764. if (traits_type::eq(__ch, __dlm))
  765. break;
  766. if (traits_type::eq_int_type(__sb.sputc(__ch), traits_type::eof()))
  767. break;
  768. ++__gc_;
  769. this->rdbuf()->sbumpc();
  770. }
  771. #ifndef _LIBCPP_NO_EXCEPTIONS
  772. }
  773. catch (...)
  774. {
  775. __state |= ios_base::badbit;
  776. // according to the spec, exceptions here are caught but not rethrown
  777. }
  778. #endif // _LIBCPP_NO_EXCEPTIONS
  779. if (__gc_ == 0)
  780. __state |= ios_base::failbit;
  781. this->setstate(__state);
  782. }
  783. return *this;
  784. }
  785. template<class _CharT, class _Traits>
  786. basic_istream<_CharT, _Traits>&
  787. basic_istream<_CharT, _Traits>::getline(char_type* __s, streamsize __n, char_type __dlm)
  788. {
  789. ios_base::iostate __state = ios_base::goodbit;
  790. __gc_ = 0;
  791. sentry __sen(*this, true);
  792. if (__sen)
  793. {
  794. #ifndef _LIBCPP_NO_EXCEPTIONS
  795. try
  796. {
  797. #endif // _LIBCPP_NO_EXCEPTIONS
  798. while (true)
  799. {
  800. typename traits_type::int_type __i = this->rdbuf()->sgetc();
  801. if (traits_type::eq_int_type(__i, traits_type::eof()))
  802. {
  803. __state |= ios_base::eofbit;
  804. break;
  805. }
  806. char_type __ch = traits_type::to_char_type(__i);
  807. if (traits_type::eq(__ch, __dlm))
  808. {
  809. this->rdbuf()->sbumpc();
  810. ++__gc_;
  811. break;
  812. }
  813. if (__gc_ >= __n-1)
  814. {
  815. __state |= ios_base::failbit;
  816. break;
  817. }
  818. *__s++ = __ch;
  819. this->rdbuf()->sbumpc();
  820. ++__gc_;
  821. }
  822. #ifndef _LIBCPP_NO_EXCEPTIONS
  823. }
  824. catch (...)
  825. {
  826. __state |= ios_base::badbit;
  827. this->__setstate_nothrow(__state);
  828. if (this->exceptions() & ios_base::badbit)
  829. {
  830. if (__n > 0)
  831. *__s = char_type();
  832. if (__gc_ == 0)
  833. __state |= ios_base::failbit;
  834. throw;
  835. }
  836. }
  837. #endif // _LIBCPP_NO_EXCEPTIONS
  838. }
  839. if (__n > 0)
  840. *__s = char_type();
  841. if (__gc_ == 0)
  842. __state |= ios_base::failbit;
  843. this->setstate(__state);
  844. return *this;
  845. }
  846. template<class _CharT, class _Traits>
  847. basic_istream<_CharT, _Traits>&
  848. basic_istream<_CharT, _Traits>::ignore(streamsize __n, int_type __dlm)
  849. {
  850. ios_base::iostate __state = ios_base::goodbit;
  851. __gc_ = 0;
  852. sentry __sen(*this, true);
  853. if (__sen)
  854. {
  855. #ifndef _LIBCPP_NO_EXCEPTIONS
  856. try
  857. {
  858. #endif // _LIBCPP_NO_EXCEPTIONS
  859. if (__n == numeric_limits<streamsize>::max())
  860. {
  861. while (true)
  862. {
  863. typename traits_type::int_type __i = this->rdbuf()->sbumpc();
  864. if (traits_type::eq_int_type(__i, traits_type::eof()))
  865. {
  866. __state |= ios_base::eofbit;
  867. break;
  868. }
  869. ++__gc_;
  870. if (traits_type::eq_int_type(__i, __dlm))
  871. break;
  872. }
  873. }
  874. else
  875. {
  876. while (__gc_ < __n)
  877. {
  878. typename traits_type::int_type __i = this->rdbuf()->sbumpc();
  879. if (traits_type::eq_int_type(__i, traits_type::eof()))
  880. {
  881. __state |= ios_base::eofbit;
  882. break;
  883. }
  884. ++__gc_;
  885. if (traits_type::eq_int_type(__i, __dlm))
  886. break;
  887. }
  888. }
  889. #ifndef _LIBCPP_NO_EXCEPTIONS
  890. }
  891. catch (...)
  892. {
  893. __state |= ios_base::badbit;
  894. this->__setstate_nothrow(__state);
  895. if (this->exceptions() & ios_base::badbit)
  896. {
  897. throw;
  898. }
  899. }
  900. #endif // _LIBCPP_NO_EXCEPTIONS
  901. this->setstate(__state);
  902. }
  903. return *this;
  904. }
  905. template<class _CharT, class _Traits>
  906. typename basic_istream<_CharT, _Traits>::int_type
  907. basic_istream<_CharT, _Traits>::peek()
  908. {
  909. ios_base::iostate __state = ios_base::goodbit;
  910. __gc_ = 0;
  911. int_type __r = traits_type::eof();
  912. sentry __sen(*this, true);
  913. if (__sen)
  914. {
  915. #ifndef _LIBCPP_NO_EXCEPTIONS
  916. try
  917. {
  918. #endif // _LIBCPP_NO_EXCEPTIONS
  919. __r = this->rdbuf()->sgetc();
  920. if (traits_type::eq_int_type(__r, traits_type::eof()))
  921. __state |= ios_base::eofbit;
  922. #ifndef _LIBCPP_NO_EXCEPTIONS
  923. }
  924. catch (...)
  925. {
  926. __state |= ios_base::badbit;
  927. this->__setstate_nothrow(__state);
  928. if (this->exceptions() & ios_base::badbit)
  929. {
  930. throw;
  931. }
  932. }
  933. #endif // _LIBCPP_NO_EXCEPTIONS
  934. this->setstate(__state);
  935. }
  936. return __r;
  937. }
  938. template<class _CharT, class _Traits>
  939. basic_istream<_CharT, _Traits>&
  940. basic_istream<_CharT, _Traits>::read(char_type* __s, streamsize __n)
  941. {
  942. ios_base::iostate __state = ios_base::goodbit;
  943. __gc_ = 0;
  944. sentry __sen(*this, true);
  945. if (__sen)
  946. {
  947. #ifndef _LIBCPP_NO_EXCEPTIONS
  948. try
  949. {
  950. #endif // _LIBCPP_NO_EXCEPTIONS
  951. __gc_ = this->rdbuf()->sgetn(__s, __n);
  952. if (__gc_ != __n)
  953. __state |= ios_base::failbit | ios_base::eofbit;
  954. #ifndef _LIBCPP_NO_EXCEPTIONS
  955. }
  956. catch (...)
  957. {
  958. __state |= ios_base::badbit;
  959. this->__setstate_nothrow(__state);
  960. if (this->exceptions() & ios_base::badbit)
  961. {
  962. throw;
  963. }
  964. }
  965. #endif // _LIBCPP_NO_EXCEPTIONS
  966. }
  967. else
  968. {
  969. __state |= ios_base::failbit;
  970. }
  971. this->setstate(__state);
  972. return *this;
  973. }
  974. template<class _CharT, class _Traits>
  975. streamsize
  976. basic_istream<_CharT, _Traits>::readsome(char_type* __s, streamsize __n)
  977. {
  978. ios_base::iostate __state = ios_base::goodbit;
  979. __gc_ = 0;
  980. sentry __sen(*this, true);
  981. if (__sen)
  982. {
  983. #ifndef _LIBCPP_NO_EXCEPTIONS
  984. try
  985. {
  986. #endif // _LIBCPP_NO_EXCEPTIONS
  987. streamsize __c = this->rdbuf()->in_avail();
  988. switch (__c)
  989. {
  990. case -1:
  991. __state |= ios_base::eofbit;
  992. break;
  993. case 0:
  994. break;
  995. default:
  996. __n = _VSTD::min(__c, __n);
  997. __gc_ = this->rdbuf()->sgetn(__s, __n);
  998. if (__gc_ != __n)
  999. __state |= ios_base::failbit | ios_base::eofbit;
  1000. break;
  1001. }
  1002. #ifndef _LIBCPP_NO_EXCEPTIONS
  1003. }
  1004. catch (...)
  1005. {
  1006. __state |= ios_base::badbit;
  1007. this->__setstate_nothrow(__state);
  1008. if (this->exceptions() & ios_base::badbit)
  1009. {
  1010. throw;
  1011. }
  1012. }
  1013. #endif // _LIBCPP_NO_EXCEPTIONS
  1014. }
  1015. else
  1016. {
  1017. __state |= ios_base::failbit;
  1018. }
  1019. this->setstate(__state);
  1020. return __gc_;
  1021. }
  1022. template<class _CharT, class _Traits>
  1023. basic_istream<_CharT, _Traits>&
  1024. basic_istream<_CharT, _Traits>::putback(char_type __c)
  1025. {
  1026. ios_base::iostate __state = this->rdstate() & ~ios_base::eofbit;
  1027. __gc_ = 0;
  1028. this->clear(__state);
  1029. sentry __sen(*this, true);
  1030. if (__sen)
  1031. {
  1032. #ifndef _LIBCPP_NO_EXCEPTIONS
  1033. try
  1034. {
  1035. #endif // _LIBCPP_NO_EXCEPTIONS
  1036. if (this->rdbuf() == nullptr || this->rdbuf()->sputbackc(__c) == traits_type::eof())
  1037. __state |= ios_base::badbit;
  1038. #ifndef _LIBCPP_NO_EXCEPTIONS
  1039. }
  1040. catch (...)
  1041. {
  1042. __state |= ios_base::badbit;
  1043. this->__setstate_nothrow(__state);
  1044. if (this->exceptions() & ios_base::badbit)
  1045. {
  1046. throw;
  1047. }
  1048. }
  1049. #endif // _LIBCPP_NO_EXCEPTIONS
  1050. }
  1051. else
  1052. {
  1053. __state |= ios_base::failbit;
  1054. }
  1055. this->setstate(__state);
  1056. return *this;
  1057. }
  1058. template<class _CharT, class _Traits>
  1059. basic_istream<_CharT, _Traits>&
  1060. basic_istream<_CharT, _Traits>::unget()
  1061. {
  1062. ios_base::iostate __state = this->rdstate() & ~ios_base::eofbit;
  1063. __gc_ = 0;
  1064. this->clear(__state);
  1065. sentry __sen(*this, true);
  1066. if (__sen)
  1067. {
  1068. #ifndef _LIBCPP_NO_EXCEPTIONS
  1069. try
  1070. {
  1071. #endif // _LIBCPP_NO_EXCEPTIONS
  1072. if (this->rdbuf() == nullptr || this->rdbuf()->sungetc() == traits_type::eof())
  1073. __state |= ios_base::badbit;
  1074. #ifndef _LIBCPP_NO_EXCEPTIONS
  1075. }
  1076. catch (...)
  1077. {
  1078. __state |= ios_base::badbit;
  1079. this->__setstate_nothrow(__state);
  1080. if (this->exceptions() & ios_base::badbit)
  1081. {
  1082. throw;
  1083. }
  1084. }
  1085. #endif // _LIBCPP_NO_EXCEPTIONS
  1086. }
  1087. else
  1088. {
  1089. __state |= ios_base::failbit;
  1090. }
  1091. this->setstate(__state);
  1092. return *this;
  1093. }
  1094. template<class _CharT, class _Traits>
  1095. int
  1096. basic_istream<_CharT, _Traits>::sync()
  1097. {
  1098. ios_base::iostate __state = ios_base::goodbit;
  1099. int __r = 0;
  1100. sentry __sen(*this, true);
  1101. if (__sen)
  1102. {
  1103. #ifndef _LIBCPP_NO_EXCEPTIONS
  1104. try
  1105. {
  1106. #endif // _LIBCPP_NO_EXCEPTIONS
  1107. if (this->rdbuf() == nullptr)
  1108. return -1;
  1109. if (this->rdbuf()->pubsync() == -1)
  1110. {
  1111. __state |= ios_base::badbit;
  1112. return -1;
  1113. }
  1114. #ifndef _LIBCPP_NO_EXCEPTIONS
  1115. }
  1116. catch (...)
  1117. {
  1118. __state |= ios_base::badbit;
  1119. this->__setstate_nothrow(__state);
  1120. if (this->exceptions() & ios_base::badbit)
  1121. {
  1122. throw;
  1123. }
  1124. }
  1125. #endif // _LIBCPP_NO_EXCEPTIONS
  1126. this->setstate(__state);
  1127. }
  1128. return __r;
  1129. }
  1130. template<class _CharT, class _Traits>
  1131. typename basic_istream<_CharT, _Traits>::pos_type
  1132. basic_istream<_CharT, _Traits>::tellg()
  1133. {
  1134. ios_base::iostate __state = ios_base::goodbit;
  1135. pos_type __r(-1);
  1136. sentry __sen(*this, true);
  1137. if (__sen)
  1138. {
  1139. #ifndef _LIBCPP_NO_EXCEPTIONS
  1140. try
  1141. {
  1142. #endif // _LIBCPP_NO_EXCEPTIONS
  1143. __r = this->rdbuf()->pubseekoff(0, ios_base::cur, ios_base::in);
  1144. #ifndef _LIBCPP_NO_EXCEPTIONS
  1145. }
  1146. catch (...)
  1147. {
  1148. __state |= ios_base::badbit;
  1149. this->__setstate_nothrow(__state);
  1150. if (this->exceptions() & ios_base::badbit)
  1151. {
  1152. throw;
  1153. }
  1154. }
  1155. #endif // _LIBCPP_NO_EXCEPTIONS
  1156. this->setstate(__state);
  1157. }
  1158. return __r;
  1159. }
  1160. template<class _CharT, class _Traits>
  1161. basic_istream<_CharT, _Traits>&
  1162. basic_istream<_CharT, _Traits>::seekg(pos_type __pos)
  1163. {
  1164. ios_base::iostate __state = this->rdstate() & ~ios_base::eofbit;
  1165. this->clear(__state);
  1166. sentry __sen(*this, true);
  1167. if (__sen)
  1168. {
  1169. #ifndef _LIBCPP_NO_EXCEPTIONS
  1170. try
  1171. {
  1172. #endif // _LIBCPP_NO_EXCEPTIONS
  1173. if (this->rdbuf()->pubseekpos(__pos, ios_base::in) == pos_type(-1))
  1174. __state |= ios_base::failbit;
  1175. #ifndef _LIBCPP_NO_EXCEPTIONS
  1176. }
  1177. catch (...)
  1178. {
  1179. __state |= ios_base::badbit;
  1180. this->__setstate_nothrow(__state);
  1181. if (this->exceptions() & ios_base::badbit)
  1182. {
  1183. throw;
  1184. }
  1185. }
  1186. #endif // _LIBCPP_NO_EXCEPTIONS
  1187. this->setstate(__state);
  1188. }
  1189. return *this;
  1190. }
  1191. template<class _CharT, class _Traits>
  1192. basic_istream<_CharT, _Traits>&
  1193. basic_istream<_CharT, _Traits>::seekg(off_type __off, ios_base::seekdir __dir)
  1194. {
  1195. ios_base::iostate __state = this->rdstate() & ~ios_base::eofbit;
  1196. this->clear(__state);
  1197. sentry __sen(*this, true);
  1198. if (__sen)
  1199. {
  1200. #ifndef _LIBCPP_NO_EXCEPTIONS
  1201. try
  1202. {
  1203. #endif // _LIBCPP_NO_EXCEPTIONS
  1204. if (this->rdbuf()->pubseekoff(__off, __dir, ios_base::in) == pos_type(-1))
  1205. __state |= ios_base::failbit;
  1206. #ifndef _LIBCPP_NO_EXCEPTIONS
  1207. }
  1208. catch (...)
  1209. {
  1210. __state |= ios_base::badbit;
  1211. this->__setstate_nothrow(__state);
  1212. if (this->exceptions() & ios_base::badbit)
  1213. {
  1214. throw;
  1215. }
  1216. }
  1217. #endif // _LIBCPP_NO_EXCEPTIONS
  1218. this->setstate(__state);
  1219. }
  1220. return *this;
  1221. }
  1222. template <class _CharT, class _Traits>
  1223. basic_istream<_CharT, _Traits>&
  1224. ws(basic_istream<_CharT, _Traits>& __is)
  1225. {
  1226. ios_base::iostate __state = ios_base::goodbit;
  1227. typename basic_istream<_CharT, _Traits>::sentry __sen(__is, true);
  1228. if (__sen)
  1229. {
  1230. #ifndef _LIBCPP_NO_EXCEPTIONS
  1231. try
  1232. {
  1233. #endif // _LIBCPP_NO_EXCEPTIONS
  1234. const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__is.getloc());
  1235. while (true)
  1236. {
  1237. typename _Traits::int_type __i = __is.rdbuf()->sgetc();
  1238. if (_Traits::eq_int_type(__i, _Traits::eof()))
  1239. {
  1240. __state |= ios_base::eofbit;
  1241. break;
  1242. }
  1243. if (!__ct.is(__ct.space, _Traits::to_char_type(__i)))
  1244. break;
  1245. __is.rdbuf()->sbumpc();
  1246. }
  1247. #ifndef _LIBCPP_NO_EXCEPTIONS
  1248. }
  1249. catch (...)
  1250. {
  1251. __state |= ios_base::badbit;
  1252. __is.__setstate_nothrow(__state);
  1253. if (__is.exceptions() & ios_base::badbit)
  1254. {
  1255. throw;
  1256. }
  1257. }
  1258. #endif // _LIBCPP_NO_EXCEPTIONS
  1259. __is.setstate(__state);
  1260. }
  1261. return __is;
  1262. }
  1263. template <class _Stream, class _Tp, class = void>
  1264. struct __is_istreamable : false_type { };
  1265. template <class _Stream, class _Tp>
  1266. struct __is_istreamable<_Stream, _Tp, decltype(
  1267. declval<_Stream>() >> declval<_Tp>(), void()
  1268. )> : true_type { };
  1269. template <class _Stream, class _Tp, class = typename enable_if<
  1270. _And<is_base_of<ios_base, _Stream>,
  1271. __is_istreamable<_Stream&, _Tp&&> >::value
  1272. >::type>
  1273. _LIBCPP_INLINE_VISIBILITY
  1274. _Stream&& operator>>(_Stream&& __is, _Tp&& __x)
  1275. {
  1276. __is >> _VSTD::forward<_Tp>(__x);
  1277. return _VSTD::move(__is);
  1278. }
  1279. template <class _CharT, class _Traits>
  1280. class _LIBCPP_TEMPLATE_VIS basic_iostream
  1281. : public basic_istream<_CharT, _Traits>,
  1282. public basic_ostream<_CharT, _Traits>
  1283. {
  1284. public:
  1285. // types:
  1286. typedef _CharT char_type;
  1287. typedef _Traits traits_type;
  1288. typedef typename traits_type::int_type int_type;
  1289. typedef typename traits_type::pos_type pos_type;
  1290. typedef typename traits_type::off_type off_type;
  1291. // constructor/destructor
  1292. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
  1293. explicit basic_iostream(basic_streambuf<char_type, traits_type>* __sb)
  1294. : basic_istream<_CharT, _Traits>(__sb)
  1295. {}
  1296. virtual ~basic_iostream();
  1297. protected:
  1298. inline _LIBCPP_INLINE_VISIBILITY
  1299. basic_iostream(basic_iostream&& __rhs);
  1300. // assign/swap
  1301. inline _LIBCPP_INLINE_VISIBILITY
  1302. basic_iostream& operator=(basic_iostream&& __rhs);
  1303. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
  1304. void swap(basic_iostream& __rhs)
  1305. { basic_istream<char_type, traits_type>::swap(__rhs); }
  1306. };
  1307. template <class _CharT, class _Traits>
  1308. basic_iostream<_CharT, _Traits>::basic_iostream(basic_iostream&& __rhs)
  1309. : basic_istream<_CharT, _Traits>(_VSTD::move(__rhs))
  1310. {
  1311. }
  1312. template <class _CharT, class _Traits>
  1313. basic_iostream<_CharT, _Traits>&
  1314. basic_iostream<_CharT, _Traits>::operator=(basic_iostream&& __rhs)
  1315. {
  1316. swap(__rhs);
  1317. return *this;
  1318. }
  1319. template <class _CharT, class _Traits>
  1320. basic_iostream<_CharT, _Traits>::~basic_iostream()
  1321. {
  1322. }
  1323. template<class _CharT, class _Traits, class _Allocator>
  1324. basic_istream<_CharT, _Traits>&
  1325. operator>>(basic_istream<_CharT, _Traits>& __is,
  1326. basic_string<_CharT, _Traits, _Allocator>& __str)
  1327. {
  1328. ios_base::iostate __state = ios_base::goodbit;
  1329. typename basic_istream<_CharT, _Traits>::sentry __sen(__is);
  1330. if (__sen)
  1331. {
  1332. #ifndef _LIBCPP_NO_EXCEPTIONS
  1333. try
  1334. {
  1335. #endif
  1336. __str.clear();
  1337. streamsize __n = __is.width();
  1338. if (__n <= 0)
  1339. __n = __str.max_size();
  1340. if (__n <= 0)
  1341. __n = numeric_limits<streamsize>::max();
  1342. streamsize __c = 0;
  1343. const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__is.getloc());
  1344. while (__c < __n)
  1345. {
  1346. typename _Traits::int_type __i = __is.rdbuf()->sgetc();
  1347. if (_Traits::eq_int_type(__i, _Traits::eof()))
  1348. {
  1349. __state |= ios_base::eofbit;
  1350. break;
  1351. }
  1352. _CharT __ch = _Traits::to_char_type(__i);
  1353. if (__ct.is(__ct.space, __ch))
  1354. break;
  1355. __str.push_back(__ch);
  1356. ++__c;
  1357. __is.rdbuf()->sbumpc();
  1358. }
  1359. __is.width(0);
  1360. if (__c == 0)
  1361. __state |= ios_base::failbit;
  1362. #ifndef _LIBCPP_NO_EXCEPTIONS
  1363. }
  1364. catch (...)
  1365. {
  1366. __state |= ios_base::badbit;
  1367. __is.__setstate_nothrow(__state);
  1368. if (__is.exceptions() & ios_base::badbit)
  1369. {
  1370. throw;
  1371. }
  1372. }
  1373. #endif
  1374. __is.setstate(__state);
  1375. }
  1376. return __is;
  1377. }
  1378. template<class _CharT, class _Traits, class _Allocator>
  1379. basic_istream<_CharT, _Traits>&
  1380. getline(basic_istream<_CharT, _Traits>& __is,
  1381. basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm)
  1382. {
  1383. ios_base::iostate __state = ios_base::goodbit;
  1384. typename basic_istream<_CharT, _Traits>::sentry __sen(__is, true);
  1385. if (__sen)
  1386. {
  1387. #ifndef _LIBCPP_NO_EXCEPTIONS
  1388. try
  1389. {
  1390. #endif
  1391. __str.clear();
  1392. streamsize __extr = 0;
  1393. while (true)
  1394. {
  1395. typename _Traits::int_type __i = __is.rdbuf()->sbumpc();
  1396. if (_Traits::eq_int_type(__i, _Traits::eof()))
  1397. {
  1398. __state |= ios_base::eofbit;
  1399. break;
  1400. }
  1401. ++__extr;
  1402. _CharT __ch = _Traits::to_char_type(__i);
  1403. if (_Traits::eq(__ch, __dlm))
  1404. break;
  1405. __str.push_back(__ch);
  1406. if (__str.size() == __str.max_size())
  1407. {
  1408. __state |= ios_base::failbit;
  1409. break;
  1410. }
  1411. }
  1412. if (__extr == 0)
  1413. __state |= ios_base::failbit;
  1414. #ifndef _LIBCPP_NO_EXCEPTIONS
  1415. }
  1416. catch (...)
  1417. {
  1418. __state |= ios_base::badbit;
  1419. __is.__setstate_nothrow(__state);
  1420. if (__is.exceptions() & ios_base::badbit)
  1421. {
  1422. throw;
  1423. }
  1424. }
  1425. #endif
  1426. __is.setstate(__state);
  1427. }
  1428. return __is;
  1429. }
  1430. template<class _CharT, class _Traits, class _Allocator>
  1431. inline _LIBCPP_INLINE_VISIBILITY
  1432. basic_istream<_CharT, _Traits>&
  1433. getline(basic_istream<_CharT, _Traits>& __is,
  1434. basic_string<_CharT, _Traits, _Allocator>& __str)
  1435. {
  1436. return getline(__is, __str, __is.widen('\n'));
  1437. }
  1438. template<class _CharT, class _Traits, class _Allocator>
  1439. inline _LIBCPP_INLINE_VISIBILITY
  1440. basic_istream<_CharT, _Traits>&
  1441. getline(basic_istream<_CharT, _Traits>&& __is,
  1442. basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm)
  1443. {
  1444. return getline(__is, __str, __dlm);
  1445. }
  1446. template<class _CharT, class _Traits, class _Allocator>
  1447. inline _LIBCPP_INLINE_VISIBILITY
  1448. basic_istream<_CharT, _Traits>&
  1449. getline(basic_istream<_CharT, _Traits>&& __is,
  1450. basic_string<_CharT, _Traits, _Allocator>& __str)
  1451. {
  1452. return getline(__is, __str, __is.widen('\n'));
  1453. }
  1454. template <class _CharT, class _Traits, size_t _Size>
  1455. basic_istream<_CharT, _Traits>&
  1456. operator>>(basic_istream<_CharT, _Traits>& __is, bitset<_Size>& __x)
  1457. {
  1458. ios_base::iostate __state = ios_base::goodbit;
  1459. typename basic_istream<_CharT, _Traits>::sentry __sen(__is);
  1460. if (__sen)
  1461. {
  1462. #ifndef _LIBCPP_NO_EXCEPTIONS
  1463. try
  1464. {
  1465. #endif
  1466. basic_string<_CharT, _Traits> __str;
  1467. const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__is.getloc());
  1468. size_t __c = 0;
  1469. _CharT __zero = __ct.widen('0');
  1470. _CharT __one = __ct.widen('1');
  1471. while (__c != _Size)
  1472. {
  1473. typename _Traits::int_type __i = __is.rdbuf()->sgetc();
  1474. if (_Traits::eq_int_type(__i, _Traits::eof()))
  1475. {
  1476. __state |= ios_base::eofbit;
  1477. break;
  1478. }
  1479. _CharT __ch = _Traits::to_char_type(__i);
  1480. if (!_Traits::eq(__ch, __zero) && !_Traits::eq(__ch, __one))
  1481. break;
  1482. __str.push_back(__ch);
  1483. ++__c;
  1484. __is.rdbuf()->sbumpc();
  1485. }
  1486. __x = bitset<_Size>(__str);
  1487. if (_Size > 0 && __c == 0)
  1488. __state |= ios_base::failbit;
  1489. #ifndef _LIBCPP_NO_EXCEPTIONS
  1490. }
  1491. catch (...)
  1492. {
  1493. __state |= ios_base::badbit;
  1494. __is.__setstate_nothrow(__state);
  1495. if (__is.exceptions() & ios_base::badbit)
  1496. {
  1497. throw;
  1498. }
  1499. }
  1500. #endif
  1501. __is.setstate(__state);
  1502. }
  1503. return __is;
  1504. }
  1505. extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_istream<char>;
  1506. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  1507. extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_istream<wchar_t>;
  1508. #endif
  1509. extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_iostream<char>;
  1510. _LIBCPP_END_NAMESPACE_STD
  1511. _LIBCPP_POP_MACROS
  1512. #endif // _LIBCPP_ISTREAM