/home/ntakagi/work/STLport-5.1.5/stlport/stl/_fstream.h

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



Generated on Mon Mar 10 15:32:22 2008 by  doxygen 1.5.1