std_fstream.h 24.8 KB
Newer Older
1
// File based streams -*- C++ -*-
2

3
// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003
4
// Free Software Foundation, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2, or (at your option)
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING.  If not, write to the Free
// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
// USA.

// As a special exception, you may use this file as part of a free software
// library without restriction.  Specifically, if other files instantiate
// templates or use macros or inline functions from this file, or you compile
// this file and link it with other files to produce an executable, this
// file does not by itself cause the resulting executable to be covered by
// the GNU General Public License.  This exception does not however
// invalidate any other reasons why the executable file might be covered by
// the GNU General Public License.

31 32 33 34
//
// ISO C++ 14882: 27.8  File-based streams
//

35 36 37
/** @file fstream
 *  This is a Standard C++ Library header.  You should @c #include this header
 *  in your programs, rather than any of the "st[dl]_*.h" implementation files.
Phil Edwards committed
38 39
 */

40 41
#ifndef _GLIBCXX_FSTREAM
#define _GLIBCXX_FSTREAM 1
42 43 44 45 46 47

#pragma GCC system_header

#include <istream>
#include <ostream>
#include <locale>	// For codecvt
48
#include <cstdio>       // For SEEK_SET, SEEK_CUR, SEEK_END, BUFSIZ     
49
#include <bits/basic_file.h>
50 51
#include <bits/gthr.h>

Phil Edwards committed
52
namespace std
53
{
54 55 56 57 58 59 60 61 62
  // [27.8.1.1] template class basic_filebuf
  /**
   *  @brief  The actual work of input and output (for files).
   *
   *  This class associates both its input and output sequence with an
   *  external disk file, and maintains a joint file position for both
   *  sequences.  Many of its sematics are described in terms of similar
   *  behavior in the Standard C Library's @c FILE streams.
  */
63 64 65 66 67
  // Requirements on traits_type, specific to this class:
  // traits_type::pos_type must be fpos<traits_type::state_type>
  // traits_type::off_type must be streamoff
  // traits_type::state_type must be Assignable and DefaultConstructable,
  // and traits_type::state_type() must be the initial state for codecvt.
68 69 70 71 72 73 74 75 76 77
  template<typename _CharT, typename _Traits>
    class basic_filebuf : public basic_streambuf<_CharT, _Traits>
    {
    public:
      // Types:
      typedef _CharT                     	        char_type;
      typedef _Traits                    	        traits_type;
      typedef typename traits_type::int_type 		int_type;
      typedef typename traits_type::pos_type 		pos_type;
      typedef typename traits_type::off_type 		off_type;
Phil Edwards committed
78

79 80 81 82 83 84
      //@{
      /**
       *  @if maint
       *  @doctodo
       *  @endif
      */
85 86
      typedef basic_streambuf<char_type, traits_type>  	__streambuf_type;
      typedef basic_filebuf<char_type, traits_type>     __filebuf_type;
87
      typedef __basic_file<char>		        __file_type;
88 89
      typedef typename traits_type::state_type          __state_type;
      typedef codecvt<char_type, char, __state_type>    __codecvt_type;
90
      //@}
91 92 93

      friend class ios_base; // For sync_with_stdio.

94
    protected:
95
      // Data Members:
96
      // MT lock inherited from libio or other low-level io library.
97 98 99 100 101
      /**
       *  @if maint
       *  @doctodo
       *  @endif
      */
102 103
      __c_lock          	_M_lock;

104
      // External buffer.
105 106 107 108 109
      /**
       *  @if maint
       *  @doctodo
       *  @endif
      */
110
      __file_type 		_M_file;
111

112 113 114 115 116 117 118
      /**
       *  @if maint
       *  Place to stash in || out || in | out settings for current filebuf.
       *  @endif
      */
      ios_base::openmode 	_M_mode;

119
      // Beginning state type for codecvt.
120 121 122 123 124
      /**
       *  @if maint
       *  @doctodo
       *  @endif
      */
Phil Edwards committed
125
      __state_type 		_M_state_beg;
126

127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
      // During output, the state that corresponds to pptr(),
      // during input, the state that corresponds to egptr() and
      // _M_ext_next.
      /**
       *  @if maint
       *  @doctodo
       *  @endif
      */
      __state_type		_M_state_cur;

      // Not used for output. During input, the state that corresponds
      // to eback() and _M_ext_buf.
      /**
       *  @if maint
       *  @doctodo
       *  @endif
      */
      __state_type		_M_state_last;

146 147
      /**
       *  @if maint
148
       *  Pointer to the beginning of internal buffer.
149 150 151 152
       *  @endif
      */
      char_type*		_M_buf; 	

153 154 155 156 157 158 159 160 161
      /**
       *  @if maint
       *  Actual size of internal buffer. This number is equal to the size
       *  of the put area + 1 position, reserved for the overflow char of
       *  a full area.
       *  @endif
      */
      size_t			_M_buf_size;

162
      // Set iff _M_buf is allocated memory from _M_allocate_internal_buffer.
163 164 165 166 167
      /**
       *  @if maint
       *  @doctodo
       *  @endif
      */
168
      bool			_M_buf_allocated;
Paolo Carlini committed
169

170 171 172 173 174 175 176 177 178
      /**
       *  @if maint
       *  _M_reading == false && _M_writing == false for 'uncommitted' mode;  
       *  _M_reading == true for 'read' mode;
       *  _M_writing == true for 'write' mode;
       *
       *  NB: _M_reading == true && _M_writing == true is unused.
       *  @endif
      */ 
Paolo Carlini committed
179 180 181
      bool                      _M_reading;
      bool                      _M_writing;

182 183 184 185 186 187 188 189
      //@{
      /**
       *  @if maint
       *  Necessary bits for putback buffer management.
       *
       *  @note pbacks of over one character are not currently supported.
       *  @endif
      */
190
      char_type			_M_pback; 
191 192 193 194 195
      char_type*		_M_pback_cur_save;
      char_type*		_M_pback_end_save;
      bool			_M_pback_init; 
      //@}

196 197 198
      // Cached codecvt facet.
      const __codecvt_type* 	_M_codecvt;

199 200
      /**
       *  @if maint
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
       *  Buffer for external characters. Used for input when
       *  codecvt::always_noconv() == false. When valid, this corresponds
       *  to eback().
       *  @endif
      */ 
      char*			_M_ext_buf;

      /**
       *  @if maint
       *  Size of buffer held by _M_ext_buf.
       *  @endif
      */ 
      streamsize		_M_ext_buf_size;

      /**
       *  @if maint
       *  Pointers into the buffer held by _M_ext_buf that delimit a
       *  subsequence of bytes that have been read but not yet converted.
       *  When valid, _M_ext_next corresponds to egptr().
       *  @endif
      */ 
      const char*		_M_ext_next;
      char*			_M_ext_end;

      /**
       *  @if maint
227 228 229 230 231
       *  Initializes pback buffers, and moves normal buffers to safety.
       *  Assumptions:
       *  _M_in_cur has already been moved back
       *  @endif
      */
232
      void
233
      _M_create_pback()
234 235 236
      {
	if (!_M_pback_init)
	  {
Paolo Carlini committed
237 238
	    _M_pback_cur_save = this->gptr();
	    _M_pback_end_save = this->egptr();
239
	    this->setg(&_M_pback, &_M_pback, &_M_pback + 1);
240 241 242 243
	    _M_pback_init = true;
	  }
      }

244 245 246 247 248 249 250
      /**
       *  @if maint
       *  Deactivates pback buffer contents, and restores normal buffer.
       *  Assumptions:
       *  The pback buffer has only moved forward.
       *  @endif
      */ 
251
      void
252
      _M_destroy_pback() throw()
253 254 255 256
      {
	if (_M_pback_init)
	  {
	    // Length _M_in_cur moved in the pback buffer.
Paolo Carlini committed
257
	    _M_pback_cur_save += this->gptr() != this->eback();
258
	    this->setg(this->_M_buf, _M_pback_cur_save, _M_pback_end_save);
259 260 261 262
	    _M_pback_init = false;
	  }
      }

263 264
    public:
      // Constructors/destructor:
265 266 267 268 269 270
      /**
       *  @brief  Does not open any files.
       *
       *  The default constructor initializes the parent class using its
       *  own default ctor.
      */
271 272
      basic_filebuf();

273 274 275
      /**
       *  @brief  The destructor closes the file first.
      */
Phil Edwards committed
276 277
      virtual
      ~basic_filebuf()
278
      { this->close(); }
279 280

      // Members:
281 282 283
      /**
       *  @brief  Returns true if the external file is open.
      */
Phil Edwards committed
284
      bool
285
      is_open() const throw() { return _M_file.is_open(); }
Phil Edwards committed
286

287 288 289 290 291 292 293 294 295 296 297 298 299
      /**
       *  @brief  Opens an external file.
       *  @param  s  The name of the file.
       *  @param  mode  The open mode flags.
       *  @return  @c this on success, NULL on failure
       *
       *  If a file is already open, this function immediately fails.
       *  Otherwise it tries to open the file named @a s using the flags
       *  given in @a mode.
       *
       *  [Table 92 gives the relation between openmode combinations and the
       *  equivalent fopen() flags, but the table has not been copied yet.]
      */
Phil Edwards committed
300
      __filebuf_type*
301
      open(const char* __s, ios_base::openmode __mode);
Phil Edwards committed
302

303 304 305 306 307 308 309 310 311 312 313
      /**
       *  @brief  Closes the currently associated file.
       *  @return  @c this on success, NULL on failure
       *
       *  If no file is currently open, this function immediately fails.
       *
       *  If a "put buffer area" exists, @c overflow(eof) is called to flush
       *  all the characters.  The file is then closed.
       *
       *  If any operations fail, this function also fails.
      */
Phil Edwards committed
314
      __filebuf_type*
315
      close() throw();
316 317

    protected:
318 319 320 321 322
      /**
       *  @if maint
       *  @doctodo
       *  @endif
      */
Phil Edwards committed
323
      void
324 325
      _M_allocate_internal_buffer();

326 327 328 329 330
      /**
       *  @if maint
       *  @doctodo
       *  @endif
      */
Phil Edwards committed
331
      void
332
      _M_destroy_internal_buffer() throw();
333

334 335
      // [27.8.1.4] overridden virtual functions
      // [documentation is inherited]
Phil Edwards committed
336
      virtual streamsize
337
      showmanyc();
Phil Edwards committed
338 339

      // Stroustrup, 1998, p. 628
340 341 342
      // underflow() and uflow() functions are called to get the next
      // charater from the real input source when the buffer is empty.
      // Buffered input uses underflow()
343

344
      // [documentation is inherited]
Phil Edwards committed
345
      virtual int_type
346
      underflow();
347

348
      // [documentation is inherited]
Phil Edwards committed
349
      virtual int_type
350 351 352 353 354 355 356
      pbackfail(int_type __c = _Traits::eof());

      // Stroustrup, 1998, p 648
      // The overflow() function is called to transfer characters to the
      // real output destination when the buffer is full. A call to
      // overflow(c) outputs the contents of the buffer plus the
      // character c.
Phil Edwards committed
357
      // 27.5.2.4.5
358
      // Consume some sequence of the characters in the pending sequence.
359 360 361 362 363
      /**
       *  @if maint
       *  @doctodo
       *  @endif
      */
364 365
      virtual int_type
      overflow(int_type __c = _Traits::eof());
Phil Edwards committed
366

367 368
      // Convert internal byte sequence to external, char-based
      // sequence via codecvt.
369 370 371 372 373
      /**
       *  @if maint
       *  @doctodo
       *  @endif
      */
Benjamin Kosnik committed
374 375
      bool
      _M_convert_to_external(char_type*, streamsize);
376

377 378 379 380 381 382 383 384 385 386 387 388
      /**
       *  @brief  Manipulates the buffer.
       *  @param  s  Pointer to a buffer area.
       *  @param  n  Size of @a s.
       *  @return  @c this
       *
       *  If no file has been opened, and both @a s and @a n are zero, then
       *  the stream becomes unbuffered.  Otherwise, @c s is used as a
       *  buffer; see
       *  http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#2
       *  for more.
      */
Phil Edwards committed
389
      virtual __streambuf_type*
390
      setbuf(char_type* __s, streamsize __n);
Phil Edwards committed
391

392
      // [documentation is inherited]
Phil Edwards committed
393
      virtual pos_type
394 395 396
      seekoff(off_type __off, ios_base::seekdir __way,
	      ios_base::openmode __mode = ios_base::in | ios_base::out);

397
      // [documentation is inherited]
Phil Edwards committed
398
      virtual pos_type
399 400 401
      seekpos(pos_type __pos,
	      ios_base::openmode __mode = ios_base::in | ios_base::out);

402 403 404 405 406 407 408
      // Common code for seekoff and seekpos
      /**
       *  @if maint
       *  @doctodo
       *  @endif
      */
      pos_type
409
      _M_seek(off_type __off, ios_base::seekdir __way, __state_type __state);
410

411
      // [documentation is inherited]
Phil Edwards committed
412
      virtual int
413
      sync();
Phil Edwards committed
414

415
      // [documentation is inherited]
Phil Edwards committed
416
      virtual void
417 418
      imbue(const locale& __loc);

419
      // [documentation is inherited]
Phil Edwards committed
420
      virtual streamsize
421 422 423
      xsgetn(char_type* __s, streamsize __n)
      {
	// Clear out pback buffer before going on to the real deal...
424
	streamsize __ret = 0;
425
	if (this->_M_pback_init)
426
	  {
Paolo Carlini committed
427
	    if (__n && this->gptr() == this->eback())
428
	      {
Paolo Carlini committed
429 430
		*__s++ = *this->gptr();
		this->gbump(1);
431
		__ret = 1;
432
	      }
433
	    _M_destroy_pback();
434 435 436 437 438
	  }
	if (__ret < __n)
	  __ret += __streambuf_type::xsgetn(__s, __n - __ret);
	return __ret;
      }
Phil Edwards committed
439

440
      // [documentation is inherited]
Phil Edwards committed
441
      virtual streamsize
Paolo Carlini committed
442
      xsputn(const char_type* __s, streamsize __n);
Phil Edwards committed
443

444
      // Flushes output buffer, then writes unshift sequence.
445 446 447 448 449
      /**
       *  @if maint
       *  @doctodo
       *  @endif
      */
450 451
      bool
      _M_terminate_output();
452

453 454 455 456 457 458 459 460 461 462 463 464 465 466
      /**
       *  @if maint 
       *  This function sets the pointers of the internal buffer, both get
       *  and put areas. Typically:
       *
       *   __off == egptr() - eback() upon underflow/uflow ('read' mode);
       *   __off == 0 upon overflow ('write' mode);
       *   __off == -1 upon open, setbuf, seekoff/pos ('uncommitted' mode).
       * 
       *  NB: epptr() - pbase() == _M_buf_size - 1, since _M_buf_size
       *  reflects the actual allocated memory and the last cell is reserved
       *  for the overflow char of a full put area.
       *  @endif
      */
467 468 469
      void
      _M_set_buffer(streamsize __off)
      {
470 471
 	const bool __testin = this->_M_mode & ios_base::in;
 	const bool __testout = this->_M_mode & ios_base::out;
472
	
Paolo Carlini committed
473
	if (__testin && __off > 0)
474
	  this->setg(this->_M_buf, this->_M_buf, this->_M_buf + __off);
Paolo Carlini committed
475 476 477 478 479 480 481
	else
	  this->setg(this->_M_buf, this->_M_buf, this->_M_buf);

	if (__testout && __off == 0 && this->_M_buf_size > 1 )
	  this->setp(this->_M_buf, this->_M_buf + this->_M_buf_size - 1);
	else
	  this->setp(NULL, NULL);
482
      }
483 484
    };

485
  // [27.8.1.5] Template class basic_ifstream
Phil Edwards committed
486
  /**
487 488 489 490 491 492
   *  @brief  Controlling input for files.
   *
   *  This class supports reading from named files, using the inherited
   *  functions from std::basic_istream.  To control the associated
   *  sequence, an instance of std::basic_filebuf is used, which this page
   *  refers to as @c sb.
Phil Edwards committed
493
  */
494 495 496 497 498 499 500 501 502 503 504 505 506 507
  template<typename _CharT, typename _Traits>
    class basic_ifstream : public basic_istream<_CharT, _Traits>
    {
    public:
      // Types:
      typedef _CharT 					char_type;
      typedef _Traits 					traits_type;
      typedef typename traits_type::int_type 		int_type;
      typedef typename traits_type::pos_type 		pos_type;
      typedef typename traits_type::off_type 		off_type;

      // Non-standard types:
      typedef basic_filebuf<char_type, traits_type> 	__filebuf_type;
      typedef basic_istream<char_type, traits_type>	__istream_type;
Phil Edwards committed
508

509
    private:
510 511 512 513 514
      /**
       *  @if maint
       *  @doctodo
       *  @endif
      */
515 516 517
      __filebuf_type	_M_filebuf;

    public:
518 519 520 521 522 523 524 525
      // Constructors/Destructors:
      /**
       *  @brief  Default constructor.
       *
       *  Initializes @c sb using its default constructor, and passes
       *  @c &sb to the base class initializer.  Does not open any files
       *  (you haven't given it a filename to open).
      */
526
      basic_ifstream() : __istream_type(), _M_filebuf()
527 528
      { this->init(&_M_filebuf); }

Phil Edwards committed
529
      /**
530 531
       *  @brief  Create an input file stream.
       *  @param  s  Null terminated string specifying the filename.
Phil Edwards committed
532 533
       *  @param  mode  Open file in specified mode (see std::ios_base).
       *
534 535
       *  @c ios_base::in is automatically included in @a mode.
       *
Phil Edwards committed
536 537 538 539
       *  Tip:  When using std::string to hold the filename, you must use
       *  .c_str() before passing it to this constructor.
      */
      explicit
540
      basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in)
541
      : __istream_type(), _M_filebuf()
Phil Edwards committed
542 543 544
      {
	this->init(&_M_filebuf);
	this->open(__s, __mode);
545
      }
Phil Edwards committed
546

547 548 549 550 551 552
      /**
       *  @brief  The destructor does nothing.
       *
       *  The file is closed by the filebuf object, not the formatting
       *  stream.
      */
553 554 555 556
      ~basic_ifstream()
      { }

      // Members:
Phil Edwards committed
557
      /**
558 559 560 561
       *  @brief  Accessing the underlying buffer.
       *  @return  The current basic_filebuf buffer.
       *
       *  This hides both signatures of std::basic_ios::rdbuf().
Phil Edwards committed
562 563 564
      */
      __filebuf_type*
      rdbuf() const
565 566
      { return const_cast<__filebuf_type*>(&_M_filebuf); }

567 568 569 570
      /**
       *  @brief  Wrapper to test for an open file.
       *  @return  @c rdbuf()->is_open()
      */
Phil Edwards committed
571
      bool
572
      is_open() { return _M_filebuf.is_open(); }
573

574 575 576 577 578 579 580 581 582 583 584
      /**
       *  @brief  Opens an external file.
       *  @param  s  The name of the file.
       *  @param  mode  The open mode flags.
       *
       *  Calls @c std::basic_filebuf::open(s,mode|in).  If that function
       *  fails, @c failbit is set in the stream's error state.
       *
       *  Tip:  When using std::string to hold the filename, you must use
       *  .c_str() before passing it to this constructor.
      */
Phil Edwards committed
585
      void
586
      open(const char* __s, ios_base::openmode __mode = ios_base::in)
Phil Edwards committed
587
      {
588
	if (!_M_filebuf.open(__s, __mode | ios_base::in))
Phil Edwards committed
589
	  this->setstate(ios_base::failbit);
590 591
      }

592 593 594 595 596 597
      /**
       *  @brief  Close the file.
       *
       *  Calls @c std::basic_filebuf::close().  If that function
       *  fails, @c failbit is set in the stream's error state.
      */
Phil Edwards committed
598
      void
599
      close()
Phil Edwards committed
600
      {
601
	if (!_M_filebuf.close())
Phil Edwards committed
602
	  this->setstate(ios_base::failbit);
603 604 605
      }
    };

Phil Edwards committed
606

607
  // [27.8.1.8] Template class basic_ofstream
Phil Edwards committed
608
  /**
609 610 611 612 613 614
   *  @brief  Controlling output for files.
   *
   *  This class supports reading from named files, using the inherited
   *  functions from std::basic_ostream.  To control the associated
   *  sequence, an instance of std::basic_filebuf is used, which this page
   *  refers to as @c sb.
Phil Edwards committed
615
  */
616 617 618 619 620 621 622 623 624 625 626 627 628 629
  template<typename _CharT, typename _Traits>
    class basic_ofstream : public basic_ostream<_CharT,_Traits>
    {
    public:
      // Types:
      typedef _CharT 					char_type;
      typedef _Traits 					traits_type;
      typedef typename traits_type::int_type 		int_type;
      typedef typename traits_type::pos_type 		pos_type;
      typedef typename traits_type::off_type 		off_type;

      // Non-standard types:
      typedef basic_filebuf<char_type, traits_type> 	__filebuf_type;
      typedef basic_ostream<char_type, traits_type>	__ostream_type;
Phil Edwards committed
630

631
    private:
632 633 634 635 636
      /**
       *  @if maint
       *  @doctodo
       *  @endif
      */
637 638 639 640
      __filebuf_type	_M_filebuf;

    public:
      // Constructors:
641 642 643 644 645 646 647
      /**
       *  @brief  Default constructor.
       *
       *  Initializes @c sb using its default constructor, and passes
       *  @c &sb to the base class initializer.  Does not open any files
       *  (you haven't given it a filename to open).
      */
648
      basic_ofstream(): __ostream_type(), _M_filebuf()
649
      { this->init(&_M_filebuf); }
Phil Edwards committed
650 651

      /**
652 653
       *  @brief  Create an output file stream.
       *  @param  s  Null terminated string specifying the filename.
Phil Edwards committed
654 655
       *  @param  mode  Open file in specified mode (see std::ios_base).
       *
656 657 658
       *  @c ios_base::out|ios_base::trunc is automatically included in
       *  @a mode.
       *
Phil Edwards committed
659 660 661 662 663
       *  Tip:  When using std::string to hold the filename, you must use
       *  .c_str() before passing it to this constructor.
      */
      explicit
      basic_ofstream(const char* __s,
664
		     ios_base::openmode __mode = ios_base::out|ios_base::trunc)
665
      : __ostream_type(), _M_filebuf()
Phil Edwards committed
666 667 668
      {
	this->init(&_M_filebuf);
	this->open(__s, __mode);
669 670
      }

671 672 673 674 675 676
      /**
       *  @brief  The destructor does nothing.
       *
       *  The file is closed by the filebuf object, not the formatting
       *  stream.
      */
677 678 679 680
      ~basic_ofstream()
      { }

      // Members:
Phil Edwards committed
681
      /**
682 683 684 685
       *  @brief  Accessing the underlying buffer.
       *  @return  The current basic_filebuf buffer.
       *
       *  This hides both signatures of std::basic_ios::rdbuf().
Phil Edwards committed
686 687
      */
      __filebuf_type*
688
      rdbuf() const
689
      { return const_cast<__filebuf_type*>(&_M_filebuf); }
Phil Edwards committed
690 691

      /**
692 693
       *  @brief  Wrapper to test for an open file.
       *  @return  @c rdbuf()->is_open()
Phil Edwards committed
694 695
      */
      bool
696
      is_open() { return _M_filebuf.is_open(); }
697

Phil Edwards committed
698
      /**
699 700 701 702 703 704
       *  @brief  Opens an external file.
       *  @param  s  The name of the file.
       *  @param  mode  The open mode flags.
       *
       *  Calls @c std::basic_filebuf::open(s,mode|out|trunc).  If that
       *  function fails, @c failbit is set in the stream's error state.
Phil Edwards committed
705 706 707 708 709 710
       *
       *  Tip:  When using std::string to hold the filename, you must use
       *  .c_str() before passing it to this constructor.
      */
      void
      open(const char* __s,
711
	   ios_base::openmode __mode = ios_base::out | ios_base::trunc)
Phil Edwards committed
712
      {
713
	if (!_M_filebuf.open(__s, __mode | ios_base::out))
Phil Edwards committed
714
	  this->setstate(ios_base::failbit);
715 716
      }

717 718 719 720 721 722
      /**
       *  @brief  Close the file.
       *
       *  Calls @c std::basic_filebuf::close().  If that function
       *  fails, @c failbit is set in the stream's error state.
      */
Phil Edwards committed
723
      void
724
      close()
Phil Edwards committed
725
      {
726
	if (!_M_filebuf.close())
Phil Edwards committed
727
	  this->setstate(ios_base::failbit);
728 729 730 731
      }
    };


732
  // [27.8.1.11] Template class basic_fstream
Phil Edwards committed
733
  /**
734 735 736 737 738 739
   *  @brief  Controlling intput and output for files.
   *
   *  This class supports reading from and writing to named files, using
   *  the inherited functions from std::basic_iostream.  To control the
   *  associated sequence, an instance of std::basic_filebuf is used, which
   *  this page refers to as @c sb.
Phil Edwards committed
740
  */
741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757
  template<typename _CharT, typename _Traits>
    class basic_fstream : public basic_iostream<_CharT, _Traits>
    {
    public:
      // Types:
      typedef _CharT 					char_type;
      typedef _Traits 					traits_type;
      typedef typename traits_type::int_type 		int_type;
      typedef typename traits_type::pos_type 		pos_type;
      typedef typename traits_type::off_type 		off_type;

      // Non-standard types:
      typedef basic_filebuf<char_type, traits_type> 	__filebuf_type;
      typedef basic_ios<char_type, traits_type>		__ios_type;
      typedef basic_iostream<char_type, traits_type>	__iostream_type;

    private:
758 759 760 761 762
      /**
       *  @if maint
       *  @doctodo
       *  @endif
      */
763
      __filebuf_type	_M_filebuf;
Phil Edwards committed
764

765 766
    public:
      // Constructors/destructor:
767 768 769 770 771 772 773
      /**
       *  @brief  Default constructor.
       *
       *  Initializes @c sb using its default constructor, and passes
       *  @c &sb to the base class initializer.  Does not open any files
       *  (you haven't given it a filename to open).
      */
774
      basic_fstream()
775
      : __iostream_type(), _M_filebuf()
776 777
      { this->init(&_M_filebuf); }

Phil Edwards committed
778
      /**
779 780
       *  @brief  Create an input/output file stream.
       *  @param  s  Null terminated string specifying the filename.
Phil Edwards committed
781 782 783 784 785 786
       *  @param  mode  Open file in specified mode (see std::ios_base).
       *
       *  Tip:  When using std::string to hold the filename, you must use
       *  .c_str() before passing it to this constructor.
      */
      explicit
787 788 789
      basic_fstream(const char* __s,
		    ios_base::openmode __mode = ios_base::in | ios_base::out)
      : __iostream_type(NULL), _M_filebuf()
Phil Edwards committed
790 791 792
      {
	this->init(&_M_filebuf);
	this->open(__s, __mode);
793
      }
Phil Edwards committed
794

795 796 797 798 799 800
      /**
       *  @brief  The destructor does nothing.
       *
       *  The file is closed by the filebuf object, not the formatting
       *  stream.
      */
801 802
      ~basic_fstream()
      { }
Phil Edwards committed
803

804
      // Members:
Phil Edwards committed
805
      /**
806 807 808 809
       *  @brief  Accessing the underlying buffer.
       *  @return  The current basic_filebuf buffer.
       *
       *  This hides both signatures of std::basic_ios::rdbuf().
Phil Edwards committed
810 811
      */
      __filebuf_type*
812
      rdbuf() const
813 814
      { return const_cast<__filebuf_type*>(&_M_filebuf); }

Phil Edwards committed
815
      /**
816 817
       *  @brief  Wrapper to test for an open file.
       *  @return  @c rdbuf()->is_open()
Phil Edwards committed
818 819
      */
      bool
820
      is_open() { return _M_filebuf.is_open(); }
821

Phil Edwards committed
822
      /**
823 824 825 826 827 828
       *  @brief  Opens an external file.
       *  @param  s  The name of the file.
       *  @param  mode  The open mode flags.
       *
       *  Calls @c std::basic_filebuf::open(s,mode).  If that
       *  function fails, @c failbit is set in the stream's error state.
Phil Edwards committed
829 830 831 832 833 834
       *
       *  Tip:  When using std::string to hold the filename, you must use
       *  .c_str() before passing it to this constructor.
      */
      void
      open(const char* __s,
835
	   ios_base::openmode __mode = ios_base::in | ios_base::out)
Phil Edwards committed
836
      {
837
	if (!_M_filebuf.open(__s, __mode))
838
	  this->setstate(ios_base::failbit);
839 840
      }

841 842 843 844 845 846
      /**
       *  @brief  Close the file.
       *
       *  Calls @c std::basic_filebuf::close().  If that function
       *  fails, @c failbit is set in the stream's error state.
      */
Phil Edwards committed
847
      void
848
      close()
Phil Edwards committed
849
      {
850
	if (!_M_filebuf.close())
851
	  this->setstate(ios_base::failbit);
852 853 854 855
      }
    };
} // namespace std

856
#ifndef _GLIBCXX_EXPORT_TEMPLATE
857
# include <bits/fstream.tcc>
858
#endif
859

860
#endif /* _GLIBCXX_FSTREAM */