1*404b540aSrobert // Input streams -*- C++ -*-
2*404b540aSrobert
3*404b540aSrobert // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
4*404b540aSrobert // Free Software Foundation, Inc.
5*404b540aSrobert //
6*404b540aSrobert // This file is part of the GNU ISO C++ Library. This library is free
7*404b540aSrobert // software; you can redistribute it and/or modify it under the
8*404b540aSrobert // terms of the GNU General Public License as published by the
9*404b540aSrobert // Free Software Foundation; either version 2, or (at your option)
10*404b540aSrobert // any later version.
11*404b540aSrobert
12*404b540aSrobert // This library is distributed in the hope that it will be useful,
13*404b540aSrobert // but WITHOUT ANY WARRANTY; without even the implied warranty of
14*404b540aSrobert // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15*404b540aSrobert // GNU General Public License for more details.
16*404b540aSrobert
17*404b540aSrobert // You should have received a copy of the GNU General Public License along
18*404b540aSrobert // with this library; see the file COPYING. If not, write to the Free
19*404b540aSrobert // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20*404b540aSrobert // USA.
21*404b540aSrobert
22*404b540aSrobert // As a special exception, you may use this file as part of a free software
23*404b540aSrobert // library without restriction. Specifically, if other files instantiate
24*404b540aSrobert // templates or use macros or inline functions from this file, or you compile
25*404b540aSrobert // this file and link it with other files to produce an executable, this
26*404b540aSrobert // file does not by itself cause the resulting executable to be covered by
27*404b540aSrobert // the GNU General Public License. This exception does not however
28*404b540aSrobert // invalidate any other reasons why the executable file might be covered by
29*404b540aSrobert // the GNU General Public License.
30*404b540aSrobert
31*404b540aSrobert //
32*404b540aSrobert // ISO C++ 14882: 27.6.1 Input streams
33*404b540aSrobert //
34*404b540aSrobert
35*404b540aSrobert /** @file istream
36*404b540aSrobert * This is a Standard C++ Library header.
37*404b540aSrobert */
38*404b540aSrobert
39*404b540aSrobert #ifndef _GLIBCXX_ISTREAM
40*404b540aSrobert #define _GLIBCXX_ISTREAM 1
41*404b540aSrobert
42*404b540aSrobert #pragma GCC system_header
43*404b540aSrobert
44*404b540aSrobert #include <ios>
45*404b540aSrobert #include <limits> // For numeric_limits
46*404b540aSrobert
_GLIBCXX_BEGIN_NAMESPACE(std)47*404b540aSrobert _GLIBCXX_BEGIN_NAMESPACE(std)
48*404b540aSrobert
49*404b540aSrobert // [27.6.1.1] Template class basic_istream
50*404b540aSrobert /**
51*404b540aSrobert * @brief Controlling input.
52*404b540aSrobert *
53*404b540aSrobert * This is the base class for all input streams. It provides text
54*404b540aSrobert * formatting of all builtin types, and communicates with any class
55*404b540aSrobert * derived from basic_streambuf to do the actual input.
56*404b540aSrobert */
57*404b540aSrobert template<typename _CharT, typename _Traits>
58*404b540aSrobert class basic_istream : virtual public basic_ios<_CharT, _Traits>
59*404b540aSrobert {
60*404b540aSrobert public:
61*404b540aSrobert // Types (inherited from basic_ios (27.4.4)):
62*404b540aSrobert typedef _CharT char_type;
63*404b540aSrobert typedef typename _Traits::int_type int_type;
64*404b540aSrobert typedef typename _Traits::pos_type pos_type;
65*404b540aSrobert typedef typename _Traits::off_type off_type;
66*404b540aSrobert typedef _Traits traits_type;
67*404b540aSrobert
68*404b540aSrobert // Non-standard Types:
69*404b540aSrobert typedef basic_streambuf<_CharT, _Traits> __streambuf_type;
70*404b540aSrobert typedef basic_ios<_CharT, _Traits> __ios_type;
71*404b540aSrobert typedef basic_istream<_CharT, _Traits> __istream_type;
72*404b540aSrobert typedef num_get<_CharT, istreambuf_iterator<_CharT, _Traits> >
73*404b540aSrobert __num_get_type;
74*404b540aSrobert typedef ctype<_CharT> __ctype_type;
75*404b540aSrobert
76*404b540aSrobert template<typename _CharT2, typename _Traits2>
77*404b540aSrobert friend basic_istream<_CharT2, _Traits2>&
78*404b540aSrobert operator>>(basic_istream<_CharT2, _Traits2>&, _CharT2&);
79*404b540aSrobert
80*404b540aSrobert template<typename _CharT2, typename _Traits2>
81*404b540aSrobert friend basic_istream<_CharT2, _Traits2>&
82*404b540aSrobert operator>>(basic_istream<_CharT2, _Traits2>&, _CharT2*);
83*404b540aSrobert
84*404b540aSrobert protected:
85*404b540aSrobert // Data Members:
86*404b540aSrobert /**
87*404b540aSrobert * @if maint
88*404b540aSrobert * The number of characters extracted in the previous unformatted
89*404b540aSrobert * function; see gcount().
90*404b540aSrobert * @endif
91*404b540aSrobert */
92*404b540aSrobert streamsize _M_gcount;
93*404b540aSrobert
94*404b540aSrobert public:
95*404b540aSrobert // [27.6.1.1.1] constructor/destructor
96*404b540aSrobert /**
97*404b540aSrobert * @brief Base constructor.
98*404b540aSrobert *
99*404b540aSrobert * This ctor is almost never called by the user directly, rather from
100*404b540aSrobert * derived classes' initialization lists, which pass a pointer to
101*404b540aSrobert * their own stream buffer.
102*404b540aSrobert */
103*404b540aSrobert explicit
104*404b540aSrobert basic_istream(__streambuf_type* __sb): _M_gcount(streamsize(0))
105*404b540aSrobert { this->init(__sb); }
106*404b540aSrobert
107*404b540aSrobert /**
108*404b540aSrobert * @brief Base destructor.
109*404b540aSrobert *
110*404b540aSrobert * This does very little apart from providing a virtual base dtor.
111*404b540aSrobert */
112*404b540aSrobert virtual
113*404b540aSrobert ~basic_istream()
114*404b540aSrobert { _M_gcount = streamsize(0); }
115*404b540aSrobert
116*404b540aSrobert // [27.6.1.1.2] prefix/suffix
117*404b540aSrobert class sentry;
118*404b540aSrobert friend class sentry;
119*404b540aSrobert
120*404b540aSrobert // [27.6.1.2] formatted input
121*404b540aSrobert // [27.6.1.2.3] basic_istream::operator>>
122*404b540aSrobert //@{
123*404b540aSrobert /**
124*404b540aSrobert * @brief Interface for manipulators.
125*404b540aSrobert *
126*404b540aSrobert * Manuipulators such as @c std::ws and @c std::dec use these
127*404b540aSrobert * functions in constructs like "std::cin >> std::ws". For more
128*404b540aSrobert * information, see the iomanip header.
129*404b540aSrobert */
130*404b540aSrobert __istream_type&
131*404b540aSrobert operator>>(__istream_type& (*__pf)(__istream_type&))
132*404b540aSrobert { return __pf(*this); }
133*404b540aSrobert
134*404b540aSrobert __istream_type&
135*404b540aSrobert operator>>(__ios_type& (*__pf)(__ios_type&))
136*404b540aSrobert {
137*404b540aSrobert __pf(*this);
138*404b540aSrobert return *this;
139*404b540aSrobert }
140*404b540aSrobert
141*404b540aSrobert __istream_type&
142*404b540aSrobert operator>>(ios_base& (*__pf)(ios_base&))
143*404b540aSrobert {
144*404b540aSrobert __pf(*this);
145*404b540aSrobert return *this;
146*404b540aSrobert }
147*404b540aSrobert //@}
148*404b540aSrobert
149*404b540aSrobert // [27.6.1.2.2] arithmetic extractors
150*404b540aSrobert /**
151*404b540aSrobert * @name Arithmetic Extractors
152*404b540aSrobert *
153*404b540aSrobert * All the @c operator>> functions (aka <em>formatted input
154*404b540aSrobert * functions</em>) have some common behavior. Each starts by
155*404b540aSrobert * constructing a temporary object of type std::basic_istream::sentry
156*404b540aSrobert * with the second argument (noskipws) set to false. This has several
157*404b540aSrobert * effects, concluding with the setting of a status flag; see the
158*404b540aSrobert * sentry documentation for more.
159*404b540aSrobert *
160*404b540aSrobert * If the sentry status is good, the function tries to extract
161*404b540aSrobert * whatever data is appropriate for the type of the argument.
162*404b540aSrobert *
163*404b540aSrobert * If an exception is thrown during extraction, ios_base::badbit
164*404b540aSrobert * will be turned on in the stream's error state without causing an
165*404b540aSrobert * ios_base::failure to be thrown. The original exception will then
166*404b540aSrobert * be rethrown.
167*404b540aSrobert */
168*404b540aSrobert //@{
169*404b540aSrobert /**
170*404b540aSrobert * @brief Basic arithmetic extractors
171*404b540aSrobert * @param A variable of builtin type.
172*404b540aSrobert * @return @c *this if successful
173*404b540aSrobert *
174*404b540aSrobert * These functions use the stream's current locale (specifically, the
175*404b540aSrobert * @c num_get facet) to parse the input data.
176*404b540aSrobert */
177*404b540aSrobert __istream_type&
178*404b540aSrobert operator>>(bool& __n)
179*404b540aSrobert { return _M_extract(__n); }
180*404b540aSrobert
181*404b540aSrobert __istream_type&
182*404b540aSrobert operator>>(short& __n);
183*404b540aSrobert
184*404b540aSrobert __istream_type&
185*404b540aSrobert operator>>(unsigned short& __n)
186*404b540aSrobert { return _M_extract(__n); }
187*404b540aSrobert
188*404b540aSrobert __istream_type&
189*404b540aSrobert operator>>(int& __n);
190*404b540aSrobert
191*404b540aSrobert __istream_type&
192*404b540aSrobert operator>>(unsigned int& __n)
193*404b540aSrobert { return _M_extract(__n); }
194*404b540aSrobert
195*404b540aSrobert __istream_type&
196*404b540aSrobert operator>>(long& __n)
197*404b540aSrobert { return _M_extract(__n); }
198*404b540aSrobert
199*404b540aSrobert __istream_type&
200*404b540aSrobert operator>>(unsigned long& __n)
201*404b540aSrobert { return _M_extract(__n); }
202*404b540aSrobert
203*404b540aSrobert #ifdef _GLIBCXX_USE_LONG_LONG
204*404b540aSrobert __istream_type&
205*404b540aSrobert operator>>(long long& __n)
206*404b540aSrobert { return _M_extract(__n); }
207*404b540aSrobert
208*404b540aSrobert __istream_type&
209*404b540aSrobert operator>>(unsigned long long& __n)
210*404b540aSrobert { return _M_extract(__n); }
211*404b540aSrobert #endif
212*404b540aSrobert
213*404b540aSrobert __istream_type&
214*404b540aSrobert operator>>(float& __f)
215*404b540aSrobert { return _M_extract(__f); }
216*404b540aSrobert
217*404b540aSrobert __istream_type&
218*404b540aSrobert operator>>(double& __f)
219*404b540aSrobert { return _M_extract(__f); }
220*404b540aSrobert
221*404b540aSrobert __istream_type&
222*404b540aSrobert operator>>(long double& __f)
223*404b540aSrobert { return _M_extract(__f); }
224*404b540aSrobert
225*404b540aSrobert __istream_type&
226*404b540aSrobert operator>>(void*& __p)
227*404b540aSrobert { return _M_extract(__p); }
228*404b540aSrobert
229*404b540aSrobert /**
230*404b540aSrobert * @brief Extracting into another streambuf.
231*404b540aSrobert * @param sb A pointer to a streambuf
232*404b540aSrobert *
233*404b540aSrobert * This function behaves like one of the basic arithmetic extractors,
234*404b540aSrobert * in that it also constructs a sentry object and has the same error
235*404b540aSrobert * handling behavior.
236*404b540aSrobert *
237*404b540aSrobert * If @a sb is NULL, the stream will set failbit in its error state.
238*404b540aSrobert *
239*404b540aSrobert * Characters are extracted from this stream and inserted into the
240*404b540aSrobert * @a sb streambuf until one of the following occurs:
241*404b540aSrobert *
242*404b540aSrobert * - the input stream reaches end-of-file,
243*404b540aSrobert * - insertion into the output buffer fails (in this case, the
244*404b540aSrobert * character that would have been inserted is not extracted), or
245*404b540aSrobert * - an exception occurs (and in this case is caught)
246*404b540aSrobert *
247*404b540aSrobert * If the function inserts no characters, failbit is set.
248*404b540aSrobert */
249*404b540aSrobert __istream_type&
250*404b540aSrobert operator>>(__streambuf_type* __sb);
251*404b540aSrobert //@}
252*404b540aSrobert
253*404b540aSrobert // [27.6.1.3] unformatted input
254*404b540aSrobert /**
255*404b540aSrobert * @brief Character counting
256*404b540aSrobert * @return The number of characters extracted by the previous
257*404b540aSrobert * unformatted input function dispatched for this stream.
258*404b540aSrobert */
259*404b540aSrobert streamsize
260*404b540aSrobert gcount() const
261*404b540aSrobert { return _M_gcount; }
262*404b540aSrobert
263*404b540aSrobert /**
264*404b540aSrobert * @name Unformatted Input Functions
265*404b540aSrobert *
266*404b540aSrobert * All the unformatted input functions have some common behavior.
267*404b540aSrobert * Each starts by constructing a temporary object of type
268*404b540aSrobert * std::basic_istream::sentry with the second argument (noskipws)
269*404b540aSrobert * set to true. This has several effects, concluding with the
270*404b540aSrobert * setting of a status flag; see the sentry documentation for more.
271*404b540aSrobert *
272*404b540aSrobert * If the sentry status is good, the function tries to extract
273*404b540aSrobert * whatever data is appropriate for the type of the argument.
274*404b540aSrobert *
275*404b540aSrobert * The number of characters extracted is stored for later retrieval
276*404b540aSrobert * by gcount().
277*404b540aSrobert *
278*404b540aSrobert * If an exception is thrown during extraction, ios_base::badbit
279*404b540aSrobert * will be turned on in the stream's error state without causing an
280*404b540aSrobert * ios_base::failure to be thrown. The original exception will then
281*404b540aSrobert * be rethrown.
282*404b540aSrobert */
283*404b540aSrobert //@{
284*404b540aSrobert /**
285*404b540aSrobert * @brief Simple extraction.
286*404b540aSrobert * @return A character, or eof().
287*404b540aSrobert *
288*404b540aSrobert * Tries to extract a character. If none are available, sets failbit
289*404b540aSrobert * and returns traits::eof().
290*404b540aSrobert */
291*404b540aSrobert int_type
292*404b540aSrobert get();
293*404b540aSrobert
294*404b540aSrobert /**
295*404b540aSrobert * @brief Simple extraction.
296*404b540aSrobert * @param c The character in which to store data.
297*404b540aSrobert * @return *this
298*404b540aSrobert *
299*404b540aSrobert * Tries to extract a character and store it in @a c. If none are
300*404b540aSrobert * available, sets failbit and returns traits::eof().
301*404b540aSrobert *
302*404b540aSrobert * @note This function is not overloaded on signed char and
303*404b540aSrobert * unsigned char.
304*404b540aSrobert */
305*404b540aSrobert __istream_type&
306*404b540aSrobert get(char_type& __c);
307*404b540aSrobert
308*404b540aSrobert /**
309*404b540aSrobert * @brief Simple multiple-character extraction.
310*404b540aSrobert * @param s Pointer to an array.
311*404b540aSrobert * @param n Maximum number of characters to store in @a s.
312*404b540aSrobert * @param delim A "stop" character.
313*404b540aSrobert * @return *this
314*404b540aSrobert *
315*404b540aSrobert * Characters are extracted and stored into @a s until one of the
316*404b540aSrobert * following happens:
317*404b540aSrobert *
318*404b540aSrobert * - @c n-1 characters are stored
319*404b540aSrobert * - the input sequence reaches EOF
320*404b540aSrobert * - the next character equals @a delim, in which case the character
321*404b540aSrobert * is not extracted
322*404b540aSrobert *
323*404b540aSrobert * If no characters are stored, failbit is set in the stream's error
324*404b540aSrobert * state.
325*404b540aSrobert *
326*404b540aSrobert * In any case, a null character is stored into the next location in
327*404b540aSrobert * the array.
328*404b540aSrobert *
329*404b540aSrobert * @note This function is not overloaded on signed char and
330*404b540aSrobert * unsigned char.
331*404b540aSrobert */
332*404b540aSrobert __istream_type&
333*404b540aSrobert get(char_type* __s, streamsize __n, char_type __delim);
334*404b540aSrobert
335*404b540aSrobert /**
336*404b540aSrobert * @brief Simple multiple-character extraction.
337*404b540aSrobert * @param s Pointer to an array.
338*404b540aSrobert * @param n Maximum number of characters to store in @a s.
339*404b540aSrobert * @return *this
340*404b540aSrobert *
341*404b540aSrobert * Returns @c get(s,n,widen('\n')).
342*404b540aSrobert */
343*404b540aSrobert __istream_type&
344*404b540aSrobert get(char_type* __s, streamsize __n)
345*404b540aSrobert { return this->get(__s, __n, this->widen('\n')); }
346*404b540aSrobert
347*404b540aSrobert /**
348*404b540aSrobert * @brief Extraction into another streambuf.
349*404b540aSrobert * @param sb A streambuf in which to store data.
350*404b540aSrobert * @param delim A "stop" character.
351*404b540aSrobert * @return *this
352*404b540aSrobert *
353*404b540aSrobert * Characters are extracted and inserted into @a sb until one of the
354*404b540aSrobert * following happens:
355*404b540aSrobert *
356*404b540aSrobert * - the input sequence reaches EOF
357*404b540aSrobert * - insertion into the output buffer fails (in this case, the
358*404b540aSrobert * character that would have been inserted is not extracted)
359*404b540aSrobert * - the next character equals @a delim (in this case, the character
360*404b540aSrobert * is not extracted)
361*404b540aSrobert * - an exception occurs (and in this case is caught)
362*404b540aSrobert *
363*404b540aSrobert * If no characters are stored, failbit is set in the stream's error
364*404b540aSrobert * state.
365*404b540aSrobert */
366*404b540aSrobert __istream_type&
367*404b540aSrobert get(__streambuf_type& __sb, char_type __delim);
368*404b540aSrobert
369*404b540aSrobert /**
370*404b540aSrobert * @brief Extraction into another streambuf.
371*404b540aSrobert * @param sb A streambuf in which to store data.
372*404b540aSrobert * @return *this
373*404b540aSrobert *
374*404b540aSrobert * Returns @c get(sb,widen('\n')).
375*404b540aSrobert */
376*404b540aSrobert __istream_type&
377*404b540aSrobert get(__streambuf_type& __sb)
378*404b540aSrobert { return this->get(__sb, this->widen('\n')); }
379*404b540aSrobert
380*404b540aSrobert /**
381*404b540aSrobert * @brief String extraction.
382*404b540aSrobert * @param s A character array in which to store the data.
383*404b540aSrobert * @param n Maximum number of characters to extract.
384*404b540aSrobert * @param delim A "stop" character.
385*404b540aSrobert * @return *this
386*404b540aSrobert *
387*404b540aSrobert * Extracts and stores characters into @a s until one of the
388*404b540aSrobert * following happens. Note that these criteria are required to be
389*404b540aSrobert * tested in the order listed here, to allow an input line to exactly
390*404b540aSrobert * fill the @a s array without setting failbit.
391*404b540aSrobert *
392*404b540aSrobert * -# the input sequence reaches end-of-file, in which case eofbit
393*404b540aSrobert * is set in the stream error state
394*404b540aSrobert * -# the next character equals @c delim, in which case the character
395*404b540aSrobert * is extracted (and therefore counted in @c gcount()) but not stored
396*404b540aSrobert * -# @c n-1 characters are stored, in which case failbit is set
397*404b540aSrobert * in the stream error state
398*404b540aSrobert *
399*404b540aSrobert * If no characters are extracted, failbit is set. (An empty line of
400*404b540aSrobert * input should therefore not cause failbit to be set.)
401*404b540aSrobert *
402*404b540aSrobert * In any case, a null character is stored in the next location in
403*404b540aSrobert * the array.
404*404b540aSrobert */
405*404b540aSrobert __istream_type&
406*404b540aSrobert getline(char_type* __s, streamsize __n, char_type __delim);
407*404b540aSrobert
408*404b540aSrobert /**
409*404b540aSrobert * @brief String extraction.
410*404b540aSrobert * @param s A character array in which to store the data.
411*404b540aSrobert * @param n Maximum number of characters to extract.
412*404b540aSrobert * @return *this
413*404b540aSrobert *
414*404b540aSrobert * Returns @c getline(s,n,widen('\n')).
415*404b540aSrobert */
416*404b540aSrobert __istream_type&
417*404b540aSrobert getline(char_type* __s, streamsize __n)
418*404b540aSrobert { return this->getline(__s, __n, this->widen('\n')); }
419*404b540aSrobert
420*404b540aSrobert /**
421*404b540aSrobert * @brief Discarding characters
422*404b540aSrobert * @param n Number of characters to discard.
423*404b540aSrobert * @param delim A "stop" character.
424*404b540aSrobert * @return *this
425*404b540aSrobert *
426*404b540aSrobert * Extracts characters and throws them away until one of the
427*404b540aSrobert * following happens:
428*404b540aSrobert * - if @a n @c != @c std::numeric_limits<int>::max(), @a n
429*404b540aSrobert * characters are extracted
430*404b540aSrobert * - the input sequence reaches end-of-file
431*404b540aSrobert * - the next character equals @a delim (in this case, the character
432*404b540aSrobert * is extracted); note that this condition will never occur if
433*404b540aSrobert * @a delim equals @c traits::eof().
434*404b540aSrobert *
435*404b540aSrobert * NB: Provide three overloads, instead of the single function
436*404b540aSrobert * (with defaults) mandated by the Standard: this leads to a
437*404b540aSrobert * better performing implementation, while still conforming to
438*404b540aSrobert * the Standard.
439*404b540aSrobert */
440*404b540aSrobert __istream_type&
441*404b540aSrobert ignore();
442*404b540aSrobert
443*404b540aSrobert __istream_type&
444*404b540aSrobert ignore(streamsize __n);
445*404b540aSrobert
446*404b540aSrobert __istream_type&
447*404b540aSrobert ignore(streamsize __n, int_type __delim);
448*404b540aSrobert
449*404b540aSrobert /**
450*404b540aSrobert * @brief Looking ahead in the stream
451*404b540aSrobert * @return The next character, or eof().
452*404b540aSrobert *
453*404b540aSrobert * If, after constructing the sentry object, @c good() is false,
454*404b540aSrobert * returns @c traits::eof(). Otherwise reads but does not extract
455*404b540aSrobert * the next input character.
456*404b540aSrobert */
457*404b540aSrobert int_type
458*404b540aSrobert peek();
459*404b540aSrobert
460*404b540aSrobert /**
461*404b540aSrobert * @brief Extraction without delimiters.
462*404b540aSrobert * @param s A character array.
463*404b540aSrobert * @param n Maximum number of characters to store.
464*404b540aSrobert * @return *this
465*404b540aSrobert *
466*404b540aSrobert * If the stream state is @c good(), extracts characters and stores
467*404b540aSrobert * them into @a s until one of the following happens:
468*404b540aSrobert * - @a n characters are stored
469*404b540aSrobert * - the input sequence reaches end-of-file, in which case the error
470*404b540aSrobert * state is set to @c failbit|eofbit.
471*404b540aSrobert *
472*404b540aSrobert * @note This function is not overloaded on signed char and
473*404b540aSrobert * unsigned char.
474*404b540aSrobert */
475*404b540aSrobert __istream_type&
476*404b540aSrobert read(char_type* __s, streamsize __n);
477*404b540aSrobert
478*404b540aSrobert /**
479*404b540aSrobert * @brief Extraction until the buffer is exhausted, but no more.
480*404b540aSrobert * @param s A character array.
481*404b540aSrobert * @param n Maximum number of characters to store.
482*404b540aSrobert * @return The number of characters extracted.
483*404b540aSrobert *
484*404b540aSrobert * Extracts characters and stores them into @a s depending on the
485*404b540aSrobert * number of characters remaining in the streambuf's buffer,
486*404b540aSrobert * @c rdbuf()->in_avail(), called @c A here:
487*404b540aSrobert * - if @c A @c == @c -1, sets eofbit and extracts no characters
488*404b540aSrobert * - if @c A @c == @c 0, extracts no characters
489*404b540aSrobert * - if @c A @c > @c 0, extracts @c min(A,n)
490*404b540aSrobert *
491*404b540aSrobert * The goal is to empty the current buffer, and to not request any
492*404b540aSrobert * more from the external input sequence controlled by the streambuf.
493*404b540aSrobert */
494*404b540aSrobert streamsize
495*404b540aSrobert readsome(char_type* __s, streamsize __n);
496*404b540aSrobert
497*404b540aSrobert /**
498*404b540aSrobert * @brief Unextracting a single character.
499*404b540aSrobert * @param c The character to push back into the input stream.
500*404b540aSrobert * @return *this
501*404b540aSrobert *
502*404b540aSrobert * If @c rdbuf() is not null, calls @c rdbuf()->sputbackc(c).
503*404b540aSrobert *
504*404b540aSrobert * If @c rdbuf() is null or if @c sputbackc() fails, sets badbit in
505*404b540aSrobert * the error state.
506*404b540aSrobert *
507*404b540aSrobert * @note Since no characters are extracted, the next call to
508*404b540aSrobert * @c gcount() will return 0, as required by DR 60.
509*404b540aSrobert */
510*404b540aSrobert __istream_type&
511*404b540aSrobert putback(char_type __c);
512*404b540aSrobert
513*404b540aSrobert /**
514*404b540aSrobert * @brief Unextracting the previous character.
515*404b540aSrobert * @return *this
516*404b540aSrobert *
517*404b540aSrobert * If @c rdbuf() is not null, calls @c rdbuf()->sungetc(c).
518*404b540aSrobert *
519*404b540aSrobert * If @c rdbuf() is null or if @c sungetc() fails, sets badbit in
520*404b540aSrobert * the error state.
521*404b540aSrobert *
522*404b540aSrobert * @note Since no characters are extracted, the next call to
523*404b540aSrobert * @c gcount() will return 0, as required by DR 60.
524*404b540aSrobert */
525*404b540aSrobert __istream_type&
526*404b540aSrobert unget();
527*404b540aSrobert
528*404b540aSrobert /**
529*404b540aSrobert * @brief Synchronizing the stream buffer.
530*404b540aSrobert * @return 0 on success, -1 on failure
531*404b540aSrobert *
532*404b540aSrobert * If @c rdbuf() is a null pointer, returns -1.
533*404b540aSrobert *
534*404b540aSrobert * Otherwise, calls @c rdbuf()->pubsync(), and if that returns -1,
535*404b540aSrobert * sets badbit and returns -1.
536*404b540aSrobert *
537*404b540aSrobert * Otherwise, returns 0.
538*404b540aSrobert *
539*404b540aSrobert * @note This function does not count the number of characters
540*404b540aSrobert * extracted, if any, and therefore does not affect the next
541*404b540aSrobert * call to @c gcount().
542*404b540aSrobert */
543*404b540aSrobert int
544*404b540aSrobert sync();
545*404b540aSrobert
546*404b540aSrobert /**
547*404b540aSrobert * @brief Getting the current read position.
548*404b540aSrobert * @return A file position object.
549*404b540aSrobert *
550*404b540aSrobert * If @c fail() is not false, returns @c pos_type(-1) to indicate
551*404b540aSrobert * failure. Otherwise returns @c rdbuf()->pubseekoff(0,cur,in).
552*404b540aSrobert *
553*404b540aSrobert * @note This function does not count the number of characters
554*404b540aSrobert * extracted, if any, and therefore does not affect the next
555*404b540aSrobert * call to @c gcount().
556*404b540aSrobert */
557*404b540aSrobert pos_type
558*404b540aSrobert tellg();
559*404b540aSrobert
560*404b540aSrobert /**
561*404b540aSrobert * @brief Changing the current read position.
562*404b540aSrobert * @param pos A file position object.
563*404b540aSrobert * @return *this
564*404b540aSrobert *
565*404b540aSrobert * If @c fail() is not true, calls @c rdbuf()->pubseekpos(pos). If
566*404b540aSrobert * that function fails, sets failbit.
567*404b540aSrobert *
568*404b540aSrobert * @note This function does not count the number of characters
569*404b540aSrobert * extracted, if any, and therefore does not affect the next
570*404b540aSrobert * call to @c gcount().
571*404b540aSrobert */
572*404b540aSrobert __istream_type&
573*404b540aSrobert seekg(pos_type);
574*404b540aSrobert
575*404b540aSrobert /**
576*404b540aSrobert * @brief Changing the current read position.
577*404b540aSrobert * @param off A file offset object.
578*404b540aSrobert * @param dir The direction in which to seek.
579*404b540aSrobert * @return *this
580*404b540aSrobert *
581*404b540aSrobert * If @c fail() is not true, calls @c rdbuf()->pubseekoff(off,dir).
582*404b540aSrobert * If that function fails, sets failbit.
583*404b540aSrobert *
584*404b540aSrobert * @note This function does not count the number of characters
585*404b540aSrobert * extracted, if any, and therefore does not affect the next
586*404b540aSrobert * call to @c gcount().
587*404b540aSrobert */
588*404b540aSrobert __istream_type&
589*404b540aSrobert seekg(off_type, ios_base::seekdir);
590*404b540aSrobert //@}
591*404b540aSrobert
592*404b540aSrobert protected:
593*404b540aSrobert explicit
594*404b540aSrobert basic_istream(): _M_gcount(streamsize(0)) { }
595*404b540aSrobert
596*404b540aSrobert template<typename _ValueT>
597*404b540aSrobert __istream_type&
598*404b540aSrobert _M_extract(_ValueT& __v);
599*404b540aSrobert };
600*404b540aSrobert
601*404b540aSrobert // Explicit specialization declarations, defined in src/istream.cc.
602*404b540aSrobert template<>
603*404b540aSrobert basic_istream<char>&
604*404b540aSrobert basic_istream<char>::
605*404b540aSrobert getline(char_type* __s, streamsize __n, char_type __delim);
606*404b540aSrobert
607*404b540aSrobert template<>
608*404b540aSrobert basic_istream<char>&
609*404b540aSrobert basic_istream<char>::
610*404b540aSrobert ignore(streamsize __n);
611*404b540aSrobert
612*404b540aSrobert template<>
613*404b540aSrobert basic_istream<char>&
614*404b540aSrobert basic_istream<char>::
615*404b540aSrobert ignore(streamsize __n, int_type __delim);
616*404b540aSrobert
617*404b540aSrobert #ifdef _GLIBCXX_USE_WCHAR_T
618*404b540aSrobert template<>
619*404b540aSrobert basic_istream<wchar_t>&
620*404b540aSrobert basic_istream<wchar_t>::
621*404b540aSrobert getline(char_type* __s, streamsize __n, char_type __delim);
622*404b540aSrobert
623*404b540aSrobert template<>
624*404b540aSrobert basic_istream<wchar_t>&
625*404b540aSrobert basic_istream<wchar_t>::
626*404b540aSrobert ignore(streamsize __n);
627*404b540aSrobert
628*404b540aSrobert template<>
629*404b540aSrobert basic_istream<wchar_t>&
630*404b540aSrobert basic_istream<wchar_t>::
631*404b540aSrobert ignore(streamsize __n, int_type __delim);
632*404b540aSrobert #endif
633*404b540aSrobert
634*404b540aSrobert /**
635*404b540aSrobert * @brief Performs setup work for input streams.
636*404b540aSrobert *
637*404b540aSrobert * Objects of this class are created before all of the standard
638*404b540aSrobert * extractors are run. It is responsible for "exception-safe prefix and
639*404b540aSrobert * suffix operations," although only prefix actions are currently required
640*404b540aSrobert * by the standard. Additional actions may be added by the
641*404b540aSrobert * implementation, and we list them in
642*404b540aSrobert * http://gcc.gnu.org/onlinedocs/libstdc++/17_intro/howto.html#5
643*404b540aSrobert * under [27.6] notes.
644*404b540aSrobert */
645*404b540aSrobert template<typename _CharT, typename _Traits>
646*404b540aSrobert class basic_istream<_CharT, _Traits>::sentry
647*404b540aSrobert {
648*404b540aSrobert public:
649*404b540aSrobert /// Easy access to dependant types.
650*404b540aSrobert typedef _Traits traits_type;
651*404b540aSrobert typedef basic_streambuf<_CharT, _Traits> __streambuf_type;
652*404b540aSrobert typedef basic_istream<_CharT, _Traits> __istream_type;
653*404b540aSrobert typedef typename __istream_type::__ctype_type __ctype_type;
654*404b540aSrobert typedef typename _Traits::int_type __int_type;
655*404b540aSrobert
656*404b540aSrobert /**
657*404b540aSrobert * @brief The constructor performs all the work.
658*404b540aSrobert * @param is The input stream to guard.
659*404b540aSrobert * @param noskipws Whether to consume whitespace or not.
660*404b540aSrobert *
661*404b540aSrobert * If the stream state is good (@a is.good() is true), then the
662*404b540aSrobert * following actions are performed, otherwise the sentry state is
663*404b540aSrobert * false ("not okay") and failbit is set in the stream state.
664*404b540aSrobert *
665*404b540aSrobert * The sentry's preparatory actions are:
666*404b540aSrobert *
667*404b540aSrobert * -# if the stream is tied to an output stream, @c is.tie()->flush()
668*404b540aSrobert * is called to synchronize the output sequence
669*404b540aSrobert * -# if @a noskipws is false, and @c ios_base::skipws is set in
670*404b540aSrobert * @c is.flags(), the sentry extracts and discards whitespace
671*404b540aSrobert * characters from the stream. The currently imbued locale is
672*404b540aSrobert * used to determine whether each character is whitespace.
673*404b540aSrobert *
674*404b540aSrobert * If the stream state is still good, then the sentry state becomes
675*404b540aSrobert * true ("okay").
676*404b540aSrobert */
677*404b540aSrobert explicit
678*404b540aSrobert sentry(basic_istream<_CharT, _Traits>& __is, bool __noskipws = false);
679*404b540aSrobert
680*404b540aSrobert /**
681*404b540aSrobert * @brief Quick status checking.
682*404b540aSrobert * @return The sentry state.
683*404b540aSrobert *
684*404b540aSrobert * For ease of use, sentries may be converted to booleans. The
685*404b540aSrobert * return value is that of the sentry state (true == okay).
686*404b540aSrobert */
687*404b540aSrobert operator bool() const
688*404b540aSrobert { return _M_ok; }
689*404b540aSrobert
690*404b540aSrobert private:
691*404b540aSrobert bool _M_ok;
692*404b540aSrobert };
693*404b540aSrobert
694*404b540aSrobert // [27.6.1.2.3] character extraction templates
695*404b540aSrobert //@{
696*404b540aSrobert /**
697*404b540aSrobert * @brief Character extractors
698*404b540aSrobert * @param in An input stream.
699*404b540aSrobert * @param c A character reference.
700*404b540aSrobert * @return in
701*404b540aSrobert *
702*404b540aSrobert * Behaves like one of the formatted arithmetic extractors described in
703*404b540aSrobert * std::basic_istream. After constructing a sentry object with good
704*404b540aSrobert * status, this function extracts a character (if one is available) and
705*404b540aSrobert * stores it in @a c. Otherwise, sets failbit in the input stream.
706*404b540aSrobert */
707*404b540aSrobert template<typename _CharT, typename _Traits>
708*404b540aSrobert basic_istream<_CharT, _Traits>&
709*404b540aSrobert operator>>(basic_istream<_CharT, _Traits>& __in, _CharT& __c);
710*404b540aSrobert
711*404b540aSrobert template<class _Traits>
712*404b540aSrobert inline basic_istream<char, _Traits>&
713*404b540aSrobert operator>>(basic_istream<char, _Traits>& __in, unsigned char& __c)
714*404b540aSrobert { return (__in >> reinterpret_cast<char&>(__c)); }
715*404b540aSrobert
716*404b540aSrobert template<class _Traits>
717*404b540aSrobert inline basic_istream<char, _Traits>&
718*404b540aSrobert operator>>(basic_istream<char, _Traits>& __in, signed char& __c)
719*404b540aSrobert { return (__in >> reinterpret_cast<char&>(__c)); }
720*404b540aSrobert //@}
721*404b540aSrobert
722*404b540aSrobert //@{
723*404b540aSrobert /**
724*404b540aSrobert * @brief Character string extractors
725*404b540aSrobert * @param in An input stream.
726*404b540aSrobert * @param s A pointer to a character array.
727*404b540aSrobert * @return in
728*404b540aSrobert *
729*404b540aSrobert * Behaves like one of the formatted arithmetic extractors described in
730*404b540aSrobert * std::basic_istream. After constructing a sentry object with good
731*404b540aSrobert * status, this function extracts up to @c n characters and stores them
732*404b540aSrobert * into the array starting at @a s. @c n is defined as:
733*404b540aSrobert *
734*404b540aSrobert * - if @c width() is greater than zero, @c n is width()
735*404b540aSrobert * - otherwise @c n is "the number of elements of the largest array of
736*404b540aSrobert * @c char_type that can store a terminating @c eos." [27.6.1.2.3]/6
737*404b540aSrobert *
738*404b540aSrobert * Characters are extracted and stored until one of the following happens:
739*404b540aSrobert * - @c n-1 characters are stored
740*404b540aSrobert * - EOF is reached
741*404b540aSrobert * - the next character is whitespace according to the current locale
742*404b540aSrobert * - the next character is a null byte (i.e., @c charT() )
743*404b540aSrobert *
744*404b540aSrobert * @c width(0) is then called for the input stream.
745*404b540aSrobert *
746*404b540aSrobert * If no characters are extracted, sets failbit.
747*404b540aSrobert */
748*404b540aSrobert template<typename _CharT, typename _Traits>
749*404b540aSrobert basic_istream<_CharT, _Traits>&
750*404b540aSrobert operator>>(basic_istream<_CharT, _Traits>& __in, _CharT* __s);
751*404b540aSrobert
752*404b540aSrobert // Explicit specialization declaration, defined in src/istream.cc.
753*404b540aSrobert template<>
754*404b540aSrobert basic_istream<char>&
755*404b540aSrobert operator>>(basic_istream<char>& __in, char* __s);
756*404b540aSrobert
757*404b540aSrobert template<class _Traits>
758*404b540aSrobert inline basic_istream<char, _Traits>&
759*404b540aSrobert operator>>(basic_istream<char, _Traits>& __in, unsigned char* __s)
760*404b540aSrobert { return (__in >> reinterpret_cast<char*>(__s)); }
761*404b540aSrobert
762*404b540aSrobert template<class _Traits>
763*404b540aSrobert inline basic_istream<char, _Traits>&
764*404b540aSrobert operator>>(basic_istream<char, _Traits>& __in, signed char* __s)
765*404b540aSrobert { return (__in >> reinterpret_cast<char*>(__s)); }
766*404b540aSrobert //@}
767*404b540aSrobert
768*404b540aSrobert // 27.6.1.5 Template class basic_iostream
769*404b540aSrobert /**
770*404b540aSrobert * @brief Merging istream and ostream capabilities.
771*404b540aSrobert *
772*404b540aSrobert * This class multiply inherits from the input and output stream classes
773*404b540aSrobert * simply to provide a single interface.
774*404b540aSrobert */
775*404b540aSrobert template<typename _CharT, typename _Traits>
776*404b540aSrobert class basic_iostream
777*404b540aSrobert : public basic_istream<_CharT, _Traits>,
778*404b540aSrobert public basic_ostream<_CharT, _Traits>
779*404b540aSrobert {
780*404b540aSrobert public:
781*404b540aSrobert // _GLIBCXX_RESOLVE_LIB_DEFECTS
782*404b540aSrobert // 271. basic_iostream missing typedefs
783*404b540aSrobert // Types (inherited):
784*404b540aSrobert typedef _CharT char_type;
785*404b540aSrobert typedef typename _Traits::int_type int_type;
786*404b540aSrobert typedef typename _Traits::pos_type pos_type;
787*404b540aSrobert typedef typename _Traits::off_type off_type;
788*404b540aSrobert typedef _Traits traits_type;
789*404b540aSrobert
790*404b540aSrobert // Non-standard Types:
791*404b540aSrobert typedef basic_istream<_CharT, _Traits> __istream_type;
792*404b540aSrobert typedef basic_ostream<_CharT, _Traits> __ostream_type;
793*404b540aSrobert
794*404b540aSrobert /**
795*404b540aSrobert * @brief Constructor does nothing.
796*404b540aSrobert *
797*404b540aSrobert * Both of the parent classes are initialized with the same
798*404b540aSrobert * streambuf pointer passed to this constructor.
799*404b540aSrobert */
800*404b540aSrobert explicit
basic_iostream(basic_streambuf<_CharT,_Traits> * __sb)801*404b540aSrobert basic_iostream(basic_streambuf<_CharT, _Traits>* __sb)
802*404b540aSrobert : __istream_type(), __ostream_type()
803*404b540aSrobert { this->init(__sb); }
804*404b540aSrobert
805*404b540aSrobert /**
806*404b540aSrobert * @brief Destructor does nothing.
807*404b540aSrobert */
808*404b540aSrobert virtual
~basic_iostream()809*404b540aSrobert ~basic_iostream() { }
810*404b540aSrobert
811*404b540aSrobert protected:
812*404b540aSrobert explicit
basic_iostream()813*404b540aSrobert basic_iostream() : __istream_type(), __ostream_type()
814*404b540aSrobert { }
815*404b540aSrobert };
816*404b540aSrobert
817*404b540aSrobert // [27.6.1.4] standard basic_istream manipulators
818*404b540aSrobert /**
819*404b540aSrobert * @brief Quick and easy way to eat whitespace
820*404b540aSrobert *
821*404b540aSrobert * This manipulator extracts whitespace characters, stopping when the
822*404b540aSrobert * next character is non-whitespace, or when the input sequence is empty.
823*404b540aSrobert * If the sequence is empty, @c eofbit is set in the stream, but not
824*404b540aSrobert * @c failbit.
825*404b540aSrobert *
826*404b540aSrobert * The current locale is used to distinguish whitespace characters.
827*404b540aSrobert *
828*404b540aSrobert * Example:
829*404b540aSrobert * @code
830*404b540aSrobert * MyClass mc;
831*404b540aSrobert *
832*404b540aSrobert * std::cin >> std::ws >> mc;
833*404b540aSrobert * @endcode
834*404b540aSrobert * will skip leading whitespace before calling operator>> on cin and your
835*404b540aSrobert * object. Note that the same effect can be achieved by creating a
836*404b540aSrobert * std::basic_istream::sentry inside your definition of operator>>.
837*404b540aSrobert */
838*404b540aSrobert template<typename _CharT, typename _Traits>
839*404b540aSrobert basic_istream<_CharT, _Traits>&
840*404b540aSrobert ws(basic_istream<_CharT, _Traits>& __is);
841*404b540aSrobert
842*404b540aSrobert _GLIBCXX_END_NAMESPACE
843*404b540aSrobert
844*404b540aSrobert #ifndef _GLIBCXX_EXPORT_TEMPLATE
845*404b540aSrobert # include <bits/istream.tcc>
846*404b540aSrobert #endif
847*404b540aSrobert
848*404b540aSrobert #endif /* _GLIBCXX_ISTREAM */
849