1 // Input streams -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2001, 2002, 2003, 2004, 2005
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
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
);
172 operator>>(short& __n
);
175 operator>>(unsigned short& __n
);
178 operator>>(int& __n
);
181 operator>>(unsigned int& __n
);
184 operator>>(long& __n
);
187 operator>>(unsigned long& __n
);
189 #ifdef _GLIBCXX_USE_LONG_LONG
191 operator>>(long long& __n
);
194 operator>>(unsigned long long& __n
);
198 operator>>(float& __f
);
201 operator>>(double& __f
);
204 operator>>(long double& __f
);
207 operator>>(void*& __p
);
210 * @brief Extracting into another streambuf.
211 * @param sb A pointer to a streambuf
213 * This function behaves like one of the basic arithmetic extractors,
214 * in that it also constructs a sentry object and has the same error
217 * If @a sb is NULL, the stream will set failbit in its error state.
219 * Characters are extracted from this stream and inserted into the
220 * @a sb streambuf until one of the following occurs:
222 * - the input stream reaches end-of-file,
223 * - insertion into the output buffer fails (in this case, the
224 * character that would have been inserted is not extracted), or
225 * - an exception occurs (and in this case is caught)
227 * If the function inserts no characters, failbit is set.
230 operator>>(__streambuf_type
* __sb
);
233 // [27.6.1.3] unformatted input
235 * @brief Character counting
236 * @return The number of characters extracted by the previous
237 * unformatted input function dispatched for this stream.
241 { return _M_gcount
; }
244 * @name Unformatted Input Functions
246 * All the unformatted input functions have some common behavior.
247 * Each starts by constructing a temporary object of type
248 * std::basic_istream::sentry with the second argument (noskipws)
249 * set to true. This has several effects, concluding with the
250 * setting of a status flag; see the sentry documentation for more.
252 * If the sentry status is good, the function tries to extract
253 * whatever data is appropriate for the type of the argument.
255 * The number of characters extracted is stored for later retrieval
258 * If an exception is thrown during extraction, ios_base::badbit
259 * will be turned on in the stream's error state without causing an
260 * ios_base::failure to be thrown. The original exception will then
265 * @brief Simple extraction.
266 * @return A character, or eof().
268 * Tries to extract a character. If none are available, sets failbit
269 * and returns traits::eof().
275 * @brief Simple extraction.
276 * @param c The character in which to store data.
279 * Tries to extract a character and store it in @a c. If none are
280 * available, sets failbit and returns traits::eof().
282 * @note This function is not overloaded on signed char and
289 * @brief Simple multiple-character extraction.
290 * @param s Pointer to an array.
291 * @param n Maximum number of characters to store in @a s.
292 * @param delim A "stop" character.
295 * Characters are extracted and stored into @a s until one of the
298 * - @c n-1 characters are stored
299 * - the input sequence reaches EOF
300 * - the next character equals @a delim, in which case the character
303 * If no characters are stored, failbit is set in the stream's error
306 * In any case, a null character is stored into the next location in
309 * @note This function is not overloaded on signed char and
313 get(char_type
* __s
, streamsize __n
, char_type __delim
);
316 * @brief Simple multiple-character extraction.
317 * @param s Pointer to an array.
318 * @param n Maximum number of characters to store in @a s.
321 * Returns @c get(s,n,widen('\n')).
323 inline __istream_type
&
324 get(char_type
* __s
, streamsize __n
)
325 { return this->get(__s
, __n
, this->widen('\n')); }
328 * @brief Extraction into another streambuf.
329 * @param sb A streambuf in which to store data.
330 * @param delim A "stop" character.
333 * Characters are extracted and inserted into @a sb until one of the
336 * - the input sequence reaches EOF
337 * - insertion into the output buffer fails (in this case, the
338 * character that would have been inserted is not extracted)
339 * - the next character equals @a delim (in this case, the character
341 * - an exception occurs (and in this case is caught)
343 * If no characters are stored, failbit is set in the stream's error
347 get(__streambuf_type
& __sb
, char_type __delim
);
350 * @brief Extraction into another streambuf.
351 * @param sb A streambuf in which to store data.
354 * Returns @c get(sb,widen('\n')).
356 inline __istream_type
&
357 get(__streambuf_type
& __sb
)
358 { return this->get(__sb
, this->widen('\n')); }
361 * @brief String extraction.
362 * @param s A character array in which to store the data.
363 * @param n Maximum number of characters to extract.
364 * @param delim A "stop" character.
367 * Extracts and stores characters into @a s until one of the
368 * following happens. Note that these criteria are required to be
369 * tested in the order listed here, to allow an input line to exactly
370 * fill the @a s array without setting failbit.
372 * -# the input sequence reaches end-of-file, in which case eofbit
373 * is set in the stream error state
374 * -# the next character equals @c delim, in which case the character
375 * is extracted (and therefore counted in @c gcount()) but not stored
376 * -# @c n-1 characters are stored, in which case failbit is set
377 * in the stream error state
379 * If no characters are extracted, failbit is set. (An empty line of
380 * input should therefore not cause failbit to be set.)
382 * In any case, a null character is stored in the next location in
386 getline(char_type
* __s
, streamsize __n
, char_type __delim
);
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.
394 * Returns @c getline(s,n,widen('\n')).
396 inline __istream_type
&
397 getline(char_type
* __s
, streamsize __n
)
398 { return this->getline(__s
, __n
, this->widen('\n')); }
401 * @brief Discarding characters
402 * @param n Number of characters to discard.
403 * @param delim A "stop" character.
406 * Extracts characters and throws them away until one of the
408 * - if @a n @c != @c std::numeric_limits<int>::max(), @a n
409 * characters are extracted
410 * - the input sequence reaches end-of-file
411 * - the next character equals @a delim (in this case, the character
412 * is extracted); note that this condition will never occur if
413 * @a delim equals @c traits::eof().
415 * NB: Provide three overloads, instead of the single function
416 * (with defaults) mandated by the Standard: this leads to a
417 * better performing implementation, while still conforming to
424 ignore(streamsize __n
);
427 ignore(streamsize __n
, int_type __delim
);
430 * @brief Looking ahead in the stream
431 * @return The next character, or eof().
433 * If, after constructing the sentry object, @c good() is false,
434 * returns @c traits::eof(). Otherwise reads but does not extract
435 * the next input character.
441 * @brief Extraction without delimiters.
442 * @param s A character array.
443 * @param n Maximum number of characters to store.
446 * If the stream state is @c good(), extracts characters and stores
447 * them into @a s until one of the following happens:
448 * - @a n characters are stored
449 * - the input sequence reaches end-of-file, in which case the error
450 * state is set to @c failbit|eofbit.
452 * @note This function is not overloaded on signed char and
456 read(char_type
* __s
, streamsize __n
);
459 * @brief Extraction until the buffer is exhausted, but no more.
460 * @param s A character array.
461 * @param n Maximum number of characters to store.
462 * @return The number of characters extracted.
464 * Extracts characters and stores them into @a s depending on the
465 * number of characters remaining in the streambuf's buffer,
466 * @c rdbuf()->in_avail(), called @c A here:
467 * - if @c A @c == @c -1, sets eofbit and extracts no characters
468 * - if @c A @c == @c 0, extracts no characters
469 * - if @c A @c > @c 0, extracts @c min(A,n)
471 * The goal is to empty the current buffer, and to not request any
472 * more from the external input sequence controlled by the streambuf.
475 readsome(char_type
* __s
, streamsize __n
);
478 * @brief Unextracting a single character.
479 * @param c The character to push back into the input stream.
482 * If @c rdbuf() is not null, calls @c rdbuf()->sputbackc(c).
484 * If @c rdbuf() is null or if @c sputbackc() fails, sets badbit in
487 * @note Since no characters are extracted, the next call to
488 * @c gcount() will return 0, as required by DR 60.
491 putback(char_type __c
);
494 * @brief Unextracting the previous character.
497 * If @c rdbuf() is not null, calls @c rdbuf()->sungetc(c).
499 * If @c rdbuf() is null or if @c sungetc() fails, sets badbit in
502 * @note Since no characters are extracted, the next call to
503 * @c gcount() will return 0, as required by DR 60.
509 * @brief Synchronizing the stream buffer.
510 * @return 0 on success, -1 on failure
512 * If @c rdbuf() is a null pointer, returns -1.
514 * Otherwise, calls @c rdbuf()->pubsync(), and if that returns -1,
515 * sets badbit and returns -1.
517 * Otherwise, returns 0.
519 * @note This function does not count the number of characters
520 * extracted, if any, and therefore does not affect the next
521 * call to @c gcount().
527 * @brief Getting the current read position.
528 * @return A file position object.
530 * If @c fail() is not false, returns @c pos_type(-1) to indicate
531 * failure. Otherwise returns @c rdbuf()->pubseekoff(0,cur,in).
533 * @note This function does not count the number of characters
534 * extracted, if any, and therefore does not affect the next
535 * call to @c gcount().
541 * @brief Changing the current read position.
542 * @param pos A file position object.
545 * If @c fail() is not true, calls @c rdbuf()->pubseekpos(pos). If
546 * that function fails, sets failbit.
548 * @note This function does not count the number of characters
549 * extracted, if any, and therefore does not affect the next
550 * call to @c gcount().
556 * @brief Changing the current read position.
557 * @param off A file offset object.
558 * @param dir The direction in which to seek.
561 * If @c fail() is not true, calls @c rdbuf()->pubseekoff(off,dir).
562 * If that function fails, sets failbit.
564 * @note This function does not count the number of characters
565 * extracted, if any, and therefore does not affect the next
566 * call to @c gcount().
569 seekg(off_type
, ios_base::seekdir
);
574 basic_istream(): _M_gcount(streamsize(0)) { }
577 // Explicit specialization declarations, defined in src/istream.cc.
580 basic_istream
<char>::
581 getline(char_type
* __s
, streamsize __n
, char_type __delim
);
585 basic_istream
<char>::
586 ignore(streamsize __n
);
590 basic_istream
<char>::
591 ignore(streamsize __n
, int_type __delim
);
593 #ifdef _GLIBCXX_USE_WCHAR_T
595 basic_istream
<wchar_t>&
596 basic_istream
<wchar_t>::
597 getline(char_type
* __s
, streamsize __n
, char_type __delim
);
600 basic_istream
<wchar_t>&
601 basic_istream
<wchar_t>::
602 ignore(streamsize __n
);
605 basic_istream
<wchar_t>&
606 basic_istream
<wchar_t>::
607 ignore(streamsize __n
, int_type __delim
);
611 * @brief Performs setup work for input streams.
613 * Objects of this class are created before all of the standard
614 * extractors are run. It is responsible for "exception-safe prefix and
615 * suffix operations," although only prefix actions are currently required
616 * by the standard. Additional actions may be added by the
617 * implementation, and we list them in
618 * http://gcc.gnu.org/onlinedocs/libstdc++/17_intro/howto.html#5
619 * under [27.6] notes.
621 template<typename _CharT
, typename _Traits
>
622 class basic_istream
<_CharT
, _Traits
>::sentry
625 /// Easy access to dependant types.
626 typedef _Traits traits_type
;
627 typedef basic_streambuf
<_CharT
, _Traits
> __streambuf_type
;
628 typedef basic_istream
<_CharT
, _Traits
> __istream_type
;
629 typedef typename
__istream_type::__ctype_type __ctype_type
;
630 typedef typename
_Traits::int_type __int_type
;
633 * @brief The constructor performs all the work.
634 * @param is The input stream to guard.
635 * @param noskipws Whether to consume whitespace or not.
637 * If the stream state is good (@a is.good() is true), then the
638 * following actions are performed, otherwise the sentry state is
639 * false ("not okay") and failbit is set in the stream state.
641 * The sentry's preparatory actions are:
643 * -# if the stream is tied to an output stream, @c is.tie()->flush()
644 * is called to synchronize the output sequence
645 * -# if @a noskipws is false, and @c ios_base::skipws is set in
646 * @c is.flags(), the sentry extracts and discards whitespace
647 * characters from the stream. The currently imbued locale is
648 * used to determine whether each character is whitespace.
650 * If the stream state is still good, then the sentry state becomes
654 sentry(basic_istream
<_CharT
, _Traits
>& __is
, bool __noskipws
= false);
657 * @brief Quick status checking.
658 * @return The sentry state.
660 * For ease of use, sentries may be converted to booleans. The
661 * return value is that of the sentry state (true == okay).
663 operator bool() const { return _M_ok
; }
669 // [27.6.1.2.3] character extraction templates
672 * @brief Character extractors
673 * @param in An input stream.
674 * @param c A character reference.
677 * Behaves like one of the formatted arithmetic extractors described in
678 * std::basic_istream. After constructing a sentry object with good
679 * status, this function extracts a character (if one is available) and
680 * stores it in @a c. Otherwise, sets failbit in the input stream.
682 template<typename _CharT
, typename _Traits
>
683 basic_istream
<_CharT
, _Traits
>&
684 operator>>(basic_istream
<_CharT
, _Traits
>& __in
, _CharT
& __c
);
686 template<class _Traits
>
687 basic_istream
<char, _Traits
>&
688 operator>>(basic_istream
<char, _Traits
>& __in
, unsigned char& __c
)
689 { return (__in
>> reinterpret_cast<char&>(__c
)); }
691 template<class _Traits
>
692 basic_istream
<char, _Traits
>&
693 operator>>(basic_istream
<char, _Traits
>& __in
, signed char& __c
)
694 { return (__in
>> reinterpret_cast<char&>(__c
)); }
699 * @brief Character string extractors
700 * @param in An input stream.
701 * @param s A pointer to a character array.
704 * Behaves like one of the formatted arithmetic extractors described in
705 * std::basic_istream. After constructing a sentry object with good
706 * status, this function extracts up to @c n characters and stores them
707 * into the array starting at @a s. @c n is defined as:
709 * - if @c width() is greater than zero, @c n is width()
710 * - otherwise @c n is "the number of elements of the largest array of
711 * @c char_type that can store a terminating @c eos." [27.6.1.2.3]/6
713 * Characters are extracted and stored until one of the following happens:
714 * - @c n-1 characters are stored
716 * - the next character is whitespace according to the current locale
717 * - the next character is a null byte (i.e., @c charT() )
719 * @c width(0) is then called for the input stream.
721 * If no characters are extracted, sets failbit.
723 template<typename _CharT
, typename _Traits
>
724 basic_istream
<_CharT
, _Traits
>&
725 operator>>(basic_istream
<_CharT
, _Traits
>& __in
, _CharT
* __s
);
727 // Explicit specialization declaration, defined in src/istream.cc.
730 operator>>(basic_istream
<char>& __in
, char* __s
);
732 template<class _Traits
>
733 basic_istream
<char, _Traits
>&
734 operator>>(basic_istream
<char, _Traits
>& __in
, unsigned char* __s
)
735 { return (__in
>> reinterpret_cast<char*>(__s
)); }
737 template<class _Traits
>
738 basic_istream
<char, _Traits
>&
739 operator>>(basic_istream
<char, _Traits
>& __in
, signed char* __s
)
740 { return (__in
>> reinterpret_cast<char*>(__s
)); }
743 // 27.6.1.5 Template class basic_iostream
745 * @brief Merging istream and ostream capabilities.
747 * This class multiply inherits from the input and output stream classes
748 * simply to provide a single interface.
750 template<typename _CharT
, typename _Traits
>
752 : public basic_istream
<_CharT
, _Traits
>,
753 public basic_ostream
<_CharT
, _Traits
>
756 // _GLIBCXX_RESOLVE_LIB_DEFECTS
757 // 271. basic_iostream missing typedefs
758 // Types (inherited):
759 typedef _CharT char_type
;
760 typedef typename
_Traits::int_type int_type
;
761 typedef typename
_Traits::pos_type pos_type
;
762 typedef typename
_Traits::off_type off_type
;
763 typedef _Traits traits_type
;
765 // Non-standard Types:
766 typedef basic_istream
<_CharT
, _Traits
> __istream_type
;
767 typedef basic_ostream
<_CharT
, _Traits
> __ostream_type
;
770 * @brief Constructor does nothing.
772 * Both of the parent classes are initialized with the same
773 * streambuf pointer passed to this constructor.
776 basic_iostream(basic_streambuf
<_CharT
, _Traits
>* __sb
)
777 : __istream_type(), __ostream_type()
778 { this->init(__sb
); }
781 * @brief Destructor does nothing.
784 ~basic_iostream() { }
788 basic_iostream() : __istream_type(), __ostream_type()
792 // [27.6.1.4] standard basic_istream manipulators
794 * @brief Quick and easy way to eat whitespace
796 * This manipulator extracts whitespace characters, stopping when the
797 * next character is non-whitespace, or when the input sequence is empty.
798 * If the sequence is empty, @c eofbit is set in the stream, but not
801 * The current locale is used to distinguish whitespace characters.
807 * std::cin >> std::ws >> mc;
809 * will skip leading whitespace before calling operator>> on cin and your
810 * object. Note that the same effect can be achieved by creating a
811 * std::basic_istream::sentry inside your definition of operator>>.
813 template<typename _CharT
, typename _Traits
>
814 basic_istream
<_CharT
, _Traits
>&
815 ws(basic_istream
<_CharT
, _Traits
>& __is
);
818 #ifndef _GLIBCXX_EXPORT_TEMPLATE
819 # include <bits/istream.tcc>
822 #endif /* _GLIBCXX_ISTREAM */