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