197403Sobrien// File based streams -*- C++ -*-
297403Sobrien
3171827Skan// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4171827Skan// 2006, 2007
597403Sobrien// Free Software Foundation, Inc.
697403Sobrien//
797403Sobrien// This file is part of the GNU ISO C++ Library.  This library is free
897403Sobrien// software; you can redistribute it and/or modify it under the
997403Sobrien// terms of the GNU General Public License as published by the
1097403Sobrien// Free Software Foundation; either version 2, or (at your option)
1197403Sobrien// any later version.
1297403Sobrien
1397403Sobrien// This library is distributed in the hope that it will be useful,
1497403Sobrien// but WITHOUT ANY WARRANTY; without even the implied warranty of
1597403Sobrien// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1697403Sobrien// GNU General Public License for more details.
1797403Sobrien
1897403Sobrien// You should have received a copy of the GNU General Public License along
1997403Sobrien// with this library; see the file COPYING.  If not, write to the Free
20169691Skan// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
2197403Sobrien// USA.
2297403Sobrien
2397403Sobrien// As a special exception, you may use this file as part of a free software
2497403Sobrien// library without restriction.  Specifically, if other files instantiate
2597403Sobrien// templates or use macros or inline functions from this file, or you compile
2697403Sobrien// this file and link it with other files to produce an executable, this
2797403Sobrien// file does not by itself cause the resulting executable to be covered by
2897403Sobrien// the GNU General Public License.  This exception does not however
2997403Sobrien// invalidate any other reasons why the executable file might be covered by
3097403Sobrien// the GNU General Public License.
3197403Sobrien
32169691Skan/** @file fstream
33169691Skan *  This is a Standard C++ Library header.
34169691Skan */
35169691Skan
3697403Sobrien//
3797403Sobrien// ISO C++ 14882: 27.8  File-based streams
3897403Sobrien//
3997403Sobrien
40132720Skan#ifndef _GLIBCXX_FSTREAM
41132720Skan#define _GLIBCXX_FSTREAM 1
4297403Sobrien
4397403Sobrien#pragma GCC system_header
4497403Sobrien
4597403Sobrien#include <istream>
4697403Sobrien#include <ostream>
4797403Sobrien#include <locale>	// For codecvt
48132720Skan#include <cstdio>       // For SEEK_SET, SEEK_CUR, SEEK_END, BUFSIZ
4997403Sobrien#include <bits/basic_file.h>
5097403Sobrien#include <bits/gthr.h>
5197403Sobrien
52169691Skan_GLIBCXX_BEGIN_NAMESPACE(std)
53169691Skan
54117397Skan  // [27.8.1.1] template class basic_filebuf
55117397Skan  /**
56117397Skan   *  @brief  The actual work of input and output (for files).
57117397Skan   *
58117397Skan   *  This class associates both its input and output sequence with an
59117397Skan   *  external disk file, and maintains a joint file position for both
60117397Skan   *  sequences.  Many of its sematics are described in terms of similar
61117397Skan   *  behavior in the Standard C Library's @c FILE streams.
62117397Skan  */
63132720Skan  // Requirements on traits_type, specific to this class:
64132720Skan  // traits_type::pos_type must be fpos<traits_type::state_type>
65132720Skan  // traits_type::off_type must be streamoff
66132720Skan  // traits_type::state_type must be Assignable and DefaultConstructable,
67132720Skan  // and traits_type::state_type() must be the initial state for codecvt.
6897403Sobrien  template<typename _CharT, typename _Traits>
6997403Sobrien    class basic_filebuf : public basic_streambuf<_CharT, _Traits>
7097403Sobrien    {
7197403Sobrien    public:
7297403Sobrien      // Types:
7397403Sobrien      typedef _CharT                     	        char_type;
7497403Sobrien      typedef _Traits                    	        traits_type;
7597403Sobrien      typedef typename traits_type::int_type 		int_type;
7697403Sobrien      typedef typename traits_type::pos_type 		pos_type;
7797403Sobrien      typedef typename traits_type::off_type 		off_type;
7897403Sobrien
7997403Sobrien      typedef basic_streambuf<char_type, traits_type>  	__streambuf_type;
8097403Sobrien      typedef basic_filebuf<char_type, traits_type>     __filebuf_type;
8197403Sobrien      typedef __basic_file<char>		        __file_type;
8297403Sobrien      typedef typename traits_type::state_type          __state_type;
8397403Sobrien      typedef codecvt<char_type, char, __state_type>    __codecvt_type;
8497403Sobrien
8597403Sobrien      friend class ios_base; // For sync_with_stdio.
8697403Sobrien
8797403Sobrien    protected:
8897403Sobrien      // Data Members:
8997403Sobrien      // MT lock inherited from libio or other low-level io library.
9097403Sobrien      __c_lock          	_M_lock;
9197403Sobrien
9297403Sobrien      // External buffer.
9397403Sobrien      __file_type 		_M_file;
9497403Sobrien
95117397Skan      /**
96117397Skan       *  @if maint
97132720Skan       *  Place to stash in || out || in | out settings for current filebuf.
98132720Skan       *  @endif
99132720Skan      */
100132720Skan      ios_base::openmode 	_M_mode;
101132720Skan
102132720Skan      // Beginning state type for codecvt.
10397403Sobrien      __state_type 		_M_state_beg;
10497403Sobrien
105132720Skan      // During output, the state that corresponds to pptr(),
106132720Skan      // during input, the state that corresponds to egptr() and
107132720Skan      // _M_ext_next.
108132720Skan      __state_type		_M_state_cur;
109132720Skan
110132720Skan      // Not used for output. During input, the state that corresponds
111132720Skan      // to eback() and _M_ext_buf.
112132720Skan      __state_type		_M_state_last;
113132720Skan
114132720Skan      /**
115132720Skan       *  @if maint
116132720Skan       *  Pointer to the beginning of internal buffer.
117132720Skan       *  @endif
118132720Skan      */
119132720Skan      char_type*		_M_buf;
120132720Skan
121132720Skan      /**
122132720Skan       *  @if maint
123132720Skan       *  Actual size of internal buffer. This number is equal to the size
124132720Skan       *  of the put area + 1 position, reserved for the overflow char of
125132720Skan       *  a full area.
126132720Skan       *  @endif
127132720Skan      */
128132720Skan      size_t			_M_buf_size;
129132720Skan
13097403Sobrien      // Set iff _M_buf is allocated memory from _M_allocate_internal_buffer.
13197403Sobrien      bool			_M_buf_allocated;
13297403Sobrien
133117397Skan      /**
134117397Skan       *  @if maint
135132720Skan       *  _M_reading == false && _M_writing == false for 'uncommitted' mode;
136132720Skan       *  _M_reading == true for 'read' mode;
137132720Skan       *  _M_writing == true for 'write' mode;
138132720Skan       *
139132720Skan       *  NB: _M_reading == true && _M_writing == true is unused.
140117397Skan       *  @endif
141132720Skan      */
142132720Skan      bool                      _M_reading;
143132720Skan      bool                      _M_writing;
144132720Skan
145132720Skan      //@{
146132720Skan      /**
147132720Skan       *  @if maint
148132720Skan       *  Necessary bits for putback buffer management.
149132720Skan       *
150132720Skan       *  @note pbacks of over one character are not currently supported.
151132720Skan       *  @endif
152117397Skan      */
153132720Skan      char_type			_M_pback;
154132720Skan      char_type*		_M_pback_cur_save;
155132720Skan      char_type*		_M_pback_end_save;
156132720Skan      bool			_M_pback_init;
157132720Skan      //@}
15897403Sobrien
159132720Skan      // Cached codecvt facet.
160132720Skan      const __codecvt_type* 	_M_codecvt;
161132720Skan
162132720Skan      /**
163132720Skan       *  @if maint
164132720Skan       *  Buffer for external characters. Used for input when
165132720Skan       *  codecvt::always_noconv() == false. When valid, this corresponds
166132720Skan       *  to eback().
167132720Skan       *  @endif
168132720Skan      */
169132720Skan      char*			_M_ext_buf;
170132720Skan
171132720Skan      /**
172132720Skan       *  @if maint
173132720Skan       *  Size of buffer held by _M_ext_buf.
174132720Skan       *  @endif
175132720Skan      */
176132720Skan      streamsize		_M_ext_buf_size;
177132720Skan
178132720Skan      /**
179132720Skan       *  @if maint
180132720Skan       *  Pointers into the buffer held by _M_ext_buf that delimit a
181132720Skan       *  subsequence of bytes that have been read but not yet converted.
182132720Skan       *  When valid, _M_ext_next corresponds to egptr().
183132720Skan       *  @endif
184132720Skan      */
185132720Skan      const char*		_M_ext_next;
186132720Skan      char*			_M_ext_end;
187132720Skan
188132720Skan      /**
189132720Skan       *  @if maint
190132720Skan       *  Initializes pback buffers, and moves normal buffers to safety.
191132720Skan       *  Assumptions:
192132720Skan       *  _M_in_cur has already been moved back
193132720Skan       *  @endif
194132720Skan      */
195132720Skan      void
196132720Skan      _M_create_pback()
197132720Skan      {
198132720Skan	if (!_M_pback_init)
199132720Skan	  {
200132720Skan	    _M_pback_cur_save = this->gptr();
201132720Skan	    _M_pback_end_save = this->egptr();
202132720Skan	    this->setg(&_M_pback, &_M_pback, &_M_pback + 1);
203132720Skan	    _M_pback_init = true;
204132720Skan	  }
205132720Skan      }
206132720Skan
207132720Skan      /**
208132720Skan       *  @if maint
209132720Skan       *  Deactivates pback buffer contents, and restores normal buffer.
210132720Skan       *  Assumptions:
211132720Skan       *  The pback buffer has only moved forward.
212132720Skan       *  @endif
213132720Skan      */
214132720Skan      void
215132720Skan      _M_destroy_pback() throw()
216132720Skan      {
217132720Skan	if (_M_pback_init)
218132720Skan	  {
219132720Skan	    // Length _M_in_cur moved in the pback buffer.
220132720Skan	    _M_pback_cur_save += this->gptr() != this->eback();
221169691Skan	    this->setg(_M_buf, _M_pback_cur_save, _M_pback_end_save);
222132720Skan	    _M_pback_init = false;
223132720Skan	  }
224132720Skan      }
225132720Skan
22697403Sobrien    public:
22797403Sobrien      // Constructors/destructor:
228117397Skan      /**
229117397Skan       *  @brief  Does not open any files.
230117397Skan       *
231117397Skan       *  The default constructor initializes the parent class using its
232117397Skan       *  own default ctor.
233117397Skan      */
23497403Sobrien      basic_filebuf();
23597403Sobrien
236117397Skan      /**
237117397Skan       *  @brief  The destructor closes the file first.
238117397Skan      */
23997403Sobrien      virtual
24097403Sobrien      ~basic_filebuf()
241132720Skan      { this->close(); }
24297403Sobrien
24397403Sobrien      // Members:
244117397Skan      /**
245117397Skan       *  @brief  Returns true if the external file is open.
246117397Skan      */
24797403Sobrien      bool
248169691Skan      is_open() const throw()
249169691Skan      { return _M_file.is_open(); }
25097403Sobrien
251117397Skan      /**
252117397Skan       *  @brief  Opens an external file.
253117397Skan       *  @param  s  The name of the file.
254117397Skan       *  @param  mode  The open mode flags.
255117397Skan       *  @return  @c this on success, NULL on failure
256117397Skan       *
257117397Skan       *  If a file is already open, this function immediately fails.
258117397Skan       *  Otherwise it tries to open the file named @a s using the flags
259117397Skan       *  given in @a mode.
260117397Skan       *
261171827Skan       *  Table 92, adapted here, gives the relation between openmode
262171827Skan       *  combinations and the equivalent fopen() flags.
263171827Skan       *  (NB: lines in|out|app and binary|in|out|app per DR 596)
264171827Skan       *  +---------------------------------------------------------+
265171827Skan       *  | ios_base Flag combination            stdio equivalent   |
266171827Skan       *  |binary  in  out  trunc  app                              |
267171827Skan       *  +---------------------------------------------------------+
268171827Skan       *  |             +                        "w"                |
269171827Skan       *  |             +           +            "a"                |
270171827Skan       *  |             +     +                  "w"                |
271171827Skan       *  |         +                            "r"                |
272171827Skan       *  |         +   +                        "r+"               |
273171827Skan       *  |         +   +     +                  "w+"               |
274171827Skan       *  |         +   +           +            "a+"               |
275171827Skan       *  +---------------------------------------------------------+
276171827Skan       *  |   +         +                        "wb"               |
277171827Skan       *  |   +         +           +            "ab"               |
278171827Skan       *  |   +         +     +                  "wb"               |
279171827Skan       *  |   +     +                            "rb"               |
280171827Skan       *  |   +     +   +                        "r+b"              |
281171827Skan       *  |   +     +   +     +                  "w+b"              |
282171827Skan       *  |   +     +   +           +            "a+b"              |
283171827Skan       *  +---------------------------------------------------------+
284171827Skan       */
28597403Sobrien      __filebuf_type*
28697403Sobrien      open(const char* __s, ios_base::openmode __mode);
28797403Sobrien
288117397Skan      /**
289117397Skan       *  @brief  Closes the currently associated file.
290117397Skan       *  @return  @c this on success, NULL on failure
291117397Skan       *
292117397Skan       *  If no file is currently open, this function immediately fails.
293117397Skan       *
294117397Skan       *  If a "put buffer area" exists, @c overflow(eof) is called to flush
295117397Skan       *  all the characters.  The file is then closed.
296117397Skan       *
297117397Skan       *  If any operations fail, this function also fails.
298117397Skan      */
29997403Sobrien      __filebuf_type*
300117397Skan      close() throw();
30197403Sobrien
30297403Sobrien    protected:
30397403Sobrien      void
30497403Sobrien      _M_allocate_internal_buffer();
30597403Sobrien
30697403Sobrien      void
307117397Skan      _M_destroy_internal_buffer() throw();
30897403Sobrien
309117397Skan      // [27.8.1.4] overridden virtual functions
31097403Sobrien      virtual streamsize
31197403Sobrien      showmanyc();
31297403Sobrien
31397403Sobrien      // Stroustrup, 1998, p. 628
31497403Sobrien      // underflow() and uflow() functions are called to get the next
31597403Sobrien      // charater from the real input source when the buffer is empty.
31697403Sobrien      // Buffered input uses underflow()
31797403Sobrien
31897403Sobrien      virtual int_type
319110614Skan      underflow();
32097403Sobrien
32197403Sobrien      virtual int_type
32297403Sobrien      pbackfail(int_type __c = _Traits::eof());
32397403Sobrien
32497403Sobrien      // Stroustrup, 1998, p 648
32597403Sobrien      // The overflow() function is called to transfer characters to the
32697403Sobrien      // real output destination when the buffer is full. A call to
32797403Sobrien      // overflow(c) outputs the contents of the buffer plus the
32897403Sobrien      // character c.
32997403Sobrien      // 27.5.2.4.5
33097403Sobrien      // Consume some sequence of the characters in the pending sequence.
331132720Skan      virtual int_type
332132720Skan      overflow(int_type __c = _Traits::eof());
33397403Sobrien
33497403Sobrien      // Convert internal byte sequence to external, char-based
33597403Sobrien      // sequence via codecvt.
336132720Skan      bool
337132720Skan      _M_convert_to_external(char_type*, streamsize);
33897403Sobrien
339117397Skan      /**
340117397Skan       *  @brief  Manipulates the buffer.
341117397Skan       *  @param  s  Pointer to a buffer area.
342117397Skan       *  @param  n  Size of @a s.
343117397Skan       *  @return  @c this
344117397Skan       *
345117397Skan       *  If no file has been opened, and both @a s and @a n are zero, then
346117397Skan       *  the stream becomes unbuffered.  Otherwise, @c s is used as a
347117397Skan       *  buffer; see
348117397Skan       *  http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#2
349117397Skan       *  for more.
350117397Skan      */
35197403Sobrien      virtual __streambuf_type*
35297403Sobrien      setbuf(char_type* __s, streamsize __n);
35397403Sobrien
35497403Sobrien      virtual pos_type
35597403Sobrien      seekoff(off_type __off, ios_base::seekdir __way,
35697403Sobrien	      ios_base::openmode __mode = ios_base::in | ios_base::out);
35797403Sobrien
35897403Sobrien      virtual pos_type
35997403Sobrien      seekpos(pos_type __pos,
36097403Sobrien	      ios_base::openmode __mode = ios_base::in | ios_base::out);
36197403Sobrien
362132720Skan      // Common code for seekoff and seekpos
363132720Skan      pos_type
364132720Skan      _M_seek(off_type __off, ios_base::seekdir __way, __state_type __state);
365132720Skan
36697403Sobrien      virtual int
367132720Skan      sync();
36897403Sobrien
36997403Sobrien      virtual void
37097403Sobrien      imbue(const locale& __loc);
37197403Sobrien
37297403Sobrien      virtual streamsize
373146897Skan      xsgetn(char_type* __s, streamsize __n);
37497403Sobrien
37597403Sobrien      virtual streamsize
376132720Skan      xsputn(const char_type* __s, streamsize __n);
37797403Sobrien
378132720Skan      // Flushes output buffer, then writes unshift sequence.
379132720Skan      bool
380132720Skan      _M_terminate_output();
38197403Sobrien
382117397Skan      /**
383132720Skan       *  @if maint
384132720Skan       *  This function sets the pointers of the internal buffer, both get
385132720Skan       *  and put areas. Typically:
386132720Skan       *
387132720Skan       *   __off == egptr() - eback() upon underflow/uflow ('read' mode);
388132720Skan       *   __off == 0 upon overflow ('write' mode);
389132720Skan       *   __off == -1 upon open, setbuf, seekoff/pos ('uncommitted' mode).
390132720Skan       *
391132720Skan       *  NB: epptr() - pbase() == _M_buf_size - 1, since _M_buf_size
392132720Skan       *  reflects the actual allocated memory and the last cell is reserved
393132720Skan       *  for the overflow char of a full put area.
394117397Skan       *  @endif
395117397Skan      */
39697403Sobrien      void
397132720Skan      _M_set_buffer(streamsize __off)
39897403Sobrien      {
399169691Skan 	const bool __testin = _M_mode & ios_base::in;
400169691Skan 	const bool __testout = _M_mode & ios_base::out;
401132720Skan
402132720Skan	if (__testin && __off > 0)
403169691Skan	  this->setg(_M_buf, _M_buf, _M_buf + __off);
404132720Skan	else
405169691Skan	  this->setg(_M_buf, _M_buf, _M_buf);
40697403Sobrien
407169691Skan	if (__testout && __off == 0 && _M_buf_size > 1 )
408169691Skan	  this->setp(_M_buf, _M_buf + _M_buf_size - 1);
409132720Skan	else
410132720Skan	  this->setp(NULL, NULL);
41197403Sobrien      }
41297403Sobrien    };
41397403Sobrien
414117397Skan  // [27.8.1.5] Template class basic_ifstream
41597403Sobrien  /**
416117397Skan   *  @brief  Controlling input for files.
417117397Skan   *
418117397Skan   *  This class supports reading from named files, using the inherited
419117397Skan   *  functions from std::basic_istream.  To control the associated
420117397Skan   *  sequence, an instance of std::basic_filebuf is used, which this page
421117397Skan   *  refers to as @c sb.
42297403Sobrien  */
42397403Sobrien  template<typename _CharT, typename _Traits>
42497403Sobrien    class basic_ifstream : public basic_istream<_CharT, _Traits>
42597403Sobrien    {
42697403Sobrien    public:
42797403Sobrien      // Types:
42897403Sobrien      typedef _CharT 					char_type;
42997403Sobrien      typedef _Traits 					traits_type;
43097403Sobrien      typedef typename traits_type::int_type 		int_type;
43197403Sobrien      typedef typename traits_type::pos_type 		pos_type;
43297403Sobrien      typedef typename traits_type::off_type 		off_type;
43397403Sobrien
43497403Sobrien      // Non-standard types:
43597403Sobrien      typedef basic_filebuf<char_type, traits_type> 	__filebuf_type;
43697403Sobrien      typedef basic_istream<char_type, traits_type>	__istream_type;
43797403Sobrien
43897403Sobrien    private:
43997403Sobrien      __filebuf_type	_M_filebuf;
44097403Sobrien
44197403Sobrien    public:
442117397Skan      // Constructors/Destructors:
443117397Skan      /**
444117397Skan       *  @brief  Default constructor.
445117397Skan       *
446117397Skan       *  Initializes @c sb using its default constructor, and passes
447117397Skan       *  @c &sb to the base class initializer.  Does not open any files
448117397Skan       *  (you haven't given it a filename to open).
449117397Skan      */
450132720Skan      basic_ifstream() : __istream_type(), _M_filebuf()
45197403Sobrien      { this->init(&_M_filebuf); }
45297403Sobrien
45397403Sobrien      /**
454117397Skan       *  @brief  Create an input file stream.
455117397Skan       *  @param  s  Null terminated string specifying the filename.
45697403Sobrien       *  @param  mode  Open file in specified mode (see std::ios_base).
45797403Sobrien       *
458117397Skan       *  @c ios_base::in is automatically included in @a mode.
459117397Skan       *
46097403Sobrien       *  Tip:  When using std::string to hold the filename, you must use
46197403Sobrien       *  .c_str() before passing it to this constructor.
46297403Sobrien      */
46397403Sobrien      explicit
46497403Sobrien      basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in)
465132720Skan      : __istream_type(), _M_filebuf()
46697403Sobrien      {
46797403Sobrien	this->init(&_M_filebuf);
46897403Sobrien	this->open(__s, __mode);
46997403Sobrien      }
47097403Sobrien
471117397Skan      /**
472117397Skan       *  @brief  The destructor does nothing.
473117397Skan       *
474117397Skan       *  The file is closed by the filebuf object, not the formatting
475117397Skan       *  stream.
476117397Skan      */
47797403Sobrien      ~basic_ifstream()
47897403Sobrien      { }
47997403Sobrien
48097403Sobrien      // Members:
48197403Sobrien      /**
482117397Skan       *  @brief  Accessing the underlying buffer.
483117397Skan       *  @return  The current basic_filebuf buffer.
484117397Skan       *
485117397Skan       *  This hides both signatures of std::basic_ios::rdbuf().
48697403Sobrien      */
48797403Sobrien      __filebuf_type*
48897403Sobrien      rdbuf() const
48997403Sobrien      { return const_cast<__filebuf_type*>(&_M_filebuf); }
49097403Sobrien
491117397Skan      /**
492117397Skan       *  @brief  Wrapper to test for an open file.
493117397Skan       *  @return  @c rdbuf()->is_open()
494117397Skan      */
49597403Sobrien      bool
496169691Skan      is_open()
497169691Skan      { return _M_filebuf.is_open(); }
49897403Sobrien
499169691Skan      // _GLIBCXX_RESOLVE_LIB_DEFECTS
500169691Skan      // 365. Lack of const-qualification in clause 27
501169691Skan      bool
502169691Skan      is_open() const
503169691Skan      { return _M_filebuf.is_open(); }
504169691Skan
505117397Skan      /**
506117397Skan       *  @brief  Opens an external file.
507117397Skan       *  @param  s  The name of the file.
508117397Skan       *  @param  mode  The open mode flags.
509117397Skan       *
510117397Skan       *  Calls @c std::basic_filebuf::open(s,mode|in).  If that function
511117397Skan       *  fails, @c failbit is set in the stream's error state.
512117397Skan       *
513117397Skan       *  Tip:  When using std::string to hold the filename, you must use
514117397Skan       *  .c_str() before passing it to this constructor.
515117397Skan      */
51697403Sobrien      void
51797403Sobrien      open(const char* __s, ios_base::openmode __mode = ios_base::in)
51897403Sobrien      {
51997403Sobrien	if (!_M_filebuf.open(__s, __mode | ios_base::in))
52097403Sobrien	  this->setstate(ios_base::failbit);
521169691Skan	else
522169691Skan	  // _GLIBCXX_RESOLVE_LIB_DEFECTS
523169691Skan	  // 409. Closing an fstream should clear error state
524169691Skan	  this->clear();
52597403Sobrien      }
52697403Sobrien
527117397Skan      /**
528117397Skan       *  @brief  Close the file.
529117397Skan       *
530117397Skan       *  Calls @c std::basic_filebuf::close().  If that function
531117397Skan       *  fails, @c failbit is set in the stream's error state.
532117397Skan      */
53397403Sobrien      void
53497403Sobrien      close()
53597403Sobrien      {
53697403Sobrien	if (!_M_filebuf.close())
53797403Sobrien	  this->setstate(ios_base::failbit);
53897403Sobrien      }
53997403Sobrien    };
54097403Sobrien
54197403Sobrien
542117397Skan  // [27.8.1.8] Template class basic_ofstream
54397403Sobrien  /**
544117397Skan   *  @brief  Controlling output for files.
545117397Skan   *
546117397Skan   *  This class supports reading from named files, using the inherited
547117397Skan   *  functions from std::basic_ostream.  To control the associated
548117397Skan   *  sequence, an instance of std::basic_filebuf is used, which this page
549117397Skan   *  refers to as @c sb.
55097403Sobrien  */
55197403Sobrien  template<typename _CharT, typename _Traits>
55297403Sobrien    class basic_ofstream : public basic_ostream<_CharT,_Traits>
55397403Sobrien    {
55497403Sobrien    public:
55597403Sobrien      // Types:
55697403Sobrien      typedef _CharT 					char_type;
55797403Sobrien      typedef _Traits 					traits_type;
55897403Sobrien      typedef typename traits_type::int_type 		int_type;
55997403Sobrien      typedef typename traits_type::pos_type 		pos_type;
56097403Sobrien      typedef typename traits_type::off_type 		off_type;
56197403Sobrien
56297403Sobrien      // Non-standard types:
56397403Sobrien      typedef basic_filebuf<char_type, traits_type> 	__filebuf_type;
56497403Sobrien      typedef basic_ostream<char_type, traits_type>	__ostream_type;
56597403Sobrien
56697403Sobrien    private:
56797403Sobrien      __filebuf_type	_M_filebuf;
56897403Sobrien
56997403Sobrien    public:
57097403Sobrien      // Constructors:
571117397Skan      /**
572117397Skan       *  @brief  Default constructor.
573117397Skan       *
574117397Skan       *  Initializes @c sb using its default constructor, and passes
575117397Skan       *  @c &sb to the base class initializer.  Does not open any files
576117397Skan       *  (you haven't given it a filename to open).
577117397Skan      */
578132720Skan      basic_ofstream(): __ostream_type(), _M_filebuf()
57997403Sobrien      { this->init(&_M_filebuf); }
58097403Sobrien
58197403Sobrien      /**
582117397Skan       *  @brief  Create an output file stream.
583117397Skan       *  @param  s  Null terminated string specifying the filename.
58497403Sobrien       *  @param  mode  Open file in specified mode (see std::ios_base).
58597403Sobrien       *
586117397Skan       *  @c ios_base::out|ios_base::trunc is automatically included in
587117397Skan       *  @a mode.
588117397Skan       *
58997403Sobrien       *  Tip:  When using std::string to hold the filename, you must use
59097403Sobrien       *  .c_str() before passing it to this constructor.
59197403Sobrien      */
59297403Sobrien      explicit
59397403Sobrien      basic_ofstream(const char* __s,
59497403Sobrien		     ios_base::openmode __mode = ios_base::out|ios_base::trunc)
595132720Skan      : __ostream_type(), _M_filebuf()
59697403Sobrien      {
59797403Sobrien	this->init(&_M_filebuf);
59897403Sobrien	this->open(__s, __mode);
59997403Sobrien      }
60097403Sobrien
601117397Skan      /**
602117397Skan       *  @brief  The destructor does nothing.
603117397Skan       *
604117397Skan       *  The file is closed by the filebuf object, not the formatting
605117397Skan       *  stream.
606117397Skan      */
60797403Sobrien      ~basic_ofstream()
60897403Sobrien      { }
60997403Sobrien
61097403Sobrien      // Members:
61197403Sobrien      /**
612117397Skan       *  @brief  Accessing the underlying buffer.
613117397Skan       *  @return  The current basic_filebuf buffer.
614117397Skan       *
615117397Skan       *  This hides both signatures of std::basic_ios::rdbuf().
61697403Sobrien      */
61797403Sobrien      __filebuf_type*
61897403Sobrien      rdbuf() const
61997403Sobrien      { return const_cast<__filebuf_type*>(&_M_filebuf); }
62097403Sobrien
62197403Sobrien      /**
622117397Skan       *  @brief  Wrapper to test for an open file.
623117397Skan       *  @return  @c rdbuf()->is_open()
62497403Sobrien      */
62597403Sobrien      bool
626169691Skan      is_open()
627169691Skan      { return _M_filebuf.is_open(); }
62897403Sobrien
629169691Skan      // _GLIBCXX_RESOLVE_LIB_DEFECTS
630169691Skan      // 365. Lack of const-qualification in clause 27
631169691Skan      bool
632169691Skan      is_open() const
633169691Skan      { return _M_filebuf.is_open(); }
634169691Skan
63597403Sobrien      /**
636117397Skan       *  @brief  Opens an external file.
637117397Skan       *  @param  s  The name of the file.
638117397Skan       *  @param  mode  The open mode flags.
63997403Sobrien       *
640117397Skan       *  Calls @c std::basic_filebuf::open(s,mode|out|trunc).  If that
641117397Skan       *  function fails, @c failbit is set in the stream's error state.
642117397Skan       *
64397403Sobrien       *  Tip:  When using std::string to hold the filename, you must use
64497403Sobrien       *  .c_str() before passing it to this constructor.
64597403Sobrien      */
64697403Sobrien      void
64797403Sobrien      open(const char* __s,
64897403Sobrien	   ios_base::openmode __mode = ios_base::out | ios_base::trunc)
64997403Sobrien      {
65097403Sobrien	if (!_M_filebuf.open(__s, __mode | ios_base::out))
65197403Sobrien	  this->setstate(ios_base::failbit);
652169691Skan	else
653169691Skan	  // _GLIBCXX_RESOLVE_LIB_DEFECTS
654169691Skan	  // 409. Closing an fstream should clear error state
655169691Skan	  this->clear();
65697403Sobrien      }
65797403Sobrien
658117397Skan      /**
659117397Skan       *  @brief  Close the file.
660117397Skan       *
661117397Skan       *  Calls @c std::basic_filebuf::close().  If that function
662117397Skan       *  fails, @c failbit is set in the stream's error state.
663117397Skan      */
66497403Sobrien      void
66597403Sobrien      close()
66697403Sobrien      {
66797403Sobrien	if (!_M_filebuf.close())
66897403Sobrien	  this->setstate(ios_base::failbit);
66997403Sobrien      }
67097403Sobrien    };
67197403Sobrien
67297403Sobrien
673117397Skan  // [27.8.1.11] Template class basic_fstream
67497403Sobrien  /**
675117397Skan   *  @brief  Controlling intput and output for files.
676117397Skan   *
677117397Skan   *  This class supports reading from and writing to named files, using
678117397Skan   *  the inherited functions from std::basic_iostream.  To control the
679117397Skan   *  associated sequence, an instance of std::basic_filebuf is used, which
680117397Skan   *  this page refers to as @c sb.
68197403Sobrien  */
68297403Sobrien  template<typename _CharT, typename _Traits>
68397403Sobrien    class basic_fstream : public basic_iostream<_CharT, _Traits>
68497403Sobrien    {
68597403Sobrien    public:
68697403Sobrien      // Types:
68797403Sobrien      typedef _CharT 					char_type;
68897403Sobrien      typedef _Traits 					traits_type;
68997403Sobrien      typedef typename traits_type::int_type 		int_type;
69097403Sobrien      typedef typename traits_type::pos_type 		pos_type;
69197403Sobrien      typedef typename traits_type::off_type 		off_type;
69297403Sobrien
69397403Sobrien      // Non-standard types:
69497403Sobrien      typedef basic_filebuf<char_type, traits_type> 	__filebuf_type;
69597403Sobrien      typedef basic_ios<char_type, traits_type>		__ios_type;
69697403Sobrien      typedef basic_iostream<char_type, traits_type>	__iostream_type;
69797403Sobrien
69897403Sobrien    private:
69997403Sobrien      __filebuf_type	_M_filebuf;
70097403Sobrien
70197403Sobrien    public:
70297403Sobrien      // Constructors/destructor:
703117397Skan      /**
704117397Skan       *  @brief  Default constructor.
705117397Skan       *
706117397Skan       *  Initializes @c sb using its default constructor, and passes
707117397Skan       *  @c &sb to the base class initializer.  Does not open any files
708117397Skan       *  (you haven't given it a filename to open).
709117397Skan      */
71097403Sobrien      basic_fstream()
711132720Skan      : __iostream_type(), _M_filebuf()
71297403Sobrien      { this->init(&_M_filebuf); }
71397403Sobrien
71497403Sobrien      /**
715117397Skan       *  @brief  Create an input/output file stream.
716117397Skan       *  @param  s  Null terminated string specifying the filename.
71797403Sobrien       *  @param  mode  Open file in specified mode (see std::ios_base).
71897403Sobrien       *
71997403Sobrien       *  Tip:  When using std::string to hold the filename, you must use
72097403Sobrien       *  .c_str() before passing it to this constructor.
72197403Sobrien      */
72297403Sobrien      explicit
72397403Sobrien      basic_fstream(const char* __s,
72497403Sobrien		    ios_base::openmode __mode = ios_base::in | ios_base::out)
72597403Sobrien      : __iostream_type(NULL), _M_filebuf()
72697403Sobrien      {
72797403Sobrien	this->init(&_M_filebuf);
72897403Sobrien	this->open(__s, __mode);
72997403Sobrien      }
73097403Sobrien
731117397Skan      /**
732117397Skan       *  @brief  The destructor does nothing.
733117397Skan       *
734117397Skan       *  The file is closed by the filebuf object, not the formatting
735117397Skan       *  stream.
736117397Skan      */
73797403Sobrien      ~basic_fstream()
73897403Sobrien      { }
73997403Sobrien
74097403Sobrien      // Members:
74197403Sobrien      /**
742117397Skan       *  @brief  Accessing the underlying buffer.
743117397Skan       *  @return  The current basic_filebuf buffer.
744117397Skan       *
745117397Skan       *  This hides both signatures of std::basic_ios::rdbuf().
74697403Sobrien      */
74797403Sobrien      __filebuf_type*
74897403Sobrien      rdbuf() const
74997403Sobrien      { return const_cast<__filebuf_type*>(&_M_filebuf); }
75097403Sobrien
75197403Sobrien      /**
752117397Skan       *  @brief  Wrapper to test for an open file.
753117397Skan       *  @return  @c rdbuf()->is_open()
75497403Sobrien      */
75597403Sobrien      bool
756169691Skan      is_open()
757169691Skan      { return _M_filebuf.is_open(); }
75897403Sobrien
759169691Skan      // _GLIBCXX_RESOLVE_LIB_DEFECTS
760169691Skan      // 365. Lack of const-qualification in clause 27
761169691Skan      bool
762169691Skan      is_open() const
763169691Skan      { return _M_filebuf.is_open(); }
764169691Skan
76597403Sobrien      /**
766117397Skan       *  @brief  Opens an external file.
767117397Skan       *  @param  s  The name of the file.
768117397Skan       *  @param  mode  The open mode flags.
76997403Sobrien       *
770117397Skan       *  Calls @c std::basic_filebuf::open(s,mode).  If that
771117397Skan       *  function fails, @c failbit is set in the stream's error state.
772117397Skan       *
77397403Sobrien       *  Tip:  When using std::string to hold the filename, you must use
77497403Sobrien       *  .c_str() before passing it to this constructor.
77597403Sobrien      */
77697403Sobrien      void
77797403Sobrien      open(const char* __s,
77897403Sobrien	   ios_base::openmode __mode = ios_base::in | ios_base::out)
77997403Sobrien      {
78097403Sobrien	if (!_M_filebuf.open(__s, __mode))
781132720Skan	  this->setstate(ios_base::failbit);
782169691Skan	else
783169691Skan	  // _GLIBCXX_RESOLVE_LIB_DEFECTS
784169691Skan	  // 409. Closing an fstream should clear error state
785169691Skan	  this->clear();
78697403Sobrien      }
78797403Sobrien
788117397Skan      /**
789117397Skan       *  @brief  Close the file.
790117397Skan       *
791117397Skan       *  Calls @c std::basic_filebuf::close().  If that function
792117397Skan       *  fails, @c failbit is set in the stream's error state.
793117397Skan      */
79497403Sobrien      void
79597403Sobrien      close()
79697403Sobrien      {
79797403Sobrien	if (!_M_filebuf.close())
798132720Skan	  this->setstate(ios_base::failbit);
79997403Sobrien      }
80097403Sobrien    };
80197403Sobrien
802169691Skan_GLIBCXX_END_NAMESPACE
803169691Skan
804132720Skan#ifndef _GLIBCXX_EXPORT_TEMPLATE
80597403Sobrien# include <bits/fstream.tcc>
80697403Sobrien#endif
80797403Sobrien
808132720Skan#endif /* _GLIBCXX_FSTREAM */
809