_fstream.h

00001 /*
00002  * Copyright (c) 1999
00003  * Silicon Graphics Computer Systems, Inc.
00004  *
00005  * Copyright (c) 1999 
00006  * Boris Fomitchev
00007  *
00008  * This material is provided "as is", with absolutely no warranty expressed
00009  * or implied. Any use is at your own risk.
00010  *
00011  * Permission to use or copy this software for any purpose is hereby granted 
00012  * without fee, provided the above notices are retained on all copies.
00013  * Permission to modify the code and to distribute modified code is granted,
00014  * provided the above notices are retained, and a notice that the code was
00015  * modified is included with the above copyright notice.
00016  *
00017  */ 
00018 // This header defines classes basic_filebuf, basic_ifstream,
00019 // basic_ofstream, and basic_fstream.  These classes represent
00020 // streambufs and streams whose sources or destinations are files.
00021 
00022 #ifndef _STLP_INTERNAL_FSTREAM_H
00023 #define _STLP_INTERNAL_FSTREAM_H
00024 
00025 #if defined(__sgi) && !defined(__GNUC__) && !defined(_STANDARD_C_PLUS_PLUS)
00026 #error This header file requires the -LANG:std option
00027 #endif
00028 
00029 #ifndef _STLP_INTERNAL_STREAMBUF
00030 # include <stl/_streambuf.h>
00031 #endif
00032 
00033 #ifndef _STLP_INTERNAL_ISTREAM_H
00034 #include <stl/_istream.h>
00035 #endif
00036 
00037 #ifndef _STLP_INTERNAL_CODECVT_H
00038 #include <stl/_codecvt.h>
00039 #endif
00040 
00041 #ifndef _STLP_STDIO_FILE_H
00042 #include <stl/_stdio_file.h>
00043 #endif
00044 
00045 #if !defined (_STLP_USE_UNIX_IO) && !defined(_STLP_USE_WIN32_IO) \
00046     && ! defined (_STLP_USE_UNIX_EMULATION_IO) && !defined (_STLP_USE_STDIO_IO)
00047 
00048 # if defined (_STLP_UNIX)  || defined (__CYGWIN__) 
00049 // open/close/read/write
00050 #  define _STLP_USE_UNIX_IO
00051 # elif defined (_STLP_WIN32)  && ! defined (__CYGWIN__)
00052 // CreateFile/ReadFile/WriteFile
00053 #  define _STLP_USE_WIN32_IO
00054 # elif defined (_STLP_WIN16) || defined (_STLP_WIN32) || defined (_STLP_MAC)
00055 // _open/_read/_write
00056 #  define _STLP_USE_UNIX_EMULATION_IO
00057 # else
00058 // fopen/fread/fwrite
00059 #  define _STLP_USE_STDIO_IO
00060 # endif /* _STLP_UNIX */
00061 
00062 #endif /* mode selection */
00063 
00064 
00065 #if defined (_STLP_USE_WIN32_IO)
00066 typedef void* _STLP_fd;
00067 #elif defined (_STLP_USE_UNIX_EMULATION_IO) || defined (_STLP_USE_STDIO_IO) || defined (_STLP_USE_UNIX_IO)
00068 typedef int _STLP_fd;
00069 #else
00070 #error "Configure i/o !"
00071 #endif
00072 
00073 
00074 _STLP_BEGIN_NAMESPACE
00075 
00076 //----------------------------------------------------------------------
00077 // Class _Filebuf_base, a private base class to factor out the system-
00078 // dependent code from basic_filebuf<>.
00079 
00080 class _STLP_CLASS_DECLSPEC _Filebuf_base {
00081 public:                      // Opening and closing files.
00082   _Filebuf_base();
00083 
00084   bool _M_open(const char*, ios_base::openmode, long __protection);
00085   bool _M_open(const char*, ios_base::openmode);
00086   bool _M_open(int __id, ios_base::openmode = ios_base::__default_mode);
00087   bool _M_close();
00088 
00089 public:                      // Low-level I/O, like Unix read/write
00090   ptrdiff_t _M_read(char* __buf,  ptrdiff_t __n);
00091   streamoff _M_seek(streamoff __offset, ios_base::seekdir __dir);
00092   streamoff _M_file_size();
00093   bool _M_write(char* __buf,  ptrdiff_t __n);
00094 
00095 public:                      // Memory-mapped I/O.
00096   void* _M_mmap(streamoff __offset, streamoff __len);
00097   void _M_unmap(void* __mmap_base, streamoff __len);
00098 
00099 public:
00100   // Returns a value n such that, if pos is the file pointer at the
00101   // beginning of the range [first, last), pos + n is the file pointer at
00102   // the end.  On many operating systems n == __last - __first.
00103   // In Unix, writing n characters always bumps the file position by n.
00104   // In Windows text mode, however, it bumps the file position by n + m,
00105   // where m is the number of newlines in the range.  That's because an
00106   // internal \n corresponds to an external two-character sequence.
00107   streamoff _M_get_offset(char* __first, char* __last) {
00108 #if defined (_STLP_UNIX) || defined (_STLP_MAC)
00109     return __last - __first;
00110 #else // defined (_STLP_WIN32) || defined (_STLP_WIN16) || defined (_STLP_DOS)
00111     return ( (_M_openmode & ios_base::binary) != 0 )
00112       ? (__last - __first)
00113       : count(__first, __last, '\n') + (__last - __first);
00114 #endif
00115   }
00116 
00117   // Returns true if we're in binary mode or if we're using an OS or file 
00118   // system where there is no distinction between text and binary mode.
00119   bool _M_in_binary_mode() const {
00120 # if defined (_STLP_UNIX) || defined (_STLP_MAC)  || defined(__BEOS__) || defined(__SYMBIAN32__) 
00121     return true;
00122 # elif defined (_STLP_WIN32) || defined (_STLP_WIN16) || defined (_STLP_DOS) || defined (_STLP_VM)
00123     return (_M_openmode & ios_base::binary) != 0;
00124 # else 
00125 #   error "Port!"
00126 # endif
00127   }
00128 
00129 protected:                      // Static data members.
00130   static size_t _M_page_size;
00131 
00132 protected:                      // Data members.
00133   _STLP_fd _M_file_id;
00134 # ifdef _STLP_USE_STDIO_IO
00135   // for stdio, the whole FILE* is being kept here
00136   FILE* _M_file;
00137 # endif
00138 # ifdef _STLP_USE_WIN32_IO
00139   void* _M_view_id; 
00140 # endif
00141 
00142   ios_base::openmode _M_openmode     ;
00143   unsigned char      _M_is_open      ;
00144   unsigned char      _M_should_close ;
00145   unsigned char      _M_regular_file ;
00146 
00147 public :
00148   static size_t  _STLP_CALL __page_size() { return _M_page_size; } 
00149   int  __o_mode() const { return (int)_M_openmode; } 
00150   bool __is_open()      const { return (_M_is_open !=0 ); } 
00151   bool __should_close() const { return (_M_should_close != 0); } 
00152   bool __regular_file() const { return (_M_regular_file != 0); }
00153   _STLP_fd __get_fd() const { return _M_file_id; }
00154 };
00155 
00156 
00157 
00158 
00159 //----------------------------------------------------------------------
00160 // Class basic_filebuf<>.
00161 
00162 // Forward declaration of two helper classes.
00163 template <class _Traits> class _Noconv_input;
00164 _STLP_TEMPLATE_NULL
00165 class _Noconv_input<char_traits<char> >;
00166 
00167 template <class _Traits> class _Noconv_output;
00168 _STLP_TEMPLATE_NULL
00169 class _Noconv_output< char_traits<char> >;
00170 
00171 // There is a specialized version of underflow, for basic_filebuf<char>,
00172 // in fstream.cxx.
00173 
00174 template <class _CharT, class _Traits>
00175 class _Underflow;
00176 
00177  _STLP_TEMPLATE_NULL class _Underflow< char, char_traits<char> >;
00178 
00179 template <class _CharT, class _Traits>
00180 class basic_filebuf : public basic_streambuf<_CharT, _Traits>
00181 {
00182 public:                         // Types.
00183   typedef _CharT                     char_type;
00184   typedef typename _Traits::int_type int_type;
00185   typedef typename _Traits::pos_type pos_type;
00186   typedef typename _Traits::off_type off_type;
00187   typedef _Traits                    traits_type;
00188 
00189   typedef typename _Traits::state_type _State_type;
00190   typedef basic_streambuf<_CharT, _Traits> _Base;
00191   typedef basic_filebuf<_CharT, _Traits> _Self;
00192 
00193 public:                         // Constructors, destructor.
00194   basic_filebuf();  
00195   ~basic_filebuf();
00196   
00197 public:                         // Opening and closing files.
00198   bool is_open() const { return _M_base.__is_open(); }
00199 
00200   _Self* open(const char* __s, ios_base::openmode __m) {
00201     return _M_base._M_open(__s, __m) ? this : 0;
00202   }
00203 
00204 # ifndef _STLP_NO_EXTENSIONS
00205   // These two version of open() and file descriptor getter are extensions.
00206   _Self* open(const char* __s, ios_base::openmode __m,
00207                       long __protection) {
00208     return _M_base._M_open(__s, __m, __protection) ? this : 0;
00209   }
00210   
00211   _STLP_fd fd() const { return _M_base.__get_fd(); }
00212 
00213   _Self* open(int __id, ios_base::openmode _Init_mode = ios_base::__default_mode) {
00214     return this->_M_open(__id, _Init_mode);
00215   }
00216 # endif
00217 
00218   _Self* _M_open(int __id, ios_base::openmode _Init_mode = ios_base::__default_mode) {
00219     return _M_base._M_open(__id, _Init_mode) ? this : 0;
00220   }
00221 
00222   _Self* close();
00223 
00224 protected:                      // Virtual functions from basic_streambuf.
00225   virtual streamsize showmanyc();
00226   virtual int_type underflow();
00227 
00228   virtual int_type pbackfail(int_type = traits_type::eof());
00229   virtual int_type overflow(int_type = traits_type::eof());
00230 
00231   virtual basic_streambuf<_CharT, _Traits>* setbuf(char_type*, streamsize);
00232   virtual pos_type seekoff(off_type, ios_base::seekdir,
00233                            ios_base::openmode = ios_base::in | ios_base::out);
00234   virtual pos_type seekpos(pos_type,
00235                            ios_base::openmode = ios_base::in | ios_base::out);
00236 
00237   virtual int sync();
00238   virtual void imbue(const locale&);
00239 
00240 private:                        // Helper functions.
00241 
00242   // Precondition: we are currently in putback input mode.  Effect:
00243   // switches back to ordinary input mode.
00244   void _M_exit_putback_mode() {
00245     this->setg(_M_saved_eback, _M_saved_gptr, _M_saved_egptr);
00246     _M_in_putback_mode = false;
00247   }
00248   bool _M_switch_to_input_mode();
00249   void _M_exit_input_mode();
00250   bool _M_switch_to_output_mode();
00251 
00252   int_type _M_input_error();
00253   int_type _M_underflow_aux();
00254   //  friend class _Noconv_input<_Traits>;
00255   //  friend class _Noconv_output<_Traits>;
00256   friend class _Underflow<_CharT, _Traits>;
00257 
00258   int_type _M_output_error();
00259   bool _M_unshift();
00260 
00261   bool _M_allocate_buffers(_CharT* __buf, streamsize __n);
00262   bool _M_allocate_buffers();
00263   void _M_deallocate_buffers();
00264 
00265   pos_type _M_seek_return(off_type __off, _State_type __state) {
00266     if (__off != -1) {
00267       if (_M_in_input_mode)
00268         _M_exit_input_mode();
00269       _M_in_input_mode = false;
00270       _M_in_output_mode = false;
00271       _M_in_putback_mode = false;
00272       _M_in_error_mode = false;
00273       this->setg(0, 0, 0);
00274       this->setp(0, 0);
00275     }
00276     
00277     pos_type __result(__off);
00278     __result.state(__state);
00279     return __result;
00280   }
00281   
00282   bool _M_seek_init(bool __do_unshift);
00283 
00284   void _M_setup_codecvt(const locale&);
00285 
00286 private:                        // Data members used in all modes.
00287 
00288   _Filebuf_base _M_base;
00289 
00290 private:                        // Locale-related information.
00291 
00292   unsigned char _M_constant_width;
00293   unsigned char _M_always_noconv;
00294   
00295   // private:                        // Mode flags.
00296   unsigned char _M_int_buf_dynamic;  // True if internal buffer is heap allocated,
00297   // false if it was supplied by the user.
00298   unsigned char _M_in_input_mode;
00299   unsigned char _M_in_output_mode;
00300   unsigned char _M_in_error_mode;
00301   unsigned char _M_in_putback_mode;
00302   
00303   // Internal buffer: characters seen by the filebuf's clients.
00304   _CharT* _M_int_buf;
00305   _CharT* _M_int_buf_EOS;
00306   
00307   // External buffer: characters corresponding to the external file.
00308   char* _M_ext_buf;
00309   char* _M_ext_buf_EOS;
00310 
00311   // The range [_M_ext_buf, _M_ext_buf_converted) contains the external
00312   // characters corresponding to the sequence in the internal buffer.  The
00313   // range [_M_ext_buf_converted, _M_ext_buf_end) contains characters that
00314   // have been read into the external buffer but have not been converted
00315   // to an internal sequence.
00316   char* _M_ext_buf_converted;
00317   char* _M_ext_buf_end;
00318 
00319   // State corresponding to beginning of internal buffer.
00320   _State_type _M_state;
00321 
00322 private:                        // Data members used only in input mode.
00323 
00324   // Similar to _M_state except that it corresponds to
00325   // the end of the internal buffer instead of the beginning.
00326   _State_type _M_end_state;
00327 
00328   // This is a null pointer unless we are in mmap input mode.
00329   void*     _M_mmap_base;
00330   streamoff _M_mmap_len;
00331 
00332 private:                        // Data members used only in putback mode.
00333   _CharT* _M_saved_eback;
00334   _CharT* _M_saved_gptr;
00335   _CharT* _M_saved_egptr;
00336 
00337   typedef codecvt<_CharT, char, _State_type> _Codecvt;
00338   const _Codecvt* _M_codecvt;
00339 
00340   int _M_width;                 // Width of the encoding (if constant), else 1
00341   int _M_max_width;             // Largest possible width of single character.
00342 
00343 
00344   enum { _S_pback_buf_size = 8 };
00345   _CharT _M_pback_buf[_S_pback_buf_size];
00346 
00347   // for _Noconv_output
00348 public:
00349   bool _M_write(char* __buf,  ptrdiff_t __n) {return _M_base._M_write(__buf, __n); }
00350 
00351 public:
00352   int_type
00353   _M_do_noconv_input() {
00354     _M_ext_buf_converted = _M_ext_buf_end;
00355     this->setg((char_type*)_M_ext_buf, (char_type*)_M_ext_buf, (char_type*)_M_ext_buf_end);
00356     return traits_type::to_int_type(*_M_ext_buf);
00357   }
00358 };
00359 
00360 # if defined (_STLP_USE_TEMPLATE_EXPORT)
00361 _STLP_EXPORT_TEMPLATE_CLASS basic_filebuf<char, char_traits<char> >;
00362 #  if ! defined (_STLP_NO_WCHAR_T)
00363 _STLP_EXPORT_TEMPLATE_CLASS basic_filebuf<wchar_t, char_traits<wchar_t> >;
00364 #  endif
00365 # endif /* _STLP_USE_TEMPLATE_EXPORT */
00366 
00367 // public:
00368 // helper class.
00369 template <class _CharT>
00370 struct _Filebuf_Tmp_Buf
00371 {
00372   _CharT* _M_ptr;
00373   _Filebuf_Tmp_Buf(ptrdiff_t __n) : _M_ptr(0) { _M_ptr = new _CharT[__n]; }
00374   ~_Filebuf_Tmp_Buf() { delete[] _M_ptr; }
00375 };
00376 
00377 
00378 //
00379 // This class had to be designed very carefully to work
00380 // with Visual C++.
00381 //
00382 template <class _Traits>
00383 class _Noconv_output {
00384 public:
00385   typedef typename _Traits::char_type char_type;
00386   static bool  _STLP_CALL _M_doit(basic_filebuf<char_type, _Traits >*, 
00387                                   char_type*, char_type*)
00388   {
00389       return false; 
00390   }
00391 };
00392 
00393 _STLP_TEMPLATE_NULL
00394 class _STLP_CLASS_DECLSPEC _Noconv_output< char_traits<char> > {
00395 public:
00396   static bool  _STLP_CALL
00397   _M_doit(basic_filebuf<char, char_traits<char> >* __buf, 
00398           char* __first, char* __last)
00399   {
00400     ptrdiff_t __n = __last - __first;
00401     if (__buf->_M_write(__first, __n)) {
00402       return true;
00403     }
00404     else
00405       return false; 
00406   }
00407 };
00408 
00409 //----------------------------------------------------------------------
00410 // basic_filebuf<> helper functions.
00411 
00412 
00413 //----------------------------------------
00414 // Helper functions for switching between modes.
00415 
00416 //
00417 // This class had to be designed very carefully to work
00418 // with Visual C++.
00419 //
00420 template <class _Traits>
00421 class _Noconv_input {
00422 public:
00423   typedef typename _Traits::int_type int_type;
00424   typedef typename _Traits::char_type char_type;
00425 
00426   static inline int_type _STLP_CALL
00427   _M_doit(basic_filebuf<char_type, _Traits>*) 
00428   {
00429     return 0;
00430   }
00431 };
00432 
00433 _STLP_TEMPLATE_NULL
00434 class _Noconv_input<char_traits<char> > {
00435 public:
00436   static inline int _STLP_CALL
00437   _M_doit(basic_filebuf<char, char_traits<char> >* __buf)  {
00438     return __buf->_M_do_noconv_input();
00439   }
00440 };
00441 
00442 // underflow() may be called for one of two reasons.  (1) We've
00443 // been going through the special putback buffer, and we need to move back
00444 // to the regular internal buffer.  (2) We've exhausted the internal buffer,
00445 // and we need to replentish it.  
00446 template <class _CharT, class _Traits>
00447 class _Underflow {
00448 public:
00449   typedef typename _Traits::int_type int_type;
00450   typedef _Traits                    traits_type;
00451   
00452   static int_type _STLP_CALL _M_doit(basic_filebuf<_CharT, _Traits>* __this);
00453 };
00454 
00455 
00456 // Specialization of underflow: if the character type is char, maybe
00457 // we can use mmap instead of read.
00458 _STLP_TEMPLATE_NULL
00459 class _STLP_CLASS_DECLSPEC _Underflow< char, char_traits<char> > {
00460 public:
00461   typedef char_traits<char>::int_type int_type;
00462   typedef char_traits<char> traits_type;
00463   static  int _STLP_CALL _M_doit(basic_filebuf<char, traits_type >* __this);
00464 };
00465 
00466 // There is a specialized version of underflow, for basic_filebuf<char>,
00467 // in fstream.cxx.
00468 
00469 template <class _CharT, class _Traits>
00470 _STLP_TYPENAME_ON_RETURN_TYPE _Underflow<_CharT, _Traits>::int_type // _STLP_CALL
00471  _Underflow<_CharT, _Traits>::_M_doit(basic_filebuf<_CharT, _Traits>* __this)
00472 {
00473   if (!__this->_M_in_input_mode) {
00474     if (!__this->_M_switch_to_input_mode())
00475       return traits_type::eof();
00476   }
00477   
00478   else if (__this->_M_in_putback_mode) {
00479     __this->_M_exit_putback_mode();
00480     if (__this->gptr() != __this->egptr()) {
00481       int_type __c = traits_type::to_int_type(*__this->gptr());
00482       return __c;
00483     }
00484   }
00485   
00486   return __this->_M_underflow_aux();
00487 }
00488 
00489 #if defined( _STLP_USE_TEMPLATE_EXPORT ) && ! defined (_STLP_NO_WCHAR_T)
00490 _STLP_EXPORT_TEMPLATE_CLASS _Underflow<wchar_t, char_traits<wchar_t> >;
00491 #endif
00492 
00493 
00494 //----------------------------------------------------------------------
00495 // Class basic_ifstream<>
00496 
00497 template <class _CharT, class _Traits>
00498 class basic_ifstream : public basic_istream<_CharT, _Traits>
00499 {
00500 public:                         // Types
00501   typedef _CharT                     char_type;
00502   typedef typename _Traits::int_type int_type;
00503   typedef typename _Traits::pos_type pos_type;
00504   typedef typename _Traits::off_type off_type;
00505   typedef _Traits                    traits_type;
00506 
00507   typedef basic_ios<_CharT, _Traits>                _Basic_ios;
00508   typedef basic_istream<_CharT, _Traits>            _Base;
00509   typedef basic_filebuf<_CharT, _Traits>            _Buf;
00510 
00511 public:                         // Constructors, destructor.
00512 
00513   basic_ifstream() : 
00514     basic_ios<_CharT, _Traits>(),  basic_istream<_CharT, _Traits>(0), _M_buf() {
00515       this->init(&_M_buf);
00516   }
00517 
00518   explicit basic_ifstream(const char* __s, ios_base::openmode __mod = ios_base::in) : 
00519     basic_ios<_CharT, _Traits>(),  basic_istream<_CharT, _Traits>(0),
00520     _M_buf() {
00521       this->init(&_M_buf);
00522       if (!_M_buf.open(__s, __mod | ios_base::in))
00523         this->setstate(ios_base::failbit);
00524   }
00525 
00526 # ifndef _STLP_NO_EXTENSIONS
00527   explicit basic_ifstream(int __id, ios_base::openmode __mod = ios_base::in) : 
00528     basic_ios<_CharT, _Traits>(),  basic_istream<_CharT, _Traits>(0), _M_buf() {
00529     this->init(&_M_buf);
00530     if (!_M_buf.open(__id, __mod | ios_base::in))
00531       this->setstate(ios_base::failbit);
00532   }
00533   basic_ifstream(const char* __s, ios_base::openmode __m,
00534                  long __protection) : 
00535     basic_ios<_CharT, _Traits>(),  basic_istream<_CharT, _Traits>(0), _M_buf() {
00536     this->init(&_M_buf);
00537     if (!_M_buf.open(__s, __m | ios_base::in, __protection))
00538       this->setstate(ios_base::failbit);  
00539   }
00540   
00541 # endif
00542 
00543   ~basic_ifstream() {}
00544 
00545 public:                         // File and buffer operations.
00546   basic_filebuf<_CharT, _Traits>* rdbuf() const
00547     { return __CONST_CAST(_Buf*,&_M_buf); }
00548 
00549   bool is_open() {
00550     return this->rdbuf()->is_open();
00551   }
00552 
00553   void open(const char* __s, ios_base::openmode __mod = ios_base::in) {
00554     if (!this->rdbuf()->open(__s, __mod | ios_base::in))
00555       this->setstate(ios_base::failbit);
00556   }
00557 
00558   void close() {
00559     if (!this->rdbuf()->close())
00560       this->setstate(ios_base::failbit);
00561   }
00562 
00563 
00564 private:
00565   basic_filebuf<_CharT, _Traits> _M_buf;
00566 };
00567 
00568 
00569 //----------------------------------------------------------------------
00570 // Class basic_ofstream<>
00571 
00572 template <class _CharT, class _Traits>
00573 class basic_ofstream : public basic_ostream<_CharT, _Traits>
00574 {
00575 public:                         // Types
00576   typedef _CharT                     char_type;
00577   typedef typename _Traits::int_type int_type;
00578   typedef typename _Traits::pos_type pos_type;
00579   typedef typename _Traits::off_type off_type;
00580   typedef _Traits                    traits_type;
00581 
00582   typedef basic_ios<_CharT, _Traits>                _Basic_ios;
00583   typedef basic_ostream<_CharT, _Traits>            _Base;
00584   typedef basic_filebuf<_CharT, _Traits>            _Buf;
00585 
00586 public:                         // Constructors, destructor.
00587   basic_ofstream() : 
00588     basic_ios<_CharT, _Traits>(), 
00589     basic_ostream<_CharT, _Traits>(0), _M_buf() {
00590       this->init(&_M_buf);
00591   }
00592   explicit basic_ofstream(const char* __s, ios_base::openmode __mod = ios_base::out) 
00593     : basic_ios<_CharT, _Traits>(), basic_ostream<_CharT, _Traits>(0),
00594       _M_buf() {
00595         this->init(&_M_buf);
00596         if (!_M_buf.open(__s, __mod | ios_base::out))
00597           this->setstate(ios_base::failbit);
00598   }
00599 
00600 # ifndef _STLP_NO_EXTENSIONS
00601   explicit basic_ofstream(int __id, ios_base::openmode __mod = ios_base::out) 
00602     : basic_ios<_CharT, _Traits>(), basic_ostream<_CharT, _Traits>(0),
00603     _M_buf() {
00604         this->init(&_M_buf);
00605         if (!_M_buf.open(__id, __mod | ios_base::out))
00606           this->setstate(ios_base::failbit);
00607   }
00608   basic_ofstream(const char* __s, ios_base::openmode __m, long __protection) : 
00609     basic_ios<_CharT, _Traits>(),  basic_ostream<_CharT, _Traits>(0), _M_buf() {
00610     this->init(&_M_buf);
00611     if (!_M_buf.open(__s, __m | ios_base::out, __protection))
00612       this->setstate(ios_base::failbit);  
00613   }
00614 # endif
00615   
00616   ~basic_ofstream() {}
00617 
00618 public:                         // File and buffer operations.
00619   basic_filebuf<_CharT, _Traits>* rdbuf() const
00620     { return __CONST_CAST(_Buf*,&_M_buf); } 
00621 
00622   bool is_open() {
00623     return this->rdbuf()->is_open();
00624   }
00625 
00626   void open(const char* __s, ios_base::openmode __mod= ios_base::out) {
00627     if (!this->rdbuf()->open(__s, __mod | ios_base::out))
00628       this->setstate(ios_base::failbit);
00629   }
00630 
00631   void close() {
00632     if (!this->rdbuf()->close())
00633       this->setstate(ios_base::failbit);
00634   }
00635 
00636 private:
00637   basic_filebuf<_CharT, _Traits> _M_buf;
00638 };
00639 
00640 
00641 //----------------------------------------------------------------------
00642 // Class basic_fstream<>
00643 
00644 template <class _CharT, class _Traits>
00645 class basic_fstream : public basic_iostream<_CharT, _Traits>
00646 {
00647 public:                         // Types
00648   typedef _CharT                     char_type;
00649   typedef typename _Traits::int_type int_type;
00650   typedef typename _Traits::pos_type pos_type;
00651   typedef typename _Traits::off_type off_type;
00652   typedef _Traits                    traits_type;
00653 
00654   typedef basic_ios<_CharT, _Traits>                _Basic_ios;
00655   typedef basic_iostream<_CharT, _Traits>           _Base;
00656   typedef basic_filebuf<_CharT, _Traits>            _Buf;
00657 
00658 public:                         // Constructors, destructor.
00659   
00660   basic_fstream()   
00661     : basic_ios<_CharT, _Traits>(), basic_iostream<_CharT, _Traits>(0), _M_buf() {
00662       this->init(&_M_buf);
00663   }
00664 
00665   explicit basic_fstream(const char* __s,
00666                          ios_base::openmode __mod = ios_base::in | ios_base::out) :
00667     basic_ios<_CharT, _Traits>(), basic_iostream<_CharT, _Traits>(0), _M_buf() {
00668       this->init(&_M_buf);
00669       if (!_M_buf.open(__s, __mod))
00670         this->setstate(ios_base::failbit);
00671   }
00672 
00673 # ifndef _STLP_NO_EXTENSIONS
00674   explicit basic_fstream(int __id,
00675                          ios_base::openmode __mod = ios_base::in | ios_base::out) :
00676     basic_ios<_CharT, _Traits>(), basic_iostream<_CharT, _Traits>(0), _M_buf() {
00677     this->init(&_M_buf);
00678     if (!_M_buf.open(__id, __mod))
00679       this->setstate(ios_base::failbit);
00680   }
00681   basic_fstream(const char* __s, ios_base::openmode __m, long __protection) : 
00682     basic_ios<_CharT, _Traits>(),  basic_iostream<_CharT, _Traits>(0), _M_buf() {
00683     this->init(&_M_buf);
00684     if (!_M_buf.open(__s, __m, __protection))
00685       this->setstate(ios_base::failbit);  
00686   }
00687 # endif    
00688   ~basic_fstream() {}
00689 
00690 public:                         // File and buffer operations.
00691 
00692   basic_filebuf<_CharT, _Traits>* rdbuf() const
00693     { return __CONST_CAST(_Buf*,&_M_buf); } 
00694 
00695   bool is_open() {
00696     return this->rdbuf()->is_open();
00697   }
00698 
00699   void open(const char* __s, 
00700             ios_base::openmode __mod = 
00701             ios_base::in | ios_base::out) {
00702     if (!this->rdbuf()->open(__s, __mod))
00703       this->setstate(ios_base::failbit);
00704   }
00705 
00706   void close() {
00707     if (!this->rdbuf()->close())
00708       this->setstate(ios_base::failbit);
00709   }
00710 
00711 private:
00712   basic_filebuf<_CharT, _Traits> _M_buf;
00713 };
00714 
00715 _STLP_END_NAMESPACE
00716 
00717 # if !defined (_STLP_LINK_TIME_INSTANTIATION)
00718 #  include <stl/_fstream.c>
00719 # endif
00720 
00721 _STLP_BEGIN_NAMESPACE
00722 
00723 # if defined (_STLP_USE_TEMPLATE_EXPORT)
00724 _STLP_EXPORT_TEMPLATE_CLASS basic_ifstream<char, char_traits<char> >;
00725 _STLP_EXPORT_TEMPLATE_CLASS basic_ofstream<char, char_traits<char> >;
00726 _STLP_EXPORT_TEMPLATE_CLASS basic_fstream<char, char_traits<char> >;
00727 #  if ! defined (_STLP_NO_WCHAR_T)
00728 _STLP_EXPORT_TEMPLATE_CLASS basic_ifstream<wchar_t, char_traits<wchar_t> >;
00729 _STLP_EXPORT_TEMPLATE_CLASS basic_ofstream<wchar_t, char_traits<wchar_t> >;
00730 _STLP_EXPORT_TEMPLATE_CLASS basic_fstream<wchar_t, char_traits<wchar_t> >;
00731 #  endif
00732 # endif /* _STLP_USE_TEMPLATE_EXPORT */
00733 
00734 _STLP_END_NAMESPACE
00735 
00736 #endif /* _STLP_FSTREAM */
00737 
00738 
00739 // Local Variables:
00740 // mode:C++
00741 // End:

Generated on Mon Jun 5 10:20:45 2006 for Intelligence.kdevelop by  doxygen 1.4.6