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