1 // Input streams -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 // 2006, 2007, 2008, 2009, 2010, 2011
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 * This is the base class for all input streams. It provides text
52 * formatting of all builtin types, and communicates with any class
53 * derived from basic_streambuf to do the actual input.
55 template<typename _CharT, typename _Traits>
56 class basic_istream : virtual public basic_ios<_CharT, _Traits>
59 // Types (inherited from basic_ios (27.4.4)):
60 typedef _CharT char_type;
61 typedef typename _Traits::int_type int_type;
62 typedef typename _Traits::pos_type pos_type;
63 typedef typename _Traits::off_type off_type;
64 typedef _Traits traits_type;
66 // Non-standard Types:
67 typedef basic_streambuf<_CharT, _Traits> __streambuf_type;
68 typedef basic_ios<_CharT, _Traits> __ios_type;
69 typedef basic_istream<_CharT, _Traits> __istream_type;
70 typedef num_get<_CharT, istreambuf_iterator<_CharT, _Traits> >
72 typedef ctype<_CharT> __ctype_type;
77 * The number of characters extracted in the previous unformatted
78 * function; see gcount().
84 * @brief Base constructor.
86 * This ctor is almost never called by the user directly, rather from
87 * derived classes' initialization lists, which pass a pointer to
88 * their own stream buffer.
91 basic_istream(__streambuf_type* __sb)
92 : _M_gcount(streamsize(0))
96 * @brief Base destructor.
98 * This does very little apart from providing a virtual base dtor.
102 { _M_gcount = streamsize(0); }
104 /// Safe prefix/suffix operations.
110 * @brief Interface for manipulators.
112 * Manipulators such as @c std::ws and @c std::dec use these
113 * functions in constructs like
114 * <code>std::cin >> std::ws</code>.
115 * For more information, see the iomanip header.
118 operator>>(__istream_type& (*__pf)(__istream_type&))
119 { return __pf(*this); }
122 operator>>(__ios_type& (*__pf)(__ios_type&))
129 operator>>(ios_base& (*__pf)(ios_base&))
140 * All the @c operator>> functions (aka <em>formatted input
141 * functions</em>) have some common behavior. Each starts by
142 * constructing a temporary object of type std::basic_istream::sentry
143 * with the second argument (noskipws) set to false. This has several
144 * effects, concluding with the setting of a status flag; see the
145 * sentry documentation for more.
147 * If the sentry status is good, the function tries to extract
148 * whatever data is appropriate for the type of the argument.
150 * If an exception is thrown during extraction, ios_base::badbit
151 * will be turned on in the stream's error state without causing an
152 * ios_base::failure to be thrown. The original exception will then
158 * @brief Integer arithmetic extractors
159 * @param __n A variable of builtin integral type.
160 * @return @c *this if successful
162 * These functions use the stream's current locale (specifically, the
163 * @c num_get facet) to parse the input data.
166 operator>>(bool& __n)
167 { return _M_extract(__n); }
170 operator>>(short& __n);
173 operator>>(unsigned short& __n)
174 { return _M_extract(__n); }
177 operator>>(int& __n);
180 operator>>(unsigned int& __n)
181 { return _M_extract(__n); }
184 operator>>(long& __n)
185 { return _M_extract(__n); }
188 operator>>(unsigned long& __n)
189 { return _M_extract(__n); }
191 #ifdef _GLIBCXX_USE_LONG_LONG
193 operator>>(long long& __n)
194 { return _M_extract(__n); }
197 operator>>(unsigned long long& __n)
198 { return _M_extract(__n); }
204 * @brief Floating point arithmetic extractors
205 * @param __f A variable of builtin floating point type.
206 * @return @c *this if successful
208 * These functions use the stream's current locale (specifically, the
209 * @c num_get facet) to parse the input data.
212 operator>>(float& __f)
213 { return _M_extract(__f); }
216 operator>>(double& __f)
217 { return _M_extract(__f); }
220 operator>>(long double& __f)
221 { return _M_extract(__f); }
225 * @brief Basic arithmetic extractors
226 * @param __p A variable of pointer type.
227 * @return @c *this if successful
229 * These functions use the stream's current locale (specifically, the
230 * @c num_get facet) to parse the input data.
233 operator>>(void*& __p)
234 { return _M_extract(__p); }
237 * @brief Extracting into another streambuf.
238 * @param __sb A pointer to a streambuf
240 * This function behaves like one of the basic arithmetic extractors,
241 * in that it also constructs a sentry object and has the same error
244 * If @p __sb is NULL, the stream will set failbit in its error state.
246 * Characters are extracted from this stream and inserted into the
247 * @p __sb streambuf until one of the following occurs:
249 * - the input stream reaches end-of-file,
250 * - insertion into the output buffer fails (in this case, the
251 * character that would have been inserted is not extracted), or
252 * - an exception occurs (and in this case is caught)
254 * If the function inserts no characters, failbit is set.
257 operator>>(__streambuf_type* __sb);
260 // [27.6.1.3] unformatted input
262 * @brief Character counting
263 * @return The number of characters extracted by the previous
264 * unformatted input function dispatched for this stream.
268 { return _M_gcount; }
272 * @name Unformatted Input Functions
274 * All the unformatted input functions have some common behavior.
275 * Each starts by constructing a temporary object of type
276 * std::basic_istream::sentry with the second argument (noskipws)
277 * set to true. This has several effects, concluding with the
278 * setting of a status flag; see the sentry documentation for more.
280 * If the sentry status is good, the function tries to extract
281 * whatever data is appropriate for the type of the argument.
283 * The number of characters extracted is stored for later retrieval
286 * If an exception is thrown during extraction, ios_base::badbit
287 * will be turned on in the stream's error state without causing an
288 * ios_base::failure to be thrown. The original exception will then
293 * @brief Simple extraction.
294 * @return A character, or eof().
296 * Tries to extract a character. If none are available, sets failbit
297 * and returns traits::eof().
303 * @brief Simple extraction.
304 * @param __c The character in which to store data.
307 * Tries to extract a character and store it in @a __c. If none are
308 * available, sets failbit and returns traits::eof().
310 * @note This function is not overloaded on signed char and
317 * @brief Simple multiple-character extraction.
318 * @param __s Pointer to an array.
319 * @param __n Maximum number of characters to store in @a __s.
320 * @param __delim A "stop" character.
323 * Characters are extracted and stored into @a __s until one of the
326 * - @c __n-1 characters are stored
327 * - the input sequence reaches EOF
328 * - the next character equals @a __delim, in which case the character
331 * If no characters are stored, failbit is set in the stream's error
334 * In any case, a null character is stored into the next location in
337 * @note This function is not overloaded on signed char and
341 get(char_type* __s, streamsize __n, char_type __delim);
344 * @brief Simple multiple-character extraction.
345 * @param __s Pointer to an array.
346 * @param __n Maximum number of characters to store in @a s.
349 * Returns @c get(__s,__n,widen('\\n')).
352 get(char_type* __s, streamsize __n)
353 { return this->get(__s, __n, this->widen('\n')); }
356 * @brief Extraction into another streambuf.
357 * @param __sb A streambuf in which to store data.
358 * @param __delim A "stop" character.
361 * Characters are extracted and inserted into @a __sb until one of the
364 * - the input sequence reaches EOF
365 * - insertion into the output buffer fails (in this case, the
366 * character that would have been inserted is not extracted)
367 * - the next character equals @a __delim (in this case, the character
369 * - an exception occurs (and in this case is caught)
371 * If no characters are stored, failbit is set in the stream's error
375 get(__streambuf_type& __sb, char_type __delim);
378 * @brief Extraction into another streambuf.
379 * @param __sb A streambuf in which to store data.
382 * Returns @c get(__sb,widen('\\n')).
385 get(__streambuf_type& __sb)
386 { return this->get(__sb, this->widen('\n')); }
389 * @brief String extraction.
390 * @param __s A character array in which to store the data.
391 * @param __n Maximum number of characters to extract.
392 * @param __delim A "stop" character.
395 * Extracts and stores characters into @a __s until one of the
396 * following happens. Note that these criteria are required to be
397 * tested in the order listed here, to allow an input line to exactly
398 * fill the @a __s array without setting failbit.
400 * -# the input sequence reaches end-of-file, in which case eofbit
401 * is set in the stream error state
402 * -# the next character equals @c __delim, in which case the character
403 * is extracted (and therefore counted in @c gcount()) but not stored
404 * -# @c __n-1 characters are stored, in which case failbit is set
405 * in the stream error state
407 * If no characters are extracted, failbit is set. (An empty line of
408 * input should therefore not cause failbit to be set.)
410 * In any case, a null character is stored in the next location in
414 getline(char_type* __s, streamsize __n, char_type __delim);
417 * @brief String extraction.
418 * @param __s A character array in which to store the data.
419 * @param __n Maximum number of characters to extract.
422 * Returns @c getline(__s,__n,widen('\\n')).
425 getline(char_type* __s, streamsize __n)
426 { return this->getline(__s, __n, this->widen('\n')); }
429 * @brief Discarding characters
430 * @param __n Number of characters to discard.
431 * @param __delim A "stop" character.
434 * Extracts characters and throws them away until one of the
436 * - if @a __n @c != @c std::numeric_limits<int>::max(), @a __n
437 * characters are extracted
438 * - the input sequence reaches end-of-file
439 * - the next character equals @a __delim (in this case, the character
440 * is extracted); note that this condition will never occur if
441 * @a __delim equals @c traits::eof().
443 * NB: Provide three overloads, instead of the single function
444 * (with defaults) mandated by the Standard: this leads to a
445 * better performing implementation, while still conforming to
449 ignore(streamsize __n, int_type __delim);
452 ignore(streamsize __n);
458 * @brief Looking ahead in the stream
459 * @return The next character, or eof().
461 * If, after constructing the sentry object, @c good() is false,
462 * returns @c traits::eof(). Otherwise reads but does not extract
463 * the next input character.
469 * @brief Extraction without delimiters.
470 * @param __s A character array.
471 * @param __n Maximum number of characters to store.
474 * If the stream state is @c good(), extracts characters and stores
475 * them into @a __s until one of the following happens:
476 * - @a __n characters are stored
477 * - the input sequence reaches end-of-file, in which case the error
478 * state is set to @c failbit|eofbit.
480 * @note This function is not overloaded on signed char and
484 read(char_type* __s, streamsize __n);
487 * @brief Extraction until the buffer is exhausted, but no more.
488 * @param __s A character array.
489 * @param __n Maximum number of characters to store.
490 * @return The number of characters extracted.
492 * Extracts characters and stores them into @a __s depending on the
493 * number of characters remaining in the streambuf's buffer,
494 * @c rdbuf()->in_avail(), called @c A here:
495 * - if @c A @c == @c -1, sets eofbit and extracts no characters
496 * - if @c A @c == @c 0, extracts no characters
497 * - if @c A @c > @c 0, extracts @c min(A,n)
499 * The goal is to empty the current buffer, and to not request any
500 * more from the external input sequence controlled by the streambuf.
503 readsome(char_type* __s, streamsize __n);
506 * @brief Unextracting a single character.
507 * @param __c The character to push back into the input stream.
510 * If @c rdbuf() is not null, calls @c rdbuf()->sputbackc(c).
512 * If @c rdbuf() is null or if @c sputbackc() fails, sets badbit in
515 * @note This function first clears eofbit. Since no characters
516 * are extracted, the next call to @c gcount() will return 0,
517 * as required by DR 60.
520 putback(char_type __c);
523 * @brief Unextracting the previous character.
526 * If @c rdbuf() is not null, calls @c rdbuf()->sungetc(c).
528 * If @c rdbuf() is null or if @c sungetc() fails, sets badbit in
531 * @note This function first clears eofbit. Since no characters
532 * are extracted, the next call to @c gcount() will return 0,
533 * as required by DR 60.
539 * @brief Synchronizing the stream buffer.
540 * @return 0 on success, -1 on failure
542 * If @c rdbuf() is a null pointer, returns -1.
544 * Otherwise, calls @c rdbuf()->pubsync(), and if that returns -1,
545 * sets badbit and returns -1.
547 * Otherwise, returns 0.
549 * @note This function does not count the number of characters
550 * extracted, if any, and therefore does not affect the next
551 * call to @c gcount().
557 * @brief Getting the current read position.
558 * @return A file position object.
560 * If @c fail() is not false, returns @c pos_type(-1) to indicate
561 * failure. Otherwise returns @c rdbuf()->pubseekoff(0,cur,in).
563 * @note This function does not count the number of characters
564 * extracted, if any, and therefore does not affect the next
565 * call to @c gcount(). At variance with putback, unget and
566 * seekg, eofbit is not cleared first.
572 * @brief Changing the current read position.
573 * @param __pos A file position object.
576 * If @c fail() is not true, calls @c rdbuf()->pubseekpos(__pos). If
577 * that function fails, sets failbit.
579 * @note This function first clears eofbit. It does not count the
580 * number of characters extracted, if any, and therefore does
581 * not affect the next call to @c gcount().
587 * @brief Changing the current read position.
588 * @param __off A file offset object.
589 * @param __dir The direction in which to seek.
592 * If @c fail() is not true, calls @c rdbuf()->pubseekoff(__off,__dir).
593 * If that function fails, sets failbit.
595 * @note This function first clears eofbit. It does not count the
596 * number of characters extracted, if any, and therefore does
597 * not affect the next call to @c gcount().
600 seekg(off_type, ios_base::seekdir);
605 : _M_gcount(streamsize(0))
608 template<typename _ValueT>
610 _M_extract(_ValueT& __v);
613 /// Explicit specialization declarations, defined in src/istream.cc.
616 basic_istream<char>::
617 getline(char_type* __s, streamsize __n, char_type __delim);
621 basic_istream<char>::
622 ignore(streamsize __n);
626 basic_istream<char>::
627 ignore(streamsize __n, int_type __delim);
629 #ifdef _GLIBCXX_USE_WCHAR_T
631 basic_istream<wchar_t>&
632 basic_istream<wchar_t>::
633 getline(char_type* __s, streamsize __n, char_type __delim);
636 basic_istream<wchar_t>&
637 basic_istream<wchar_t>::
638 ignore(streamsize __n);
641 basic_istream<wchar_t>&
642 basic_istream<wchar_t>::
643 ignore(streamsize __n, int_type __delim);
647 * @brief Performs setup work for input streams.
649 * Objects of this class are created before all of the standard
650 * extractors are run. It is responsible for <em>exception-safe
651 * prefix and suffix operations,</em> although only prefix actions
652 * are currently required by the standard.
654 template<typename _CharT, typename _Traits>
655 class basic_istream<_CharT, _Traits>::sentry
661 /// Easy access to dependant types.
662 typedef _Traits traits_type;
663 typedef basic_streambuf<_CharT, _Traits> __streambuf_type;
664 typedef basic_istream<_CharT, _Traits> __istream_type;
665 typedef typename __istream_type::__ctype_type __ctype_type;
666 typedef typename _Traits::int_type __int_type;
669 * @brief The constructor performs all the work.
670 * @param __is The input stream to guard.
671 * @param __noskipws Whether to consume whitespace or not.
673 * If the stream state is good (@a __is.good() is true), then the
674 * following actions are performed, otherwise the sentry state
675 * is false (<em>not okay</em>) and failbit is set in the
678 * The sentry's preparatory actions are:
680 * -# if the stream is tied to an output stream, @c is.tie()->flush()
681 * is called to synchronize the output sequence
682 * -# if @a __noskipws is false, and @c ios_base::skipws is set in
683 * @c is.flags(), the sentry extracts and discards whitespace
684 * characters from the stream. The currently imbued locale is
685 * used to determine whether each character is whitespace.
687 * If the stream state is still good, then the sentry state becomes
691 sentry(basic_istream<_CharT, _Traits>& __is, bool __noskipws = false);
694 * @brief Quick status checking.
695 * @return The sentry state.
697 * For ease of use, sentries may be converted to booleans. The
698 * return value is that of the sentry state (true == okay).
700 #ifdef __GXX_EXPERIMENTAL_CXX0X__
703 operator bool() const
709 * @brief Character extractors
710 * @param __in An input stream.
711 * @param __c A character reference.
714 * Behaves like one of the formatted arithmetic extractors described in
715 * std::basic_istream. After constructing a sentry object with good
716 * status, this function extracts a character (if one is available) and
717 * stores it in @a __c. Otherwise, sets failbit in the input stream.
719 template<typename _CharT, typename _Traits>
720 basic_istream<_CharT, _Traits>&
721 operator>>(basic_istream<_CharT, _Traits>& __in, _CharT& __c);
723 template<class _Traits>
724 inline basic_istream<char, _Traits>&
725 operator>>(basic_istream<char, _Traits>& __in, unsigned char& __c)
726 { return (__in >> reinterpret_cast<char&>(__c)); }
728 template<class _Traits>
729 inline basic_istream<char, _Traits>&
730 operator>>(basic_istream<char, _Traits>& __in, signed char& __c)
731 { return (__in >> reinterpret_cast<char&>(__c)); }
736 * @brief Character string extractors
737 * @param __in An input stream.
738 * @param __s A pointer to a character array.
741 * Behaves like one of the formatted arithmetic extractors described in
742 * std::basic_istream. After constructing a sentry object with good
743 * status, this function extracts up to @c n characters and stores them
744 * into the array starting at @a __s. @c n is defined as:
746 * - if @c width() is greater than zero, @c n is width() otherwise
747 * - @c n is <em>the number of elements of the largest array of *
748 * - @c char_type that can store a terminating @c eos.</em>
751 * Characters are extracted and stored until one of the following happens:
752 * - @c n-1 characters are stored
754 * - the next character is whitespace according to the current locale
755 * - the next character is a null byte (i.e., @c charT() )
757 * @c width(0) is then called for the input stream.
759 * If no characters are extracted, sets failbit.
761 template<typename _CharT, typename _Traits>
762 basic_istream<_CharT, _Traits>&
763 operator>>(basic_istream<_CharT, _Traits>& __in, _CharT* __s);
765 // Explicit specialization declaration, defined in src/istream.cc.
768 operator>>(basic_istream<char>& __in, char* __s);
770 template<class _Traits>
771 inline basic_istream<char, _Traits>&
772 operator>>(basic_istream<char, _Traits>& __in, unsigned char* __s)
773 { return (__in >> reinterpret_cast<char*>(__s)); }
775 template<class _Traits>
776 inline basic_istream<char, _Traits>&
777 operator>>(basic_istream<char, _Traits>& __in, signed char* __s)
778 { return (__in >> reinterpret_cast<char*>(__s)); }
782 * @brief Template class basic_iostream
785 * This class multiply inherits from the input and output stream classes
786 * simply to provide a single interface.
788 template<typename _CharT, typename _Traits>
790 : public basic_istream<_CharT, _Traits>,
791 public basic_ostream<_CharT, _Traits>
794 // _GLIBCXX_RESOLVE_LIB_DEFECTS
795 // 271. basic_iostream missing typedefs
796 // Types (inherited):
797 typedef _CharT char_type;
798 typedef typename _Traits::int_type int_type;
799 typedef typename _Traits::pos_type pos_type;
800 typedef typename _Traits::off_type off_type;
801 typedef _Traits traits_type;
803 // Non-standard Types:
804 typedef basic_istream<_CharT, _Traits> __istream_type;
805 typedef basic_ostream<_CharT, _Traits> __ostream_type;
808 * @brief Constructor does nothing.
810 * Both of the parent classes are initialized with the same
811 * streambuf pointer passed to this constructor.
814 basic_iostream(basic_streambuf<_CharT, _Traits>* __sb)
815 : __istream_type(__sb), __ostream_type(__sb) { }
818 * @brief Destructor does nothing.
821 ~basic_iostream() { }
825 : __istream_type(), __ostream_type() { }
829 * @brief Quick and easy way to eat whitespace
831 * This manipulator extracts whitespace characters, stopping when the
832 * next character is non-whitespace, or when the input sequence is empty.
833 * If the sequence is empty, @c eofbit is set in the stream, but not
836 * The current locale is used to distinguish whitespace characters.
842 * std::cin >> std::ws >> mc;
844 * will skip leading whitespace before calling operator>> on cin and your
845 * object. Note that the same effect can be achieved by creating a
846 * std::basic_istream::sentry inside your definition of operator>>.
848 template<typename _CharT, typename _Traits>
849 basic_istream<_CharT, _Traits>&
850 ws(basic_istream<_CharT, _Traits>& __is);
852 #ifdef __GXX_EXPERIMENTAL_CXX0X__
853 // [27.7.1.6] Rvalue stream extraction
855 * @brief Generic extractor for rvalue stream
856 * @param __is An input stream.
857 * @param __x A reference to the extraction target.
860 * This is just a forwarding function to allow extraction from
861 * rvalue streams since they won't bind to the extractor functions
862 * that take an lvalue reference.
864 template<typename _CharT, typename _Traits, typename _Tp>
865 inline basic_istream<_CharT, _Traits>&
866 operator>>(basic_istream<_CharT, _Traits>&& __is, _Tp& __x)
867 { return (__is >> __x); }
868 #endif // __GXX_EXPERIMENTAL_CXX0X__
870 _GLIBCXX_END_NAMESPACE_VERSION
873 #include <bits/istream.tcc>
875 #endif /* _GLIBCXX_ISTREAM */