1 // Input streams -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
32 // ISO C++ 14882: 27.6.1 Input streams
36 * This is a Standard C++ Library header.
39 #ifndef _GLIBCXX_ISTREAM
40 #define _GLIBCXX_ISTREAM 1
42 #pragma GCC system_header
45 #include <limits> // For numeric_limits
47 _GLIBCXX_BEGIN_NAMESPACE(std
)
49 // [27.6.1.1] Template class basic_istream
51 * @brief Controlling input.
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
;
76 template<typename _CharT2
, typename _Traits2
>
77 friend basic_istream
<_CharT2
, _Traits2
>&
78 operator>>(basic_istream
<_CharT2
, _Traits2
>&, _CharT2
&);
80 template<typename _CharT2
, typename _Traits2
>
81 friend basic_istream
<_CharT2
, _Traits2
>&
82 operator>>(basic_istream
<_CharT2
, _Traits2
>&, _CharT2
*);
88 * The number of characters extracted in the previous unformatted
89 * function; see gcount().
95 // [27.6.1.1.1] constructor/destructor
97 * @brief Base constructor.
99 * This ctor is almost never called by the user directly, rather from
100 * derived classes' initialization lists, which pass a pointer to
101 * their own stream buffer.
104 basic_istream(__streambuf_type
* __sb
): _M_gcount(streamsize(0))
105 { this->init(__sb
); }
108 * @brief Base destructor.
110 * This does very little apart from providing a virtual base dtor.
114 { _M_gcount
= streamsize(0); }
116 // [27.6.1.1.2] prefix/suffix
120 // [27.6.1.2] formatted input
121 // [27.6.1.2.3] basic_istream::operator>>
124 * @brief Interface for manipulators.
126 * Manuipulators such as @c std::ws and @c std::dec use these
127 * functions in constructs like "std::cin >> std::ws". For more
128 * information, see the iomanip header.
130 inline __istream_type
&
131 operator>>(__istream_type
& (*__pf
)(__istream_type
&));
133 inline __istream_type
&
134 operator>>(__ios_type
& (*__pf
)(__ios_type
&));
136 inline __istream_type
&
137 operator>>(ios_base
& (*__pf
)(ios_base
&));
140 // [27.6.1.2.2] arithmetic extractors
142 * @name Arithmetic Extractors
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
161 * @brief Basic arithmetic extractors
162 * @param A variable of builtin type.
163 * @return @c *this if successful
165 * These functions use the stream's current locale (specifically, the
166 * @c num_get facet) to parse the input data.
169 operator>>(bool& __n
)
170 { return _M_extract(__n
); }
173 operator>>(short& __n
);
176 operator>>(unsigned short& __n
)
177 { return _M_extract(__n
); }
180 operator>>(int& __n
);
183 operator>>(unsigned int& __n
)
184 { return _M_extract(__n
); }
187 operator>>(long& __n
)
188 { return _M_extract(__n
); }
191 operator>>(unsigned long& __n
)
192 { return _M_extract(__n
); }
194 #ifdef _GLIBCXX_USE_LONG_LONG
196 operator>>(long long& __n
)
197 { return _M_extract(__n
); }
200 operator>>(unsigned long long& __n
)
201 { return _M_extract(__n
); }
205 operator>>(float& __f
)
206 { return _M_extract(__f
); }
209 operator>>(double& __f
)
210 { return _M_extract(__f
); }
213 operator>>(long double& __f
)
214 { return _M_extract(__f
); }
217 operator>>(void*& __p
)
218 { return _M_extract(__p
); }
221 * @brief Extracting into another streambuf.
222 * @param sb A pointer to a streambuf
224 * This function behaves like one of the basic arithmetic extractors,
225 * in that it also constructs a sentry object and has the same error
228 * If @a sb is NULL, the stream will set failbit in its error state.
230 * Characters are extracted from this stream and inserted into the
231 * @a sb streambuf until one of the following occurs:
233 * - the input stream reaches end-of-file,
234 * - insertion into the output buffer fails (in this case, the
235 * character that would have been inserted is not extracted), or
236 * - an exception occurs (and in this case is caught)
238 * If the function inserts no characters, failbit is set.
241 operator>>(__streambuf_type
* __sb
);
244 // [27.6.1.3] unformatted input
246 * @brief Character counting
247 * @return The number of characters extracted by the previous
248 * unformatted input function dispatched for this stream.
252 { return _M_gcount
; }
255 * @name Unformatted Input Functions
257 * All the unformatted input functions have some common behavior.
258 * Each starts by constructing a temporary object of type
259 * std::basic_istream::sentry with the second argument (noskipws)
260 * set to true. This has several effects, concluding with the
261 * setting of a status flag; see the sentry documentation for more.
263 * If the sentry status is good, the function tries to extract
264 * whatever data is appropriate for the type of the argument.
266 * The number of characters extracted is stored for later retrieval
269 * If an exception is thrown during extraction, ios_base::badbit
270 * will be turned on in the stream's error state without causing an
271 * ios_base::failure to be thrown. The original exception will then
276 * @brief Simple extraction.
277 * @return A character, or eof().
279 * Tries to extract a character. If none are available, sets failbit
280 * and returns traits::eof().
286 * @brief Simple extraction.
287 * @param c The character in which to store data.
290 * Tries to extract a character and store it in @a c. If none are
291 * available, sets failbit and returns traits::eof().
293 * @note This function is not overloaded on signed char and
300 * @brief Simple multiple-character extraction.
301 * @param s Pointer to an array.
302 * @param n Maximum number of characters to store in @a s.
303 * @param delim A "stop" character.
306 * Characters are extracted and stored into @a s until one of the
309 * - @c n-1 characters are stored
310 * - the input sequence reaches EOF
311 * - the next character equals @a delim, in which case the character
314 * If no characters are stored, failbit is set in the stream's error
317 * In any case, a null character is stored into the next location in
320 * @note This function is not overloaded on signed char and
324 get(char_type
* __s
, streamsize __n
, char_type __delim
);
327 * @brief Simple multiple-character extraction.
328 * @param s Pointer to an array.
329 * @param n Maximum number of characters to store in @a s.
332 * Returns @c get(s,n,widen('\n')).
334 inline __istream_type
&
335 get(char_type
* __s
, streamsize __n
)
336 { return this->get(__s
, __n
, this->widen('\n')); }
339 * @brief Extraction into another streambuf.
340 * @param sb A streambuf in which to store data.
341 * @param delim A "stop" character.
344 * Characters are extracted and inserted into @a sb until one of the
347 * - the input sequence reaches EOF
348 * - insertion into the output buffer fails (in this case, the
349 * character that would have been inserted is not extracted)
350 * - the next character equals @a delim (in this case, the character
352 * - an exception occurs (and in this case is caught)
354 * If no characters are stored, failbit is set in the stream's error
358 get(__streambuf_type
& __sb
, char_type __delim
);
361 * @brief Extraction into another streambuf.
362 * @param sb A streambuf in which to store data.
365 * Returns @c get(sb,widen('\n')).
367 inline __istream_type
&
368 get(__streambuf_type
& __sb
)
369 { return this->get(__sb
, this->widen('\n')); }
372 * @brief String extraction.
373 * @param s A character array in which to store the data.
374 * @param n Maximum number of characters to extract.
375 * @param delim A "stop" character.
378 * Extracts and stores characters into @a s until one of the
379 * following happens. Note that these criteria are required to be
380 * tested in the order listed here, to allow an input line to exactly
381 * fill the @a s array without setting failbit.
383 * -# the input sequence reaches end-of-file, in which case eofbit
384 * is set in the stream error state
385 * -# the next character equals @c delim, in which case the character
386 * is extracted (and therefore counted in @c gcount()) but not stored
387 * -# @c n-1 characters are stored, in which case failbit is set
388 * in the stream error state
390 * If no characters are extracted, failbit is set. (An empty line of
391 * input should therefore not cause failbit to be set.)
393 * In any case, a null character is stored in the next location in
397 getline(char_type
* __s
, streamsize __n
, char_type __delim
);
400 * @brief String extraction.
401 * @param s A character array in which to store the data.
402 * @param n Maximum number of characters to extract.
405 * Returns @c getline(s,n,widen('\n')).
407 inline __istream_type
&
408 getline(char_type
* __s
, streamsize __n
)
409 { return this->getline(__s
, __n
, this->widen('\n')); }
412 * @brief Discarding characters
413 * @param n Number of characters to discard.
414 * @param delim A "stop" character.
417 * Extracts characters and throws them away until one of the
419 * - if @a n @c != @c std::numeric_limits<int>::max(), @a n
420 * characters are extracted
421 * - the input sequence reaches end-of-file
422 * - the next character equals @a delim (in this case, the character
423 * is extracted); note that this condition will never occur if
424 * @a delim equals @c traits::eof().
426 * NB: Provide three overloads, instead of the single function
427 * (with defaults) mandated by the Standard: this leads to a
428 * better performing implementation, while still conforming to
435 ignore(streamsize __n
);
438 ignore(streamsize __n
, int_type __delim
);
441 * @brief Looking ahead in the stream
442 * @return The next character, or eof().
444 * If, after constructing the sentry object, @c good() is false,
445 * returns @c traits::eof(). Otherwise reads but does not extract
446 * the next input character.
452 * @brief Extraction without delimiters.
453 * @param s A character array.
454 * @param n Maximum number of characters to store.
457 * If the stream state is @c good(), extracts characters and stores
458 * them into @a s until one of the following happens:
459 * - @a n characters are stored
460 * - the input sequence reaches end-of-file, in which case the error
461 * state is set to @c failbit|eofbit.
463 * @note This function is not overloaded on signed char and
467 read(char_type
* __s
, streamsize __n
);
470 * @brief Extraction until the buffer is exhausted, but no more.
471 * @param s A character array.
472 * @param n Maximum number of characters to store.
473 * @return The number of characters extracted.
475 * Extracts characters and stores them into @a s depending on the
476 * number of characters remaining in the streambuf's buffer,
477 * @c rdbuf()->in_avail(), called @c A here:
478 * - if @c A @c == @c -1, sets eofbit and extracts no characters
479 * - if @c A @c == @c 0, extracts no characters
480 * - if @c A @c > @c 0, extracts @c min(A,n)
482 * The goal is to empty the current buffer, and to not request any
483 * more from the external input sequence controlled by the streambuf.
486 readsome(char_type
* __s
, streamsize __n
);
489 * @brief Unextracting a single character.
490 * @param c The character to push back into the input stream.
493 * If @c rdbuf() is not null, calls @c rdbuf()->sputbackc(c).
495 * If @c rdbuf() is null or if @c sputbackc() fails, sets badbit in
498 * @note Since no characters are extracted, the next call to
499 * @c gcount() will return 0, as required by DR 60.
502 putback(char_type __c
);
505 * @brief Unextracting the previous character.
508 * If @c rdbuf() is not null, calls @c rdbuf()->sungetc(c).
510 * If @c rdbuf() is null or if @c sungetc() fails, sets badbit in
513 * @note Since no characters are extracted, the next call to
514 * @c gcount() will return 0, as required by DR 60.
520 * @brief Synchronizing the stream buffer.
521 * @return 0 on success, -1 on failure
523 * If @c rdbuf() is a null pointer, returns -1.
525 * Otherwise, calls @c rdbuf()->pubsync(), and if that returns -1,
526 * sets badbit and returns -1.
528 * Otherwise, returns 0.
530 * @note This function does not count the number of characters
531 * extracted, if any, and therefore does not affect the next
532 * call to @c gcount().
538 * @brief Getting the current read position.
539 * @return A file position object.
541 * If @c fail() is not false, returns @c pos_type(-1) to indicate
542 * failure. Otherwise returns @c rdbuf()->pubseekoff(0,cur,in).
544 * @note This function does not count the number of characters
545 * extracted, if any, and therefore does not affect the next
546 * call to @c gcount().
552 * @brief Changing the current read position.
553 * @param pos A file position object.
556 * If @c fail() is not true, calls @c rdbuf()->pubseekpos(pos). If
557 * that function fails, sets failbit.
559 * @note This function does not count the number of characters
560 * extracted, if any, and therefore does not affect the next
561 * call to @c gcount().
567 * @brief Changing the current read position.
568 * @param off A file offset object.
569 * @param dir The direction in which to seek.
572 * If @c fail() is not true, calls @c rdbuf()->pubseekoff(off,dir).
573 * If that function fails, sets failbit.
575 * @note This function does not count the number of characters
576 * extracted, if any, and therefore does not affect the next
577 * call to @c gcount().
580 seekg(off_type
, ios_base::seekdir
);
585 basic_istream(): _M_gcount(streamsize(0)) { }
587 template<typename _ValueT
>
589 _M_extract(_ValueT
& __v
);
592 // Explicit specialization declarations, defined in src/istream.cc.
595 basic_istream
<char>::
596 getline(char_type
* __s
, streamsize __n
, char_type __delim
);
600 basic_istream
<char>::
601 ignore(streamsize __n
);
605 basic_istream
<char>::
606 ignore(streamsize __n
, int_type __delim
);
608 #ifdef _GLIBCXX_USE_WCHAR_T
610 basic_istream
<wchar_t>&
611 basic_istream
<wchar_t>::
612 getline(char_type
* __s
, streamsize __n
, char_type __delim
);
615 basic_istream
<wchar_t>&
616 basic_istream
<wchar_t>::
617 ignore(streamsize __n
);
620 basic_istream
<wchar_t>&
621 basic_istream
<wchar_t>::
622 ignore(streamsize __n
, int_type __delim
);
626 * @brief Performs setup work for input streams.
628 * Objects of this class are created before all of the standard
629 * extractors are run. It is responsible for "exception-safe prefix and
630 * suffix operations," although only prefix actions are currently required
631 * by the standard. Additional actions may be added by the
632 * implementation, and we list them in
633 * http://gcc.gnu.org/onlinedocs/libstdc++/17_intro/howto.html#5
634 * under [27.6] notes.
636 template<typename _CharT
, typename _Traits
>
637 class basic_istream
<_CharT
, _Traits
>::sentry
640 /// Easy access to dependant types.
641 typedef _Traits traits_type
;
642 typedef basic_streambuf
<_CharT
, _Traits
> __streambuf_type
;
643 typedef basic_istream
<_CharT
, _Traits
> __istream_type
;
644 typedef typename
__istream_type::__ctype_type __ctype_type
;
645 typedef typename
_Traits::int_type __int_type
;
648 * @brief The constructor performs all the work.
649 * @param is The input stream to guard.
650 * @param noskipws Whether to consume whitespace or not.
652 * If the stream state is good (@a is.good() is true), then the
653 * following actions are performed, otherwise the sentry state is
654 * false ("not okay") and failbit is set in the stream state.
656 * The sentry's preparatory actions are:
658 * -# if the stream is tied to an output stream, @c is.tie()->flush()
659 * is called to synchronize the output sequence
660 * -# if @a noskipws is false, and @c ios_base::skipws is set in
661 * @c is.flags(), the sentry extracts and discards whitespace
662 * characters from the stream. The currently imbued locale is
663 * used to determine whether each character is whitespace.
665 * If the stream state is still good, then the sentry state becomes
669 sentry(basic_istream
<_CharT
, _Traits
>& __is
, bool __noskipws
= false);
672 * @brief Quick status checking.
673 * @return The sentry state.
675 * For ease of use, sentries may be converted to booleans. The
676 * return value is that of the sentry state (true == okay).
678 operator bool() const { return _M_ok
; }
684 // [27.6.1.2.3] character extraction templates
687 * @brief Character extractors
688 * @param in An input stream.
689 * @param c A character reference.
692 * Behaves like one of the formatted arithmetic extractors described in
693 * std::basic_istream. After constructing a sentry object with good
694 * status, this function extracts a character (if one is available) and
695 * stores it in @a c. Otherwise, sets failbit in the input stream.
697 template<typename _CharT
, typename _Traits
>
698 basic_istream
<_CharT
, _Traits
>&
699 operator>>(basic_istream
<_CharT
, _Traits
>& __in
, _CharT
& __c
);
701 template<class _Traits
>
702 basic_istream
<char, _Traits
>&
703 operator>>(basic_istream
<char, _Traits
>& __in
, unsigned char& __c
)
704 { return (__in
>> reinterpret_cast<char&>(__c
)); }
706 template<class _Traits
>
707 basic_istream
<char, _Traits
>&
708 operator>>(basic_istream
<char, _Traits
>& __in
, signed char& __c
)
709 { return (__in
>> reinterpret_cast<char&>(__c
)); }
714 * @brief Character string extractors
715 * @param in An input stream.
716 * @param s A pointer to a character array.
719 * Behaves like one of the formatted arithmetic extractors described in
720 * std::basic_istream. After constructing a sentry object with good
721 * status, this function extracts up to @c n characters and stores them
722 * into the array starting at @a s. @c n is defined as:
724 * - if @c width() is greater than zero, @c n is width()
725 * - otherwise @c n is "the number of elements of the largest array of
726 * @c char_type that can store a terminating @c eos." [27.6.1.2.3]/6
728 * Characters are extracted and stored until one of the following happens:
729 * - @c n-1 characters are stored
731 * - the next character is whitespace according to the current locale
732 * - the next character is a null byte (i.e., @c charT() )
734 * @c width(0) is then called for the input stream.
736 * If no characters are extracted, sets failbit.
738 template<typename _CharT
, typename _Traits
>
739 basic_istream
<_CharT
, _Traits
>&
740 operator>>(basic_istream
<_CharT
, _Traits
>& __in
, _CharT
* __s
);
742 // Explicit specialization declaration, defined in src/istream.cc.
745 operator>>(basic_istream
<char>& __in
, char* __s
);
747 template<class _Traits
>
748 basic_istream
<char, _Traits
>&
749 operator>>(basic_istream
<char, _Traits
>& __in
, unsigned char* __s
)
750 { return (__in
>> reinterpret_cast<char*>(__s
)); }
752 template<class _Traits
>
753 basic_istream
<char, _Traits
>&
754 operator>>(basic_istream
<char, _Traits
>& __in
, signed char* __s
)
755 { return (__in
>> reinterpret_cast<char*>(__s
)); }
758 // 27.6.1.5 Template class basic_iostream
760 * @brief Merging istream and ostream capabilities.
762 * This class multiply inherits from the input and output stream classes
763 * simply to provide a single interface.
765 template<typename _CharT
, typename _Traits
>
767 : public basic_istream
<_CharT
, _Traits
>,
768 public basic_ostream
<_CharT
, _Traits
>
771 // _GLIBCXX_RESOLVE_LIB_DEFECTS
772 // 271. basic_iostream missing typedefs
773 // Types (inherited):
774 typedef _CharT char_type
;
775 typedef typename
_Traits::int_type int_type
;
776 typedef typename
_Traits::pos_type pos_type
;
777 typedef typename
_Traits::off_type off_type
;
778 typedef _Traits traits_type
;
780 // Non-standard Types:
781 typedef basic_istream
<_CharT
, _Traits
> __istream_type
;
782 typedef basic_ostream
<_CharT
, _Traits
> __ostream_type
;
785 * @brief Constructor does nothing.
787 * Both of the parent classes are initialized with the same
788 * streambuf pointer passed to this constructor.
791 basic_iostream(basic_streambuf
<_CharT
, _Traits
>* __sb
)
792 : __istream_type(), __ostream_type()
793 { this->init(__sb
); }
796 * @brief Destructor does nothing.
799 ~basic_iostream() { }
803 basic_iostream() : __istream_type(), __ostream_type()
807 // [27.6.1.4] standard basic_istream manipulators
809 * @brief Quick and easy way to eat whitespace
811 * This manipulator extracts whitespace characters, stopping when the
812 * next character is non-whitespace, or when the input sequence is empty.
813 * If the sequence is empty, @c eofbit is set in the stream, but not
816 * The current locale is used to distinguish whitespace characters.
822 * std::cin >> std::ws >> mc;
824 * will skip leading whitespace before calling operator>> on cin and your
825 * object. Note that the same effect can be achieved by creating a
826 * std::basic_istream::sentry inside your definition of operator>>.
828 template<typename _CharT
, typename _Traits
>
829 basic_istream
<_CharT
, _Traits
>&
830 ws(basic_istream
<_CharT
, _Traits
>& __is
);
832 _GLIBCXX_END_NAMESPACE
834 #ifndef _GLIBCXX_EXPORT_TEMPLATE
835 # include <bits/istream.tcc>
838 #endif /* _GLIBCXX_ISTREAM */