1 // Input streams -*- C++ -*-
3 // Copyright (C) 1997-2013 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
26 // ISO C++ 14882: 27.6.1 Input streams
29 /** @file include/istream
30 * This is a Standard C++ Library header.
33 #ifndef _GLIBCXX_ISTREAM
34 #define _GLIBCXX_ISTREAM 1
36 #pragma GCC system_header
41 namespace std _GLIBCXX_VISIBILITY(default)
43 _GLIBCXX_BEGIN_NAMESPACE_VERSION
46 * @brief Template class basic_istream.
49 * @tparam _CharT Type of character stream.
50 * @tparam _Traits Traits for character type, defaults to
51 * char_traits<_CharT>.
53 * This is the base class for all input streams. It provides text
54 * formatting of all builtin types, and communicates with any class
55 * derived from basic_streambuf to do the actual input.
57 template<typename _CharT, typename _Traits>
58 class basic_istream : virtual public basic_ios<_CharT, _Traits>
61 // Types (inherited from basic_ios (27.4.4)):
62 typedef _CharT char_type;
63 typedef typename _Traits::int_type int_type;
64 typedef typename _Traits::pos_type pos_type;
65 typedef typename _Traits::off_type off_type;
66 typedef _Traits traits_type;
68 // Non-standard Types:
69 typedef basic_streambuf<_CharT, _Traits> __streambuf_type;
70 typedef basic_ios<_CharT, _Traits> __ios_type;
71 typedef basic_istream<_CharT, _Traits> __istream_type;
72 typedef num_get<_CharT, istreambuf_iterator<_CharT, _Traits> >
74 typedef ctype<_CharT> __ctype_type;
79 * The number of characters extracted in the previous unformatted
80 * function; see gcount().
86 * @brief Base constructor.
88 * This ctor is almost never called by the user directly, rather from
89 * derived classes' initialization lists, which pass a pointer to
90 * their own stream buffer.
93 basic_istream(__streambuf_type* __sb)
94 : _M_gcount(streamsize(0))
98 * @brief Base destructor.
100 * This does very little apart from providing a virtual base dtor.
104 { _M_gcount = streamsize(0); }
106 /// Safe prefix/suffix operations.
112 * @brief Interface for manipulators.
114 * Manipulators such as @c std::ws and @c std::dec use these
115 * functions in constructs like
116 * <code>std::cin >> std::ws</code>.
117 * For more information, see the iomanip header.
120 operator>>(__istream_type& (*__pf)(__istream_type&))
121 { return __pf(*this); }
124 operator>>(__ios_type& (*__pf)(__ios_type&))
131 operator>>(ios_base& (*__pf)(ios_base&))
142 * All the @c operator>> functions (aka <em>formatted input
143 * functions</em>) have some common behavior. Each starts by
144 * constructing a temporary object of type std::basic_istream::sentry
145 * with the second argument (noskipws) set to false. This has several
146 * effects, concluding with the setting of a status flag; see the
147 * sentry documentation for more.
149 * If the sentry status is good, the function tries to extract
150 * whatever data is appropriate for the type of the argument.
152 * If an exception is thrown during extraction, ios_base::badbit
153 * will be turned on in the stream's error state without causing an
154 * ios_base::failure to be thrown. The original exception will then
160 * @brief Integer arithmetic extractors
161 * @param __n A variable of builtin integral type.
162 * @return @c *this if successful
164 * These functions use the stream's current locale (specifically, the
165 * @c num_get facet) to parse the input data.
168 operator>>(bool& __n)
169 { return _M_extract(__n); }
172 operator>>(short& __n);
175 operator>>(unsigned short& __n)
176 { return _M_extract(__n); }
179 operator>>(int& __n);
182 operator>>(unsigned int& __n)
183 { return _M_extract(__n); }
186 operator>>(long& __n)
187 { return _M_extract(__n); }
190 operator>>(unsigned long& __n)
191 { return _M_extract(__n); }
193 #ifdef _GLIBCXX_USE_LONG_LONG
195 operator>>(long long& __n)
196 { return _M_extract(__n); }
199 operator>>(unsigned long long& __n)
200 { return _M_extract(__n); }
206 * @brief Floating point arithmetic extractors
207 * @param __f A variable of builtin floating point type.
208 * @return @c *this if successful
210 * These functions use the stream's current locale (specifically, the
211 * @c num_get facet) to parse the input data.
214 operator>>(float& __f)
215 { return _M_extract(__f); }
218 operator>>(double& __f)
219 { return _M_extract(__f); }
222 operator>>(long double& __f)
223 { return _M_extract(__f); }
227 * @brief Basic arithmetic extractors
228 * @param __p A variable of pointer type.
229 * @return @c *this if successful
231 * These functions use the stream's current locale (specifically, the
232 * @c num_get facet) to parse the input data.
235 operator>>(void*& __p)
236 { return _M_extract(__p); }
239 * @brief Extracting into another streambuf.
240 * @param __sb A pointer to a streambuf
242 * This function behaves like one of the basic arithmetic extractors,
243 * in that it also constructs a sentry object and has the same error
246 * If @p __sb is NULL, the stream will set failbit in its error state.
248 * Characters are extracted from this stream and inserted into the
249 * @p __sb streambuf until one of the following occurs:
251 * - the input stream reaches end-of-file,
252 * - insertion into the output buffer fails (in this case, the
253 * character that would have been inserted is not extracted), or
254 * - an exception occurs (and in this case is caught)
256 * If the function inserts no characters, failbit is set.
259 operator>>(__streambuf_type* __sb);
262 // [27.6.1.3] unformatted input
264 * @brief Character counting
265 * @return The number of characters extracted by the previous
266 * unformatted input function dispatched for this stream.
270 { return _M_gcount; }
274 * @name Unformatted Input Functions
276 * All the unformatted input functions have some common behavior.
277 * Each starts by constructing a temporary object of type
278 * std::basic_istream::sentry with the second argument (noskipws)
279 * set to true. This has several effects, concluding with the
280 * setting of a status flag; see the sentry documentation for more.
282 * If the sentry status is good, the function tries to extract
283 * whatever data is appropriate for the type of the argument.
285 * The number of characters extracted is stored for later retrieval
288 * If an exception is thrown during extraction, ios_base::badbit
289 * will be turned on in the stream's error state without causing an
290 * ios_base::failure to be thrown. The original exception will then
295 * @brief Simple extraction.
296 * @return A character, or eof().
298 * Tries to extract a character. If none are available, sets failbit
299 * and returns traits::eof().
305 * @brief Simple extraction.
306 * @param __c The character in which to store data.
309 * Tries to extract a character and store it in @a __c. If none are
310 * available, sets failbit and returns traits::eof().
312 * @note This function is not overloaded on signed char and
319 * @brief Simple multiple-character extraction.
320 * @param __s Pointer to an array.
321 * @param __n Maximum number of characters to store in @a __s.
322 * @param __delim A "stop" character.
325 * Characters are extracted and stored into @a __s until one of the
328 * - @c __n-1 characters are stored
329 * - the input sequence reaches EOF
330 * - the next character equals @a __delim, in which case the character
333 * If no characters are stored, failbit is set in the stream's error
336 * In any case, a null character is stored into the next location in
339 * @note This function is not overloaded on signed char and
343 get(char_type* __s, streamsize __n, char_type __delim);
346 * @brief Simple multiple-character extraction.
347 * @param __s Pointer to an array.
348 * @param __n Maximum number of characters to store in @a s.
351 * Returns @c get(__s,__n,widen('\\n')).
354 get(char_type* __s, streamsize __n)
355 { return this->get(__s, __n, this->widen('\n')); }
358 * @brief Extraction into another streambuf.
359 * @param __sb A streambuf in which to store data.
360 * @param __delim A "stop" character.
363 * Characters are extracted and inserted into @a __sb until one of the
366 * - the input sequence reaches EOF
367 * - insertion into the output buffer fails (in this case, the
368 * character that would have been inserted is not extracted)
369 * - the next character equals @a __delim (in this case, the character
371 * - an exception occurs (and in this case is caught)
373 * If no characters are stored, failbit is set in the stream's error
377 get(__streambuf_type& __sb, char_type __delim);
380 * @brief Extraction into another streambuf.
381 * @param __sb A streambuf in which to store data.
384 * Returns @c get(__sb,widen('\\n')).
387 get(__streambuf_type& __sb)
388 { return this->get(__sb, this->widen('\n')); }
391 * @brief String extraction.
392 * @param __s A character array in which to store the data.
393 * @param __n Maximum number of characters to extract.
394 * @param __delim A "stop" character.
397 * Extracts and stores characters into @a __s until one of the
398 * following happens. Note that these criteria are required to be
399 * tested in the order listed here, to allow an input line to exactly
400 * fill the @a __s array without setting failbit.
402 * -# the input sequence reaches end-of-file, in which case eofbit
403 * is set in the stream error state
404 * -# the next character equals @c __delim, in which case the character
405 * is extracted (and therefore counted in @c gcount()) but not stored
406 * -# @c __n-1 characters are stored, in which case failbit is set
407 * in the stream error state
409 * If no characters are extracted, failbit is set. (An empty line of
410 * input should therefore not cause failbit to be set.)
412 * In any case, a null character is stored in the next location in
416 getline(char_type* __s, streamsize __n, char_type __delim);
419 * @brief String extraction.
420 * @param __s A character array in which to store the data.
421 * @param __n Maximum number of characters to extract.
424 * Returns @c getline(__s,__n,widen('\\n')).
427 getline(char_type* __s, streamsize __n)
428 { return this->getline(__s, __n, this->widen('\n')); }
431 * @brief Discarding characters
432 * @param __n Number of characters to discard.
433 * @param __delim A "stop" character.
436 * Extracts characters and throws them away until one of the
438 * - if @a __n @c != @c std::numeric_limits<int>::max(), @a __n
439 * characters are extracted
440 * - the input sequence reaches end-of-file
441 * - the next character equals @a __delim (in this case, the character
442 * is extracted); note that this condition will never occur if
443 * @a __delim equals @c traits::eof().
445 * NB: Provide three overloads, instead of the single function
446 * (with defaults) mandated by the Standard: this leads to a
447 * better performing implementation, while still conforming to
451 ignore(streamsize __n, int_type __delim);
454 ignore(streamsize __n);
460 * @brief Looking ahead in the stream
461 * @return The next character, or eof().
463 * If, after constructing the sentry object, @c good() is false,
464 * returns @c traits::eof(). Otherwise reads but does not extract
465 * the next input character.
471 * @brief Extraction without delimiters.
472 * @param __s A character array.
473 * @param __n Maximum number of characters to store.
476 * If the stream state is @c good(), extracts characters and stores
477 * them into @a __s until one of the following happens:
478 * - @a __n characters are stored
479 * - the input sequence reaches end-of-file, in which case the error
480 * state is set to @c failbit|eofbit.
482 * @note This function is not overloaded on signed char and
486 read(char_type* __s, streamsize __n);
489 * @brief Extraction until the buffer is exhausted, but no more.
490 * @param __s A character array.
491 * @param __n Maximum number of characters to store.
492 * @return The number of characters extracted.
494 * Extracts characters and stores them into @a __s depending on the
495 * number of characters remaining in the streambuf's buffer,
496 * @c rdbuf()->in_avail(), called @c A here:
497 * - if @c A @c == @c -1, sets eofbit and extracts no characters
498 * - if @c A @c == @c 0, extracts no characters
499 * - if @c A @c > @c 0, extracts @c min(A,n)
501 * The goal is to empty the current buffer, and to not request any
502 * more from the external input sequence controlled by the streambuf.
505 readsome(char_type* __s, streamsize __n);
508 * @brief Unextracting a single character.
509 * @param __c The character to push back into the input stream.
512 * If @c rdbuf() is not null, calls @c rdbuf()->sputbackc(c).
514 * If @c rdbuf() is null or if @c sputbackc() fails, sets badbit in
517 * @note This function first clears eofbit. Since no characters
518 * are extracted, the next call to @c gcount() will return 0,
519 * as required by DR 60.
522 putback(char_type __c);
525 * @brief Unextracting the previous character.
528 * If @c rdbuf() is not null, calls @c rdbuf()->sungetc(c).
530 * If @c rdbuf() is null or if @c sungetc() fails, sets badbit in
533 * @note This function first clears eofbit. Since no characters
534 * are extracted, the next call to @c gcount() will return 0,
535 * as required by DR 60.
541 * @brief Synchronizing the stream buffer.
542 * @return 0 on success, -1 on failure
544 * If @c rdbuf() is a null pointer, returns -1.
546 * Otherwise, calls @c rdbuf()->pubsync(), and if that returns -1,
547 * sets badbit and returns -1.
549 * Otherwise, returns 0.
551 * @note This function does not count the number of characters
552 * extracted, if any, and therefore does not affect the next
553 * call to @c gcount().
559 * @brief Getting the current read position.
560 * @return A file position object.
562 * If @c fail() is not false, returns @c pos_type(-1) to indicate
563 * failure. Otherwise returns @c rdbuf()->pubseekoff(0,cur,in).
565 * @note This function does not count the number of characters
566 * extracted, if any, and therefore does not affect the next
567 * call to @c gcount(). At variance with putback, unget and
568 * seekg, eofbit is not cleared first.
574 * @brief Changing the current read position.
575 * @param __pos A file position object.
578 * If @c fail() is not true, calls @c rdbuf()->pubseekpos(__pos). If
579 * that function fails, sets failbit.
581 * @note This function first clears eofbit. It does not count the
582 * number of characters extracted, if any, and therefore does
583 * not affect the next call to @c gcount().
589 * @brief Changing the current read position.
590 * @param __off A file offset object.
591 * @param __dir The direction in which to seek.
594 * If @c fail() is not true, calls @c rdbuf()->pubseekoff(__off,__dir).
595 * If that function fails, sets failbit.
597 * @note This function first clears eofbit. It does not count the
598 * number of characters extracted, if any, and therefore does
599 * not affect the next call to @c gcount().
602 seekg(off_type, ios_base::seekdir);
607 : _M_gcount(streamsize(0))
610 template<typename _ValueT>
612 _M_extract(_ValueT& __v);
615 /// Explicit specialization declarations, defined in src/istream.cc.
618 basic_istream<char>::
619 getline(char_type* __s, streamsize __n, char_type __delim);
623 basic_istream<char>::
624 ignore(streamsize __n);
628 basic_istream<char>::
629 ignore(streamsize __n, int_type __delim);
631 #ifdef _GLIBCXX_USE_WCHAR_T
633 basic_istream<wchar_t>&
634 basic_istream<wchar_t>::
635 getline(char_type* __s, streamsize __n, char_type __delim);
638 basic_istream<wchar_t>&
639 basic_istream<wchar_t>::
640 ignore(streamsize __n);
643 basic_istream<wchar_t>&
644 basic_istream<wchar_t>::
645 ignore(streamsize __n, int_type __delim);
649 * @brief Performs setup work for input streams.
651 * Objects of this class are created before all of the standard
652 * extractors are run. It is responsible for <em>exception-safe
653 * prefix and suffix operations,</em> although only prefix actions
654 * are currently required by the standard.
656 template<typename _CharT, typename _Traits>
657 class basic_istream<_CharT, _Traits>::sentry
663 /// Easy access to dependent types.
664 typedef _Traits traits_type;
665 typedef basic_streambuf<_CharT, _Traits> __streambuf_type;
666 typedef basic_istream<_CharT, _Traits> __istream_type;
667 typedef typename __istream_type::__ctype_type __ctype_type;
668 typedef typename _Traits::int_type __int_type;
671 * @brief The constructor performs all the work.
672 * @param __is The input stream to guard.
673 * @param __noskipws Whether to consume whitespace or not.
675 * If the stream state is good (@a __is.good() is true), then the
676 * following actions are performed, otherwise the sentry state
677 * is false (<em>not okay</em>) and failbit is set in the
680 * The sentry's preparatory actions are:
682 * -# if the stream is tied to an output stream, @c is.tie()->flush()
683 * is called to synchronize the output sequence
684 * -# if @a __noskipws is false, and @c ios_base::skipws is set in
685 * @c is.flags(), the sentry extracts and discards whitespace
686 * characters from the stream. The currently imbued locale is
687 * used to determine whether each character is whitespace.
689 * If the stream state is still good, then the sentry state becomes
693 sentry(basic_istream<_CharT, _Traits>& __is, bool __noskipws = false);
696 * @brief Quick status checking.
697 * @return The sentry state.
699 * For ease of use, sentries may be converted to booleans. The
700 * return value is that of the sentry state (true == okay).
702 #if __cplusplus >= 201103L
705 operator bool() const
711 * @brief Character extractors
712 * @param __in An input stream.
713 * @param __c A character reference.
716 * Behaves like one of the formatted arithmetic extractors described in
717 * std::basic_istream. After constructing a sentry object with good
718 * status, this function extracts a character (if one is available) and
719 * stores it in @a __c. Otherwise, sets failbit in the input stream.
721 template<typename _CharT, typename _Traits>
722 basic_istream<_CharT, _Traits>&
723 operator>>(basic_istream<_CharT, _Traits>& __in, _CharT& __c);
725 template<class _Traits>
726 inline basic_istream<char, _Traits>&
727 operator>>(basic_istream<char, _Traits>& __in, unsigned char& __c)
728 { return (__in >> reinterpret_cast<char&>(__c)); }
730 template<class _Traits>
731 inline basic_istream<char, _Traits>&
732 operator>>(basic_istream<char, _Traits>& __in, signed char& __c)
733 { return (__in >> reinterpret_cast<char&>(__c)); }
738 * @brief Character string extractors
739 * @param __in An input stream.
740 * @param __s A pointer to a character array.
743 * Behaves like one of the formatted arithmetic extractors described in
744 * std::basic_istream. After constructing a sentry object with good
745 * status, this function extracts up to @c n characters and stores them
746 * into the array starting at @a __s. @c n is defined as:
748 * - if @c width() is greater than zero, @c n is width() otherwise
749 * - @c n is <em>the number of elements of the largest array of *
750 * - @c char_type that can store a terminating @c eos.</em>
753 * Characters are extracted and stored until one of the following happens:
754 * - @c n-1 characters are stored
756 * - the next character is whitespace according to the current locale
757 * - the next character is a null byte (i.e., @c charT() )
759 * @c width(0) is then called for the input stream.
761 * If no characters are extracted, sets failbit.
763 template<typename _CharT, typename _Traits>
764 basic_istream<_CharT, _Traits>&
765 operator>>(basic_istream<_CharT, _Traits>& __in, _CharT* __s);
767 // Explicit specialization declaration, defined in src/istream.cc.
770 operator>>(basic_istream<char>& __in, char* __s);
772 template<class _Traits>
773 inline basic_istream<char, _Traits>&
774 operator>>(basic_istream<char, _Traits>& __in, unsigned char* __s)
775 { return (__in >> reinterpret_cast<char*>(__s)); }
777 template<class _Traits>
778 inline basic_istream<char, _Traits>&
779 operator>>(basic_istream<char, _Traits>& __in, signed char* __s)
780 { return (__in >> reinterpret_cast<char*>(__s)); }
784 * @brief Template class basic_iostream
787 * @tparam _CharT Type of character stream.
788 * @tparam _Traits Traits for character type, defaults to
789 * char_traits<_CharT>.
791 * This class multiply inherits from the input and output stream classes
792 * simply to provide a single interface.
794 template<typename _CharT, typename _Traits>
796 : public basic_istream<_CharT, _Traits>,
797 public basic_ostream<_CharT, _Traits>
800 // _GLIBCXX_RESOLVE_LIB_DEFECTS
801 // 271. basic_iostream missing typedefs
802 // Types (inherited):
803 typedef _CharT char_type;
804 typedef typename _Traits::int_type int_type;
805 typedef typename _Traits::pos_type pos_type;
806 typedef typename _Traits::off_type off_type;
807 typedef _Traits traits_type;
809 // Non-standard Types:
810 typedef basic_istream<_CharT, _Traits> __istream_type;
811 typedef basic_ostream<_CharT, _Traits> __ostream_type;
814 * @brief Constructor does nothing.
816 * Both of the parent classes are initialized with the same
817 * streambuf pointer passed to this constructor.
820 basic_iostream(basic_streambuf<_CharT, _Traits>* __sb)
821 : __istream_type(__sb), __ostream_type(__sb) { }
824 * @brief Destructor does nothing.
827 ~basic_iostream() { }
831 : __istream_type(), __ostream_type() { }
835 * @brief Quick and easy way to eat whitespace
837 * This manipulator extracts whitespace characters, stopping when the
838 * next character is non-whitespace, or when the input sequence is empty.
839 * If the sequence is empty, @c eofbit is set in the stream, but not
842 * The current locale is used to distinguish whitespace characters.
848 * std::cin >> std::ws >> mc;
850 * will skip leading whitespace before calling operator>> on cin and your
851 * object. Note that the same effect can be achieved by creating a
852 * std::basic_istream::sentry inside your definition of operator>>.
854 template<typename _CharT, typename _Traits>
855 basic_istream<_CharT, _Traits>&
856 ws(basic_istream<_CharT, _Traits>& __is);
858 #if __cplusplus >= 201103L
859 // [27.7.1.6] Rvalue stream extraction
861 * @brief Generic extractor for rvalue stream
862 * @param __is An input stream.
863 * @param __x A reference to the extraction target.
866 * This is just a forwarding function to allow extraction from
867 * rvalue streams since they won't bind to the extractor functions
868 * that take an lvalue reference.
870 template<typename _CharT, typename _Traits, typename _Tp>
871 inline basic_istream<_CharT, _Traits>&
872 operator>>(basic_istream<_CharT, _Traits>&& __is, _Tp& __x)
873 { return (__is >> __x); }
876 _GLIBCXX_END_NAMESPACE_VERSION
879 #include <bits/istream.tcc>
881 #endif /* _GLIBCXX_ISTREAM */