197403Sobrien// Input streams -*- C++ -*- 297403Sobrien 3169691Skan// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 4132720Skan// Free Software Foundation, Inc. 597403Sobrien// 697403Sobrien// This file is part of the GNU ISO C++ Library. This library is free 797403Sobrien// software; you can redistribute it and/or modify it under the 897403Sobrien// terms of the GNU General Public License as published by the 997403Sobrien// Free Software Foundation; either version 2, or (at your option) 1097403Sobrien// any later version. 1197403Sobrien 1297403Sobrien// This library is distributed in the hope that it will be useful, 1397403Sobrien// but WITHOUT ANY WARRANTY; without even the implied warranty of 1497403Sobrien// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1597403Sobrien// GNU General Public License for more details. 1697403Sobrien 1797403Sobrien// You should have received a copy of the GNU General Public License along 1897403Sobrien// with this library; see the file COPYING. If not, write to the Free 19169691Skan// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 2097403Sobrien// USA. 2197403Sobrien 2297403Sobrien// As a special exception, you may use this file as part of a free software 2397403Sobrien// library without restriction. Specifically, if other files instantiate 2497403Sobrien// templates or use macros or inline functions from this file, or you compile 2597403Sobrien// this file and link it with other files to produce an executable, this 2697403Sobrien// file does not by itself cause the resulting executable to be covered by 2797403Sobrien// the GNU General Public License. This exception does not however 2897403Sobrien// invalidate any other reasons why the executable file might be covered by 2997403Sobrien// the GNU General Public License. 3097403Sobrien 3197403Sobrien// 3297403Sobrien// ISO C++ 14882: 27.6.1 Input streams 3397403Sobrien// 3497403Sobrien 3597403Sobrien/** @file istream 36169691Skan * This is a Standard C++ Library header. 3797403Sobrien */ 3897403Sobrien 39132720Skan#ifndef _GLIBCXX_ISTREAM 40132720Skan#define _GLIBCXX_ISTREAM 1 4197403Sobrien 4297403Sobrien#pragma GCC system_header 4397403Sobrien 4497403Sobrien#include <ios> 4597403Sobrien#include <limits> // For numeric_limits 4697403Sobrien 47169691Skan_GLIBCXX_BEGIN_NAMESPACE(std) 48169691Skan 49117397Skan // [27.6.1.1] Template class basic_istream 50117397Skan /** 51117397Skan * @brief Controlling input. 52117397Skan * 53117397Skan * This is the base class for all input streams. It provides text 54117397Skan * formatting of all builtin types, and communicates with any class 55117397Skan * derived from basic_streambuf to do the actual input. 56117397Skan */ 5797403Sobrien template<typename _CharT, typename _Traits> 5897403Sobrien class basic_istream : virtual public basic_ios<_CharT, _Traits> 5997403Sobrien { 6097403Sobrien public: 6197403Sobrien // Types (inherited from basic_ios (27.4.4)): 6297403Sobrien typedef _CharT char_type; 6397403Sobrien typedef typename _Traits::int_type int_type; 6497403Sobrien typedef typename _Traits::pos_type pos_type; 6597403Sobrien typedef typename _Traits::off_type off_type; 6697403Sobrien typedef _Traits traits_type; 6797403Sobrien 6897403Sobrien // Non-standard Types: 6997403Sobrien typedef basic_streambuf<_CharT, _Traits> __streambuf_type; 7097403Sobrien typedef basic_ios<_CharT, _Traits> __ios_type; 7197403Sobrien typedef basic_istream<_CharT, _Traits> __istream_type; 72132720Skan typedef num_get<_CharT, istreambuf_iterator<_CharT, _Traits> > 73132720Skan __num_get_type; 7497403Sobrien typedef ctype<_CharT> __ctype_type; 7597403Sobrien 76117397Skan template<typename _CharT2, typename _Traits2> 77117397Skan friend basic_istream<_CharT2, _Traits2>& 78117397Skan operator>>(basic_istream<_CharT2, _Traits2>&, _CharT2&); 79117397Skan 80117397Skan template<typename _CharT2, typename _Traits2> 81117397Skan friend basic_istream<_CharT2, _Traits2>& 82117397Skan operator>>(basic_istream<_CharT2, _Traits2>&, _CharT2*); 83117397Skan 8497403Sobrien protected: 8597403Sobrien // Data Members: 86117397Skan /** 87117397Skan * @if maint 88117397Skan * The number of characters extracted in the previous unformatted 89117397Skan * function; see gcount(). 90117397Skan * @endif 91117397Skan */ 9297403Sobrien streamsize _M_gcount; 9397403Sobrien 9497403Sobrien public: 95117397Skan // [27.6.1.1.1] constructor/destructor 96117397Skan /** 97117397Skan * @brief Base constructor. 98117397Skan * 99117397Skan * This ctor is almost never called by the user directly, rather from 100117397Skan * derived classes' initialization lists, which pass a pointer to 101117397Skan * their own stream buffer. 102117397Skan */ 10397403Sobrien explicit 104132720Skan basic_istream(__streambuf_type* __sb): _M_gcount(streamsize(0)) 105132720Skan { this->init(__sb); } 10697403Sobrien 107117397Skan /** 108117397Skan * @brief Base destructor. 109117397Skan * 110117397Skan * This does very little apart from providing a virtual base dtor. 111117397Skan */ 11297403Sobrien virtual 11397403Sobrien ~basic_istream() 11497403Sobrien { _M_gcount = streamsize(0); } 11597403Sobrien 116117397Skan // [27.6.1.1.2] prefix/suffix 11797403Sobrien class sentry; 11897403Sobrien friend class sentry; 11997403Sobrien 120117397Skan // [27.6.1.2] formatted input 121117397Skan // [27.6.1.2.3] basic_istream::operator>> 122117397Skan //@{ 123117397Skan /** 124117397Skan * @brief Interface for manipulators. 125117397Skan * 126117397Skan * Manuipulators such as @c std::ws and @c std::dec use these 127117397Skan * functions in constructs like "std::cin >> std::ws". For more 128117397Skan * information, see the iomanip header. 129117397Skan */ 130169691Skan __istream_type& 131169691Skan operator>>(__istream_type& (*__pf)(__istream_type&)) 132169691Skan { return __pf(*this); } 13397403Sobrien 134169691Skan __istream_type& 135169691Skan operator>>(__ios_type& (*__pf)(__ios_type&)) 136169691Skan { 137169691Skan __pf(*this); 138169691Skan return *this; 139169691Skan } 14097403Sobrien 141169691Skan __istream_type& 142169691Skan operator>>(ios_base& (*__pf)(ios_base&)) 143169691Skan { 144169691Skan __pf(*this); 145169691Skan return *this; 146169691Skan } 147117397Skan //@} 14897403Sobrien 149117397Skan // [27.6.1.2.2] arithmetic extractors 150117397Skan /** 151117397Skan * @name Arithmetic Extractors 152117397Skan * 153117397Skan * All the @c operator>> functions (aka <em>formatted input 154117397Skan * functions</em>) have some common behavior. Each starts by 155117397Skan * constructing a temporary object of type std::basic_istream::sentry 156117397Skan * with the second argument (noskipws) set to false. This has several 157117397Skan * effects, concluding with the setting of a status flag; see the 158117397Skan * sentry documentation for more. 159117397Skan * 160117397Skan * If the sentry status is good, the function tries to extract 161117397Skan * whatever data is appropriate for the type of the argument. 162117397Skan * 163117397Skan * If an exception is thrown during extraction, ios_base::badbit 164117397Skan * will be turned on in the stream's error state without causing an 165117397Skan * ios_base::failure to be thrown. The original exception will then 166117397Skan * be rethrown. 167117397Skan */ 168117397Skan //@{ 169117397Skan /** 170117397Skan * @brief Basic arithmetic extractors 171117397Skan * @param A variable of builtin type. 172117397Skan * @return @c *this if successful 173117397Skan * 174117397Skan * These functions use the stream's current locale (specifically, the 175117397Skan * @c num_get facet) to parse the input data. 176117397Skan */ 17797403Sobrien __istream_type& 178169691Skan operator>>(bool& __n) 179169691Skan { return _M_extract(__n); } 18097403Sobrien 18197403Sobrien __istream_type& 18297403Sobrien operator>>(short& __n); 18397403Sobrien 18497403Sobrien __istream_type& 185169691Skan operator>>(unsigned short& __n) 186169691Skan { return _M_extract(__n); } 18797403Sobrien 18897403Sobrien __istream_type& 18997403Sobrien operator>>(int& __n); 190169691Skan 19197403Sobrien __istream_type& 192169691Skan operator>>(unsigned int& __n) 193169691Skan { return _M_extract(__n); } 19497403Sobrien 19597403Sobrien __istream_type& 196169691Skan operator>>(long& __n) 197169691Skan { return _M_extract(__n); } 19897403Sobrien 19997403Sobrien __istream_type& 200169691Skan operator>>(unsigned long& __n) 201169691Skan { return _M_extract(__n); } 20297403Sobrien 203132720Skan#ifdef _GLIBCXX_USE_LONG_LONG 20497403Sobrien __istream_type& 205169691Skan operator>>(long long& __n) 206169691Skan { return _M_extract(__n); } 20797403Sobrien 20897403Sobrien __istream_type& 209169691Skan operator>>(unsigned long long& __n) 210169691Skan { return _M_extract(__n); } 21197403Sobrien#endif 21297403Sobrien 21397403Sobrien __istream_type& 214169691Skan operator>>(float& __f) 215169691Skan { return _M_extract(__f); } 21697403Sobrien 21797403Sobrien __istream_type& 218169691Skan operator>>(double& __f) 219169691Skan { return _M_extract(__f); } 22097403Sobrien 22197403Sobrien __istream_type& 222169691Skan operator>>(long double& __f) 223169691Skan { return _M_extract(__f); } 22497403Sobrien 22597403Sobrien __istream_type& 226169691Skan operator>>(void*& __p) 227169691Skan { return _M_extract(__p); } 22897403Sobrien 229117397Skan /** 230117397Skan * @brief Extracting into another streambuf. 231117397Skan * @param sb A pointer to a streambuf 232117397Skan * 233117397Skan * This function behaves like one of the basic arithmetic extractors, 234132720Skan * in that it also constructs a sentry object and has the same error 235117397Skan * handling behavior. 236117397Skan * 237117397Skan * If @a sb is NULL, the stream will set failbit in its error state. 238117397Skan * 239117397Skan * Characters are extracted from this stream and inserted into the 240117397Skan * @a sb streambuf until one of the following occurs: 241117397Skan * 242117397Skan * - the input stream reaches end-of-file, 243117397Skan * - insertion into the output buffer fails (in this case, the 244117397Skan * character that would have been inserted is not extracted), or 245117397Skan * - an exception occurs (and in this case is caught) 246117397Skan * 247117397Skan * If the function inserts no characters, failbit is set. 248117397Skan */ 24997403Sobrien __istream_type& 25097403Sobrien operator>>(__streambuf_type* __sb); 251117397Skan //@} 25297403Sobrien 253117397Skan // [27.6.1.3] unformatted input 254117397Skan /** 255117397Skan * @brief Character counting 256117397Skan * @return The number of characters extracted by the previous 257117397Skan * unformatted input function dispatched for this stream. 258117397Skan */ 259169691Skan streamsize 260117397Skan gcount() const 26197403Sobrien { return _M_gcount; } 26297403Sobrien 263117397Skan /** 264117397Skan * @name Unformatted Input Functions 265117397Skan * 266117397Skan * All the unformatted input functions have some common behavior. 267117397Skan * Each starts by constructing a temporary object of type 268117397Skan * std::basic_istream::sentry with the second argument (noskipws) 269117397Skan * set to true. This has several effects, concluding with the 270117397Skan * setting of a status flag; see the sentry documentation for more. 271117397Skan * 272117397Skan * If the sentry status is good, the function tries to extract 273117397Skan * whatever data is appropriate for the type of the argument. 274117397Skan * 275117397Skan * The number of characters extracted is stored for later retrieval 276117397Skan * by gcount(). 277117397Skan * 278117397Skan * If an exception is thrown during extraction, ios_base::badbit 279117397Skan * will be turned on in the stream's error state without causing an 280117397Skan * ios_base::failure to be thrown. The original exception will then 281117397Skan * be rethrown. 282117397Skan */ 283117397Skan //@{ 284117397Skan /** 285117397Skan * @brief Simple extraction. 286117397Skan * @return A character, or eof(). 287117397Skan * 288117397Skan * Tries to extract a character. If none are available, sets failbit 289117397Skan * and returns traits::eof(). 290117397Skan */ 29197403Sobrien int_type 292117397Skan get(); 29397403Sobrien 294117397Skan /** 295117397Skan * @brief Simple extraction. 296117397Skan * @param c The character in which to store data. 297117397Skan * @return *this 298117397Skan * 299117397Skan * Tries to extract a character and store it in @a c. If none are 300117397Skan * available, sets failbit and returns traits::eof(). 301117397Skan * 302117397Skan * @note This function is not overloaded on signed char and 303117397Skan * unsigned char. 304117397Skan */ 30597403Sobrien __istream_type& 30697403Sobrien get(char_type& __c); 30797403Sobrien 308117397Skan /** 309117397Skan * @brief Simple multiple-character extraction. 310117397Skan * @param s Pointer to an array. 311117397Skan * @param n Maximum number of characters to store in @a s. 312117397Skan * @param delim A "stop" character. 313117397Skan * @return *this 314117397Skan * 315117397Skan * Characters are extracted and stored into @a s until one of the 316117397Skan * following happens: 317117397Skan * 318117397Skan * - @c n-1 characters are stored 319117397Skan * - the input sequence reaches EOF 320117397Skan * - the next character equals @a delim, in which case the character 321117397Skan * is not extracted 322117397Skan * 323117397Skan * If no characters are stored, failbit is set in the stream's error 324117397Skan * state. 325117397Skan * 326117397Skan * In any case, a null character is stored into the next location in 327117397Skan * the array. 328117397Skan * 329117397Skan * @note This function is not overloaded on signed char and 330117397Skan * unsigned char. 331117397Skan */ 33297403Sobrien __istream_type& 33397403Sobrien get(char_type* __s, streamsize __n, char_type __delim); 33497403Sobrien 335117397Skan /** 336117397Skan * @brief Simple multiple-character extraction. 337117397Skan * @param s Pointer to an array. 338117397Skan * @param n Maximum number of characters to store in @a s. 339117397Skan * @return *this 340117397Skan * 341117397Skan * Returns @c get(s,n,widen('\n')). 342117397Skan */ 343169691Skan __istream_type& 34497403Sobrien get(char_type* __s, streamsize __n) 34597403Sobrien { return this->get(__s, __n, this->widen('\n')); } 34697403Sobrien 347117397Skan /** 348117397Skan * @brief Extraction into another streambuf. 349117397Skan * @param sb A streambuf in which to store data. 350117397Skan * @param delim A "stop" character. 351117397Skan * @return *this 352117397Skan * 353117397Skan * Characters are extracted and inserted into @a sb until one of the 354117397Skan * following happens: 355117397Skan * 356117397Skan * - the input sequence reaches EOF 357117397Skan * - insertion into the output buffer fails (in this case, the 358117397Skan * character that would have been inserted is not extracted) 359117397Skan * - the next character equals @a delim (in this case, the character 360117397Skan * is not extracted) 361117397Skan * - an exception occurs (and in this case is caught) 362117397Skan * 363117397Skan * If no characters are stored, failbit is set in the stream's error 364117397Skan * state. 365117397Skan */ 36697403Sobrien __istream_type& 36797403Sobrien get(__streambuf_type& __sb, char_type __delim); 36897403Sobrien 369117397Skan /** 370117397Skan * @brief Extraction into another streambuf. 371117397Skan * @param sb A streambuf in which to store data. 372117397Skan * @return *this 373117397Skan * 374117397Skan * Returns @c get(sb,widen('\n')). 375117397Skan */ 376169691Skan __istream_type& 37797403Sobrien get(__streambuf_type& __sb) 37897403Sobrien { return this->get(__sb, this->widen('\n')); } 37997403Sobrien 380117397Skan /** 381117397Skan * @brief String extraction. 382117397Skan * @param s A character array in which to store the data. 383117397Skan * @param n Maximum number of characters to extract. 384117397Skan * @param delim A "stop" character. 385117397Skan * @return *this 386117397Skan * 387117397Skan * Extracts and stores characters into @a s until one of the 388117397Skan * following happens. Note that these criteria are required to be 389117397Skan * tested in the order listed here, to allow an input line to exactly 390117397Skan * fill the @a s array without setting failbit. 391117397Skan * 392117397Skan * -# the input sequence reaches end-of-file, in which case eofbit 393117397Skan * is set in the stream error state 394117397Skan * -# the next character equals @c delim, in which case the character 395117397Skan * is extracted (and therefore counted in @c gcount()) but not stored 396117397Skan * -# @c n-1 characters are stored, in which case failbit is set 397117397Skan * in the stream error state 398117397Skan * 399117397Skan * If no characters are extracted, failbit is set. (An empty line of 400117397Skan * input should therefore not cause failbit to be set.) 401117397Skan * 402117397Skan * In any case, a null character is stored in the next location in 403117397Skan * the array. 404117397Skan */ 40597403Sobrien __istream_type& 40697403Sobrien getline(char_type* __s, streamsize __n, char_type __delim); 40797403Sobrien 408117397Skan /** 409117397Skan * @brief String extraction. 410117397Skan * @param s A character array in which to store the data. 411117397Skan * @param n Maximum number of characters to extract. 412117397Skan * @return *this 413117397Skan * 414117397Skan * Returns @c getline(s,n,widen('\n')). 415117397Skan */ 416169691Skan __istream_type& 41797403Sobrien getline(char_type* __s, streamsize __n) 41897403Sobrien { return this->getline(__s, __n, this->widen('\n')); } 41997403Sobrien 420117397Skan /** 421117397Skan * @brief Discarding characters 422117397Skan * @param n Number of characters to discard. 423117397Skan * @param delim A "stop" character. 424117397Skan * @return *this 425117397Skan * 426117397Skan * Extracts characters and throws them away until one of the 427117397Skan * following happens: 428117397Skan * - if @a n @c != @c std::numeric_limits<int>::max(), @a n 429117397Skan * characters are extracted 430117397Skan * - the input sequence reaches end-of-file 431117397Skan * - the next character equals @a delim (in this case, the character 432117397Skan * is extracted); note that this condition will never occur if 433117397Skan * @a delim equals @c traits::eof(). 434169691Skan * 435169691Skan * NB: Provide three overloads, instead of the single function 436169691Skan * (with defaults) mandated by the Standard: this leads to a 437169691Skan * better performing implementation, while still conforming to 438169691Skan * the Standard. 439117397Skan */ 44097403Sobrien __istream_type& 441169691Skan ignore(); 442169691Skan 443169691Skan __istream_type& 444169691Skan ignore(streamsize __n); 445169691Skan 446169691Skan __istream_type& 447169691Skan ignore(streamsize __n, int_type __delim); 44897403Sobrien 449117397Skan /** 450117397Skan * @brief Looking ahead in the stream 451117397Skan * @return The next character, or eof(). 452117397Skan * 453117397Skan * If, after constructing the sentry object, @c good() is false, 454117397Skan * returns @c traits::eof(). Otherwise reads but does not extract 455117397Skan * the next input character. 456117397Skan */ 45797403Sobrien int_type 458117397Skan peek(); 45997403Sobrien 460117397Skan /** 461117397Skan * @brief Extraction without delimiters. 462117397Skan * @param s A character array. 463117397Skan * @param n Maximum number of characters to store. 464117397Skan * @return *this 465117397Skan * 466117397Skan * If the stream state is @c good(), extracts characters and stores 467117397Skan * them into @a s until one of the following happens: 468117397Skan * - @a n characters are stored 469117397Skan * - the input sequence reaches end-of-file, in which case the error 470117397Skan * state is set to @c failbit|eofbit. 471117397Skan * 472117397Skan * @note This function is not overloaded on signed char and 473117397Skan * unsigned char. 474117397Skan */ 47597403Sobrien __istream_type& 47697403Sobrien read(char_type* __s, streamsize __n); 47797403Sobrien 478117397Skan /** 479117397Skan * @brief Extraction until the buffer is exhausted, but no more. 480117397Skan * @param s A character array. 481117397Skan * @param n Maximum number of characters to store. 482117397Skan * @return The number of characters extracted. 483117397Skan * 484117397Skan * Extracts characters and stores them into @a s depending on the 485117397Skan * number of characters remaining in the streambuf's buffer, 486117397Skan * @c rdbuf()->in_avail(), called @c A here: 487117397Skan * - if @c A @c == @c -1, sets eofbit and extracts no characters 488117397Skan * - if @c A @c == @c 0, extracts no characters 489117397Skan * - if @c A @c > @c 0, extracts @c min(A,n) 490117397Skan * 491117397Skan * The goal is to empty the current buffer, and to not request any 492117397Skan * more from the external input sequence controlled by the streambuf. 493117397Skan */ 49497403Sobrien streamsize 49597403Sobrien readsome(char_type* __s, streamsize __n); 49697403Sobrien 497117397Skan /** 498117397Skan * @brief Unextracting a single character. 499117397Skan * @param c The character to push back into the input stream. 500117397Skan * @return *this 501117397Skan * 502117397Skan * If @c rdbuf() is not null, calls @c rdbuf()->sputbackc(c). 503117397Skan * 504117397Skan * If @c rdbuf() is null or if @c sputbackc() fails, sets badbit in 505117397Skan * the error state. 506117397Skan * 507117397Skan * @note Since no characters are extracted, the next call to 508117397Skan * @c gcount() will return 0, as required by DR 60. 509117397Skan */ 51097403Sobrien __istream_type& 51197403Sobrien putback(char_type __c); 51297403Sobrien 513117397Skan /** 514117397Skan * @brief Unextracting the previous character. 515117397Skan * @return *this 516117397Skan * 517117397Skan * If @c rdbuf() is not null, calls @c rdbuf()->sungetc(c). 518117397Skan * 519117397Skan * If @c rdbuf() is null or if @c sungetc() fails, sets badbit in 520117397Skan * the error state. 521117397Skan * 522117397Skan * @note Since no characters are extracted, the next call to 523117397Skan * @c gcount() will return 0, as required by DR 60. 524117397Skan */ 52597403Sobrien __istream_type& 526117397Skan unget(); 52797403Sobrien 528117397Skan /** 529117397Skan * @brief Synchronizing the stream buffer. 530117397Skan * @return 0 on success, -1 on failure 531117397Skan * 532117397Skan * If @c rdbuf() is a null pointer, returns -1. 533117397Skan * 534117397Skan * Otherwise, calls @c rdbuf()->pubsync(), and if that returns -1, 535117397Skan * sets badbit and returns -1. 536117397Skan * 537117397Skan * Otherwise, returns 0. 538117397Skan * 539117397Skan * @note This function does not count the number of characters 540117397Skan * extracted, if any, and therefore does not affect the next 541117397Skan * call to @c gcount(). 542117397Skan */ 54397403Sobrien int 544117397Skan sync(); 54597403Sobrien 546117397Skan /** 547117397Skan * @brief Getting the current read position. 548117397Skan * @return A file position object. 549117397Skan * 550117397Skan * If @c fail() is not false, returns @c pos_type(-1) to indicate 551117397Skan * failure. Otherwise returns @c rdbuf()->pubseekoff(0,cur,in). 552117397Skan * 553117397Skan * @note This function does not count the number of characters 554117397Skan * extracted, if any, and therefore does not affect the next 555117397Skan * call to @c gcount(). 556117397Skan */ 55797403Sobrien pos_type 558117397Skan tellg(); 55997403Sobrien 560117397Skan /** 561117397Skan * @brief Changing the current read position. 562117397Skan * @param pos A file position object. 563117397Skan * @return *this 564117397Skan * 565117397Skan * If @c fail() is not true, calls @c rdbuf()->pubseekpos(pos). If 566117397Skan * that function fails, sets failbit. 567117397Skan * 568117397Skan * @note This function does not count the number of characters 569117397Skan * extracted, if any, and therefore does not affect the next 570117397Skan * call to @c gcount(). 571117397Skan */ 57297403Sobrien __istream_type& 57397403Sobrien seekg(pos_type); 57497403Sobrien 575117397Skan /** 576117397Skan * @brief Changing the current read position. 577117397Skan * @param off A file offset object. 578117397Skan * @param dir The direction in which to seek. 579117397Skan * @return *this 580117397Skan * 581117397Skan * If @c fail() is not true, calls @c rdbuf()->pubseekoff(off,dir). 582117397Skan * If that function fails, sets failbit. 583117397Skan * 584117397Skan * @note This function does not count the number of characters 585117397Skan * extracted, if any, and therefore does not affect the next 586117397Skan * call to @c gcount(). 587117397Skan */ 58897403Sobrien __istream_type& 58997403Sobrien seekg(off_type, ios_base::seekdir); 590117397Skan //@} 591132720Skan 592132720Skan protected: 593132720Skan explicit 594132720Skan basic_istream(): _M_gcount(streamsize(0)) { } 595169691Skan 596169691Skan template<typename _ValueT> 597169691Skan __istream_type& 598169691Skan _M_extract(_ValueT& __v); 59997403Sobrien }; 600169691Skan 601169691Skan // Explicit specialization declarations, defined in src/istream.cc. 602169691Skan template<> 603169691Skan basic_istream<char>& 604169691Skan basic_istream<char>:: 605169691Skan getline(char_type* __s, streamsize __n, char_type __delim); 60697403Sobrien 607169691Skan template<> 608169691Skan basic_istream<char>& 609169691Skan basic_istream<char>:: 610169691Skan ignore(streamsize __n); 611169691Skan 612169691Skan template<> 613169691Skan basic_istream<char>& 614169691Skan basic_istream<char>:: 615169691Skan ignore(streamsize __n, int_type __delim); 616169691Skan 617169691Skan#ifdef _GLIBCXX_USE_WCHAR_T 618169691Skan template<> 619169691Skan basic_istream<wchar_t>& 620169691Skan basic_istream<wchar_t>:: 621169691Skan getline(char_type* __s, streamsize __n, char_type __delim); 622169691Skan 623169691Skan template<> 624169691Skan basic_istream<wchar_t>& 625169691Skan basic_istream<wchar_t>:: 626169691Skan ignore(streamsize __n); 627169691Skan 628169691Skan template<> 629169691Skan basic_istream<wchar_t>& 630169691Skan basic_istream<wchar_t>:: 631169691Skan ignore(streamsize __n, int_type __delim); 632169691Skan#endif 633169691Skan 634117397Skan /** 635117397Skan * @brief Performs setup work for input streams. 636117397Skan * 637117397Skan * Objects of this class are created before all of the standard 638117397Skan * extractors are run. It is responsible for "exception-safe prefix and 639117397Skan * suffix operations," although only prefix actions are currently required 640117397Skan * by the standard. Additional actions may be added by the 641117397Skan * implementation, and we list them in 642117397Skan * http://gcc.gnu.org/onlinedocs/libstdc++/17_intro/howto.html#5 643117397Skan * under [27.6] notes. 644117397Skan */ 64597403Sobrien template<typename _CharT, typename _Traits> 64697403Sobrien class basic_istream<_CharT, _Traits>::sentry 64797403Sobrien { 64897403Sobrien public: 649117397Skan /// Easy access to dependant types. 65097403Sobrien typedef _Traits traits_type; 65197403Sobrien typedef basic_streambuf<_CharT, _Traits> __streambuf_type; 65297403Sobrien typedef basic_istream<_CharT, _Traits> __istream_type; 65397403Sobrien typedef typename __istream_type::__ctype_type __ctype_type; 65497403Sobrien typedef typename _Traits::int_type __int_type; 65597403Sobrien 656117397Skan /** 657117397Skan * @brief The constructor performs all the work. 658117397Skan * @param is The input stream to guard. 659117397Skan * @param noskipws Whether to consume whitespace or not. 660117397Skan * 661117397Skan * If the stream state is good (@a is.good() is true), then the 662117397Skan * following actions are performed, otherwise the sentry state is 663117397Skan * false ("not okay") and failbit is set in the stream state. 664117397Skan * 665117397Skan * The sentry's preparatory actions are: 666117397Skan * 667117397Skan * -# if the stream is tied to an output stream, @c is.tie()->flush() 668117397Skan * is called to synchronize the output sequence 669117397Skan * -# if @a noskipws is false, and @c ios_base::skipws is set in 670117397Skan * @c is.flags(), the sentry extracts and discards whitespace 671117397Skan * characters from the stream. The currently imbued locale is 672117397Skan * used to determine whether each character is whitespace. 673117397Skan * 674117397Skan * If the stream state is still good, then the sentry state becomes 675117397Skan * true ("okay"). 676117397Skan */ 67797403Sobrien explicit 67897403Sobrien sentry(basic_istream<_CharT, _Traits>& __is, bool __noskipws = false); 67997403Sobrien 680117397Skan /** 681117397Skan * @brief Quick status checking. 682117397Skan * @return The sentry state. 683117397Skan * 684117397Skan * For ease of use, sentries may be converted to booleans. The 685117397Skan * return value is that of the sentry state (true == okay). 686117397Skan */ 687169691Skan operator bool() const 688169691Skan { return _M_ok; } 68997403Sobrien 69097403Sobrien private: 69197403Sobrien bool _M_ok; 69297403Sobrien }; 69397403Sobrien 694117397Skan // [27.6.1.2.3] character extraction templates 695117397Skan //@{ 696117397Skan /** 697117397Skan * @brief Character extractors 698117397Skan * @param in An input stream. 699117397Skan * @param c A character reference. 700117397Skan * @return in 701117397Skan * 702117397Skan * Behaves like one of the formatted arithmetic extractors described in 703117397Skan * std::basic_istream. After constructing a sentry object with good 704117397Skan * status, this function extracts a character (if one is available) and 705117397Skan * stores it in @a c. Otherwise, sets failbit in the input stream. 706117397Skan */ 70797403Sobrien template<typename _CharT, typename _Traits> 70897403Sobrien basic_istream<_CharT, _Traits>& 70997403Sobrien operator>>(basic_istream<_CharT, _Traits>& __in, _CharT& __c); 71097403Sobrien 71197403Sobrien template<class _Traits> 712169691Skan inline basic_istream<char, _Traits>& 71397403Sobrien operator>>(basic_istream<char, _Traits>& __in, unsigned char& __c) 71497403Sobrien { return (__in >> reinterpret_cast<char&>(__c)); } 71597403Sobrien 71697403Sobrien template<class _Traits> 717169691Skan inline basic_istream<char, _Traits>& 71897403Sobrien operator>>(basic_istream<char, _Traits>& __in, signed char& __c) 71997403Sobrien { return (__in >> reinterpret_cast<char&>(__c)); } 720117397Skan //@} 72197403Sobrien 722117397Skan //@{ 723117397Skan /** 724117397Skan * @brief Character string extractors 725117397Skan * @param in An input stream. 726117397Skan * @param s A pointer to a character array. 727117397Skan * @return in 728117397Skan * 729117397Skan * Behaves like one of the formatted arithmetic extractors described in 730117397Skan * std::basic_istream. After constructing a sentry object with good 731117397Skan * status, this function extracts up to @c n characters and stores them 732117397Skan * into the array starting at @a s. @c n is defined as: 733117397Skan * 734117397Skan * - if @c width() is greater than zero, @c n is width() 735117397Skan * - otherwise @c n is "the number of elements of the largest array of 736117397Skan * @c char_type that can store a terminating @c eos." [27.6.1.2.3]/6 737117397Skan * 738117397Skan * Characters are extracted and stored until one of the following happens: 739117397Skan * - @c n-1 characters are stored 740117397Skan * - EOF is reached 741117397Skan * - the next character is whitespace according to the current locale 742117397Skan * - the next character is a null byte (i.e., @c charT() ) 743117397Skan * 744117397Skan * @c width(0) is then called for the input stream. 745117397Skan * 746117397Skan * If no characters are extracted, sets failbit. 747117397Skan */ 74897403Sobrien template<typename _CharT, typename _Traits> 74997403Sobrien basic_istream<_CharT, _Traits>& 75097403Sobrien operator>>(basic_istream<_CharT, _Traits>& __in, _CharT* __s); 751169691Skan 752169691Skan // Explicit specialization declaration, defined in src/istream.cc. 753169691Skan template<> 754169691Skan basic_istream<char>& 755169691Skan operator>>(basic_istream<char>& __in, char* __s); 756169691Skan 75797403Sobrien template<class _Traits> 758169691Skan inline basic_istream<char, _Traits>& 759169691Skan operator>>(basic_istream<char, _Traits>& __in, unsigned char* __s) 76097403Sobrien { return (__in >> reinterpret_cast<char*>(__s)); } 76197403Sobrien 76297403Sobrien template<class _Traits> 763169691Skan inline basic_istream<char, _Traits>& 764169691Skan operator>>(basic_istream<char, _Traits>& __in, signed char* __s) 76597403Sobrien { return (__in >> reinterpret_cast<char*>(__s)); } 766117397Skan //@} 76797403Sobrien 76897403Sobrien // 27.6.1.5 Template class basic_iostream 769117397Skan /** 770117397Skan * @brief Merging istream and ostream capabilities. 771117397Skan * 772117397Skan * This class multiply inherits from the input and output stream classes 773117397Skan * simply to provide a single interface. 774117397Skan */ 77597403Sobrien template<typename _CharT, typename _Traits> 77697403Sobrien class basic_iostream 77797403Sobrien : public basic_istream<_CharT, _Traits>, 77897403Sobrien public basic_ostream<_CharT, _Traits> 77997403Sobrien { 78097403Sobrien public: 781132720Skan // _GLIBCXX_RESOLVE_LIB_DEFECTS 782132720Skan // 271. basic_iostream missing typedefs 783102782Skan // Types (inherited): 784102782Skan typedef _CharT char_type; 785102782Skan typedef typename _Traits::int_type int_type; 786102782Skan typedef typename _Traits::pos_type pos_type; 787102782Skan typedef typename _Traits::off_type off_type; 788102782Skan typedef _Traits traits_type; 789102782Skan 79097403Sobrien // Non-standard Types: 79197403Sobrien typedef basic_istream<_CharT, _Traits> __istream_type; 79297403Sobrien typedef basic_ostream<_CharT, _Traits> __ostream_type; 79397403Sobrien 794117397Skan /** 795117397Skan * @brief Constructor does nothing. 796117397Skan * 797117397Skan * Both of the parent classes are initialized with the same 798117397Skan * streambuf pointer passed to this constructor. 799117397Skan */ 80097403Sobrien explicit 80197403Sobrien basic_iostream(basic_streambuf<_CharT, _Traits>* __sb) 802132720Skan : __istream_type(), __ostream_type() 803132720Skan { this->init(__sb); } 80497403Sobrien 805117397Skan /** 806117397Skan * @brief Destructor does nothing. 807117397Skan */ 80897403Sobrien virtual 80997403Sobrien ~basic_iostream() { } 810132720Skan 811132720Skan protected: 812132720Skan explicit 813132720Skan basic_iostream() : __istream_type(), __ostream_type() 814132720Skan { } 81597403Sobrien }; 81697403Sobrien 817117397Skan // [27.6.1.4] standard basic_istream manipulators 818117397Skan /** 819117397Skan * @brief Quick and easy way to eat whitespace 820117397Skan * 821117397Skan * This manipulator extracts whitespace characters, stopping when the 822117397Skan * next character is non-whitespace, or when the input sequence is empty. 823117397Skan * If the sequence is empty, @c eofbit is set in the stream, but not 824117397Skan * @c failbit. 825117397Skan * 826117397Skan * The current locale is used to distinguish whitespace characters. 827117397Skan * 828117397Skan * Example: 829117397Skan * @code 830117397Skan * MyClass mc; 831117397Skan * 832117397Skan * std::cin >> std::ws >> mc; 833117397Skan * @endcode 834117397Skan * will skip leading whitespace before calling operator>> on cin and your 835117397Skan * object. Note that the same effect can be achieved by creating a 836117397Skan * std::basic_istream::sentry inside your definition of operator>>. 837117397Skan */ 83897403Sobrien template<typename _CharT, typename _Traits> 83997403Sobrien basic_istream<_CharT, _Traits>& 84097403Sobrien ws(basic_istream<_CharT, _Traits>& __is); 84197403Sobrien 842169691Skan_GLIBCXX_END_NAMESPACE 843169691Skan 844132720Skan#ifndef _GLIBCXX_EXPORT_TEMPLATE 84597403Sobrien# include <bits/istream.tcc> 84697403Sobrien#endif 84797403Sobrien 848132720Skan#endif /* _GLIBCXX_ISTREAM */ 849