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