1 // Input streams -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 // 2006, 2007, 2008, 2009, 2010, 2011, 2012
5 // Free Software Foundation, Inc.
7 // This file is part of the GNU ISO C++ Library. This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 3, or (at your option)
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // Under Section 7 of GPL version 3, you are granted additional
19 // permissions described in the GCC Runtime Library Exception, version
20 // 3.1, as published by the Free Software Foundation.
22 // You should have received a copy of the GNU General Public License and
23 // a copy of the GCC Runtime Library Exception along with this program;
24 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 // <http://www.gnu.org/licenses/>.
28 // ISO C++ 14882: 27.6.1 Input streams
31 /** @file include/istream
32 * This is a Standard C++ Library header.
35 #ifndef _GLIBCXX_ISTREAM
36 #define _GLIBCXX_ISTREAM 1
38 #pragma GCC system_header
43 namespace std _GLIBCXX_VISIBILITY(default)
45 _GLIBCXX_BEGIN_NAMESPACE_VERSION
48 * @brief Template class basic_istream.
51 * @tparam _CharT Type of character stream.
52 * @tparam _Traits Traits for character type, defaults to
53 * char_traits<_CharT>.
55 * This is the base class for all input streams. It provides text
56 * formatting of all builtin types, and communicates with any class
57 * derived from basic_streambuf to do the actual input.
59 template<typename _CharT, typename _Traits>
60 class basic_istream : virtual public basic_ios<_CharT, _Traits>
63 // Types (inherited from basic_ios (27.4.4)):
64 typedef _CharT char_type;
65 typedef typename _Traits::int_type int_type;
66 typedef typename _Traits::pos_type pos_type;
67 typedef typename _Traits::off_type off_type;
68 typedef _Traits traits_type;
70 // Non-standard Types:
71 typedef basic_streambuf<_CharT, _Traits> __streambuf_type;
72 typedef basic_ios<_CharT, _Traits> __ios_type;
73 typedef basic_istream<_CharT, _Traits> __istream_type;
74 typedef num_get<_CharT, istreambuf_iterator<_CharT, _Traits> >
76 typedef ctype<_CharT> __ctype_type;
81 * The number of characters extracted in the previous unformatted
82 * function; see gcount().
88 * @brief Base constructor.
90 * This ctor is almost never called by the user directly, rather from
91 * derived classes' initialization lists, which pass a pointer to
92 * their own stream buffer.
95 basic_istream(__streambuf_type* __sb)
96 : _M_gcount(streamsize(0))
100 * @brief Base destructor.
102 * This does very little apart from providing a virtual base dtor.
106 { _M_gcount = streamsize(0); }
108 /// Safe prefix/suffix operations.
114 * @brief Interface for manipulators.
116 * Manipulators such as @c std::ws and @c std::dec use these
117 * functions in constructs like
118 * <code>std::cin >> std::ws</code>.
119 * For more information, see the iomanip header.
122 operator>>(__istream_type& (*__pf)(__istream_type&))
123 { return __pf(*this); }
126 operator>>(__ios_type& (*__pf)(__ios_type&))
133 operator>>(ios_base& (*__pf)(ios_base&))
144 * All the @c operator>> functions (aka <em>formatted input
145 * functions</em>) have some common behavior. Each starts by
146 * constructing a temporary object of type std::basic_istream::sentry
147 * with the second argument (noskipws) set to false. This has several
148 * effects, concluding with the setting of a status flag; see the
149 * sentry documentation for more.
151 * If the sentry status is good, the function tries to extract
152 * whatever data is appropriate for the type of the argument.
154 * If an exception is thrown during extraction, ios_base::badbit
155 * will be turned on in the stream's error state without causing an
156 * ios_base::failure to be thrown. The original exception will then
162 * @brief Integer arithmetic extractors
163 * @param __n A variable of builtin integral type.
164 * @return @c *this if successful
166 * These functions use the stream's current locale (specifically, the
167 * @c num_get facet) to parse the input data.
170 operator>>(bool& __n)
171 { return _M_extract(__n); }
174 operator>>(short& __n);
177 operator>>(unsigned short& __n)
178 { return _M_extract(__n); }
181 operator>>(int& __n);
184 operator>>(unsigned int& __n)
185 { return _M_extract(__n); }
188 operator>>(long& __n)
189 { return _M_extract(__n); }
192 operator>>(unsigned long& __n)
193 { return _M_extract(__n); }
195 #ifdef _GLIBCXX_USE_LONG_LONG
197 operator>>(long long& __n)
198 { return _M_extract(__n); }
201 operator>>(unsigned long long& __n)
202 { return _M_extract(__n); }
208 * @brief Floating point arithmetic extractors
209 * @param __f A variable of builtin floating point type.
210 * @return @c *this if successful
212 * These functions use the stream's current locale (specifically, the
213 * @c num_get facet) to parse the input data.
216 operator>>(float& __f)
217 { return _M_extract(__f); }
220 operator>>(double& __f)
221 { return _M_extract(__f); }
224 operator>>(long double& __f)
225 { return _M_extract(__f); }
229 * @brief Basic arithmetic extractors
230 * @param __p A variable of pointer type.
231 * @return @c *this if successful
233 * These functions use the stream's current locale (specifically, the
234 * @c num_get facet) to parse the input data.
237 operator>>(void*& __p)
238 { return _M_extract(__p); }
241 * @brief Extracting into another streambuf.
242 * @param __sb A pointer to a streambuf
244 * This function behaves like one of the basic arithmetic extractors,
245 * in that it also constructs a sentry object and has the same error
248 * If @p __sb is NULL, the stream will set failbit in its error state.
250 * Characters are extracted from this stream and inserted into the
251 * @p __sb streambuf until one of the following occurs:
253 * - the input stream reaches end-of-file,
254 * - insertion into the output buffer fails (in this case, the
255 * character that would have been inserted is not extracted), or
256 * - an exception occurs (and in this case is caught)
258 * If the function inserts no characters, failbit is set.
261 operator>>(__streambuf_type* __sb);
264 // [27.6.1.3] unformatted input
266 * @brief Character counting
267 * @return The number of characters extracted by the previous
268 * unformatted input function dispatched for this stream.
272 { return _M_gcount; }
276 * @name Unformatted Input Functions
278 * All the unformatted input functions have some common behavior.
279 * Each starts by constructing a temporary object of type
280 * std::basic_istream::sentry with the second argument (noskipws)
281 * set to true. This has several effects, concluding with the
282 * setting of a status flag; see the sentry documentation for more.
284 * If the sentry status is good, the function tries to extract
285 * whatever data is appropriate for the type of the argument.
287 * The number of characters extracted is stored for later retrieval
290 * If an exception is thrown during extraction, ios_base::badbit
291 * will be turned on in the stream's error state without causing an
292 * ios_base::failure to be thrown. The original exception will then
297 * @brief Simple extraction.
298 * @return A character, or eof().
300 * Tries to extract a character. If none are available, sets failbit
301 * and returns traits::eof().
307 * @brief Simple extraction.
308 * @param __c The character in which to store data.
311 * Tries to extract a character and store it in @a __c. If none are
312 * available, sets failbit and returns traits::eof().
314 * @note This function is not overloaded on signed char and
321 * @brief Simple multiple-character extraction.
322 * @param __s Pointer to an array.
323 * @param __n Maximum number of characters to store in @a __s.
324 * @param __delim A "stop" character.
327 * Characters are extracted and stored into @a __s until one of the
330 * - @c __n-1 characters are stored
331 * - the input sequence reaches EOF
332 * - the next character equals @a __delim, in which case the character
335 * If no characters are stored, failbit is set in the stream's error
338 * In any case, a null character is stored into the next location in
341 * @note This function is not overloaded on signed char and
345 get(char_type* __s, streamsize __n, char_type __delim);
348 * @brief Simple multiple-character extraction.
349 * @param __s Pointer to an array.
350 * @param __n Maximum number of characters to store in @a s.
353 * Returns @c get(__s,__n,widen('\\n')).
356 get(char_type* __s, streamsize __n)
357 { return this->get(__s, __n, this->widen('\n')); }
360 * @brief Extraction into another streambuf.
361 * @param __sb A streambuf in which to store data.
362 * @param __delim A "stop" character.
365 * Characters are extracted and inserted into @a __sb until one of the
368 * - the input sequence reaches EOF
369 * - insertion into the output buffer fails (in this case, the
370 * character that would have been inserted is not extracted)
371 * - the next character equals @a __delim (in this case, the character
373 * - an exception occurs (and in this case is caught)
375 * If no characters are stored, failbit is set in the stream's error
379 get(__streambuf_type& __sb, char_type __delim);
382 * @brief Extraction into another streambuf.
383 * @param __sb A streambuf in which to store data.
386 * Returns @c get(__sb,widen('\\n')).
389 get(__streambuf_type& __sb)
390 { return this->get(__sb, this->widen('\n')); }
393 * @brief String extraction.
394 * @param __s A character array in which to store the data.
395 * @param __n Maximum number of characters to extract.
396 * @param __delim A "stop" character.
399 * Extracts and stores characters into @a __s until one of the
400 * following happens. Note that these criteria are required to be
401 * tested in the order listed here, to allow an input line to exactly
402 * fill the @a __s array without setting failbit.
404 * -# the input sequence reaches end-of-file, in which case eofbit
405 * is set in the stream error state
406 * -# the next character equals @c __delim, in which case the character
407 * is extracted (and therefore counted in @c gcount()) but not stored
408 * -# @c __n-1 characters are stored, in which case failbit is set
409 * in the stream error state
411 * If no characters are extracted, failbit is set. (An empty line of
412 * input should therefore not cause failbit to be set.)
414 * In any case, a null character is stored in the next location in
418 getline(char_type* __s, streamsize __n, char_type __delim);
421 * @brief String extraction.
422 * @param __s A character array in which to store the data.
423 * @param __n Maximum number of characters to extract.
426 * Returns @c getline(__s,__n,widen('\\n')).
429 getline(char_type* __s, streamsize __n)
430 { return this->getline(__s, __n, this->widen('\n')); }
433 * @brief Discarding characters
434 * @param __n Number of characters to discard.
435 * @param __delim A "stop" character.
438 * Extracts characters and throws them away until one of the
440 * - if @a __n @c != @c std::numeric_limits<int>::max(), @a __n
441 * characters are extracted
442 * - the input sequence reaches end-of-file
443 * - the next character equals @a __delim (in this case, the character
444 * is extracted); note that this condition will never occur if
445 * @a __delim equals @c traits::eof().
447 * NB: Provide three overloads, instead of the single function
448 * (with defaults) mandated by the Standard: this leads to a
449 * better performing implementation, while still conforming to
453 ignore(streamsize __n, int_type __delim);
456 ignore(streamsize __n);
462 * @brief Looking ahead in the stream
463 * @return The next character, or eof().
465 * If, after constructing the sentry object, @c good() is false,
466 * returns @c traits::eof(). Otherwise reads but does not extract
467 * the next input character.
473 * @brief Extraction without delimiters.
474 * @param __s A character array.
475 * @param __n Maximum number of characters to store.
478 * If the stream state is @c good(), extracts characters and stores
479 * them into @a __s until one of the following happens:
480 * - @a __n characters are stored
481 * - the input sequence reaches end-of-file, in which case the error
482 * state is set to @c failbit|eofbit.
484 * @note This function is not overloaded on signed char and
488 read(char_type* __s, streamsize __n);
491 * @brief Extraction until the buffer is exhausted, but no more.
492 * @param __s A character array.
493 * @param __n Maximum number of characters to store.
494 * @return The number of characters extracted.
496 * Extracts characters and stores them into @a __s depending on the
497 * number of characters remaining in the streambuf's buffer,
498 * @c rdbuf()->in_avail(), called @c A here:
499 * - if @c A @c == @c -1, sets eofbit and extracts no characters
500 * - if @c A @c == @c 0, extracts no characters
501 * - if @c A @c > @c 0, extracts @c min(A,n)
503 * The goal is to empty the current buffer, and to not request any
504 * more from the external input sequence controlled by the streambuf.
507 readsome(char_type* __s, streamsize __n);
510 * @brief Unextracting a single character.
511 * @param __c The character to push back into the input stream.
514 * If @c rdbuf() is not null, calls @c rdbuf()->sputbackc(c).
516 * If @c rdbuf() is null or if @c sputbackc() fails, sets badbit in
519 * @note This function first clears eofbit. Since no characters
520 * are extracted, the next call to @c gcount() will return 0,
521 * as required by DR 60.
524 putback(char_type __c);
527 * @brief Unextracting the previous character.
530 * If @c rdbuf() is not null, calls @c rdbuf()->sungetc(c).
532 * If @c rdbuf() is null or if @c sungetc() fails, sets badbit in
535 * @note This function first clears eofbit. Since no characters
536 * are extracted, the next call to @c gcount() will return 0,
537 * as required by DR 60.
543 * @brief Synchronizing the stream buffer.
544 * @return 0 on success, -1 on failure
546 * If @c rdbuf() is a null pointer, returns -1.
548 * Otherwise, calls @c rdbuf()->pubsync(), and if that returns -1,
549 * sets badbit and returns -1.
551 * Otherwise, returns 0.
553 * @note This function does not count the number of characters
554 * extracted, if any, and therefore does not affect the next
555 * call to @c gcount().
561 * @brief Getting the current read position.
562 * @return A file position object.
564 * If @c fail() is not false, returns @c pos_type(-1) to indicate
565 * failure. Otherwise returns @c rdbuf()->pubseekoff(0,cur,in).
567 * @note This function does not count the number of characters
568 * extracted, if any, and therefore does not affect the next
569 * call to @c gcount(). At variance with putback, unget and
570 * seekg, eofbit is not cleared first.
576 * @brief Changing the current read position.
577 * @param __pos A file position object.
580 * If @c fail() is not true, calls @c rdbuf()->pubseekpos(__pos). If
581 * that function fails, sets failbit.
583 * @note This function first clears eofbit. It does not count the
584 * number of characters extracted, if any, and therefore does
585 * not affect the next call to @c gcount().
591 * @brief Changing the current read position.
592 * @param __off A file offset object.
593 * @param __dir The direction in which to seek.
596 * If @c fail() is not true, calls @c rdbuf()->pubseekoff(__off,__dir).
597 * If that function fails, sets failbit.
599 * @note This function first clears eofbit. It does not count the
600 * number of characters extracted, if any, and therefore does
601 * not affect the next call to @c gcount().
604 seekg(off_type, ios_base::seekdir);
609 : _M_gcount(streamsize(0))
612 template<typename _ValueT>
614 _M_extract(_ValueT& __v);
617 /// Explicit specialization declarations, defined in src/istream.cc.
620 basic_istream<char>::
621 getline(char_type* __s, streamsize __n, char_type __delim);
625 basic_istream<char>::
626 ignore(streamsize __n);
630 basic_istream<char>::
631 ignore(streamsize __n, int_type __delim);
633 #ifdef _GLIBCXX_USE_WCHAR_T
635 basic_istream<wchar_t>&
636 basic_istream<wchar_t>::
637 getline(char_type* __s, streamsize __n, char_type __delim);
640 basic_istream<wchar_t>&
641 basic_istream<wchar_t>::
642 ignore(streamsize __n);
645 basic_istream<wchar_t>&
646 basic_istream<wchar_t>::
647 ignore(streamsize __n, int_type __delim);
651 * @brief Performs setup work for input streams.
653 * Objects of this class are created before all of the standard
654 * extractors are run. It is responsible for <em>exception-safe
655 * prefix and suffix operations,</em> although only prefix actions
656 * are currently required by the standard.
658 template<typename _CharT, typename _Traits>
659 class basic_istream<_CharT, _Traits>::sentry
665 /// Easy access to dependant types.
666 typedef _Traits traits_type;
667 typedef basic_streambuf<_CharT, _Traits> __streambuf_type;
668 typedef basic_istream<_CharT, _Traits> __istream_type;
669 typedef typename __istream_type::__ctype_type __ctype_type;
670 typedef typename _Traits::int_type __int_type;
673 * @brief The constructor performs all the work.
674 * @param __is The input stream to guard.
675 * @param __noskipws Whether to consume whitespace or not.
677 * If the stream state is good (@a __is.good() is true), then the
678 * following actions are performed, otherwise the sentry state
679 * is false (<em>not okay</em>) and failbit is set in the
682 * The sentry's preparatory actions are:
684 * -# if the stream is tied to an output stream, @c is.tie()->flush()
685 * is called to synchronize the output sequence
686 * -# if @a __noskipws is false, and @c ios_base::skipws is set in
687 * @c is.flags(), the sentry extracts and discards whitespace
688 * characters from the stream. The currently imbued locale is
689 * used to determine whether each character is whitespace.
691 * If the stream state is still good, then the sentry state becomes
695 sentry(basic_istream<_CharT, _Traits>& __is, bool __noskipws = false);
698 * @brief Quick status checking.
699 * @return The sentry state.
701 * For ease of use, sentries may be converted to booleans. The
702 * return value is that of the sentry state (true == okay).
704 #if __cplusplus >= 201103L
707 operator bool() const
713 * @brief Character extractors
714 * @param __in An input stream.
715 * @param __c A character reference.
718 * Behaves like one of the formatted arithmetic extractors described in
719 * std::basic_istream. After constructing a sentry object with good
720 * status, this function extracts a character (if one is available) and
721 * stores it in @a __c. Otherwise, sets failbit in the input stream.
723 template<typename _CharT, typename _Traits>
724 basic_istream<_CharT, _Traits>&
725 operator>>(basic_istream<_CharT, _Traits>& __in, _CharT& __c);
727 template<class _Traits>
728 inline basic_istream<char, _Traits>&
729 operator>>(basic_istream<char, _Traits>& __in, unsigned char& __c)
730 { return (__in >> reinterpret_cast<char&>(__c)); }
732 template<class _Traits>
733 inline basic_istream<char, _Traits>&
734 operator>>(basic_istream<char, _Traits>& __in, signed char& __c)
735 { return (__in >> reinterpret_cast<char&>(__c)); }
740 * @brief Character string extractors
741 * @param __in An input stream.
742 * @param __s A pointer to a character array.
745 * Behaves like one of the formatted arithmetic extractors described in
746 * std::basic_istream. After constructing a sentry object with good
747 * status, this function extracts up to @c n characters and stores them
748 * into the array starting at @a __s. @c n is defined as:
750 * - if @c width() is greater than zero, @c n is width() otherwise
751 * - @c n is <em>the number of elements of the largest array of *
752 * - @c char_type that can store a terminating @c eos.</em>
755 * Characters are extracted and stored until one of the following happens:
756 * - @c n-1 characters are stored
758 * - the next character is whitespace according to the current locale
759 * - the next character is a null byte (i.e., @c charT() )
761 * @c width(0) is then called for the input stream.
763 * If no characters are extracted, sets failbit.
765 template<typename _CharT, typename _Traits>
766 basic_istream<_CharT, _Traits>&
767 operator>>(basic_istream<_CharT, _Traits>& __in, _CharT* __s);
769 // Explicit specialization declaration, defined in src/istream.cc.
772 operator>>(basic_istream<char>& __in, char* __s);
774 template<class _Traits>
775 inline basic_istream<char, _Traits>&
776 operator>>(basic_istream<char, _Traits>& __in, unsigned char* __s)
777 { return (__in >> reinterpret_cast<char*>(__s)); }
779 template<class _Traits>
780 inline basic_istream<char, _Traits>&
781 operator>>(basic_istream<char, _Traits>& __in, signed char* __s)
782 { return (__in >> reinterpret_cast<char*>(__s)); }
786 * @brief Template class basic_iostream
789 * @tparam _CharT Type of character stream.
790 * @tparam _Traits Traits for character type, defaults to
791 * char_traits<_CharT>.
793 * This class multiply inherits from the input and output stream classes
794 * simply to provide a single interface.
796 template<typename _CharT, typename _Traits>
798 : public basic_istream<_CharT, _Traits>,
799 public basic_ostream<_CharT, _Traits>
802 // _GLIBCXX_RESOLVE_LIB_DEFECTS
803 // 271. basic_iostream missing typedefs
804 // Types (inherited):
805 typedef _CharT char_type;
806 typedef typename _Traits::int_type int_type;
807 typedef typename _Traits::pos_type pos_type;
808 typedef typename _Traits::off_type off_type;
809 typedef _Traits traits_type;
811 // Non-standard Types:
812 typedef basic_istream<_CharT, _Traits> __istream_type;
813 typedef basic_ostream<_CharT, _Traits> __ostream_type;
816 * @brief Constructor does nothing.
818 * Both of the parent classes are initialized with the same
819 * streambuf pointer passed to this constructor.
822 basic_iostream(basic_streambuf<_CharT, _Traits>* __sb)
823 : __istream_type(__sb), __ostream_type(__sb) { }
826 * @brief Destructor does nothing.
829 ~basic_iostream() { }
833 : __istream_type(), __ostream_type() { }
837 * @brief Quick and easy way to eat whitespace
839 * This manipulator extracts whitespace characters, stopping when the
840 * next character is non-whitespace, or when the input sequence is empty.
841 * If the sequence is empty, @c eofbit is set in the stream, but not
844 * The current locale is used to distinguish whitespace characters.
850 * std::cin >> std::ws >> mc;
852 * will skip leading whitespace before calling operator>> on cin and your
853 * object. Note that the same effect can be achieved by creating a
854 * std::basic_istream::sentry inside your definition of operator>>.
856 template<typename _CharT, typename _Traits>
857 basic_istream<_CharT, _Traits>&
858 ws(basic_istream<_CharT, _Traits>& __is);
860 #if __cplusplus >= 201103L
861 // [27.7.1.6] Rvalue stream extraction
863 * @brief Generic extractor for rvalue stream
864 * @param __is An input stream.
865 * @param __x A reference to the extraction target.
868 * This is just a forwarding function to allow extraction from
869 * rvalue streams since they won't bind to the extractor functions
870 * that take an lvalue reference.
872 template<typename _CharT, typename _Traits, typename _Tp>
873 inline basic_istream<_CharT, _Traits>&
874 operator>>(basic_istream<_CharT, _Traits>&& __is, _Tp& __x)
875 { return (__is >> __x); }
878 _GLIBCXX_END_NAMESPACE_VERSION
881 #include <bits/istream.tcc>
883 #endif /* _GLIBCXX_ISTREAM */