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