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