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 _GLIBCXX_BEGIN_NAMESPACE(std)
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;
80 * The number of characters extracted in the previous unformatted
81 * function; see gcount().
86 // [27.6.1.1.1] constructor/destructor
88 * @brief Base constructor.
90 * This ctor is almost never called by the user directly, rather from
91 * derived classes' initialization lists, which pass a pointer to
92 * their own stream buffer.
95 basic_istream(__streambuf_type* __sb)
96 : _M_gcount(streamsize(0))
100 * @brief Base destructor.
102 * This does very little apart from providing a virtual base dtor.
106 { _M_gcount = streamsize(0); }
108 // [27.6.1.1.2] prefix/suffix
112 // [27.6.1.2] formatted input
113 // [27.6.1.2.3] basic_istream::operator>>
116 * @brief Interface for manipulators.
118 * Manipulators such as @c std::ws and @c std::dec use these
119 * functions in constructs like "std::cin >> std::ws". For more
120 * information, see the iomanip header.
123 operator>>(__istream_type& (*__pf)(__istream_type&))
124 { return __pf(*this); }
127 operator>>(__ios_type& (*__pf)(__ios_type&))
134 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)
171 { return _M_extract(__n); }
174 operator>>(short& __n);
177 operator>>(unsigned short& __n)
178 { return _M_extract(__n); }
181 operator>>(int& __n);
184 operator>>(unsigned int& __n)
185 { return _M_extract(__n); }
188 operator>>(long& __n)
189 { return _M_extract(__n); }
192 operator>>(unsigned long& __n)
193 { return _M_extract(__n); }
195 #ifdef _GLIBCXX_USE_LONG_LONG
197 operator>>(long long& __n)
198 { return _M_extract(__n); }
201 operator>>(unsigned long long& __n)
202 { return _M_extract(__n); }
206 operator>>(float& __f)
207 { return _M_extract(__f); }
210 operator>>(double& __f)
211 { return _M_extract(__f); }
214 operator>>(long double& __f)
215 { return _M_extract(__f); }
218 operator>>(void*& __p)
219 { return _M_extract(__p); }
222 * @brief Extracting into another streambuf.
223 * @param sb A pointer to a streambuf
225 * This function behaves like one of the basic arithmetic extractors,
226 * in that it also constructs a sentry object and has the same error
229 * If @a sb is NULL, the stream will set failbit in its error state.
231 * Characters are extracted from this stream and inserted into the
232 * @a sb streambuf until one of the following occurs:
234 * - the input stream reaches end-of-file,
235 * - insertion into the output buffer fails (in this case, the
236 * character that would have been inserted is not extracted), or
237 * - an exception occurs (and in this case is caught)
239 * If the function inserts no characters, failbit is set.
242 operator>>(__streambuf_type* __sb);
245 // [27.6.1.3] unformatted input
247 * @brief Character counting
248 * @return The number of characters extracted by the previous
249 * unformatted input function dispatched for this stream.
253 { return _M_gcount; }
256 * @name Unformatted Input Functions
258 * All the unformatted input functions have some common behavior.
259 * Each starts by constructing a temporary object of type
260 * std::basic_istream::sentry with the second argument (noskipws)
261 * set to true. This has several effects, concluding with the
262 * setting of a status flag; see the sentry documentation for more.
264 * If the sentry status is good, the function tries to extract
265 * whatever data is appropriate for the type of the argument.
267 * The number of characters extracted is stored for later retrieval
270 * If an exception is thrown during extraction, ios_base::badbit
271 * will be turned on in the stream's error state without causing an
272 * ios_base::failure to be thrown. The original exception will then
277 * @brief Simple extraction.
278 * @return A character, or eof().
280 * Tries to extract a character. If none are available, sets failbit
281 * and returns traits::eof().
287 * @brief Simple extraction.
288 * @param c The character in which to store data.
291 * Tries to extract a character and store it in @a c. If none are
292 * available, sets failbit and returns traits::eof().
294 * @note This function is not overloaded on signed char and
301 * @brief Simple multiple-character extraction.
302 * @param s Pointer to an array.
303 * @param n Maximum number of characters to store in @a s.
304 * @param delim A "stop" character.
307 * Characters are extracted and stored into @a s until one of the
310 * - @c n-1 characters are stored
311 * - the input sequence reaches EOF
312 * - the next character equals @a delim, in which case the character
315 * If no characters are stored, failbit is set in the stream's error
318 * In any case, a null character is stored into the next location in
321 * @note This function is not overloaded on signed char and
325 get(char_type* __s, streamsize __n, char_type __delim);
328 * @brief Simple multiple-character extraction.
329 * @param s Pointer to an array.
330 * @param n Maximum number of characters to store in @a s.
333 * Returns @c get(s,n,widen('\n')).
336 get(char_type* __s, streamsize __n)
337 { return this->get(__s, __n, this->widen('\n')); }
340 * @brief Extraction into another streambuf.
341 * @param sb A streambuf in which to store data.
342 * @param delim A "stop" character.
345 * Characters are extracted and inserted into @a sb until one of the
348 * - the input sequence reaches EOF
349 * - insertion into the output buffer fails (in this case, the
350 * character that would have been inserted is not extracted)
351 * - the next character equals @a delim (in this case, the character
353 * - an exception occurs (and in this case is caught)
355 * If no characters are stored, failbit is set in the stream's error
359 get(__streambuf_type& __sb, char_type __delim);
362 * @brief Extraction into another streambuf.
363 * @param sb A streambuf in which to store data.
366 * Returns @c get(sb,widen('\n')).
369 get(__streambuf_type& __sb)
370 { return this->get(__sb, this->widen('\n')); }
373 * @brief String extraction.
374 * @param s A character array in which to store the data.
375 * @param n Maximum number of characters to extract.
376 * @param delim A "stop" character.
379 * Extracts and stores characters into @a s until one of the
380 * following happens. Note that these criteria are required to be
381 * tested in the order listed here, to allow an input line to exactly
382 * fill the @a s array without setting failbit.
384 * -# the input sequence reaches end-of-file, in which case eofbit
385 * is set in the stream error state
386 * -# the next character equals @c delim, in which case the character
387 * is extracted (and therefore counted in @c gcount()) but not stored
388 * -# @c n-1 characters are stored, in which case failbit is set
389 * in the stream error state
391 * If no characters are extracted, failbit is set. (An empty line of
392 * input should therefore not cause failbit to be set.)
394 * In any case, a null character is stored in the next location in
398 getline(char_type* __s, streamsize __n, char_type __delim);
401 * @brief String extraction.
402 * @param s A character array in which to store the data.
403 * @param n Maximum number of characters to extract.
406 * Returns @c getline(s,n,widen('\n')).
409 getline(char_type* __s, streamsize __n)
410 { return this->getline(__s, __n, this->widen('\n')); }
413 * @brief Discarding characters
414 * @param n Number of characters to discard.
415 * @param delim A "stop" character.
418 * Extracts characters and throws them away until one of the
420 * - if @a n @c != @c std::numeric_limits<int>::max(), @a n
421 * characters are extracted
422 * - the input sequence reaches end-of-file
423 * - the next character equals @a delim (in this case, the character
424 * is extracted); note that this condition will never occur if
425 * @a delim equals @c traits::eof().
427 * NB: Provide three overloads, instead of the single function
428 * (with defaults) mandated by the Standard: this leads to a
429 * better performing implementation, while still conforming to
436 ignore(streamsize __n);
439 ignore(streamsize __n, int_type __delim);
442 * @brief Looking ahead in the stream
443 * @return The next character, or eof().
445 * If, after constructing the sentry object, @c good() is false,
446 * returns @c traits::eof(). Otherwise reads but does not extract
447 * the next input character.
453 * @brief Extraction without delimiters.
454 * @param s A character array.
455 * @param n Maximum number of characters to store.
458 * If the stream state is @c good(), extracts characters and stores
459 * them into @a s until one of the following happens:
460 * - @a n characters are stored
461 * - the input sequence reaches end-of-file, in which case the error
462 * state is set to @c failbit|eofbit.
464 * @note This function is not overloaded on signed char and
468 read(char_type* __s, streamsize __n);
471 * @brief Extraction until the buffer is exhausted, but no more.
472 * @param s A character array.
473 * @param n Maximum number of characters to store.
474 * @return The number of characters extracted.
476 * Extracts characters and stores them into @a s depending on the
477 * number of characters remaining in the streambuf's buffer,
478 * @c rdbuf()->in_avail(), called @c A here:
479 * - if @c A @c == @c -1, sets eofbit and extracts no characters
480 * - if @c A @c == @c 0, extracts no characters
481 * - if @c A @c > @c 0, extracts @c min(A,n)
483 * The goal is to empty the current buffer, and to not request any
484 * more from the external input sequence controlled by the streambuf.
487 readsome(char_type* __s, streamsize __n);
490 * @brief Unextracting a single character.
491 * @param c The character to push back into the input stream.
494 * If @c rdbuf() is not null, calls @c rdbuf()->sputbackc(c).
496 * If @c rdbuf() is null or if @c sputbackc() fails, sets badbit in
499 * @note Since no characters are extracted, the next call to
500 * @c gcount() will return 0, as required by DR 60.
503 putback(char_type __c);
506 * @brief Unextracting the previous character.
509 * If @c rdbuf() is not null, calls @c rdbuf()->sungetc(c).
511 * If @c rdbuf() is null or if @c sungetc() fails, sets badbit in
514 * @note Since no characters are extracted, the next call to
515 * @c gcount() will return 0, as required by DR 60.
521 * @brief Synchronizing the stream buffer.
522 * @return 0 on success, -1 on failure
524 * If @c rdbuf() is a null pointer, returns -1.
526 * Otherwise, calls @c rdbuf()->pubsync(), and if that returns -1,
527 * sets badbit and returns -1.
529 * Otherwise, returns 0.
531 * @note This function does not count the number of characters
532 * extracted, if any, and therefore does not affect the next
533 * call to @c gcount().
539 * @brief Getting the current read position.
540 * @return A file position object.
542 * If @c fail() is not false, returns @c pos_type(-1) to indicate
543 * failure. Otherwise returns @c rdbuf()->pubseekoff(0,cur,in).
545 * @note This function does not count the number of characters
546 * extracted, if any, and therefore does not affect the next
547 * call to @c gcount().
553 * @brief Changing the current read position.
554 * @param pos A file position object.
557 * If @c fail() is not true, calls @c rdbuf()->pubseekpos(pos). If
558 * that function fails, sets failbit.
560 * @note This function does not count the number of characters
561 * extracted, if any, and therefore does not affect the next
562 * call to @c gcount().
568 * @brief Changing the current read position.
569 * @param off A file offset object.
570 * @param dir The direction in which to seek.
573 * If @c fail() is not true, calls @c rdbuf()->pubseekoff(off,dir).
574 * If that function fails, sets failbit.
576 * @note This function does not count the number of characters
577 * extracted, if any, and therefore does not affect the next
578 * call to @c gcount().
581 seekg(off_type, ios_base::seekdir);
586 : _M_gcount(streamsize(0))
589 template<typename _ValueT>
591 _M_extract(_ValueT& __v);
594 // Explicit specialization declarations, defined in src/istream.cc.
597 basic_istream<char>::
598 getline(char_type* __s, streamsize __n, char_type __delim);
602 basic_istream<char>::
603 ignore(streamsize __n);
607 basic_istream<char>::
608 ignore(streamsize __n, int_type __delim);
610 #ifdef _GLIBCXX_USE_WCHAR_T
612 basic_istream<wchar_t>&
613 basic_istream<wchar_t>::
614 getline(char_type* __s, streamsize __n, char_type __delim);
617 basic_istream<wchar_t>&
618 basic_istream<wchar_t>::
619 ignore(streamsize __n);
622 basic_istream<wchar_t>&
623 basic_istream<wchar_t>::
624 ignore(streamsize __n, int_type __delim);
628 * @brief Performs setup work for input streams.
630 * Objects of this class are created before all of the standard
631 * extractors are run. It is responsible for "exception-safe prefix and
632 * suffix operations," although only prefix actions are currently required
635 template<typename _CharT, typename _Traits>
636 class basic_istream<_CharT, _Traits>::sentry
639 /// Easy access to dependant types.
640 typedef _Traits traits_type;
641 typedef basic_streambuf<_CharT, _Traits> __streambuf_type;
642 typedef basic_istream<_CharT, _Traits> __istream_type;
643 typedef typename __istream_type::__ctype_type __ctype_type;
644 typedef typename _Traits::int_type __int_type;
647 * @brief The constructor performs all the work.
648 * @param is The input stream to guard.
649 * @param noskipws Whether to consume whitespace or not.
651 * If the stream state is good (@a is.good() is true), then the
652 * following actions are performed, otherwise the sentry state is
653 * false ("not okay") and failbit is set in the stream state.
655 * The sentry's preparatory actions are:
657 * -# if the stream is tied to an output stream, @c is.tie()->flush()
658 * is called to synchronize the output sequence
659 * -# if @a noskipws is false, and @c ios_base::skipws is set in
660 * @c is.flags(), the sentry extracts and discards whitespace
661 * characters from the stream. The currently imbued locale is
662 * used to determine whether each character is whitespace.
664 * If the stream state is still good, then the sentry state becomes
668 sentry(basic_istream<_CharT, _Traits>& __is, bool __noskipws = false);
671 * @brief Quick status checking.
672 * @return The sentry state.
674 * For ease of use, sentries may be converted to booleans. The
675 * return value is that of the sentry state (true == okay).
677 operator bool() const
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 inline 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 inline 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 inline 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 inline 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(__sb), __ostream_type(__sb) { }
795 * @brief Destructor does nothing.
798 ~basic_iostream() { }
802 : __istream_type(), __ostream_type() { }
805 // [27.6.1.4] standard basic_istream manipulators
807 * @brief Quick and easy way to eat whitespace
809 * This manipulator extracts whitespace characters, stopping when the
810 * next character is non-whitespace, or when the input sequence is empty.
811 * If the sequence is empty, @c eofbit is set in the stream, but not
814 * The current locale is used to distinguish whitespace characters.
820 * std::cin >> std::ws >> mc;
822 * will skip leading whitespace before calling operator>> on cin and your
823 * object. Note that the same effect can be achieved by creating a
824 * std::basic_istream::sentry inside your definition of operator>>.
826 template<typename _CharT, typename _Traits>
827 basic_istream<_CharT, _Traits>&
828 ws(basic_istream<_CharT, _Traits>& __is);
830 _GLIBCXX_END_NAMESPACE
832 #ifndef _GLIBCXX_EXPORT_TEMPLATE
833 # include <bits/istream.tcc>
836 #endif /* _GLIBCXX_ISTREAM */