1 /*
2  * Copyright (c) 1999
3  * Silicon Graphics Computer Systems, Inc.
4  *
5  * Copyright (c) 1999
6  * Boris Fomitchev
7  *
8  * This material is provided "as is", with absolutely no warranty expressed
9  * or implied. Any use is at your own risk.
10  *
11  * Permission to use or copy this software for any purpose is hereby granted
12  * without fee, provided the above notices are retained on all copies.
13  * Permission to modify the code and to distribute modified code is granted,
14  * provided the above notices are retained, and a notice that the code was
15  * modified is included with the above copyright notice.
16  *
17  */
18 // This header defines classes basic_filebuf, basic_ifstream,
19 // basic_ofstream, and basic_fstream.  These classes represent
20 // streambufs and streams whose sources or destinations are files.
21 
22 #ifndef _STLP_INTERNAL_FSTREAM_H
23 #define _STLP_INTERNAL_FSTREAM_H
24 
25 #if defined(__sgi) && !defined(__GNUC__) && !defined(_STANDARD_C_PLUS_PLUS)
26 #  error This header file requires the -LANG:std option
27 #endif
28 
29 #ifndef _STLP_INTERNAL_STREAMBUF
30 #  include <stl/_streambuf.h>
31 #endif
32 
33 #ifndef _STLP_INTERNAL_ISTREAM
34 #  include <stl/_istream.h>
35 #endif
36 
37 #ifndef _STLP_INTERNAL_CODECVT_H
38 #  include <stl/_codecvt.h>
39 #endif
40 
41 #if defined (_STLP_USE_WIN32_IO)
42 typedef void* _STLP_fd;
43 #elif defined (_STLP_USE_UNIX_EMULATION_IO) || defined (_STLP_USE_STDIO_IO) || defined (_STLP_USE_UNIX_IO)
44 typedef int _STLP_fd;
45 #else
46 #  error "Configure i/o !"
47 #endif
48 
49 _STLP_BEGIN_NAMESPACE
50 
51 //----------------------------------------------------------------------
52 // Class _Filebuf_base, a private base class to factor out the system-
53 // dependent code from basic_filebuf<>.
54 
55 class _STLP_CLASS_DECLSPEC _Filebuf_base {
56 public:                      // Opening and closing files.
57   _Filebuf_base();
58 
59   bool _M_open(const char*, ios_base::openmode, long __protection);
60   bool _M_open(const char*, ios_base::openmode);
61   bool _M_open(int __id, ios_base::openmode = ios_base::__default_mode);
62 #if defined (_STLP_USE_WIN32_IO)
63   bool _M_open(_STLP_fd __id, ios_base::openmode = ios_base::__default_mode);
64 #endif /* _STLP_USE_WIN32_IO */
65   bool _M_close();
66 
67 public:                      // Low-level I/O, like Unix read/write
68   ptrdiff_t _M_read(char* __buf,  ptrdiff_t __n);
69   streamoff _M_seek(streamoff __offset, ios_base::seekdir __dir);
70   streamoff _M_file_size();
71   bool _M_write(char* __buf,  ptrdiff_t __n);
72 
73 public:                      // Memory-mapped I/O.
74   void* _M_mmap(streamoff __offset, streamoff __len);
75   void _M_unmap(void* __mmap_base, streamoff __len);
76 
77 public:
78   // Returns a value n such that, if pos is the file pointer at the
79   // beginning of the range [first, last), pos + n is the file pointer at
80   // the end.  On many operating systems n == __last - __first.
81   // In Unix, writing n characters always bumps the file position by n.
82   // In Windows text mode, however, it bumps the file position by n + m,
83   // where m is the number of newlines in the range.  That's because an
84   // internal \n corresponds to an external two-character sequence.
_M_get_offset(char * __first,char * __last)85   streamoff _M_get_offset(char* __first, char* __last) {
86 #if defined (_STLP_UNIX) || defined (_STLP_MAC)
87     return __last - __first;
88 #else // defined (_STLP_WIN32)
89     return ( (_M_openmode & ios_base::binary) != 0 )
90       ? (__last - __first)
91       : count(__first, __last, '\n') + (__last - __first);
92 #endif
93   }
94 
95   // Returns true if we're in binary mode or if we're using an OS or file
96   // system where there is no distinction between text and binary mode.
_M_in_binary_mode()97   bool _M_in_binary_mode() const {
98 #if defined (_STLP_UNIX) || defined (_STLP_MAC) || defined(__BEOS__) || defined (__amigaos__)
99     return true;
100 #elif defined (_STLP_WIN32) || defined (_STLP_VM)
101     return (_M_openmode & ios_base::binary) != 0;
102 #else
103 #  error "Port!"
104 #endif
105   }
106 
107   static void _S_initialize();
108 
109 protected:                      // Static data members.
110   static size_t _M_page_size;
111 
112 protected:                      // Data members.
113   _STLP_fd _M_file_id;
114 #if defined (_STLP_USE_STDIO_IO)
115   // for stdio, the whole FILE* is being kept here
116   FILE* _M_file;
117 #endif
118   ios_base::openmode _M_openmode     ;
119   unsigned char      _M_is_open      ;
120   unsigned char      _M_should_close ;
121   unsigned char      _M_regular_file ;
122 
123 #if defined (_STLP_USE_WIN32_IO)
124   _STLP_fd _M_view_id;
125 #endif
126 
127 public :
__page_size()128   static size_t  _STLP_CALL __page_size() { return _M_page_size; }
__o_mode()129   int  __o_mode() const { return (int)_M_openmode; }
__is_open()130   bool __is_open()      const { return (_M_is_open !=0 ); }
__should_close()131   bool __should_close() const { return (_M_should_close != 0); }
__regular_file()132   bool __regular_file() const { return (_M_regular_file != 0); }
__get_fd()133   _STLP_fd __get_fd() const { return _M_file_id; }
134 };
135 
136 //----------------------------------------------------------------------
137 // Class basic_filebuf<>.
138 
139 // Forward declaration of two helper classes.
140 template <class _Traits> class _Noconv_input;
141 template <class _Traits> class _Noconv_output;
142 
143 // There is a specialized version of underflow, for basic_filebuf<char>,
144 // in fstream.cpp.
145 template <class _CharT, class _Traits>
146 class _Underflow;
147 
148 template <class _CharT, class _Traits>
149 class basic_filebuf : public basic_streambuf<_CharT, _Traits> {
150 public:                         // Types.
151   typedef _CharT                     char_type;
152   typedef typename _Traits::int_type int_type;
153   typedef typename _Traits::pos_type pos_type;
154   typedef typename _Traits::off_type off_type;
155   typedef _Traits                    traits_type;
156 
157   typedef typename _Traits::state_type _State_type;
158   typedef basic_streambuf<_CharT, _Traits> _Base;
159   typedef basic_filebuf<_CharT, _Traits> _Self;
160 
161 public:                         // Constructors, destructor.
162   basic_filebuf();
163   ~basic_filebuf();
164 
165 public:                         // Opening and closing files.
is_open()166   bool is_open() const { return _M_base.__is_open(); }
167 
open(const char * __s,ios_base::openmode __m)168   _Self* open(const char* __s, ios_base::openmode __m) {
169     return _M_base._M_open(__s, __m) ? this : 0;
170   }
171 
172 #if !defined (_STLP_NO_EXTENSIONS)
173   // These two version of open() and file descriptor getter are extensions.
open(const char * __s,ios_base::openmode __m,long __protection)174   _Self* open(const char* __s, ios_base::openmode __m,
175               long __protection) {
176     return _M_base._M_open(__s, __m, __protection) ? this : 0;
177   }
178 
fd()179   _STLP_fd fd() const { return _M_base.__get_fd(); }
180 
181   _Self* open(int __id, ios_base::openmode _Init_mode = ios_base::__default_mode) {
182     return this->_M_open(__id, _Init_mode);
183   }
184 
185 #  if defined (_STLP_USE_WIN32_IO)
186   _Self* open(_STLP_fd __id, ios_base::openmode _Init_mode = ios_base::__default_mode) {
187     return _M_base._M_open(__id, _Init_mode) ? this : 0;
188   }
189 #  endif /* _STLP_USE_WIN32_IO */
190 
191 #endif
192 
193   _Self* _M_open(int __id, ios_base::openmode _Init_mode = ios_base::__default_mode) {
194     return _M_base._M_open(__id, _Init_mode) ? this : 0;
195   }
196 
197   _Self* close();
198 
199 protected:                      // Virtual functions from basic_streambuf.
200   virtual streamsize showmanyc();
201   virtual int_type underflow();
202 
203   virtual int_type pbackfail(int_type = traits_type::eof());
204   virtual int_type overflow(int_type = traits_type::eof());
205 
206   virtual basic_streambuf<_CharT, _Traits>* setbuf(char_type*, streamsize);
207   virtual pos_type seekoff(off_type, ios_base::seekdir,
208                            ios_base::openmode = ios_base::in | ios_base::out);
209   virtual pos_type seekpos(pos_type,
210                            ios_base::openmode = ios_base::in | ios_base::out);
211 
212   virtual int sync();
213   virtual void imbue(const locale&);
214 
215 private:                        // Helper functions.
216 
217   // Precondition: we are currently in putback input mode.  Effect:
218   // switches back to ordinary input mode.
_M_exit_putback_mode()219   void _M_exit_putback_mode() {
220     this->setg(_M_saved_eback, _M_saved_gptr, _M_saved_egptr);
221     _M_in_putback_mode = false;
222   }
223   bool _M_switch_to_input_mode();
224   void _M_exit_input_mode();
225   bool _M_switch_to_output_mode();
226 
227   int_type _M_input_error();
228   int_type _M_underflow_aux();
229   friend class _Underflow<_CharT, _Traits>;
230 
231   int_type _M_output_error();
232   bool _M_unshift();
233 
234   bool _M_allocate_buffers(_CharT* __buf, streamsize __n);
235   bool _M_allocate_buffers();
236   void _M_deallocate_buffers();
237 
_M_seek_return(off_type __off,_State_type __state)238   pos_type _M_seek_return(off_type __off, _State_type __state) {
239     if (__off != -1) {
240       if (_M_in_input_mode)
241         _M_exit_input_mode();
242       _M_in_input_mode = false;
243       _M_in_output_mode = false;
244       _M_in_putback_mode = false;
245       _M_in_error_mode = false;
246       this->setg(0, 0, 0);
247       this->setp(0, 0);
248     }
249 
250     pos_type __result(__off);
251     __result.state(__state);
252     return __result;
253   }
254 
255   bool _M_seek_init(bool __do_unshift);
256 
257   void _M_setup_codecvt(const locale&, bool __on_imbue = true);
258 
259 private:                        // Data members used in all modes.
260 
261   _Filebuf_base _M_base;
262 
263 private:                        // Locale-related information.
264 
265   unsigned char _M_constant_width;
266   unsigned char _M_always_noconv;
267 
268   // private:                        // Mode flags.
269   unsigned char _M_int_buf_dynamic;  // True if internal buffer is heap allocated,
270   // false if it was supplied by the user.
271   unsigned char _M_in_input_mode;
272   unsigned char _M_in_output_mode;
273   unsigned char _M_in_error_mode;
274   unsigned char _M_in_putback_mode;
275 
276   // Internal buffer: characters seen by the filebuf's clients.
277   _CharT* _M_int_buf;
278   _CharT* _M_int_buf_EOS;
279 
280   // External buffer: characters corresponding to the external file.
281   char* _M_ext_buf;
282   char* _M_ext_buf_EOS;
283 
284   // The range [_M_ext_buf, _M_ext_buf_converted) contains the external
285   // characters corresponding to the sequence in the internal buffer.  The
286   // range [_M_ext_buf_converted, _M_ext_buf_end) contains characters that
287   // have been read into the external buffer but have not been converted
288   // to an internal sequence.
289   char* _M_ext_buf_converted;
290   char* _M_ext_buf_end;
291 
292   // State corresponding to beginning of internal buffer.
293   _State_type _M_state;
294 
295 private:                        // Data members used only in input mode.
296 
297   // Similar to _M_state except that it corresponds to
298   // the end of the internal buffer instead of the beginning.
299   _State_type _M_end_state;
300 
301   // This is a null pointer unless we are in mmap input mode.
302   void*     _M_mmap_base;
303   streamoff _M_mmap_len;
304 
305 private:                        // Data members used only in putback mode.
306   _CharT* _M_saved_eback;
307   _CharT* _M_saved_gptr;
308   _CharT* _M_saved_egptr;
309 
310   typedef codecvt<_CharT, char, _State_type> _Codecvt;
311   const _Codecvt* _M_codecvt;
312 
313   int _M_width;                 // Width of the encoding (if constant), else 1
314   int _M_max_width;             // Largest possible width of single character.
315 
316 
317   enum { _S_pback_buf_size = 8 };
318   _CharT _M_pback_buf[_S_pback_buf_size];
319 
320   // for _Noconv_output
321 public:
_M_write(char * __buf,ptrdiff_t __n)322   bool _M_write(char* __buf,  ptrdiff_t __n) {return _M_base._M_write(__buf, __n); }
323 
324 public:
325   int_type
_M_do_noconv_input()326   _M_do_noconv_input() {
327     _M_ext_buf_converted = _M_ext_buf_end;
328     /* this-> */ _Base::setg((char_type*)_M_ext_buf, (char_type*)_M_ext_buf, (char_type*)_M_ext_buf_end);
329     return traits_type::to_int_type(*_M_ext_buf);
330   }
331 };
332 
333 #if defined (_STLP_USE_TEMPLATE_EXPORT)
334 _STLP_EXPORT_TEMPLATE_CLASS basic_filebuf<char, char_traits<char> >;
335 #  if ! defined (_STLP_NO_WCHAR_T)
336 _STLP_EXPORT_TEMPLATE_CLASS basic_filebuf<wchar_t, char_traits<wchar_t> >;
337 #  endif
338 #endif /* _STLP_USE_TEMPLATE_EXPORT */
339 
340 //
341 // This class had to be designed very carefully to work
342 // with Visual C++.
343 //
344 template <class _Traits>
345 class _Noconv_output {
346 public:
347   typedef typename _Traits::char_type char_type;
_M_doit(basic_filebuf<char_type,_Traits> *,char_type *,char_type *)348   static bool  _STLP_CALL _M_doit(basic_filebuf<char_type, _Traits >*,
349                                   char_type*, char_type*)
350   { return false; }
351 };
352 
353 _STLP_TEMPLATE_NULL
354 class _STLP_CLASS_DECLSPEC _Noconv_output< char_traits<char> > {
355 public:
356   static bool  _STLP_CALL
_M_doit(basic_filebuf<char,char_traits<char>> * __buf,char * __first,char * __last)357   _M_doit(basic_filebuf<char, char_traits<char> >* __buf,
358           char* __first, char* __last) {
359     ptrdiff_t __n = __last - __first;
360     return (__buf->_M_write(__first, __n));
361   }
362 };
363 
364 //----------------------------------------------------------------------
365 // basic_filebuf<> helper functions.
366 
367 
368 //----------------------------------------
369 // Helper functions for switching between modes.
370 
371 //
372 // This class had to be designed very carefully to work
373 // with Visual C++.
374 //
375 template <class _Traits>
376 class _Noconv_input {
377 public:
378   typedef typename _Traits::int_type int_type;
379   typedef typename _Traits::char_type char_type;
380 
381   static inline int_type _STLP_CALL
_M_doit(basic_filebuf<char_type,_Traits> *)382   _M_doit(basic_filebuf<char_type, _Traits>*)
383   { return _Traits::eof(); }
384 };
385 
386 _STLP_TEMPLATE_NULL
387 class _Noconv_input<char_traits<char> > {
388 public:
389   static inline int _STLP_CALL
_M_doit(basic_filebuf<char,char_traits<char>> * __buf)390   _M_doit(basic_filebuf<char, char_traits<char> >* __buf) {
391     return __buf->_M_do_noconv_input();
392   }
393 };
394 
395 // underflow() may be called for one of two reasons.  (1) We've
396 // been going through the special putback buffer, and we need to move back
397 // to the regular internal buffer.  (2) We've exhausted the internal buffer,
398 // and we need to replentish it.
399 template <class _CharT, class _Traits>
400 class _Underflow {
401 public:
402   typedef typename _Traits::int_type int_type;
403   typedef _Traits                    traits_type;
404 
405   // There is a specialized version of underflow, for basic_filebuf<char>,
406   // in fstream.cpp.
_M_doit(basic_filebuf<_CharT,_Traits> * __this)407   static int_type _STLP_CALL _M_doit(basic_filebuf<_CharT, _Traits>* __this) {
408     if (!__this->_M_in_input_mode) {
409       if (!__this->_M_switch_to_input_mode())
410         return traits_type::eof();
411     }
412     else if (__this->_M_in_putback_mode) {
413       __this->_M_exit_putback_mode();
414       if (__this->gptr() != __this->egptr()) {
415         int_type __c = traits_type::to_int_type(*__this->gptr());
416         return __c;
417       }
418     }
419 
420     return __this->_M_underflow_aux();
421   }
422 };
423 
424 // Specialization of underflow: if the character type is char, maybe
425 // we can use mmap instead of read.
426 _STLP_TEMPLATE_NULL
427 class _STLP_CLASS_DECLSPEC _Underflow< char, char_traits<char> >
428 {
429   public:
430     typedef char_traits<char>::int_type int_type;
431     typedef char_traits<char> traits_type;
432     static int_type _STLP_CALL _M_doit(basic_filebuf<char, traits_type >* __this);
433 };
434 
435 #if defined (_STLP_USE_TEMPLATE_EXPORT) && !defined (_STLP_NO_WCHAR_T)
436 _STLP_EXPORT_TEMPLATE_CLASS _Underflow<wchar_t, char_traits<wchar_t> >;
437 #endif
438 
439 //----------------------------------------------------------------------
440 // Class basic_ifstream<>
441 
442 template <class _CharT, class _Traits>
443 class basic_ifstream : public basic_istream<_CharT, _Traits> {
444 public:                         // Types
445   typedef _CharT                     char_type;
446   typedef typename _Traits::int_type int_type;
447   typedef typename _Traits::pos_type pos_type;
448   typedef typename _Traits::off_type off_type;
449   typedef _Traits                    traits_type;
450 
451   typedef basic_ios<_CharT, _Traits>                _Basic_ios;
452   typedef basic_istream<_CharT, _Traits>            _Base;
453   typedef basic_filebuf<_CharT, _Traits>            _Buf;
454 
455 public:                         // Constructors, destructor.
456 
basic_ifstream()457   basic_ifstream() :
458     basic_ios<_CharT, _Traits>(),  basic_istream<_CharT, _Traits>(0), _M_buf() {
459       this->init(&_M_buf);
460   }
461 
462   explicit basic_ifstream(const char* __s, ios_base::openmode __mod = ios_base::in) :
463     basic_ios<_CharT, _Traits>(),  basic_istream<_CharT, _Traits>(0),
464     _M_buf() {
465       this->init(&_M_buf);
466       if (!_M_buf.open(__s, __mod | ios_base::in))
467         this->setstate(ios_base::failbit);
468   }
469 
470 #if !defined (_STLP_NO_EXTENSIONS)
471   explicit basic_ifstream(int __id, ios_base::openmode __mod = ios_base::in) :
472     basic_ios<_CharT, _Traits>(),  basic_istream<_CharT, _Traits>(0), _M_buf() {
473     this->init(&_M_buf);
474     if (!_M_buf.open(__id, __mod | ios_base::in))
475       this->setstate(ios_base::failbit);
476   }
basic_ifstream(const char * __s,ios_base::openmode __m,long __protection)477   basic_ifstream(const char* __s, ios_base::openmode __m,
478      long __protection) :
479     basic_ios<_CharT, _Traits>(),  basic_istream<_CharT, _Traits>(0), _M_buf() {
480     this->init(&_M_buf);
481     if (!_M_buf.open(__s, __m | ios_base::in, __protection))
482       this->setstate(ios_base::failbit);
483   }
484 
485 #  if defined (_STLP_USE_WIN32_IO)
486   explicit basic_ifstream(_STLP_fd __id, ios_base::openmode __mod = ios_base::in) :
487     basic_ios<_CharT, _Traits>(),  basic_istream<_CharT, _Traits>(0), _M_buf() {
488     this->init(&_M_buf);
489     if (!_M_buf.open(__id, __mod | ios_base::in))
490       this->setstate(ios_base::failbit);
491   }
492 #  endif /* _STLP_USE_WIN32_IO */
493 #endif
494 
~basic_ifstream()495   ~basic_ifstream() {}
496 
497 public:                         // File and buffer operations.
rdbuf()498   basic_filebuf<_CharT, _Traits>* rdbuf() const
499     { return __CONST_CAST(_Buf*,&_M_buf); }
500 
is_open()501   bool is_open() {
502     return this->rdbuf()->is_open();
503   }
504 
505   void open(const char* __s, ios_base::openmode __mod = ios_base::in) {
506     if (!this->rdbuf()->open(__s, __mod | ios_base::in))
507       this->setstate(ios_base::failbit);
508   }
509 
close()510   void close() {
511     if (!this->rdbuf()->close())
512       this->setstate(ios_base::failbit);
513   }
514 
515 private:
516   basic_filebuf<_CharT, _Traits> _M_buf;
517 };
518 
519 
520 //----------------------------------------------------------------------
521 // Class basic_ofstream<>
522 
523 template <class _CharT, class _Traits>
524 class basic_ofstream : public basic_ostream<_CharT, _Traits> {
525 public:                         // Types
526   typedef _CharT                     char_type;
527   typedef typename _Traits::int_type int_type;
528   typedef typename _Traits::pos_type pos_type;
529   typedef typename _Traits::off_type off_type;
530   typedef _Traits                    traits_type;
531 
532   typedef basic_ios<_CharT, _Traits>                _Basic_ios;
533   typedef basic_ostream<_CharT, _Traits>            _Base;
534   typedef basic_filebuf<_CharT, _Traits>            _Buf;
535 
536 public:                         // Constructors, destructor.
basic_ofstream()537   basic_ofstream() :
538     basic_ios<_CharT, _Traits>(),
539     basic_ostream<_CharT, _Traits>(0), _M_buf() {
540       this->init(&_M_buf);
541   }
542   explicit basic_ofstream(const char* __s, ios_base::openmode __mod = ios_base::out)
543     : basic_ios<_CharT, _Traits>(), basic_ostream<_CharT, _Traits>(0), _M_buf() {
544     this->init(&_M_buf);
545     if (!_M_buf.open(__s, __mod | ios_base::out))
546       this->setstate(ios_base::failbit);
547   }
548 
549 #if !defined (_STLP_NO_EXTENSIONS)
550   explicit basic_ofstream(int __id, ios_base::openmode __mod = ios_base::out)
551     : basic_ios<_CharT, _Traits>(), basic_ostream<_CharT, _Traits>(0),
552     _M_buf() {
553    this->init(&_M_buf);
554    if (!_M_buf.open(__id, __mod | ios_base::out))
555      this->setstate(ios_base::failbit);
556   }
basic_ofstream(const char * __s,ios_base::openmode __m,long __protection)557   basic_ofstream(const char* __s, ios_base::openmode __m, long __protection) :
558     basic_ios<_CharT, _Traits>(),  basic_ostream<_CharT, _Traits>(0), _M_buf() {
559     this->init(&_M_buf);
560     if (!_M_buf.open(__s, __m | ios_base::out, __protection))
561       this->setstate(ios_base::failbit);
562   }
563 #  if defined (_STLP_USE_WIN32_IO)
564   explicit basic_ofstream(_STLP_fd __id, ios_base::openmode __mod = ios_base::out)
565     : basic_ios<_CharT, _Traits>(), basic_ostream<_CharT, _Traits>(0),
566     _M_buf() {
567    this->init(&_M_buf);
568    if (!_M_buf.open(__id, __mod | ios_base::out))
569      this->setstate(ios_base::failbit);
570   }
571 #  endif /* _STLP_USE_WIN32_IO */
572 #endif
573 
~basic_ofstream()574   ~basic_ofstream() {}
575 
576 public:                         // File and buffer operations.
rdbuf()577   basic_filebuf<_CharT, _Traits>* rdbuf() const
578     { return __CONST_CAST(_Buf*,&_M_buf); }
579 
is_open()580   bool is_open() {
581     return this->rdbuf()->is_open();
582   }
583 
584   void open(const char* __s, ios_base::openmode __mod= ios_base::out) {
585     if (!this->rdbuf()->open(__s, __mod | ios_base::out))
586       this->setstate(ios_base::failbit);
587   }
588 
close()589   void close() {
590     if (!this->rdbuf()->close())
591       this->setstate(ios_base::failbit);
592   }
593 
594 private:
595   basic_filebuf<_CharT, _Traits> _M_buf;
596 };
597 
598 
599 //----------------------------------------------------------------------
600 // Class basic_fstream<>
601 
602 template <class _CharT, class _Traits>
603 class basic_fstream : public basic_iostream<_CharT, _Traits> {
604 public:                         // Types
605   typedef _CharT                     char_type;
606   typedef typename _Traits::int_type int_type;
607   typedef typename _Traits::pos_type pos_type;
608   typedef typename _Traits::off_type off_type;
609   typedef _Traits                    traits_type;
610 
611   typedef basic_ios<_CharT, _Traits>                _Basic_ios;
612   typedef basic_iostream<_CharT, _Traits>           _Base;
613   typedef basic_filebuf<_CharT, _Traits>            _Buf;
614 
615 public:                         // Constructors, destructor.
616 
basic_fstream()617   basic_fstream()
618     : basic_ios<_CharT, _Traits>(), basic_iostream<_CharT, _Traits>(0), _M_buf() {
619       this->init(&_M_buf);
620   }
621 
622   explicit basic_fstream(const char* __s,
623                          ios_base::openmode __mod = ios_base::in | ios_base::out) :
624     basic_ios<_CharT, _Traits>(), basic_iostream<_CharT, _Traits>(0), _M_buf() {
625       this->init(&_M_buf);
626       if (!_M_buf.open(__s, __mod))
627         this->setstate(ios_base::failbit);
628   }
629 
630 #if !defined (_STLP_NO_EXTENSIONS)
631   explicit basic_fstream(int __id,
632                          ios_base::openmode __mod = ios_base::in | ios_base::out) :
633     basic_ios<_CharT, _Traits>(), basic_iostream<_CharT, _Traits>(0), _M_buf() {
634     this->init(&_M_buf);
635     if (!_M_buf.open(__id, __mod))
636       this->setstate(ios_base::failbit);
637   }
basic_fstream(const char * __s,ios_base::openmode __m,long __protection)638   basic_fstream(const char* __s, ios_base::openmode __m, long __protection) :
639     basic_ios<_CharT, _Traits>(),  basic_iostream<_CharT, _Traits>(0), _M_buf() {
640     this->init(&_M_buf);
641     if (!_M_buf.open(__s, __m, __protection))
642       this->setstate(ios_base::failbit);
643   }
644 #  if defined (_STLP_USE_WIN32_IO)
645   explicit basic_fstream(_STLP_fd __id,
646     ios_base::openmode __mod = ios_base::in | ios_base::out) :
647     basic_ios<_CharT, _Traits>(),  basic_iostream<_CharT, _Traits>(0), _M_buf() {
648     this->init(&_M_buf);
649     if (!_M_buf.open(__id, __mod))
650       this->setstate(ios_base::failbit);
651   }
652 #  endif /* _STLP_USE_WIN32_IO */
653 #endif
~basic_fstream()654   ~basic_fstream() {}
655 
656 public:                         // File and buffer operations.
657 
rdbuf()658   basic_filebuf<_CharT, _Traits>* rdbuf() const
659     { return __CONST_CAST(_Buf*,&_M_buf); }
660 
is_open()661   bool is_open() {
662     return this->rdbuf()->is_open();
663   }
664 
665   void open(const char* __s,
666       ios_base::openmode __mod =
667       ios_base::in | ios_base::out) {
668     if (!this->rdbuf()->open(__s, __mod))
669       this->setstate(ios_base::failbit);
670   }
671 
close()672   void close() {
673     if (!this->rdbuf()->close())
674       this->setstate(ios_base::failbit);
675   }
676 
677 private:
678   basic_filebuf<_CharT, _Traits> _M_buf;
679 
680 #if defined (_STLP_MSVC) && (_STLP_MSVC >= 1300 && _STLP_MSVC <= 1310)
681   typedef basic_fstream<_CharT, _Traits> _Self;
682   //explicitely defined as private to avoid warnings:
683   basic_fstream(_Self const&);
684   _Self& operator = (_Self const&);
685 #endif
686 };
687 
688 _STLP_END_NAMESPACE
689 
690 #if defined (_STLP_EXPOSE_STREAM_IMPLEMENTATION) && !defined (_STLP_LINK_TIME_INSTANTIATION)
691 #  include <stl/_fstream.c>
692 #endif
693 
694 _STLP_BEGIN_NAMESPACE
695 
696 #if defined (_STLP_USE_TEMPLATE_EXPORT)
697 _STLP_EXPORT_TEMPLATE_CLASS basic_ifstream<char, char_traits<char> >;
698 _STLP_EXPORT_TEMPLATE_CLASS basic_ofstream<char, char_traits<char> >;
699 _STLP_EXPORT_TEMPLATE_CLASS basic_fstream<char, char_traits<char> >;
700 #  if ! defined (_STLP_NO_WCHAR_T)
701 _STLP_EXPORT_TEMPLATE_CLASS basic_ifstream<wchar_t, char_traits<wchar_t> >;
702 _STLP_EXPORT_TEMPLATE_CLASS basic_ofstream<wchar_t, char_traits<wchar_t> >;
703 _STLP_EXPORT_TEMPLATE_CLASS basic_fstream<wchar_t, char_traits<wchar_t> >;
704 #  endif
705 #endif /* _STLP_USE_TEMPLATE_EXPORT */
706 
707 _STLP_END_NAMESPACE
708 
709 #endif /* _STLP_FSTREAM */
710 
711 
712 // Local Variables:
713 // mode:C++
714 // End:
715