1 // Input streams -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
5 // Free Software Foundation, Inc.
7 // This file is part of the GNU ISO C++ Library. This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 2, or (at your option)
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this library; see the file COPYING. If not, write to
20 // the Free Software Foundation, 51 Franklin Street, Fifth Floor,
21 // Boston, MA 02110-1301, USA.
23 // As a special exception, you may use this file as part of a free software
24 // library without restriction. Specifically, if other files instantiate
25 // templates or use macros or inline functions from this file, or you compile
26 // this file and link it with other files to produce an executable, this
27 // file does not by itself cause the resulting executable to be covered by
28 // the GNU General Public License. This exception does not however
29 // invalidate any other reasons why the executable file might be covered by
30 // the GNU General Public License.
33 // ISO C++ 14882: 27.6.1 Input streams
37 * This is a Standard C++ Library header.
40 #ifndef _GLIBCXX_ISTREAM
41 #define _GLIBCXX_ISTREAM 1
43 #pragma GCC system_header
48 #include <limits> // For numeric_limits
50 _GLIBCXX_BEGIN_NAMESPACE(std)
52 // [27.6.1.1] Template class basic_istream
54 * @brief Controlling input.
56 * This is the base class for all input streams. It provides text
57 * formatting of all builtin types, and communicates with any class
58 * derived from basic_streambuf to do the actual input.
60 template<typename _CharT, typename _Traits>
61 class basic_istream : virtual public basic_ios<_CharT, _Traits>
64 // Types (inherited from basic_ios (27.4.4)):
65 typedef _CharT char_type;
66 typedef typename _Traits::int_type int_type;
67 typedef typename _Traits::pos_type pos_type;
68 typedef typename _Traits::off_type off_type;
69 typedef _Traits traits_type;
71 // Non-standard Types:
72 typedef basic_streambuf<_CharT, _Traits> __streambuf_type;
73 typedef basic_ios<_CharT, _Traits> __ios_type;
74 typedef basic_istream<_CharT, _Traits> __istream_type;
75 typedef num_get<_CharT, istreambuf_iterator<_CharT, _Traits> >
77 typedef ctype<_CharT> __ctype_type;
79 template<typename _CharT2, typename _Traits2>
80 friend basic_istream<_CharT2, _Traits2>&
81 operator>>(basic_istream<_CharT2, _Traits2>&, _CharT2&);
83 template<typename _CharT2, typename _Traits2>
84 friend basic_istream<_CharT2, _Traits2>&
85 operator>>(basic_istream<_CharT2, _Traits2>&, _CharT2*);
91 * The number of characters extracted in the previous unformatted
92 * function; see gcount().
98 // [27.6.1.1.1] constructor/destructor
100 * @brief Base constructor.
102 * This ctor is almost never called by the user directly, rather from
103 * derived classes' initialization lists, which pass a pointer to
104 * their own stream buffer.
107 basic_istream(__streambuf_type* __sb): _M_gcount(streamsize(0))
108 { this->init(__sb); }
111 * @brief Base destructor.
113 * This does very little apart from providing a virtual base dtor.
117 { _M_gcount = streamsize(0); }
119 // [27.6.1.1.2] prefix/suffix
123 // [27.6.1.2] formatted input
124 // [27.6.1.2.3] basic_istream::operator>>
127 * @brief Interface for manipulators.
129 * Manuipulators such as @c std::ws and @c std::dec use these
130 * functions in constructs like "std::cin >> std::ws". For more
131 * information, see the iomanip header.
134 operator>>(__istream_type& (*__pf)(__istream_type&))
135 { return __pf(*this); }
138 operator>>(__ios_type& (*__pf)(__ios_type&))
145 operator>>(ios_base& (*__pf)(ios_base&))
152 // [27.6.1.2.2] arithmetic extractors
154 * @name Arithmetic Extractors
156 * All the @c operator>> functions (aka <em>formatted input
157 * functions</em>) have some common behavior. Each starts by
158 * constructing a temporary object of type std::basic_istream::sentry
159 * with the second argument (noskipws) set to false. This has several
160 * effects, concluding with the setting of a status flag; see the
161 * sentry documentation for more.
163 * If the sentry status is good, the function tries to extract
164 * whatever data is appropriate for the type of the argument.
166 * If an exception is thrown during extraction, ios_base::badbit
167 * will be turned on in the stream's error state without causing an
168 * ios_base::failure to be thrown. The original exception will then
173 * @brief Basic arithmetic extractors
174 * @param A variable of builtin type.
175 * @return @c *this if successful
177 * These functions use the stream's current locale (specifically, the
178 * @c num_get facet) to parse the input data.
181 operator>>(bool& __n)
182 { return _M_extract(__n); }
185 operator>>(short& __n);
188 operator>>(unsigned short& __n)
189 { return _M_extract(__n); }
192 operator>>(int& __n);
195 operator>>(unsigned int& __n)
196 { return _M_extract(__n); }
199 operator>>(long& __n)
200 { return _M_extract(__n); }
203 operator>>(unsigned long& __n)
204 { return _M_extract(__n); }
206 #ifdef _GLIBCXX_USE_LONG_LONG
208 operator>>(long long& __n)
209 { return _M_extract(__n); }
212 operator>>(unsigned long long& __n)
213 { return _M_extract(__n); }
217 operator>>(float& __f)
218 { return _M_extract(__f); }
221 operator>>(double& __f)
222 { return _M_extract(__f); }
225 operator>>(long double& __f)
226 { return _M_extract(__f); }
229 operator>>(void*& __p)
230 { return _M_extract(__p); }
233 * @brief Extracting into another streambuf.
234 * @param sb A pointer to a streambuf
236 * This function behaves like one of the basic arithmetic extractors,
237 * in that it also constructs a sentry object and has the same error
240 * If @a sb is NULL, the stream will set failbit in its error state.
242 * Characters are extracted from this stream and inserted into the
243 * @a sb streambuf until one of the following occurs:
245 * - the input stream reaches end-of-file,
246 * - insertion into the output buffer fails (in this case, the
247 * character that would have been inserted is not extracted), or
248 * - an exception occurs (and in this case is caught)
250 * If the function inserts no characters, failbit is set.
253 operator>>(__streambuf_type* __sb);
256 // [27.6.1.3] unformatted input
258 * @brief Character counting
259 * @return The number of characters extracted by the previous
260 * unformatted input function dispatched for this stream.
264 { return _M_gcount; }
267 * @name Unformatted Input Functions
269 * All the unformatted input functions have some common behavior.
270 * Each starts by constructing a temporary object of type
271 * std::basic_istream::sentry with the second argument (noskipws)
272 * set to true. This has several effects, concluding with the
273 * setting of a status flag; see the sentry documentation for more.
275 * If the sentry status is good, the function tries to extract
276 * whatever data is appropriate for the type of the argument.
278 * The number of characters extracted is stored for later retrieval
281 * If an exception is thrown during extraction, ios_base::badbit
282 * will be turned on in the stream's error state without causing an
283 * ios_base::failure to be thrown. The original exception will then
288 * @brief Simple extraction.
289 * @return A character, or eof().
291 * Tries to extract a character. If none are available, sets failbit
292 * and returns traits::eof().
298 * @brief Simple extraction.
299 * @param c The character in which to store data.
302 * Tries to extract a character and store it in @a c. If none are
303 * available, sets failbit and returns traits::eof().
305 * @note This function is not overloaded on signed char and
312 * @brief Simple multiple-character extraction.
313 * @param s Pointer to an array.
314 * @param n Maximum number of characters to store in @a s.
315 * @param delim A "stop" character.
318 * Characters are extracted and stored into @a s until one of the
321 * - @c n-1 characters are stored
322 * - the input sequence reaches EOF
323 * - the next character equals @a delim, in which case the character
326 * If no characters are stored, failbit is set in the stream's error
329 * In any case, a null character is stored into the next location in
332 * @note This function is not overloaded on signed char and
336 get(char_type* __s, streamsize __n, char_type __delim);
339 * @brief Simple multiple-character extraction.
340 * @param s Pointer to an array.
341 * @param n Maximum number of characters to store in @a s.
344 * Returns @c get(s,n,widen('\n')).
347 get(char_type* __s, streamsize __n)
348 { return this->get(__s, __n, this->widen('\n')); }
351 * @brief Extraction into another streambuf.
352 * @param sb A streambuf in which to store data.
353 * @param delim A "stop" character.
356 * Characters are extracted and inserted into @a sb until one of the
359 * - the input sequence reaches EOF
360 * - insertion into the output buffer fails (in this case, the
361 * character that would have been inserted is not extracted)
362 * - the next character equals @a delim (in this case, the character
364 * - an exception occurs (and in this case is caught)
366 * If no characters are stored, failbit is set in the stream's error
370 get(__streambuf_type& __sb, char_type __delim);
373 * @brief Extraction into another streambuf.
374 * @param sb A streambuf in which to store data.
377 * Returns @c get(sb,widen('\n')).
380 get(__streambuf_type& __sb)
381 { return this->get(__sb, this->widen('\n')); }
384 * @brief String extraction.
385 * @param s A character array in which to store the data.
386 * @param n Maximum number of characters to extract.
387 * @param delim A "stop" character.
390 * Extracts and stores characters into @a s until one of the
391 * following happens. Note that these criteria are required to be
392 * tested in the order listed here, to allow an input line to exactly
393 * fill the @a s array without setting failbit.
395 * -# the input sequence reaches end-of-file, in which case eofbit
396 * is set in the stream error state
397 * -# the next character equals @c delim, in which case the character
398 * is extracted (and therefore counted in @c gcount()) but not stored
399 * -# @c n-1 characters are stored, in which case failbit is set
400 * in the stream error state
402 * If no characters are extracted, failbit is set. (An empty line of
403 * input should therefore not cause failbit to be set.)
405 * In any case, a null character is stored in the next location in
409 getline(char_type* __s, streamsize __n, char_type __delim);
412 * @brief String extraction.
413 * @param s A character array in which to store the data.
414 * @param n Maximum number of characters to extract.
417 * Returns @c getline(s,n,widen('\n')).
420 getline(char_type* __s, streamsize __n)
421 { return this->getline(__s, __n, this->widen('\n')); }
424 * @brief Discarding characters
425 * @param n Number of characters to discard.
426 * @param delim A "stop" character.
429 * Extracts characters and throws them away until one of the
431 * - if @a n @c != @c std::numeric_limits<int>::max(), @a n
432 * characters are extracted
433 * - the input sequence reaches end-of-file
434 * - the next character equals @a delim (in this case, the character
435 * is extracted); note that this condition will never occur if
436 * @a delim equals @c traits::eof().
438 * NB: Provide three overloads, instead of the single function
439 * (with defaults) mandated by the Standard: this leads to a
440 * better performing implementation, while still conforming to
447 ignore(streamsize __n);
450 ignore(streamsize __n, int_type __delim);
453 * @brief Looking ahead in the stream
454 * @return The next character, or eof().
456 * If, after constructing the sentry object, @c good() is false,
457 * returns @c traits::eof(). Otherwise reads but does not extract
458 * the next input character.
464 * @brief Extraction without delimiters.
465 * @param s A character array.
466 * @param n Maximum number of characters to store.
469 * If the stream state is @c good(), extracts characters and stores
470 * them into @a s until one of the following happens:
471 * - @a n characters are stored
472 * - the input sequence reaches end-of-file, in which case the error
473 * state is set to @c failbit|eofbit.
475 * @note This function is not overloaded on signed char and
479 read(char_type* __s, streamsize __n);
482 * @brief Extraction until the buffer is exhausted, but no more.
483 * @param s A character array.
484 * @param n Maximum number of characters to store.
485 * @return The number of characters extracted.
487 * Extracts characters and stores them into @a s depending on the
488 * number of characters remaining in the streambuf's buffer,
489 * @c rdbuf()->in_avail(), called @c A here:
490 * - if @c A @c == @c -1, sets eofbit and extracts no characters
491 * - if @c A @c == @c 0, extracts no characters
492 * - if @c A @c > @c 0, extracts @c min(A,n)
494 * The goal is to empty the current buffer, and to not request any
495 * more from the external input sequence controlled by the streambuf.
498 readsome(char_type* __s, streamsize __n);
501 * @brief Unextracting a single character.
502 * @param c The character to push back into the input stream.
505 * If @c rdbuf() is not null, calls @c rdbuf()->sputbackc(c).
507 * If @c rdbuf() is null or if @c sputbackc() fails, sets badbit in
510 * @note Since no characters are extracted, the next call to
511 * @c gcount() will return 0, as required by DR 60.
514 putback(char_type __c);
517 * @brief Unextracting the previous character.
520 * If @c rdbuf() is not null, calls @c rdbuf()->sungetc(c).
522 * If @c rdbuf() is null or if @c sungetc() fails, sets badbit in
525 * @note Since no characters are extracted, the next call to
526 * @c gcount() will return 0, as required by DR 60.
532 * @brief Synchronizing the stream buffer.
533 * @return 0 on success, -1 on failure
535 * If @c rdbuf() is a null pointer, returns -1.
537 * Otherwise, calls @c rdbuf()->pubsync(), and if that returns -1,
538 * sets badbit and returns -1.
540 * Otherwise, returns 0.
542 * @note This function does not count the number of characters
543 * extracted, if any, and therefore does not affect the next
544 * call to @c gcount().
550 * @brief Getting the current read position.
551 * @return A file position object.
553 * If @c fail() is not false, returns @c pos_type(-1) to indicate
554 * failure. Otherwise returns @c rdbuf()->pubseekoff(0,cur,in).
556 * @note This function does not count the number of characters
557 * extracted, if any, and therefore does not affect the next
558 * call to @c gcount().
564 * @brief Changing the current read position.
565 * @param pos A file position object.
568 * If @c fail() is not true, calls @c rdbuf()->pubseekpos(pos). If
569 * that function fails, sets failbit.
571 * @note This function does not count the number of characters
572 * extracted, if any, and therefore does not affect the next
573 * call to @c gcount().
579 * @brief Changing the current read position.
580 * @param off A file offset object.
581 * @param dir The direction in which to seek.
584 * If @c fail() is not true, calls @c rdbuf()->pubseekoff(off,dir).
585 * If that function fails, sets failbit.
587 * @note This function does not count the number of characters
588 * extracted, if any, and therefore does not affect the next
589 * call to @c gcount().
592 seekg(off_type, ios_base::seekdir);
597 basic_istream(): _M_gcount(streamsize(0)) { }
599 template<typename _ValueT>
601 _M_extract(_ValueT& __v);
604 // Explicit specialization declarations, defined in src/istream.cc.
607 basic_istream<char>::
608 getline(char_type* __s, streamsize __n, char_type __delim);
612 basic_istream<char>::
613 ignore(streamsize __n);
617 basic_istream<char>::
618 ignore(streamsize __n, int_type __delim);
620 #ifdef _GLIBCXX_USE_WCHAR_T
622 basic_istream<wchar_t>&
623 basic_istream<wchar_t>::
624 getline(char_type* __s, streamsize __n, char_type __delim);
627 basic_istream<wchar_t>&
628 basic_istream<wchar_t>::
629 ignore(streamsize __n);
632 basic_istream<wchar_t>&
633 basic_istream<wchar_t>::
634 ignore(streamsize __n, int_type __delim);
638 * @brief Performs setup work for input streams.
640 * Objects of this class are created before all of the standard
641 * extractors are run. It is responsible for "exception-safe prefix and
642 * suffix operations," although only prefix actions are currently required
643 * by the standard. Additional actions may be added by the
644 * implementation, and we list them in
645 * http://gcc.gnu.org/onlinedocs/libstdc++/17_intro/howto.html#5
646 * under [27.6] notes.
648 template<typename _CharT, typename _Traits>
649 class basic_istream<_CharT, _Traits>::sentry
652 /// Easy access to dependant types.
653 typedef _Traits traits_type;
654 typedef basic_streambuf<_CharT, _Traits> __streambuf_type;
655 typedef basic_istream<_CharT, _Traits> __istream_type;
656 typedef typename __istream_type::__ctype_type __ctype_type;
657 typedef typename _Traits::int_type __int_type;
660 * @brief The constructor performs all the work.
661 * @param is The input stream to guard.
662 * @param noskipws Whether to consume whitespace or not.
664 * If the stream state is good (@a is.good() is true), then the
665 * following actions are performed, otherwise the sentry state is
666 * false ("not okay") and failbit is set in the stream state.
668 * The sentry's preparatory actions are:
670 * -# if the stream is tied to an output stream, @c is.tie()->flush()
671 * is called to synchronize the output sequence
672 * -# if @a noskipws is false, and @c ios_base::skipws is set in
673 * @c is.flags(), the sentry extracts and discards whitespace
674 * characters from the stream. The currently imbued locale is
675 * used to determine whether each character is whitespace.
677 * If the stream state is still good, then the sentry state becomes
681 sentry(basic_istream<_CharT, _Traits>& __is, bool __noskipws = false);
684 * @brief Quick status checking.
685 * @return The sentry state.
687 * For ease of use, sentries may be converted to booleans. The
688 * return value is that of the sentry state (true == okay).
690 operator bool() const
697 // [27.6.1.2.3] character extraction templates
700 * @brief Character extractors
701 * @param in An input stream.
702 * @param c A character reference.
705 * Behaves like one of the formatted arithmetic extractors described in
706 * std::basic_istream. After constructing a sentry object with good
707 * status, this function extracts a character (if one is available) and
708 * stores it in @a c. Otherwise, sets failbit in the input stream.
710 template<typename _CharT, typename _Traits>
711 basic_istream<_CharT, _Traits>&
712 operator>>(basic_istream<_CharT, _Traits>& __in, _CharT& __c);
714 template<class _Traits>
715 inline basic_istream<char, _Traits>&
716 operator>>(basic_istream<char, _Traits>& __in, unsigned char& __c)
717 { return (__in >> reinterpret_cast<char&>(__c)); }
719 template<class _Traits>
720 inline basic_istream<char, _Traits>&
721 operator>>(basic_istream<char, _Traits>& __in, signed char& __c)
722 { return (__in >> reinterpret_cast<char&>(__c)); }
727 * @brief Character string extractors
728 * @param in An input stream.
729 * @param s A pointer to a character array.
732 * Behaves like one of the formatted arithmetic extractors described in
733 * std::basic_istream. After constructing a sentry object with good
734 * status, this function extracts up to @c n characters and stores them
735 * into the array starting at @a s. @c n is defined as:
737 * - if @c width() is greater than zero, @c n is width()
738 * - otherwise @c n is "the number of elements of the largest array of
739 * @c char_type that can store a terminating @c eos." [27.6.1.2.3]/6
741 * Characters are extracted and stored until one of the following happens:
742 * - @c n-1 characters are stored
744 * - the next character is whitespace according to the current locale
745 * - the next character is a null byte (i.e., @c charT() )
747 * @c width(0) is then called for the input stream.
749 * If no characters are extracted, sets failbit.
751 template<typename _CharT, typename _Traits>
752 basic_istream<_CharT, _Traits>&
753 operator>>(basic_istream<_CharT, _Traits>& __in, _CharT* __s);
755 // Explicit specialization declaration, defined in src/istream.cc.
758 operator>>(basic_istream<char>& __in, char* __s);
760 template<class _Traits>
761 inline basic_istream<char, _Traits>&
762 operator>>(basic_istream<char, _Traits>& __in, unsigned char* __s)
763 { return (__in >> reinterpret_cast<char*>(__s)); }
765 template<class _Traits>
766 inline basic_istream<char, _Traits>&
767 operator>>(basic_istream<char, _Traits>& __in, signed char* __s)
768 { return (__in >> reinterpret_cast<char*>(__s)); }
771 // 27.6.1.5 Template class basic_iostream
773 * @brief Merging istream and ostream capabilities.
775 * This class multiply inherits from the input and output stream classes
776 * simply to provide a single interface.
778 template<typename _CharT, typename _Traits>
780 : public basic_istream<_CharT, _Traits>,
781 public basic_ostream<_CharT, _Traits>
784 // _GLIBCXX_RESOLVE_LIB_DEFECTS
785 // 271. basic_iostream missing typedefs
786 // Types (inherited):
787 typedef _CharT char_type;
788 typedef typename _Traits::int_type int_type;
789 typedef typename _Traits::pos_type pos_type;
790 typedef typename _Traits::off_type off_type;
791 typedef _Traits traits_type;
793 // Non-standard Types:
794 typedef basic_istream<_CharT, _Traits> __istream_type;
795 typedef basic_ostream<_CharT, _Traits> __ostream_type;
798 * @brief Constructor does nothing.
800 * Both of the parent classes are initialized with the same
801 * streambuf pointer passed to this constructor.
804 basic_iostream(basic_streambuf<_CharT, _Traits>* __sb)
805 : __istream_type(), __ostream_type()
806 { this->init(__sb); }
809 * @brief Destructor does nothing.
812 ~basic_iostream() { }
816 basic_iostream() : __istream_type(), __ostream_type()
820 // [27.6.1.4] standard basic_istream manipulators
822 * @brief Quick and easy way to eat whitespace
824 * This manipulator extracts whitespace characters, stopping when the
825 * next character is non-whitespace, or when the input sequence is empty.
826 * If the sequence is empty, @c eofbit is set in the stream, but not
829 * The current locale is used to distinguish whitespace characters.
835 * std::cin >> std::ws >> mc;
837 * will skip leading whitespace before calling operator>> on cin and your
838 * object. Note that the same effect can be achieved by creating a
839 * std::basic_istream::sentry inside your definition of operator>>.
841 template<typename _CharT, typename _Traits>
842 basic_istream<_CharT, _Traits>&
843 ws(basic_istream<_CharT, _Traits>& __is);
845 _GLIBCXX_END_NAMESPACE
847 #ifndef _GLIBCXX_EXPORT_TEMPLATE
848 # include <bits/istream.tcc>
851 #endif /* _GLIBCXX_ISTREAM */