1 // Input streams -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2001, 2002, 2003
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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
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. You should @c #include this header
37 * in your programs, rather than any of the "st[dl]_*.h" implementation files.
40 #ifndef _GLIBCXX_ISTREAM
41 #define _GLIBCXX_ISTREAM 1
43 #pragma GCC system_header
46 #include <limits> // For numeric_limits
50 // [27.6.1.1] Template class basic_istream
52 * @brief Controlling input.
54 * This is the base class for all input streams. It provides text
55 * formatting of all builtin types, and communicates with any class
56 * derived from basic_streambuf to do the actual input.
58 template<typename _CharT
, typename _Traits
>
59 class basic_istream
: virtual public basic_ios
<_CharT
, _Traits
>
62 // Types (inherited from basic_ios (27.4.4)):
63 typedef _CharT char_type
;
64 typedef typename
_Traits::int_type int_type
;
65 typedef typename
_Traits::pos_type pos_type
;
66 typedef typename
_Traits::off_type off_type
;
67 typedef _Traits traits_type
;
69 // Non-standard Types:
70 typedef basic_streambuf
<_CharT
, _Traits
> __streambuf_type
;
71 typedef basic_ios
<_CharT
, _Traits
> __ios_type
;
72 typedef basic_istream
<_CharT
, _Traits
> __istream_type
;
73 typedef num_get
<_CharT
, istreambuf_iterator
<_CharT
, _Traits
> >
75 typedef ctype
<_CharT
> __ctype_type
;
77 template<typename _CharT2
, typename _Traits2
>
78 friend basic_istream
<_CharT2
, _Traits2
>&
79 operator>>(basic_istream
<_CharT2
, _Traits2
>&, _CharT2
&);
81 template<typename _CharT2
, typename _Traits2
>
82 friend basic_istream
<_CharT2
, _Traits2
>&
83 operator>>(basic_istream
<_CharT2
, _Traits2
>&, _CharT2
*);
89 * The number of characters extracted in the previous unformatted
90 * function; see gcount().
96 // [27.6.1.1.1] constructor/destructor
98 * @brief Base constructor.
100 * This ctor is almost never called by the user directly, rather from
101 * derived classes' initialization lists, which pass a pointer to
102 * their own stream buffer.
105 basic_istream(__streambuf_type
* __sb
): _M_gcount(streamsize(0))
106 { this->init(__sb
); }
109 * @brief Base destructor.
111 * This does very little apart from providing a virtual base dtor.
115 { _M_gcount
= streamsize(0); }
117 // [27.6.1.1.2] prefix/suffix
121 // [27.6.1.2] formatted input
122 // [27.6.1.2.3] basic_istream::operator>>
125 * @brief Interface for manipulators.
127 * Manuipulators such as @c std::ws and @c std::dec use these
128 * functions in constructs like "std::cin >> std::ws". For more
129 * information, see the iomanip header.
131 inline __istream_type
&
132 operator>>(__istream_type
& (*__pf
)(__istream_type
&));
134 inline __istream_type
&
135 operator>>(__ios_type
& (*__pf
)(__ios_type
&));
137 inline __istream_type
&
138 operator>>(ios_base
& (*__pf
)(ios_base
&));
141 // [27.6.1.2.2] arithmetic extractors
143 * @name Arithmetic Extractors
145 * All the @c operator>> functions (aka <em>formatted input
146 * functions</em>) have some common behavior. Each starts by
147 * constructing a temporary object of type std::basic_istream::sentry
148 * with the second argument (noskipws) set to false. This has several
149 * effects, concluding with the setting of a status flag; see the
150 * sentry documentation for more.
152 * If the sentry status is good, the function tries to extract
153 * whatever data is appropriate for the type of the argument.
155 * If an exception is thrown during extraction, ios_base::badbit
156 * will be turned on in the stream's error state without causing an
157 * ios_base::failure to be thrown. The original exception will then
162 * @brief Basic arithmetic extractors
163 * @param A variable of builtin type.
164 * @return @c *this if successful
166 * These functions use the stream's current locale (specifically, the
167 * @c num_get facet) to parse the input data.
170 operator>>(bool& __n
);
173 operator>>(short& __n
);
176 operator>>(unsigned short& __n
);
179 operator>>(int& __n
);
182 operator>>(unsigned int& __n
);
185 operator>>(long& __n
);
188 operator>>(unsigned long& __n
);
190 #ifdef _GLIBCXX_USE_LONG_LONG
192 operator>>(long long& __n
);
195 operator>>(unsigned long long& __n
);
199 operator>>(float& __f
);
202 operator>>(double& __f
);
205 operator>>(long double& __f
);
208 operator>>(void*& __p
);
211 * @brief Extracting into another streambuf.
212 * @param sb A pointer to a streambuf
214 * This function behaves like one of the basic arithmetic extractors,
215 * in that it also constructs a sentry object and has the same error
218 * If @a sb is NULL, the stream will set failbit in its error state.
220 * Characters are extracted from this stream and inserted into the
221 * @a sb streambuf until one of the following occurs:
223 * - the input stream reaches end-of-file,
224 * - insertion into the output buffer fails (in this case, the
225 * character that would have been inserted is not extracted), or
226 * - an exception occurs (and in this case is caught)
228 * If the function inserts no characters, failbit is set.
231 operator>>(__streambuf_type
* __sb
);
234 // [27.6.1.3] unformatted input
236 * @brief Character counting
237 * @return The number of characters extracted by the previous
238 * unformatted input function dispatched for this stream.
242 { return _M_gcount
; }
245 * @name Unformatted Input Functions
247 * All the unformatted input functions have some common behavior.
248 * Each starts by constructing a temporary object of type
249 * std::basic_istream::sentry with the second argument (noskipws)
250 * set to true. This has several effects, concluding with the
251 * setting of a status flag; see the sentry documentation for more.
253 * If the sentry status is good, the function tries to extract
254 * whatever data is appropriate for the type of the argument.
256 * The number of characters extracted is stored for later retrieval
259 * If an exception is thrown during extraction, ios_base::badbit
260 * will be turned on in the stream's error state without causing an
261 * ios_base::failure to be thrown. The original exception will then
266 * @brief Simple extraction.
267 * @return A character, or eof().
269 * Tries to extract a character. If none are available, sets failbit
270 * and returns traits::eof().
276 * @brief Simple extraction.
277 * @param c The character in which to store data.
280 * Tries to extract a character and store it in @a c. If none are
281 * available, sets failbit and returns traits::eof().
283 * @note This function is not overloaded on signed char and
290 * @brief Simple multiple-character extraction.
291 * @param s Pointer to an array.
292 * @param n Maximum number of characters to store in @a s.
293 * @param delim A "stop" character.
296 * Characters are extracted and stored into @a s until one of the
299 * - @c n-1 characters are stored
300 * - the input sequence reaches EOF
301 * - the next character equals @a delim, in which case the character
304 * If no characters are stored, failbit is set in the stream's error
307 * In any case, a null character is stored into the next location in
310 * @note This function is not overloaded on signed char and
314 get(char_type
* __s
, streamsize __n
, char_type __delim
);
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.
322 * Returns @c get(s,n,widen('\n')).
324 inline __istream_type
&
325 get(char_type
* __s
, streamsize __n
)
326 { return this->get(__s
, __n
, this->widen('\n')); }
329 * @brief Extraction into another streambuf.
330 * @param sb A streambuf in which to store data.
331 * @param delim A "stop" character.
334 * Characters are extracted and inserted into @a sb until one of the
337 * - the input sequence reaches EOF
338 * - insertion into the output buffer fails (in this case, the
339 * character that would have been inserted is not extracted)
340 * - the next character equals @a delim (in this case, the character
342 * - an exception occurs (and in this case is caught)
344 * If no characters are stored, failbit is set in the stream's error
348 get(__streambuf_type
& __sb
, char_type __delim
);
351 * @brief Extraction into another streambuf.
352 * @param sb A streambuf in which to store data.
355 * Returns @c get(sb,widen('\n')).
357 inline __istream_type
&
358 get(__streambuf_type
& __sb
)
359 { return this->get(__sb
, this->widen('\n')); }
362 * @brief String extraction.
363 * @param s A character array in which to store the data.
364 * @param n Maximum number of characters to extract.
365 * @param delim A "stop" character.
368 * Extracts and stores characters into @a s until one of the
369 * following happens. Note that these criteria are required to be
370 * tested in the order listed here, to allow an input line to exactly
371 * fill the @a s array without setting failbit.
373 * -# the input sequence reaches end-of-file, in which case eofbit
374 * is set in the stream error state
375 * -# the next character equals @c delim, in which case the character
376 * is extracted (and therefore counted in @c gcount()) but not stored
377 * -# @c n-1 characters are stored, in which case failbit is set
378 * in the stream error state
380 * If no characters are extracted, failbit is set. (An empty line of
381 * input should therefore not cause failbit to be set.)
383 * In any case, a null character is stored in the next location in
387 getline(char_type
* __s
, streamsize __n
, char_type __delim
);
390 * @brief String extraction.
391 * @param s A character array in which to store the data.
392 * @param n Maximum number of characters to extract.
395 * Returns @c getline(s,n,widen('\n')).
397 inline __istream_type
&
398 getline(char_type
* __s
, streamsize __n
)
399 { return this->getline(__s
, __n
, this->widen('\n')); }
402 * @brief Discarding characters
403 * @param n Number of characters to discard.
404 * @param delim A "stop" character.
407 * Extracts characters and throws them away until one of the
409 * - if @a n @c != @c std::numeric_limits<int>::max(), @a n
410 * characters are extracted
411 * - the input sequence reaches end-of-file
412 * - the next character equals @a delim (in this case, the character
413 * is extracted); note that this condition will never occur if
414 * @a delim equals @c traits::eof().
417 ignore(streamsize __n
= 1, int_type __delim
= traits_type::eof());
420 * @brief Looking ahead in the stream
421 * @return The next character, or eof().
423 * If, after constructing the sentry object, @c good() is false,
424 * returns @c traits::eof(). Otherwise reads but does not extract
425 * the next input character.
431 * @brief Extraction without delimiters.
432 * @param s A character array.
433 * @param n Maximum number of characters to store.
436 * If the stream state is @c good(), extracts characters and stores
437 * them into @a s until one of the following happens:
438 * - @a n characters are stored
439 * - the input sequence reaches end-of-file, in which case the error
440 * state is set to @c failbit|eofbit.
442 * @note This function is not overloaded on signed char and
446 read(char_type
* __s
, streamsize __n
);
449 * @brief Extraction until the buffer is exhausted, but no more.
450 * @param s A character array.
451 * @param n Maximum number of characters to store.
452 * @return The number of characters extracted.
454 * Extracts characters and stores them into @a s depending on the
455 * number of characters remaining in the streambuf's buffer,
456 * @c rdbuf()->in_avail(), called @c A here:
457 * - if @c A @c == @c -1, sets eofbit and extracts no characters
458 * - if @c A @c == @c 0, extracts no characters
459 * - if @c A @c > @c 0, extracts @c min(A,n)
461 * The goal is to empty the current buffer, and to not request any
462 * more from the external input sequence controlled by the streambuf.
465 readsome(char_type
* __s
, streamsize __n
);
468 * @brief Unextracting a single character.
469 * @param c The character to push back into the input stream.
472 * If @c rdbuf() is not null, calls @c rdbuf()->sputbackc(c).
474 * If @c rdbuf() is null or if @c sputbackc() fails, sets badbit in
477 * @note Since no characters are extracted, the next call to
478 * @c gcount() will return 0, as required by DR 60.
481 putback(char_type __c
);
484 * @brief Unextracting the previous character.
487 * If @c rdbuf() is not null, calls @c rdbuf()->sungetc(c).
489 * If @c rdbuf() is null or if @c sungetc() fails, sets badbit in
492 * @note Since no characters are extracted, the next call to
493 * @c gcount() will return 0, as required by DR 60.
499 * @brief Synchronizing the stream buffer.
500 * @return 0 on success, -1 on failure
502 * If @c rdbuf() is a null pointer, returns -1.
504 * Otherwise, calls @c rdbuf()->pubsync(), and if that returns -1,
505 * sets badbit and returns -1.
507 * Otherwise, returns 0.
509 * @note This function does not count the number of characters
510 * extracted, if any, and therefore does not affect the next
511 * call to @c gcount().
517 * @brief Getting the current read position.
518 * @return A file position object.
520 * If @c fail() is not false, returns @c pos_type(-1) to indicate
521 * failure. Otherwise returns @c rdbuf()->pubseekoff(0,cur,in).
523 * @note This function does not count the number of characters
524 * extracted, if any, and therefore does not affect the next
525 * call to @c gcount().
531 * @brief Changing the current read position.
532 * @param pos A file position object.
535 * If @c fail() is not true, calls @c rdbuf()->pubseekpos(pos). If
536 * that function fails, sets failbit.
538 * @note This function does not count the number of characters
539 * extracted, if any, and therefore does not affect the next
540 * call to @c gcount().
546 * @brief Changing the current read position.
547 * @param off A file offset object.
548 * @param dir The direction in which to seek.
551 * If @c fail() is not true, calls @c rdbuf()->pubseekoff(off,dir).
552 * If that function fails, sets failbit.
554 * @note This function does not count the number of characters
555 * extracted, if any, and therefore does not affect the next
556 * call to @c gcount().
559 seekg(off_type
, ios_base::seekdir
);
564 basic_istream(): _M_gcount(streamsize(0)) { }
568 * @brief Performs setup work for input streams.
570 * Objects of this class are created before all of the standard
571 * extractors are run. It is responsible for "exception-safe prefix and
572 * suffix operations," although only prefix actions are currently required
573 * by the standard. Additional actions may be added by the
574 * implementation, and we list them in
575 * http://gcc.gnu.org/onlinedocs/libstdc++/17_intro/howto.html#5
576 * under [27.6] notes.
578 template<typename _CharT
, typename _Traits
>
579 class basic_istream
<_CharT
, _Traits
>::sentry
582 /// Easy access to dependant types.
583 typedef _Traits traits_type
;
584 typedef basic_streambuf
<_CharT
, _Traits
> __streambuf_type
;
585 typedef basic_istream
<_CharT
, _Traits
> __istream_type
;
586 typedef typename
__istream_type::__ctype_type __ctype_type
;
587 typedef typename
_Traits::int_type __int_type
;
590 * @brief The constructor performs all the work.
591 * @param is The input stream to guard.
592 * @param noskipws Whether to consume whitespace or not.
594 * If the stream state is good (@a is.good() is true), then the
595 * following actions are performed, otherwise the sentry state is
596 * false ("not okay") and failbit is set in the stream state.
598 * The sentry's preparatory actions are:
600 * -# if the stream is tied to an output stream, @c is.tie()->flush()
601 * is called to synchronize the output sequence
602 * -# if @a noskipws is false, and @c ios_base::skipws is set in
603 * @c is.flags(), the sentry extracts and discards whitespace
604 * characters from the stream. The currently imbued locale is
605 * used to determine whether each character is whitespace.
607 * If the stream state is still good, then the sentry state becomes
611 sentry(basic_istream
<_CharT
, _Traits
>& __is
, bool __noskipws
= false);
614 * @brief Quick status checking.
615 * @return The sentry state.
617 * For ease of use, sentries may be converted to booleans. The
618 * return value is that of the sentry state (true == okay).
620 operator bool() const { return _M_ok
; }
626 // [27.6.1.2.3] character extraction templates
629 * @brief Character extractors
630 * @param in An input stream.
631 * @param c A character reference.
634 * Behaves like one of the formatted arithmetic extractors described in
635 * std::basic_istream. After constructing a sentry object with good
636 * status, this function extracts a character (if one is available) and
637 * stores it in @a c. Otherwise, sets failbit in the input stream.
639 template<typename _CharT
, typename _Traits
>
640 basic_istream
<_CharT
, _Traits
>&
641 operator>>(basic_istream
<_CharT
, _Traits
>& __in
, _CharT
& __c
);
643 template<class _Traits
>
644 basic_istream
<char, _Traits
>&
645 operator>>(basic_istream
<char, _Traits
>& __in
, unsigned char& __c
)
646 { return (__in
>> reinterpret_cast<char&>(__c
)); }
648 template<class _Traits
>
649 basic_istream
<char, _Traits
>&
650 operator>>(basic_istream
<char, _Traits
>& __in
, signed char& __c
)
651 { return (__in
>> reinterpret_cast<char&>(__c
)); }
656 * @brief Character string extractors
657 * @param in An input stream.
658 * @param s A pointer to a character array.
661 * Behaves like one of the formatted arithmetic extractors described in
662 * std::basic_istream. After constructing a sentry object with good
663 * status, this function extracts up to @c n characters and stores them
664 * into the array starting at @a s. @c n is defined as:
666 * - if @c width() is greater than zero, @c n is width()
667 * - otherwise @c n is "the number of elements of the largest array of
668 * @c char_type that can store a terminating @c eos." [27.6.1.2.3]/6
670 * Characters are extracted and stored until one of the following happens:
671 * - @c n-1 characters are stored
673 * - the next character is whitespace according to the current locale
674 * - the next character is a null byte (i.e., @c charT() )
676 * @c width(0) is then called for the input stream.
678 * If no characters are extracted, sets failbit.
680 template<typename _CharT
, typename _Traits
>
681 basic_istream
<_CharT
, _Traits
>&
682 operator>>(basic_istream
<_CharT
, _Traits
>& __in
, _CharT
* __s
);
684 template<class _Traits
>
685 basic_istream
<char,_Traits
>&
686 operator>>(basic_istream
<char,_Traits
>& __in
, unsigned char* __s
)
687 { return (__in
>> reinterpret_cast<char*>(__s
)); }
689 template<class _Traits
>
690 basic_istream
<char,_Traits
>&
691 operator>>(basic_istream
<char,_Traits
>& __in
, signed char* __s
)
692 { return (__in
>> reinterpret_cast<char*>(__s
)); }
695 // 27.6.1.5 Template class basic_iostream
697 * @brief Merging istream and ostream capabilities.
699 * This class multiply inherits from the input and output stream classes
700 * simply to provide a single interface.
702 template<typename _CharT
, typename _Traits
>
704 : public basic_istream
<_CharT
, _Traits
>,
705 public basic_ostream
<_CharT
, _Traits
>
708 // _GLIBCXX_RESOLVE_LIB_DEFECTS
709 // 271. basic_iostream missing typedefs
710 // Types (inherited):
711 typedef _CharT char_type
;
712 typedef typename
_Traits::int_type int_type
;
713 typedef typename
_Traits::pos_type pos_type
;
714 typedef typename
_Traits::off_type off_type
;
715 typedef _Traits traits_type
;
717 // Non-standard Types:
718 typedef basic_istream
<_CharT
, _Traits
> __istream_type
;
719 typedef basic_ostream
<_CharT
, _Traits
> __ostream_type
;
722 * @brief Constructor does nothing.
724 * Both of the parent classes are initialized with the same
725 * streambuf pointer passed to this constructor.
728 basic_iostream(basic_streambuf
<_CharT
, _Traits
>* __sb
)
729 : __istream_type(), __ostream_type()
730 { this->init(__sb
); }
733 * @brief Destructor does nothing.
736 ~basic_iostream() { }
740 basic_iostream() : __istream_type(), __ostream_type()
744 // [27.6.1.4] standard basic_istream manipulators
746 * @brief Quick and easy way to eat whitespace
748 * This manipulator extracts whitespace characters, stopping when the
749 * next character is non-whitespace, or when the input sequence is empty.
750 * If the sequence is empty, @c eofbit is set in the stream, but not
753 * The current locale is used to distinguish whitespace characters.
759 * std::cin >> std::ws >> mc;
761 * will skip leading whitespace before calling operator>> on cin and your
762 * object. Note that the same effect can be achieved by creating a
763 * std::basic_istream::sentry inside your definition of operator>>.
765 template<typename _CharT
, typename _Traits
>
766 basic_istream
<_CharT
, _Traits
>&
767 ws(basic_istream
<_CharT
, _Traits
>& __is
);
770 #ifndef _GLIBCXX_EXPORT_TEMPLATE
771 # include <bits/istream.tcc>
774 #endif /* _GLIBCXX_ISTREAM */