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