Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libstdc++-v3 / include / std / std_istream.h
blob0a6bd992467e997e5e738e2edbd9a67dee7b8ec6
1 // Input streams -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2001, 2002, 2003, 2004
4 // Free Software Foundation, Inc.
5 //
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)
10 // any later version.
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,
20 // USA.
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
35 /** @file istream
36 * This is a Standard C++ Library header.
39 #ifndef _GLIBCXX_ISTREAM
40 #define _GLIBCXX_ISTREAM 1
42 #pragma GCC system_header
44 #include <ios>
45 #include <limits> // For numeric_limits
47 namespace std
49 // [27.6.1.1] Template class basic_istream
50 /**
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>
60 public:
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> >
73 __num_get_type;
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*);
84 protected:
85 // Data Members:
86 /**
87 * @if maint
88 * The number of characters extracted in the previous unformatted
89 * function; see gcount().
90 * @endif
92 streamsize _M_gcount;
94 public:
95 // [27.6.1.1.1] constructor/destructor
96 /**
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.
103 explicit
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.
112 virtual
113 ~basic_istream()
114 { _M_gcount = streamsize(0); }
116 // [27.6.1.1.2] prefix/suffix
117 class sentry;
118 friend class sentry;
120 // [27.6.1.2] formatted input
121 // [27.6.1.2.3] basic_istream::operator>>
122 //@{
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&));
138 //@}
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
157 * be rethrown.
159 //@{
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.
168 __istream_type&
169 operator>>(bool& __n);
171 __istream_type&
172 operator>>(short& __n);
174 __istream_type&
175 operator>>(unsigned short& __n);
177 __istream_type&
178 operator>>(int& __n);
180 __istream_type&
181 operator>>(unsigned int& __n);
183 __istream_type&
184 operator>>(long& __n);
186 __istream_type&
187 operator>>(unsigned long& __n);
189 #ifdef _GLIBCXX_USE_LONG_LONG
190 __istream_type&
191 operator>>(long long& __n);
193 __istream_type&
194 operator>>(unsigned long long& __n);
195 #endif
197 __istream_type&
198 operator>>(float& __f);
200 __istream_type&
201 operator>>(double& __f);
203 __istream_type&
204 operator>>(long double& __f);
206 __istream_type&
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
215 * handling behavior.
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.
229 __istream_type&
230 operator>>(__streambuf_type* __sb);
231 //@}
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.
239 inline streamsize
240 gcount() const
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
256 * by gcount().
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
261 * be rethrown.
263 //@{
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().
271 int_type
272 get();
275 * @brief Simple extraction.
276 * @param c The character in which to store data.
277 * @return *this
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
283 * unsigned char.
285 __istream_type&
286 get(char_type& __c);
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.
293 * @return *this
295 * Characters are extracted and stored into @a s until one of the
296 * following happens:
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
301 * is not extracted
303 * If no characters are stored, failbit is set in the stream's error
304 * state.
306 * In any case, a null character is stored into the next location in
307 * the array.
309 * @note This function is not overloaded on signed char and
310 * unsigned char.
312 __istream_type&
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.
319 * @return *this
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.
331 * @return *this
333 * Characters are extracted and inserted into @a sb until one of the
334 * following happens:
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
340 * is not extracted)
341 * - an exception occurs (and in this case is caught)
343 * If no characters are stored, failbit is set in the stream's error
344 * state.
346 __istream_type&
347 get(__streambuf_type& __sb, char_type __delim);
350 * @brief Extraction into another streambuf.
351 * @param sb A streambuf in which to store data.
352 * @return *this
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.
365 * @return *this
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
383 * the array.
385 __istream_type&
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.
392 * @return *this
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.
404 * @return *this
406 * Extracts characters and throws them away until one of the
407 * following happens:
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
418 * the Standard.
420 __istream_type&
421 ignore();
423 __istream_type&
424 ignore(streamsize __n);
426 __istream_type&
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.
437 int_type
438 peek();
441 * @brief Extraction without delimiters.
442 * @param s A character array.
443 * @param n Maximum number of characters to store.
444 * @return *this
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
453 * unsigned char.
455 __istream_type&
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.
474 streamsize
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.
480 * @return *this
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
485 * the error state.
487 * @note Since no characters are extracted, the next call to
488 * @c gcount() will return 0, as required by DR 60.
490 __istream_type&
491 putback(char_type __c);
494 * @brief Unextracting the previous character.
495 * @return *this
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
500 * the error state.
502 * @note Since no characters are extracted, the next call to
503 * @c gcount() will return 0, as required by DR 60.
505 __istream_type&
506 unget();
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().
523 int
524 sync();
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().
537 pos_type
538 tellg();
541 * @brief Changing the current read position.
542 * @param pos A file position object.
543 * @return *this
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().
552 __istream_type&
553 seekg(pos_type);
556 * @brief Changing the current read position.
557 * @param off A file offset object.
558 * @param dir The direction in which to seek.
559 * @return *this
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().
568 __istream_type&
569 seekg(off_type, ios_base::seekdir);
570 //@}
572 protected:
573 explicit
574 basic_istream(): _M_gcount(streamsize(0)) { }
577 // Explicit specialization declarations, defined in src/istream.cc.
578 template<>
579 basic_istream<char>&
580 basic_istream<char>::
581 getline(char_type* __s, streamsize __n, char_type __delim);
583 template<>
584 basic_istream<char>&
585 basic_istream<char>::
586 ignore(streamsize __n);
588 template<>
589 basic_istream<char>&
590 basic_istream<char>::
591 ignore(streamsize __n, int_type __delim);
593 #ifdef _GLIBCXX_USE_WCHAR_T
594 template<>
595 basic_istream<wchar_t>&
596 basic_istream<wchar_t>::
597 getline(char_type* __s, streamsize __n, char_type __delim);
599 template<>
600 basic_istream<wchar_t>&
601 basic_istream<wchar_t>::
602 ignore(streamsize __n);
604 template<>
605 basic_istream<wchar_t>&
606 basic_istream<wchar_t>::
607 ignore(streamsize __n, int_type __delim);
608 #endif
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
624 public:
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
651 * true ("okay").
653 explicit
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; }
665 private:
666 bool _M_ok;
669 // [27.6.1.2.3] character extraction templates
670 //@{
672 * @brief Character extractors
673 * @param in An input stream.
674 * @param c A character reference.
675 * @return in
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)); }
695 //@}
697 //@{
699 * @brief Character string extractors
700 * @param in An input stream.
701 * @param s A pointer to a character array.
702 * @return in
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
715 * - EOF is reached
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 template<class _Traits>
728 basic_istream<char, _Traits>&
729 operator>>(basic_istream<char, _Traits>& __in, unsigned char* __s)
730 { return (__in >> reinterpret_cast<char*>(__s)); }
732 template<class _Traits>
733 basic_istream<char, _Traits>&
734 operator>>(basic_istream<char, _Traits>& __in, signed char* __s)
735 { return (__in >> reinterpret_cast<char*>(__s)); }
736 //@}
738 // 27.6.1.5 Template class basic_iostream
740 * @brief Merging istream and ostream capabilities.
742 * This class multiply inherits from the input and output stream classes
743 * simply to provide a single interface.
745 template<typename _CharT, typename _Traits>
746 class basic_iostream
747 : public basic_istream<_CharT, _Traits>,
748 public basic_ostream<_CharT, _Traits>
750 public:
751 // _GLIBCXX_RESOLVE_LIB_DEFECTS
752 // 271. basic_iostream missing typedefs
753 // Types (inherited):
754 typedef _CharT char_type;
755 typedef typename _Traits::int_type int_type;
756 typedef typename _Traits::pos_type pos_type;
757 typedef typename _Traits::off_type off_type;
758 typedef _Traits traits_type;
760 // Non-standard Types:
761 typedef basic_istream<_CharT, _Traits> __istream_type;
762 typedef basic_ostream<_CharT, _Traits> __ostream_type;
765 * @brief Constructor does nothing.
767 * Both of the parent classes are initialized with the same
768 * streambuf pointer passed to this constructor.
770 explicit
771 basic_iostream(basic_streambuf<_CharT, _Traits>* __sb)
772 : __istream_type(), __ostream_type()
773 { this->init(__sb); }
776 * @brief Destructor does nothing.
778 virtual
779 ~basic_iostream() { }
781 protected:
782 explicit
783 basic_iostream() : __istream_type(), __ostream_type()
787 // [27.6.1.4] standard basic_istream manipulators
789 * @brief Quick and easy way to eat whitespace
791 * This manipulator extracts whitespace characters, stopping when the
792 * next character is non-whitespace, or when the input sequence is empty.
793 * If the sequence is empty, @c eofbit is set in the stream, but not
794 * @c failbit.
796 * The current locale is used to distinguish whitespace characters.
798 * Example:
799 * @code
800 * MyClass mc;
802 * std::cin >> std::ws >> mc;
803 * @endcode
804 * will skip leading whitespace before calling operator>> on cin and your
805 * object. Note that the same effect can be achieved by creating a
806 * std::basic_istream::sentry inside your definition of operator>>.
808 template<typename _CharT, typename _Traits>
809 basic_istream<_CharT, _Traits>&
810 ws(basic_istream<_CharT, _Traits>& __is);
811 } // namespace std
813 #ifndef _GLIBCXX_EXPORT_TEMPLATE
814 # include <bits/istream.tcc>
815 #endif
817 #endif /* _GLIBCXX_ISTREAM */