istream 47 KB

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