1*e4b17023SJohn Marino// File based streams -*- C++ -*- 2*e4b17023SJohn Marino 3*e4b17023SJohn Marino// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 4*e4b17023SJohn Marino// 2006, 2007, 2008, 2009, 2010, 2011 5*e4b17023SJohn Marino// Free Software Foundation, Inc. 6*e4b17023SJohn Marino// 7*e4b17023SJohn Marino// This file is part of the GNU ISO C++ Library. This library is free 8*e4b17023SJohn Marino// software; you can redistribute it and/or modify it under the 9*e4b17023SJohn Marino// terms of the GNU General Public License as published by the 10*e4b17023SJohn Marino// Free Software Foundation; either version 3, or (at your option) 11*e4b17023SJohn Marino// any later version. 12*e4b17023SJohn Marino 13*e4b17023SJohn Marino// This library is distributed in the hope that it will be useful, 14*e4b17023SJohn Marino// but WITHOUT ANY WARRANTY; without even the implied warranty of 15*e4b17023SJohn Marino// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16*e4b17023SJohn Marino// GNU General Public License for more details. 17*e4b17023SJohn Marino 18*e4b17023SJohn Marino// Under Section 7 of GPL version 3, you are granted additional 19*e4b17023SJohn Marino// permissions described in the GCC Runtime Library Exception, version 20*e4b17023SJohn Marino// 3.1, as published by the Free Software Foundation. 21*e4b17023SJohn Marino 22*e4b17023SJohn Marino// You should have received a copy of the GNU General Public License and 23*e4b17023SJohn Marino// a copy of the GCC Runtime Library Exception along with this program; 24*e4b17023SJohn Marino// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 25*e4b17023SJohn Marino// <http://www.gnu.org/licenses/>. 26*e4b17023SJohn Marino 27*e4b17023SJohn Marino/** @file include/fstream 28*e4b17023SJohn Marino * This is a Standard C++ Library header. 29*e4b17023SJohn Marino */ 30*e4b17023SJohn Marino 31*e4b17023SJohn Marino// 32*e4b17023SJohn Marino// ISO C++ 14882: 27.8 File-based streams 33*e4b17023SJohn Marino// 34*e4b17023SJohn Marino 35*e4b17023SJohn Marino#ifndef _GLIBCXX_FSTREAM 36*e4b17023SJohn Marino#define _GLIBCXX_FSTREAM 1 37*e4b17023SJohn Marino 38*e4b17023SJohn Marino#pragma GCC system_header 39*e4b17023SJohn Marino 40*e4b17023SJohn Marino#include <istream> 41*e4b17023SJohn Marino#include <ostream> 42*e4b17023SJohn Marino#include <bits/codecvt.h> 43*e4b17023SJohn Marino#include <cstdio> // For BUFSIZ 44*e4b17023SJohn Marino#include <bits/basic_file.h> // For __basic_file, __c_lock 45*e4b17023SJohn Marino#ifdef __GXX_EXPERIMENTAL_CXX0X__ 46*e4b17023SJohn Marino#include <string> // For std::string overloads. 47*e4b17023SJohn Marino#endif 48*e4b17023SJohn Marino 49*e4b17023SJohn Marinonamespace std _GLIBCXX_VISIBILITY(default) 50*e4b17023SJohn Marino{ 51*e4b17023SJohn Marino_GLIBCXX_BEGIN_NAMESPACE_VERSION 52*e4b17023SJohn Marino 53*e4b17023SJohn Marino // [27.8.1.1] template class basic_filebuf 54*e4b17023SJohn Marino /** 55*e4b17023SJohn Marino * @brief The actual work of input and output (for files). 56*e4b17023SJohn Marino * @ingroup io 57*e4b17023SJohn Marino * 58*e4b17023SJohn Marino * This class associates both its input and output sequence with an 59*e4b17023SJohn Marino * external disk file, and maintains a joint file position for both 60*e4b17023SJohn Marino * sequences. Many of its semantics are described in terms of similar 61*e4b17023SJohn Marino * behavior in the Standard C Library's @c FILE streams. 62*e4b17023SJohn Marino */ 63*e4b17023SJohn Marino // Requirements on traits_type, specific to this class: 64*e4b17023SJohn Marino // traits_type::pos_type must be fpos<traits_type::state_type> 65*e4b17023SJohn Marino // traits_type::off_type must be streamoff 66*e4b17023SJohn Marino // traits_type::state_type must be Assignable and DefaultConstructible, 67*e4b17023SJohn Marino // and traits_type::state_type() must be the initial state for codecvt. 68*e4b17023SJohn Marino template<typename _CharT, typename _Traits> 69*e4b17023SJohn Marino class basic_filebuf : public basic_streambuf<_CharT, _Traits> 70*e4b17023SJohn Marino { 71*e4b17023SJohn Marino public: 72*e4b17023SJohn Marino // Types: 73*e4b17023SJohn Marino typedef _CharT char_type; 74*e4b17023SJohn Marino typedef _Traits traits_type; 75*e4b17023SJohn Marino typedef typename traits_type::int_type int_type; 76*e4b17023SJohn Marino typedef typename traits_type::pos_type pos_type; 77*e4b17023SJohn Marino typedef typename traits_type::off_type off_type; 78*e4b17023SJohn Marino 79*e4b17023SJohn Marino typedef basic_streambuf<char_type, traits_type> __streambuf_type; 80*e4b17023SJohn Marino typedef basic_filebuf<char_type, traits_type> __filebuf_type; 81*e4b17023SJohn Marino typedef __basic_file<char> __file_type; 82*e4b17023SJohn Marino typedef typename traits_type::state_type __state_type; 83*e4b17023SJohn Marino typedef codecvt<char_type, char, __state_type> __codecvt_type; 84*e4b17023SJohn Marino 85*e4b17023SJohn Marino friend class ios_base; // For sync_with_stdio. 86*e4b17023SJohn Marino 87*e4b17023SJohn Marino protected: 88*e4b17023SJohn Marino // Data Members: 89*e4b17023SJohn Marino // MT lock inherited from libio or other low-level io library. 90*e4b17023SJohn Marino __c_lock _M_lock; 91*e4b17023SJohn Marino 92*e4b17023SJohn Marino // External buffer. 93*e4b17023SJohn Marino __file_type _M_file; 94*e4b17023SJohn Marino 95*e4b17023SJohn Marino /// Place to stash in || out || in | out settings for current filebuf. 96*e4b17023SJohn Marino ios_base::openmode _M_mode; 97*e4b17023SJohn Marino 98*e4b17023SJohn Marino // Beginning state type for codecvt. 99*e4b17023SJohn Marino __state_type _M_state_beg; 100*e4b17023SJohn Marino 101*e4b17023SJohn Marino // During output, the state that corresponds to pptr(), 102*e4b17023SJohn Marino // during input, the state that corresponds to egptr() and 103*e4b17023SJohn Marino // _M_ext_next. 104*e4b17023SJohn Marino __state_type _M_state_cur; 105*e4b17023SJohn Marino 106*e4b17023SJohn Marino // Not used for output. During input, the state that corresponds 107*e4b17023SJohn Marino // to eback() and _M_ext_buf. 108*e4b17023SJohn Marino __state_type _M_state_last; 109*e4b17023SJohn Marino 110*e4b17023SJohn Marino /// Pointer to the beginning of internal buffer. 111*e4b17023SJohn Marino char_type* _M_buf; 112*e4b17023SJohn Marino 113*e4b17023SJohn Marino /** 114*e4b17023SJohn Marino * Actual size of internal buffer. This number is equal to the size 115*e4b17023SJohn Marino * of the put area + 1 position, reserved for the overflow char of 116*e4b17023SJohn Marino * a full area. 117*e4b17023SJohn Marino */ 118*e4b17023SJohn Marino size_t _M_buf_size; 119*e4b17023SJohn Marino 120*e4b17023SJohn Marino // Set iff _M_buf is allocated memory from _M_allocate_internal_buffer. 121*e4b17023SJohn Marino bool _M_buf_allocated; 122*e4b17023SJohn Marino 123*e4b17023SJohn Marino /** 124*e4b17023SJohn Marino * _M_reading == false && _M_writing == false for @b uncommitted mode; 125*e4b17023SJohn Marino * _M_reading == true for @b read mode; 126*e4b17023SJohn Marino * _M_writing == true for @b write mode; 127*e4b17023SJohn Marino * 128*e4b17023SJohn Marino * NB: _M_reading == true && _M_writing == true is unused. 129*e4b17023SJohn Marino */ 130*e4b17023SJohn Marino bool _M_reading; 131*e4b17023SJohn Marino bool _M_writing; 132*e4b17023SJohn Marino 133*e4b17023SJohn Marino //@{ 134*e4b17023SJohn Marino /** 135*e4b17023SJohn Marino * Necessary bits for putback buffer management. 136*e4b17023SJohn Marino * 137*e4b17023SJohn Marino * @note pbacks of over one character are not currently supported. 138*e4b17023SJohn Marino */ 139*e4b17023SJohn Marino char_type _M_pback; 140*e4b17023SJohn Marino char_type* _M_pback_cur_save; 141*e4b17023SJohn Marino char_type* _M_pback_end_save; 142*e4b17023SJohn Marino bool _M_pback_init; 143*e4b17023SJohn Marino //@} 144*e4b17023SJohn Marino 145*e4b17023SJohn Marino // Cached codecvt facet. 146*e4b17023SJohn Marino const __codecvt_type* _M_codecvt; 147*e4b17023SJohn Marino 148*e4b17023SJohn Marino /** 149*e4b17023SJohn Marino * Buffer for external characters. Used for input when 150*e4b17023SJohn Marino * codecvt::always_noconv() == false. When valid, this corresponds 151*e4b17023SJohn Marino * to eback(). 152*e4b17023SJohn Marino */ 153*e4b17023SJohn Marino char* _M_ext_buf; 154*e4b17023SJohn Marino 155*e4b17023SJohn Marino /** 156*e4b17023SJohn Marino * Size of buffer held by _M_ext_buf. 157*e4b17023SJohn Marino */ 158*e4b17023SJohn Marino streamsize _M_ext_buf_size; 159*e4b17023SJohn Marino 160*e4b17023SJohn Marino /** 161*e4b17023SJohn Marino * Pointers into the buffer held by _M_ext_buf that delimit a 162*e4b17023SJohn Marino * subsequence of bytes that have been read but not yet converted. 163*e4b17023SJohn Marino * When valid, _M_ext_next corresponds to egptr(). 164*e4b17023SJohn Marino */ 165*e4b17023SJohn Marino const char* _M_ext_next; 166*e4b17023SJohn Marino char* _M_ext_end; 167*e4b17023SJohn Marino 168*e4b17023SJohn Marino /** 169*e4b17023SJohn Marino * Initializes pback buffers, and moves normal buffers to safety. 170*e4b17023SJohn Marino * Assumptions: 171*e4b17023SJohn Marino * _M_in_cur has already been moved back 172*e4b17023SJohn Marino */ 173*e4b17023SJohn Marino void 174*e4b17023SJohn Marino _M_create_pback() 175*e4b17023SJohn Marino { 176*e4b17023SJohn Marino if (!_M_pback_init) 177*e4b17023SJohn Marino { 178*e4b17023SJohn Marino _M_pback_cur_save = this->gptr(); 179*e4b17023SJohn Marino _M_pback_end_save = this->egptr(); 180*e4b17023SJohn Marino this->setg(&_M_pback, &_M_pback, &_M_pback + 1); 181*e4b17023SJohn Marino _M_pback_init = true; 182*e4b17023SJohn Marino } 183*e4b17023SJohn Marino } 184*e4b17023SJohn Marino 185*e4b17023SJohn Marino /** 186*e4b17023SJohn Marino * Deactivates pback buffer contents, and restores normal buffer. 187*e4b17023SJohn Marino * Assumptions: 188*e4b17023SJohn Marino * The pback buffer has only moved forward. 189*e4b17023SJohn Marino */ 190*e4b17023SJohn Marino void 191*e4b17023SJohn Marino _M_destroy_pback() throw() 192*e4b17023SJohn Marino { 193*e4b17023SJohn Marino if (_M_pback_init) 194*e4b17023SJohn Marino { 195*e4b17023SJohn Marino // Length _M_in_cur moved in the pback buffer. 196*e4b17023SJohn Marino _M_pback_cur_save += this->gptr() != this->eback(); 197*e4b17023SJohn Marino this->setg(_M_buf, _M_pback_cur_save, _M_pback_end_save); 198*e4b17023SJohn Marino _M_pback_init = false; 199*e4b17023SJohn Marino } 200*e4b17023SJohn Marino } 201*e4b17023SJohn Marino 202*e4b17023SJohn Marino public: 203*e4b17023SJohn Marino // Constructors/destructor: 204*e4b17023SJohn Marino /** 205*e4b17023SJohn Marino * @brief Does not open any files. 206*e4b17023SJohn Marino * 207*e4b17023SJohn Marino * The default constructor initializes the parent class using its 208*e4b17023SJohn Marino * own default ctor. 209*e4b17023SJohn Marino */ 210*e4b17023SJohn Marino basic_filebuf(); 211*e4b17023SJohn Marino 212*e4b17023SJohn Marino /** 213*e4b17023SJohn Marino * @brief The destructor closes the file first. 214*e4b17023SJohn Marino */ 215*e4b17023SJohn Marino virtual 216*e4b17023SJohn Marino ~basic_filebuf() 217*e4b17023SJohn Marino { this->close(); } 218*e4b17023SJohn Marino 219*e4b17023SJohn Marino // Members: 220*e4b17023SJohn Marino /** 221*e4b17023SJohn Marino * @brief Returns true if the external file is open. 222*e4b17023SJohn Marino */ 223*e4b17023SJohn Marino bool 224*e4b17023SJohn Marino is_open() const throw() 225*e4b17023SJohn Marino { return _M_file.is_open(); } 226*e4b17023SJohn Marino 227*e4b17023SJohn Marino /** 228*e4b17023SJohn Marino * @brief Opens an external file. 229*e4b17023SJohn Marino * @param __s The name of the file. 230*e4b17023SJohn Marino * @param __mode The open mode flags. 231*e4b17023SJohn Marino * @return @c this on success, NULL on failure 232*e4b17023SJohn Marino * 233*e4b17023SJohn Marino * If a file is already open, this function immediately fails. 234*e4b17023SJohn Marino * Otherwise it tries to open the file named @a __s using the flags 235*e4b17023SJohn Marino * given in @a mode. 236*e4b17023SJohn Marino * 237*e4b17023SJohn Marino * Table 92, adapted here, gives the relation between openmode 238*e4b17023SJohn Marino * combinations and the equivalent fopen() flags. 239*e4b17023SJohn Marino * (NB: lines app, in|out|app, in|app, binary|app, binary|in|out|app, 240*e4b17023SJohn Marino * and binary|in|app per DR 596) 241*e4b17023SJohn Marino * +---------------------------------------------------------+ 242*e4b17023SJohn Marino * | ios_base Flag combination stdio equivalent | 243*e4b17023SJohn Marino * |binary in out trunc app | 244*e4b17023SJohn Marino * +---------------------------------------------------------+ 245*e4b17023SJohn Marino * | + w | 246*e4b17023SJohn Marino * | + + a | 247*e4b17023SJohn Marino * | + a | 248*e4b17023SJohn Marino * | + + w | 249*e4b17023SJohn Marino * | + r | 250*e4b17023SJohn Marino * | + + r+ | 251*e4b17023SJohn Marino * | + + + w+ | 252*e4b17023SJohn Marino * | + + + a+ | 253*e4b17023SJohn Marino * | + + a+ | 254*e4b17023SJohn Marino * +---------------------------------------------------------+ 255*e4b17023SJohn Marino * | + + wb | 256*e4b17023SJohn Marino * | + + + ab | 257*e4b17023SJohn Marino * | + + ab | 258*e4b17023SJohn Marino * | + + + wb | 259*e4b17023SJohn Marino * | + + rb | 260*e4b17023SJohn Marino * | + + + r+b | 261*e4b17023SJohn Marino * | + + + + w+b | 262*e4b17023SJohn Marino * | + + + + a+b | 263*e4b17023SJohn Marino * | + + + a+b | 264*e4b17023SJohn Marino * +---------------------------------------------------------+ 265*e4b17023SJohn Marino */ 266*e4b17023SJohn Marino __filebuf_type* 267*e4b17023SJohn Marino open(const char* __s, ios_base::openmode __mode); 268*e4b17023SJohn Marino 269*e4b17023SJohn Marino#ifdef __GXX_EXPERIMENTAL_CXX0X__ 270*e4b17023SJohn Marino /** 271*e4b17023SJohn Marino * @brief Opens an external file. 272*e4b17023SJohn Marino * @param __s The name of the file. 273*e4b17023SJohn Marino * @param __mode The open mode flags. 274*e4b17023SJohn Marino * @return @c this on success, NULL on failure 275*e4b17023SJohn Marino */ 276*e4b17023SJohn Marino __filebuf_type* 277*e4b17023SJohn Marino open(const std::string& __s, ios_base::openmode __mode) 278*e4b17023SJohn Marino { return open(__s.c_str(), __mode); } 279*e4b17023SJohn Marino#endif 280*e4b17023SJohn Marino 281*e4b17023SJohn Marino /** 282*e4b17023SJohn Marino * @brief Closes the currently associated file. 283*e4b17023SJohn Marino * @return @c this on success, NULL on failure 284*e4b17023SJohn Marino * 285*e4b17023SJohn Marino * If no file is currently open, this function immediately fails. 286*e4b17023SJohn Marino * 287*e4b17023SJohn Marino * If a <em>put buffer area</em> exists, @c overflow(eof) is 288*e4b17023SJohn Marino * called to flush all the characters. The file is then 289*e4b17023SJohn Marino * closed. 290*e4b17023SJohn Marino * 291*e4b17023SJohn Marino * If any operations fail, this function also fails. 292*e4b17023SJohn Marino */ 293*e4b17023SJohn Marino __filebuf_type* 294*e4b17023SJohn Marino close(); 295*e4b17023SJohn Marino 296*e4b17023SJohn Marino protected: 297*e4b17023SJohn Marino void 298*e4b17023SJohn Marino _M_allocate_internal_buffer(); 299*e4b17023SJohn Marino 300*e4b17023SJohn Marino void 301*e4b17023SJohn Marino _M_destroy_internal_buffer() throw(); 302*e4b17023SJohn Marino 303*e4b17023SJohn Marino // [27.8.1.4] overridden virtual functions 304*e4b17023SJohn Marino virtual streamsize 305*e4b17023SJohn Marino showmanyc(); 306*e4b17023SJohn Marino 307*e4b17023SJohn Marino // Stroustrup, 1998, p. 628 308*e4b17023SJohn Marino // underflow() and uflow() functions are called to get the next 309*e4b17023SJohn Marino // character from the real input source when the buffer is empty. 310*e4b17023SJohn Marino // Buffered input uses underflow() 311*e4b17023SJohn Marino 312*e4b17023SJohn Marino virtual int_type 313*e4b17023SJohn Marino underflow(); 314*e4b17023SJohn Marino 315*e4b17023SJohn Marino virtual int_type 316*e4b17023SJohn Marino pbackfail(int_type __c = _Traits::eof()); 317*e4b17023SJohn Marino 318*e4b17023SJohn Marino // Stroustrup, 1998, p 648 319*e4b17023SJohn Marino // The overflow() function is called to transfer characters to the 320*e4b17023SJohn Marino // real output destination when the buffer is full. A call to 321*e4b17023SJohn Marino // overflow(c) outputs the contents of the buffer plus the 322*e4b17023SJohn Marino // character c. 323*e4b17023SJohn Marino // 27.5.2.4.5 324*e4b17023SJohn Marino // Consume some sequence of the characters in the pending sequence. 325*e4b17023SJohn Marino virtual int_type 326*e4b17023SJohn Marino overflow(int_type __c = _Traits::eof()); 327*e4b17023SJohn Marino 328*e4b17023SJohn Marino // Convert internal byte sequence to external, char-based 329*e4b17023SJohn Marino // sequence via codecvt. 330*e4b17023SJohn Marino bool 331*e4b17023SJohn Marino _M_convert_to_external(char_type*, streamsize); 332*e4b17023SJohn Marino 333*e4b17023SJohn Marino /** 334*e4b17023SJohn Marino * @brief Manipulates the buffer. 335*e4b17023SJohn Marino * @param __s Pointer to a buffer area. 336*e4b17023SJohn Marino * @param __n Size of @a __s. 337*e4b17023SJohn Marino * @return @c this 338*e4b17023SJohn Marino * 339*e4b17023SJohn Marino * If no file has been opened, and both @a __s and @a __n are zero, then 340*e4b17023SJohn Marino * the stream becomes unbuffered. Otherwise, @c __s is used as a 341*e4b17023SJohn Marino * buffer; see 342*e4b17023SJohn Marino * http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt11ch25s02.html 343*e4b17023SJohn Marino * for more. 344*e4b17023SJohn Marino */ 345*e4b17023SJohn Marino virtual __streambuf_type* 346*e4b17023SJohn Marino setbuf(char_type* __s, streamsize __n); 347*e4b17023SJohn Marino 348*e4b17023SJohn Marino virtual pos_type 349*e4b17023SJohn Marino seekoff(off_type __off, ios_base::seekdir __way, 350*e4b17023SJohn Marino ios_base::openmode __mode = ios_base::in | ios_base::out); 351*e4b17023SJohn Marino 352*e4b17023SJohn Marino virtual pos_type 353*e4b17023SJohn Marino seekpos(pos_type __pos, 354*e4b17023SJohn Marino ios_base::openmode __mode = ios_base::in | ios_base::out); 355*e4b17023SJohn Marino 356*e4b17023SJohn Marino // Common code for seekoff, seekpos, and overflow 357*e4b17023SJohn Marino pos_type 358*e4b17023SJohn Marino _M_seek(off_type __off, ios_base::seekdir __way, __state_type __state); 359*e4b17023SJohn Marino 360*e4b17023SJohn Marino int 361*e4b17023SJohn Marino _M_get_ext_pos(__state_type &__state); 362*e4b17023SJohn Marino 363*e4b17023SJohn Marino virtual int 364*e4b17023SJohn Marino sync(); 365*e4b17023SJohn Marino 366*e4b17023SJohn Marino virtual void 367*e4b17023SJohn Marino imbue(const locale& __loc); 368*e4b17023SJohn Marino 369*e4b17023SJohn Marino virtual streamsize 370*e4b17023SJohn Marino xsgetn(char_type* __s, streamsize __n); 371*e4b17023SJohn Marino 372*e4b17023SJohn Marino virtual streamsize 373*e4b17023SJohn Marino xsputn(const char_type* __s, streamsize __n); 374*e4b17023SJohn Marino 375*e4b17023SJohn Marino // Flushes output buffer, then writes unshift sequence. 376*e4b17023SJohn Marino bool 377*e4b17023SJohn Marino _M_terminate_output(); 378*e4b17023SJohn Marino 379*e4b17023SJohn Marino /** 380*e4b17023SJohn Marino * This function sets the pointers of the internal buffer, both get 381*e4b17023SJohn Marino * and put areas. Typically: 382*e4b17023SJohn Marino * 383*e4b17023SJohn Marino * __off == egptr() - eback() upon underflow/uflow (@b read mode); 384*e4b17023SJohn Marino * __off == 0 upon overflow (@b write mode); 385*e4b17023SJohn Marino * __off == -1 upon open, setbuf, seekoff/pos (@b uncommitted mode). 386*e4b17023SJohn Marino * 387*e4b17023SJohn Marino * NB: epptr() - pbase() == _M_buf_size - 1, since _M_buf_size 388*e4b17023SJohn Marino * reflects the actual allocated memory and the last cell is reserved 389*e4b17023SJohn Marino * for the overflow char of a full put area. 390*e4b17023SJohn Marino */ 391*e4b17023SJohn Marino void 392*e4b17023SJohn Marino _M_set_buffer(streamsize __off) 393*e4b17023SJohn Marino { 394*e4b17023SJohn Marino const bool __testin = _M_mode & ios_base::in; 395*e4b17023SJohn Marino const bool __testout = _M_mode & ios_base::out; 396*e4b17023SJohn Marino 397*e4b17023SJohn Marino if (__testin && __off > 0) 398*e4b17023SJohn Marino this->setg(_M_buf, _M_buf, _M_buf + __off); 399*e4b17023SJohn Marino else 400*e4b17023SJohn Marino this->setg(_M_buf, _M_buf, _M_buf); 401*e4b17023SJohn Marino 402*e4b17023SJohn Marino if (__testout && __off == 0 && _M_buf_size > 1 ) 403*e4b17023SJohn Marino this->setp(_M_buf, _M_buf + _M_buf_size - 1); 404*e4b17023SJohn Marino else 405*e4b17023SJohn Marino this->setp(0, 0); 406*e4b17023SJohn Marino } 407*e4b17023SJohn Marino }; 408*e4b17023SJohn Marino 409*e4b17023SJohn Marino // [27.8.1.5] Template class basic_ifstream 410*e4b17023SJohn Marino /** 411*e4b17023SJohn Marino * @brief Controlling input for files. 412*e4b17023SJohn Marino * @ingroup io 413*e4b17023SJohn Marino * 414*e4b17023SJohn Marino * This class supports reading from named files, using the inherited 415*e4b17023SJohn Marino * functions from std::basic_istream. To control the associated 416*e4b17023SJohn Marino * sequence, an instance of std::basic_filebuf is used, which this page 417*e4b17023SJohn Marino * refers to as @c sb. 418*e4b17023SJohn Marino */ 419*e4b17023SJohn Marino template<typename _CharT, typename _Traits> 420*e4b17023SJohn Marino class basic_ifstream : public basic_istream<_CharT, _Traits> 421*e4b17023SJohn Marino { 422*e4b17023SJohn Marino public: 423*e4b17023SJohn Marino // Types: 424*e4b17023SJohn Marino typedef _CharT char_type; 425*e4b17023SJohn Marino typedef _Traits traits_type; 426*e4b17023SJohn Marino typedef typename traits_type::int_type int_type; 427*e4b17023SJohn Marino typedef typename traits_type::pos_type pos_type; 428*e4b17023SJohn Marino typedef typename traits_type::off_type off_type; 429*e4b17023SJohn Marino 430*e4b17023SJohn Marino // Non-standard types: 431*e4b17023SJohn Marino typedef basic_filebuf<char_type, traits_type> __filebuf_type; 432*e4b17023SJohn Marino typedef basic_istream<char_type, traits_type> __istream_type; 433*e4b17023SJohn Marino 434*e4b17023SJohn Marino private: 435*e4b17023SJohn Marino __filebuf_type _M_filebuf; 436*e4b17023SJohn Marino 437*e4b17023SJohn Marino public: 438*e4b17023SJohn Marino // Constructors/Destructors: 439*e4b17023SJohn Marino /** 440*e4b17023SJohn Marino * @brief Default constructor. 441*e4b17023SJohn Marino * 442*e4b17023SJohn Marino * Initializes @c sb using its default constructor, and passes 443*e4b17023SJohn Marino * @c &sb to the base class initializer. Does not open any files 444*e4b17023SJohn Marino * (you haven't given it a filename to open). 445*e4b17023SJohn Marino */ 446*e4b17023SJohn Marino basic_ifstream() : __istream_type(), _M_filebuf() 447*e4b17023SJohn Marino { this->init(&_M_filebuf); } 448*e4b17023SJohn Marino 449*e4b17023SJohn Marino /** 450*e4b17023SJohn Marino * @brief Create an input file stream. 451*e4b17023SJohn Marino * @param __s Null terminated string specifying the filename. 452*e4b17023SJohn Marino * @param __mode Open file in specified mode (see std::ios_base). 453*e4b17023SJohn Marino * 454*e4b17023SJohn Marino * @c ios_base::in is automatically included in @a __mode. 455*e4b17023SJohn Marino * 456*e4b17023SJohn Marino * Tip: When using std::string to hold the filename, you must use 457*e4b17023SJohn Marino * .c_str() before passing it to this constructor. 458*e4b17023SJohn Marino */ 459*e4b17023SJohn Marino explicit 460*e4b17023SJohn Marino basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in) 461*e4b17023SJohn Marino : __istream_type(), _M_filebuf() 462*e4b17023SJohn Marino { 463*e4b17023SJohn Marino this->init(&_M_filebuf); 464*e4b17023SJohn Marino this->open(__s, __mode); 465*e4b17023SJohn Marino } 466*e4b17023SJohn Marino 467*e4b17023SJohn Marino#ifdef __GXX_EXPERIMENTAL_CXX0X__ 468*e4b17023SJohn Marino /** 469*e4b17023SJohn Marino * @brief Create an input file stream. 470*e4b17023SJohn Marino * @param __s std::string specifying the filename. 471*e4b17023SJohn Marino * @param __mode Open file in specified mode (see std::ios_base). 472*e4b17023SJohn Marino * 473*e4b17023SJohn Marino * @c ios_base::in is automatically included in @a __mode. 474*e4b17023SJohn Marino */ 475*e4b17023SJohn Marino explicit 476*e4b17023SJohn Marino basic_ifstream(const std::string& __s, 477*e4b17023SJohn Marino ios_base::openmode __mode = ios_base::in) 478*e4b17023SJohn Marino : __istream_type(), _M_filebuf() 479*e4b17023SJohn Marino { 480*e4b17023SJohn Marino this->init(&_M_filebuf); 481*e4b17023SJohn Marino this->open(__s, __mode); 482*e4b17023SJohn Marino } 483*e4b17023SJohn Marino#endif 484*e4b17023SJohn Marino 485*e4b17023SJohn Marino /** 486*e4b17023SJohn Marino * @brief The destructor does nothing. 487*e4b17023SJohn Marino * 488*e4b17023SJohn Marino * The file is closed by the filebuf object, not the formatting 489*e4b17023SJohn Marino * stream. 490*e4b17023SJohn Marino */ 491*e4b17023SJohn Marino ~basic_ifstream() 492*e4b17023SJohn Marino { } 493*e4b17023SJohn Marino 494*e4b17023SJohn Marino // Members: 495*e4b17023SJohn Marino /** 496*e4b17023SJohn Marino * @brief Accessing the underlying buffer. 497*e4b17023SJohn Marino * @return The current basic_filebuf buffer. 498*e4b17023SJohn Marino * 499*e4b17023SJohn Marino * This hides both signatures of std::basic_ios::rdbuf(). 500*e4b17023SJohn Marino */ 501*e4b17023SJohn Marino __filebuf_type* 502*e4b17023SJohn Marino rdbuf() const 503*e4b17023SJohn Marino { return const_cast<__filebuf_type*>(&_M_filebuf); } 504*e4b17023SJohn Marino 505*e4b17023SJohn Marino /** 506*e4b17023SJohn Marino * @brief Wrapper to test for an open file. 507*e4b17023SJohn Marino * @return @c rdbuf()->is_open() 508*e4b17023SJohn Marino */ 509*e4b17023SJohn Marino bool 510*e4b17023SJohn Marino is_open() 511*e4b17023SJohn Marino { return _M_filebuf.is_open(); } 512*e4b17023SJohn Marino 513*e4b17023SJohn Marino // _GLIBCXX_RESOLVE_LIB_DEFECTS 514*e4b17023SJohn Marino // 365. Lack of const-qualification in clause 27 515*e4b17023SJohn Marino bool 516*e4b17023SJohn Marino is_open() const 517*e4b17023SJohn Marino { return _M_filebuf.is_open(); } 518*e4b17023SJohn Marino 519*e4b17023SJohn Marino /** 520*e4b17023SJohn Marino * @brief Opens an external file. 521*e4b17023SJohn Marino * @param __s The name of the file. 522*e4b17023SJohn Marino * @param __mode The open mode flags. 523*e4b17023SJohn Marino * 524*e4b17023SJohn Marino * Calls @c std::basic_filebuf::open(s,__mode|in). If that function 525*e4b17023SJohn Marino * fails, @c failbit is set in the stream's error state. 526*e4b17023SJohn Marino * 527*e4b17023SJohn Marino * Tip: When using std::string to hold the filename, you must use 528*e4b17023SJohn Marino * .c_str() before passing it to this constructor. 529*e4b17023SJohn Marino */ 530*e4b17023SJohn Marino void 531*e4b17023SJohn Marino open(const char* __s, ios_base::openmode __mode = ios_base::in) 532*e4b17023SJohn Marino { 533*e4b17023SJohn Marino if (!_M_filebuf.open(__s, __mode | ios_base::in)) 534*e4b17023SJohn Marino this->setstate(ios_base::failbit); 535*e4b17023SJohn Marino else 536*e4b17023SJohn Marino // _GLIBCXX_RESOLVE_LIB_DEFECTS 537*e4b17023SJohn Marino // 409. Closing an fstream should clear error state 538*e4b17023SJohn Marino this->clear(); 539*e4b17023SJohn Marino } 540*e4b17023SJohn Marino 541*e4b17023SJohn Marino#ifdef __GXX_EXPERIMENTAL_CXX0X__ 542*e4b17023SJohn Marino /** 543*e4b17023SJohn Marino * @brief Opens an external file. 544*e4b17023SJohn Marino * @param __s The name of the file. 545*e4b17023SJohn Marino * @param __mode The open mode flags. 546*e4b17023SJohn Marino * 547*e4b17023SJohn Marino * Calls @c std::basic_filebuf::open(__s,__mode|in). If that function 548*e4b17023SJohn Marino * fails, @c failbit is set in the stream's error state. 549*e4b17023SJohn Marino */ 550*e4b17023SJohn Marino void 551*e4b17023SJohn Marino open(const std::string& __s, ios_base::openmode __mode = ios_base::in) 552*e4b17023SJohn Marino { 553*e4b17023SJohn Marino if (!_M_filebuf.open(__s, __mode | ios_base::in)) 554*e4b17023SJohn Marino this->setstate(ios_base::failbit); 555*e4b17023SJohn Marino else 556*e4b17023SJohn Marino // _GLIBCXX_RESOLVE_LIB_DEFECTS 557*e4b17023SJohn Marino // 409. Closing an fstream should clear error state 558*e4b17023SJohn Marino this->clear(); 559*e4b17023SJohn Marino } 560*e4b17023SJohn Marino#endif 561*e4b17023SJohn Marino 562*e4b17023SJohn Marino /** 563*e4b17023SJohn Marino * @brief Close the file. 564*e4b17023SJohn Marino * 565*e4b17023SJohn Marino * Calls @c std::basic_filebuf::close(). If that function 566*e4b17023SJohn Marino * fails, @c failbit is set in the stream's error state. 567*e4b17023SJohn Marino */ 568*e4b17023SJohn Marino void 569*e4b17023SJohn Marino close() 570*e4b17023SJohn Marino { 571*e4b17023SJohn Marino if (!_M_filebuf.close()) 572*e4b17023SJohn Marino this->setstate(ios_base::failbit); 573*e4b17023SJohn Marino } 574*e4b17023SJohn Marino }; 575*e4b17023SJohn Marino 576*e4b17023SJohn Marino 577*e4b17023SJohn Marino // [27.8.1.8] Template class basic_ofstream 578*e4b17023SJohn Marino /** 579*e4b17023SJohn Marino * @brief Controlling output for files. 580*e4b17023SJohn Marino * @ingroup io 581*e4b17023SJohn Marino * 582*e4b17023SJohn Marino * This class supports reading from named files, using the inherited 583*e4b17023SJohn Marino * functions from std::basic_ostream. To control the associated 584*e4b17023SJohn Marino * sequence, an instance of std::basic_filebuf is used, which this page 585*e4b17023SJohn Marino * refers to as @c sb. 586*e4b17023SJohn Marino */ 587*e4b17023SJohn Marino template<typename _CharT, typename _Traits> 588*e4b17023SJohn Marino class basic_ofstream : public basic_ostream<_CharT,_Traits> 589*e4b17023SJohn Marino { 590*e4b17023SJohn Marino public: 591*e4b17023SJohn Marino // Types: 592*e4b17023SJohn Marino typedef _CharT char_type; 593*e4b17023SJohn Marino typedef _Traits traits_type; 594*e4b17023SJohn Marino typedef typename traits_type::int_type int_type; 595*e4b17023SJohn Marino typedef typename traits_type::pos_type pos_type; 596*e4b17023SJohn Marino typedef typename traits_type::off_type off_type; 597*e4b17023SJohn Marino 598*e4b17023SJohn Marino // Non-standard types: 599*e4b17023SJohn Marino typedef basic_filebuf<char_type, traits_type> __filebuf_type; 600*e4b17023SJohn Marino typedef basic_ostream<char_type, traits_type> __ostream_type; 601*e4b17023SJohn Marino 602*e4b17023SJohn Marino private: 603*e4b17023SJohn Marino __filebuf_type _M_filebuf; 604*e4b17023SJohn Marino 605*e4b17023SJohn Marino public: 606*e4b17023SJohn Marino // Constructors: 607*e4b17023SJohn Marino /** 608*e4b17023SJohn Marino * @brief Default constructor. 609*e4b17023SJohn Marino * 610*e4b17023SJohn Marino * Initializes @c sb using its default constructor, and passes 611*e4b17023SJohn Marino * @c &sb to the base class initializer. Does not open any files 612*e4b17023SJohn Marino * (you haven't given it a filename to open). 613*e4b17023SJohn Marino */ 614*e4b17023SJohn Marino basic_ofstream(): __ostream_type(), _M_filebuf() 615*e4b17023SJohn Marino { this->init(&_M_filebuf); } 616*e4b17023SJohn Marino 617*e4b17023SJohn Marino /** 618*e4b17023SJohn Marino * @brief Create an output file stream. 619*e4b17023SJohn Marino * @param __s Null terminated string specifying the filename. 620*e4b17023SJohn Marino * @param __mode Open file in specified mode (see std::ios_base). 621*e4b17023SJohn Marino * 622*e4b17023SJohn Marino * @c ios_base::out|ios_base::trunc is automatically included in 623*e4b17023SJohn Marino * @p __mode. 624*e4b17023SJohn Marino * 625*e4b17023SJohn Marino * Tip: When using std::string to hold the filename, you must use 626*e4b17023SJohn Marino * .c_str() before passing it to this constructor. 627*e4b17023SJohn Marino */ 628*e4b17023SJohn Marino explicit 629*e4b17023SJohn Marino basic_ofstream(const char* __s, 630*e4b17023SJohn Marino ios_base::openmode __mode = ios_base::out|ios_base::trunc) 631*e4b17023SJohn Marino : __ostream_type(), _M_filebuf() 632*e4b17023SJohn Marino { 633*e4b17023SJohn Marino this->init(&_M_filebuf); 634*e4b17023SJohn Marino this->open(__s, __mode); 635*e4b17023SJohn Marino } 636*e4b17023SJohn Marino 637*e4b17023SJohn Marino#ifdef __GXX_EXPERIMENTAL_CXX0X__ 638*e4b17023SJohn Marino /** 639*e4b17023SJohn Marino * @brief Create an output file stream. 640*e4b17023SJohn Marino * @param __s std::string specifying the filename. 641*e4b17023SJohn Marino * @param __mode Open file in specified mode (see std::ios_base). 642*e4b17023SJohn Marino * 643*e4b17023SJohn Marino * @c ios_base::out|ios_base::trunc is automatically included in 644*e4b17023SJohn Marino * @a __mode. 645*e4b17023SJohn Marino */ 646*e4b17023SJohn Marino explicit 647*e4b17023SJohn Marino basic_ofstream(const std::string& __s, 648*e4b17023SJohn Marino ios_base::openmode __mode = ios_base::out|ios_base::trunc) 649*e4b17023SJohn Marino : __ostream_type(), _M_filebuf() 650*e4b17023SJohn Marino { 651*e4b17023SJohn Marino this->init(&_M_filebuf); 652*e4b17023SJohn Marino this->open(__s, __mode); 653*e4b17023SJohn Marino } 654*e4b17023SJohn Marino#endif 655*e4b17023SJohn Marino 656*e4b17023SJohn Marino /** 657*e4b17023SJohn Marino * @brief The destructor does nothing. 658*e4b17023SJohn Marino * 659*e4b17023SJohn Marino * The file is closed by the filebuf object, not the formatting 660*e4b17023SJohn Marino * stream. 661*e4b17023SJohn Marino */ 662*e4b17023SJohn Marino ~basic_ofstream() 663*e4b17023SJohn Marino { } 664*e4b17023SJohn Marino 665*e4b17023SJohn Marino // Members: 666*e4b17023SJohn Marino /** 667*e4b17023SJohn Marino * @brief Accessing the underlying buffer. 668*e4b17023SJohn Marino * @return The current basic_filebuf buffer. 669*e4b17023SJohn Marino * 670*e4b17023SJohn Marino * This hides both signatures of std::basic_ios::rdbuf(). 671*e4b17023SJohn Marino */ 672*e4b17023SJohn Marino __filebuf_type* 673*e4b17023SJohn Marino rdbuf() const 674*e4b17023SJohn Marino { return const_cast<__filebuf_type*>(&_M_filebuf); } 675*e4b17023SJohn Marino 676*e4b17023SJohn Marino /** 677*e4b17023SJohn Marino * @brief Wrapper to test for an open file. 678*e4b17023SJohn Marino * @return @c rdbuf()->is_open() 679*e4b17023SJohn Marino */ 680*e4b17023SJohn Marino bool 681*e4b17023SJohn Marino is_open() 682*e4b17023SJohn Marino { return _M_filebuf.is_open(); } 683*e4b17023SJohn Marino 684*e4b17023SJohn Marino // _GLIBCXX_RESOLVE_LIB_DEFECTS 685*e4b17023SJohn Marino // 365. Lack of const-qualification in clause 27 686*e4b17023SJohn Marino bool 687*e4b17023SJohn Marino is_open() const 688*e4b17023SJohn Marino { return _M_filebuf.is_open(); } 689*e4b17023SJohn Marino 690*e4b17023SJohn Marino /** 691*e4b17023SJohn Marino * @brief Opens an external file. 692*e4b17023SJohn Marino * @param __s The name of the file. 693*e4b17023SJohn Marino * @param __mode The open mode flags. 694*e4b17023SJohn Marino * 695*e4b17023SJohn Marino * Calls @c std::basic_filebuf::open(__s,__mode|out|trunc). If that 696*e4b17023SJohn Marino * function fails, @c failbit is set in the stream's error state. 697*e4b17023SJohn Marino * 698*e4b17023SJohn Marino * Tip: When using std::string to hold the filename, you must use 699*e4b17023SJohn Marino * .c_str() before passing it to this constructor. 700*e4b17023SJohn Marino */ 701*e4b17023SJohn Marino void 702*e4b17023SJohn Marino open(const char* __s, 703*e4b17023SJohn Marino ios_base::openmode __mode = ios_base::out | ios_base::trunc) 704*e4b17023SJohn Marino { 705*e4b17023SJohn Marino if (!_M_filebuf.open(__s, __mode | ios_base::out)) 706*e4b17023SJohn Marino this->setstate(ios_base::failbit); 707*e4b17023SJohn Marino else 708*e4b17023SJohn Marino // _GLIBCXX_RESOLVE_LIB_DEFECTS 709*e4b17023SJohn Marino // 409. Closing an fstream should clear error state 710*e4b17023SJohn Marino this->clear(); 711*e4b17023SJohn Marino } 712*e4b17023SJohn Marino 713*e4b17023SJohn Marino#ifdef __GXX_EXPERIMENTAL_CXX0X__ 714*e4b17023SJohn Marino /** 715*e4b17023SJohn Marino * @brief Opens an external file. 716*e4b17023SJohn Marino * @param __s The name of the file. 717*e4b17023SJohn Marino * @param __mode The open mode flags. 718*e4b17023SJohn Marino * 719*e4b17023SJohn Marino * Calls @c std::basic_filebuf::open(s,mode|out|trunc). If that 720*e4b17023SJohn Marino * function fails, @c failbit is set in the stream's error state. 721*e4b17023SJohn Marino */ 722*e4b17023SJohn Marino void 723*e4b17023SJohn Marino open(const std::string& __s, 724*e4b17023SJohn Marino ios_base::openmode __mode = ios_base::out | ios_base::trunc) 725*e4b17023SJohn Marino { 726*e4b17023SJohn Marino if (!_M_filebuf.open(__s, __mode | ios_base::out)) 727*e4b17023SJohn Marino this->setstate(ios_base::failbit); 728*e4b17023SJohn Marino else 729*e4b17023SJohn Marino // _GLIBCXX_RESOLVE_LIB_DEFECTS 730*e4b17023SJohn Marino // 409. Closing an fstream should clear error state 731*e4b17023SJohn Marino this->clear(); 732*e4b17023SJohn Marino } 733*e4b17023SJohn Marino#endif 734*e4b17023SJohn Marino 735*e4b17023SJohn Marino /** 736*e4b17023SJohn Marino * @brief Close the file. 737*e4b17023SJohn Marino * 738*e4b17023SJohn Marino * Calls @c std::basic_filebuf::close(). If that function 739*e4b17023SJohn Marino * fails, @c failbit is set in the stream's error state. 740*e4b17023SJohn Marino */ 741*e4b17023SJohn Marino void 742*e4b17023SJohn Marino close() 743*e4b17023SJohn Marino { 744*e4b17023SJohn Marino if (!_M_filebuf.close()) 745*e4b17023SJohn Marino this->setstate(ios_base::failbit); 746*e4b17023SJohn Marino } 747*e4b17023SJohn Marino }; 748*e4b17023SJohn Marino 749*e4b17023SJohn Marino 750*e4b17023SJohn Marino // [27.8.1.11] Template class basic_fstream 751*e4b17023SJohn Marino /** 752*e4b17023SJohn Marino * @brief Controlling input and output for files. 753*e4b17023SJohn Marino * @ingroup io 754*e4b17023SJohn Marino * 755*e4b17023SJohn Marino * This class supports reading from and writing to named files, using 756*e4b17023SJohn Marino * the inherited functions from std::basic_iostream. To control the 757*e4b17023SJohn Marino * associated sequence, an instance of std::basic_filebuf is used, which 758*e4b17023SJohn Marino * this page refers to as @c sb. 759*e4b17023SJohn Marino */ 760*e4b17023SJohn Marino template<typename _CharT, typename _Traits> 761*e4b17023SJohn Marino class basic_fstream : public basic_iostream<_CharT, _Traits> 762*e4b17023SJohn Marino { 763*e4b17023SJohn Marino public: 764*e4b17023SJohn Marino // Types: 765*e4b17023SJohn Marino typedef _CharT char_type; 766*e4b17023SJohn Marino typedef _Traits traits_type; 767*e4b17023SJohn Marino typedef typename traits_type::int_type int_type; 768*e4b17023SJohn Marino typedef typename traits_type::pos_type pos_type; 769*e4b17023SJohn Marino typedef typename traits_type::off_type off_type; 770*e4b17023SJohn Marino 771*e4b17023SJohn Marino // Non-standard types: 772*e4b17023SJohn Marino typedef basic_filebuf<char_type, traits_type> __filebuf_type; 773*e4b17023SJohn Marino typedef basic_ios<char_type, traits_type> __ios_type; 774*e4b17023SJohn Marino typedef basic_iostream<char_type, traits_type> __iostream_type; 775*e4b17023SJohn Marino 776*e4b17023SJohn Marino private: 777*e4b17023SJohn Marino __filebuf_type _M_filebuf; 778*e4b17023SJohn Marino 779*e4b17023SJohn Marino public: 780*e4b17023SJohn Marino // Constructors/destructor: 781*e4b17023SJohn Marino /** 782*e4b17023SJohn Marino * @brief Default constructor. 783*e4b17023SJohn Marino * 784*e4b17023SJohn Marino * Initializes @c sb using its default constructor, and passes 785*e4b17023SJohn Marino * @c &sb to the base class initializer. Does not open any files 786*e4b17023SJohn Marino * (you haven't given it a filename to open). 787*e4b17023SJohn Marino */ 788*e4b17023SJohn Marino basic_fstream() 789*e4b17023SJohn Marino : __iostream_type(), _M_filebuf() 790*e4b17023SJohn Marino { this->init(&_M_filebuf); } 791*e4b17023SJohn Marino 792*e4b17023SJohn Marino /** 793*e4b17023SJohn Marino * @brief Create an input/output file stream. 794*e4b17023SJohn Marino * @param __s Null terminated string specifying the filename. 795*e4b17023SJohn Marino * @param __mode Open file in specified mode (see std::ios_base). 796*e4b17023SJohn Marino * 797*e4b17023SJohn Marino * Tip: When using std::string to hold the filename, you must use 798*e4b17023SJohn Marino * .c_str() before passing it to this constructor. 799*e4b17023SJohn Marino */ 800*e4b17023SJohn Marino explicit 801*e4b17023SJohn Marino basic_fstream(const char* __s, 802*e4b17023SJohn Marino ios_base::openmode __mode = ios_base::in | ios_base::out) 803*e4b17023SJohn Marino : __iostream_type(0), _M_filebuf() 804*e4b17023SJohn Marino { 805*e4b17023SJohn Marino this->init(&_M_filebuf); 806*e4b17023SJohn Marino this->open(__s, __mode); 807*e4b17023SJohn Marino } 808*e4b17023SJohn Marino 809*e4b17023SJohn Marino#ifdef __GXX_EXPERIMENTAL_CXX0X__ 810*e4b17023SJohn Marino /** 811*e4b17023SJohn Marino * @brief Create an input/output file stream. 812*e4b17023SJohn Marino * @param __s Null terminated string specifying the filename. 813*e4b17023SJohn Marino * @param __mode Open file in specified mode (see std::ios_base). 814*e4b17023SJohn Marino */ 815*e4b17023SJohn Marino explicit 816*e4b17023SJohn Marino basic_fstream(const std::string& __s, 817*e4b17023SJohn Marino ios_base::openmode __mode = ios_base::in | ios_base::out) 818*e4b17023SJohn Marino : __iostream_type(0), _M_filebuf() 819*e4b17023SJohn Marino { 820*e4b17023SJohn Marino this->init(&_M_filebuf); 821*e4b17023SJohn Marino this->open(__s, __mode); 822*e4b17023SJohn Marino } 823*e4b17023SJohn Marino#endif 824*e4b17023SJohn Marino 825*e4b17023SJohn Marino /** 826*e4b17023SJohn Marino * @brief The destructor does nothing. 827*e4b17023SJohn Marino * 828*e4b17023SJohn Marino * The file is closed by the filebuf object, not the formatting 829*e4b17023SJohn Marino * stream. 830*e4b17023SJohn Marino */ 831*e4b17023SJohn Marino ~basic_fstream() 832*e4b17023SJohn Marino { } 833*e4b17023SJohn Marino 834*e4b17023SJohn Marino // Members: 835*e4b17023SJohn Marino /** 836*e4b17023SJohn Marino * @brief Accessing the underlying buffer. 837*e4b17023SJohn Marino * @return The current basic_filebuf buffer. 838*e4b17023SJohn Marino * 839*e4b17023SJohn Marino * This hides both signatures of std::basic_ios::rdbuf(). 840*e4b17023SJohn Marino */ 841*e4b17023SJohn Marino __filebuf_type* 842*e4b17023SJohn Marino rdbuf() const 843*e4b17023SJohn Marino { return const_cast<__filebuf_type*>(&_M_filebuf); } 844*e4b17023SJohn Marino 845*e4b17023SJohn Marino /** 846*e4b17023SJohn Marino * @brief Wrapper to test for an open file. 847*e4b17023SJohn Marino * @return @c rdbuf()->is_open() 848*e4b17023SJohn Marino */ 849*e4b17023SJohn Marino bool 850*e4b17023SJohn Marino is_open() 851*e4b17023SJohn Marino { return _M_filebuf.is_open(); } 852*e4b17023SJohn Marino 853*e4b17023SJohn Marino // _GLIBCXX_RESOLVE_LIB_DEFECTS 854*e4b17023SJohn Marino // 365. Lack of const-qualification in clause 27 855*e4b17023SJohn Marino bool 856*e4b17023SJohn Marino is_open() const 857*e4b17023SJohn Marino { return _M_filebuf.is_open(); } 858*e4b17023SJohn Marino 859*e4b17023SJohn Marino /** 860*e4b17023SJohn Marino * @brief Opens an external file. 861*e4b17023SJohn Marino * @param __s The name of the file. 862*e4b17023SJohn Marino * @param __mode The open mode flags. 863*e4b17023SJohn Marino * 864*e4b17023SJohn Marino * Calls @c std::basic_filebuf::open(__s,__mode). If that 865*e4b17023SJohn Marino * function fails, @c failbit is set in the stream's error state. 866*e4b17023SJohn Marino * 867*e4b17023SJohn Marino * Tip: When using std::string to hold the filename, you must use 868*e4b17023SJohn Marino * .c_str() before passing it to this constructor. 869*e4b17023SJohn Marino */ 870*e4b17023SJohn Marino void 871*e4b17023SJohn Marino open(const char* __s, 872*e4b17023SJohn Marino ios_base::openmode __mode = ios_base::in | ios_base::out) 873*e4b17023SJohn Marino { 874*e4b17023SJohn Marino if (!_M_filebuf.open(__s, __mode)) 875*e4b17023SJohn Marino this->setstate(ios_base::failbit); 876*e4b17023SJohn Marino else 877*e4b17023SJohn Marino // _GLIBCXX_RESOLVE_LIB_DEFECTS 878*e4b17023SJohn Marino // 409. Closing an fstream should clear error state 879*e4b17023SJohn Marino this->clear(); 880*e4b17023SJohn Marino } 881*e4b17023SJohn Marino 882*e4b17023SJohn Marino#ifdef __GXX_EXPERIMENTAL_CXX0X__ 883*e4b17023SJohn Marino /** 884*e4b17023SJohn Marino * @brief Opens an external file. 885*e4b17023SJohn Marino * @param __s The name of the file. 886*e4b17023SJohn Marino * @param __mode The open mode flags. 887*e4b17023SJohn Marino * 888*e4b17023SJohn Marino * Calls @c std::basic_filebuf::open(__s,__mode). If that 889*e4b17023SJohn Marino * function fails, @c failbit is set in the stream's error state. 890*e4b17023SJohn Marino */ 891*e4b17023SJohn Marino void 892*e4b17023SJohn Marino open(const std::string& __s, 893*e4b17023SJohn Marino ios_base::openmode __mode = ios_base::in | ios_base::out) 894*e4b17023SJohn Marino { 895*e4b17023SJohn Marino if (!_M_filebuf.open(__s, __mode)) 896*e4b17023SJohn Marino this->setstate(ios_base::failbit); 897*e4b17023SJohn Marino else 898*e4b17023SJohn Marino // _GLIBCXX_RESOLVE_LIB_DEFECTS 899*e4b17023SJohn Marino // 409. Closing an fstream should clear error state 900*e4b17023SJohn Marino this->clear(); 901*e4b17023SJohn Marino } 902*e4b17023SJohn Marino#endif 903*e4b17023SJohn Marino 904*e4b17023SJohn Marino /** 905*e4b17023SJohn Marino * @brief Close the file. 906*e4b17023SJohn Marino * 907*e4b17023SJohn Marino * Calls @c std::basic_filebuf::close(). If that function 908*e4b17023SJohn Marino * fails, @c failbit is set in the stream's error state. 909*e4b17023SJohn Marino */ 910*e4b17023SJohn Marino void 911*e4b17023SJohn Marino close() 912*e4b17023SJohn Marino { 913*e4b17023SJohn Marino if (!_M_filebuf.close()) 914*e4b17023SJohn Marino this->setstate(ios_base::failbit); 915*e4b17023SJohn Marino } 916*e4b17023SJohn Marino }; 917*e4b17023SJohn Marino 918*e4b17023SJohn Marino_GLIBCXX_END_NAMESPACE_VERSION 919*e4b17023SJohn Marino} // namespace 920*e4b17023SJohn Marino 921*e4b17023SJohn Marino#include <bits/fstream.tcc> 922*e4b17023SJohn Marino 923*e4b17023SJohn Marino#endif /* _GLIBCXX_FSTREAM */ 924