3 * Silicon Graphics Computer Systems, Inc.
8 * This material is provided "as is", with absolutely no warranty expressed
9 * or implied. Any use is at your own risk.
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.
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.
22 #ifndef _STLP_INTERNAL_FSTREAM_H
23 #define _STLP_INTERNAL_FSTREAM_H
25 #if defined(__sgi) && !defined(__GNUC__) && !defined(_STANDARD_C_PLUS_PLUS)
26 # error This header file requires the -LANG:std option
29 #ifndef _STLP_INTERNAL_STREAMBUF
30 # include <stl/_streambuf.h>
33 #ifndef _STLP_INTERNAL_ISTREAM
34 # include <stl/_istream.h>
37 #ifndef _STLP_INTERNAL_CODECVT_H
38 # include <stl/_codecvt.h>
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)
46 # error "Configure i/o !"
51 //----------------------------------------------------------------------
52 // Class _Filebuf_base, a private base class to factor out the system-
53 // dependent code from basic_filebuf<>.
55 class _STLP_CLASS_DECLSPEC _Filebuf_base {
56 public: // Opening and closing files.
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 */
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);
73 public: // Memory-mapped I/O.
74 void* _M_mmap(streamoff __offset, streamoff __len);
75 void _M_unmap(void* __mmap_base, streamoff __len);
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.
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 )
91 : count(__first, __last, '\n') + (__last - __first);
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.
97 bool _M_in_binary_mode() const {
98 #if defined (_STLP_UNIX) || defined (_STLP_MAC) || defined(__BEOS__) || defined (__amigaos__)
100 #elif defined (_STLP_WIN32) || defined (_STLP_VM)
101 return (_M_openmode & ios_base::binary) != 0;
107 static void _S_initialize();
109 protected: // Static data members.
110 static size_t _M_page_size;
112 protected: // Data members.
114 #if defined (_STLP_USE_STDIO_IO)
115 // for stdio, the whole FILE* is being kept here
118 ios_base::openmode _M_openmode ;
119 unsigned char _M_is_open ;
120 unsigned char _M_should_close ;
121 unsigned char _M_regular_file ;
123 #if defined (_STLP_USE_WIN32_IO)
128 static size_t _STLP_CALL __page_size() { return _M_page_size; }
129 int __o_mode() const { return (int)_M_openmode; }
130 bool __is_open() const { return (_M_is_open !=0 ); }
131 bool __should_close() const { return (_M_should_close != 0); }
132 bool __regular_file() const { return (_M_regular_file != 0); }
133 _STLP_fd __get_fd() const { return _M_file_id; }
136 //----------------------------------------------------------------------
137 // Class basic_filebuf<>.
139 // Forward declaration of two helper classes.
140 template <class _Traits> class _Noconv_input;
141 template <class _Traits> class _Noconv_output;
143 // There is a specialized version of underflow, for basic_filebuf<char>,
145 template <class _CharT, class _Traits>
148 template <class _CharT, class _Traits>
149 class basic_filebuf : public basic_streambuf<_CharT, _Traits> {
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;
157 typedef typename _Traits::state_type _State_type;
158 typedef basic_streambuf<_CharT, _Traits> _Base;
159 typedef basic_filebuf<_CharT, _Traits> _Self;
161 public: // Constructors, destructor.
165 public: // Opening and closing files.
166 bool is_open() const { return _M_base.__is_open(); }
168 _Self* open(const char* __s, ios_base::openmode __m) {
169 return _M_base._M_open(__s, __m) ? this : 0;
172 #if !defined (_STLP_NO_EXTENSIONS)
173 // These two version of open() and file descriptor getter are extensions.
174 _Self* open(const char* __s, ios_base::openmode __m,
176 return _M_base._M_open(__s, __m, __protection) ? this : 0;
179 _STLP_fd fd() const { return _M_base.__get_fd(); }
181 _Self* open(int __id, ios_base::openmode _Init_mode = ios_base::__default_mode) {
182 return this->_M_open(__id, _Init_mode);
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;
189 # endif /* _STLP_USE_WIN32_IO */
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;
199 protected: // Virtual functions from basic_streambuf.
200 virtual streamsize showmanyc();
201 virtual int_type underflow();
203 virtual int_type pbackfail(int_type = traits_type::eof());
204 virtual int_type overflow(int_type = traits_type::eof());
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);
213 virtual void imbue(const locale&);
215 private: // Helper functions.
217 // Precondition: we are currently in putback input mode. Effect:
218 // switches back to ordinary input 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;
223 bool _M_switch_to_input_mode();
224 void _M_exit_input_mode();
225 bool _M_switch_to_output_mode();
227 int_type _M_input_error();
228 int_type _M_underflow_aux();
229 friend class _Underflow<_CharT, _Traits>;
231 int_type _M_output_error();
234 bool _M_allocate_buffers(_CharT* __buf, streamsize __n);
235 bool _M_allocate_buffers();
236 void _M_deallocate_buffers();
238 pos_type _M_seek_return(off_type __off, _State_type __state) {
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;
250 pos_type __result(__off);
251 __result.state(__state);
255 bool _M_seek_init(bool __do_unshift);
257 void _M_setup_codecvt(const locale&, bool __on_imbue = true);
259 private: // Data members used in all modes.
261 _Filebuf_base _M_base;
263 private: // Locale-related information.
265 unsigned char _M_constant_width;
266 unsigned char _M_always_noconv;
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;
276 // Internal buffer: characters seen by the filebuf's clients.
278 _CharT* _M_int_buf_EOS;
280 // External buffer: characters corresponding to the external file.
282 char* _M_ext_buf_EOS;
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;
292 // State corresponding to beginning of internal buffer.
293 _State_type _M_state;
295 private: // Data members used only in input mode.
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;
301 // This is a null pointer unless we are in mmap input mode.
303 streamoff _M_mmap_len;
305 private: // Data members used only in putback mode.
306 _CharT* _M_saved_eback;
307 _CharT* _M_saved_gptr;
308 _CharT* _M_saved_egptr;
310 typedef codecvt<_CharT, char, _State_type> _Codecvt;
311 const _Codecvt* _M_codecvt;
313 int _M_width; // Width of the encoding (if constant), else 1
314 int _M_max_width; // Largest possible width of single character.
317 enum { _S_pback_buf_size = 8 };
318 _CharT _M_pback_buf[_S_pback_buf_size];
320 // for _Noconv_output
322 bool _M_write(char* __buf, ptrdiff_t __n) {return _M_base._M_write(__buf, __n); }
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);
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> >;
338 #endif /* _STLP_USE_TEMPLATE_EXPORT */
341 // This class had to be designed very carefully to work
344 template <class _Traits>
345 class _Noconv_output {
347 typedef typename _Traits::char_type char_type;
348 static bool _STLP_CALL _M_doit(basic_filebuf<char_type, _Traits >*,
349 char_type*, char_type*)
354 class _STLP_CLASS_DECLSPEC _Noconv_output< char_traits<char> > {
356 static bool _STLP_CALL
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));
364 //----------------------------------------------------------------------
365 // basic_filebuf<> helper functions.
368 //----------------------------------------
369 // Helper functions for switching between modes.
372 // This class had to be designed very carefully to work
375 template <class _Traits>
376 class _Noconv_input {
378 typedef typename _Traits::int_type int_type;
379 typedef typename _Traits::char_type char_type;
381 static inline int_type _STLP_CALL
382 _M_doit(basic_filebuf<char_type, _Traits>*)
383 { return _Traits::eof(); }
387 class _Noconv_input<char_traits<char> > {
389 static inline int _STLP_CALL
390 _M_doit(basic_filebuf<char, char_traits<char> >* __buf) {
391 return __buf->_M_do_noconv_input();
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>
402 typedef typename _Traits::int_type int_type;
403 typedef _Traits traits_type;
405 // There is a specialized version of underflow, for basic_filebuf<char>,
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();
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());
420 return __this->_M_underflow_aux();
424 // Specialization of underflow: if the character type is char, maybe
425 // we can use mmap instead of read.
427 class _STLP_CLASS_DECLSPEC _Underflow< char, char_traits<char> >
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);
435 #if defined (_STLP_USE_TEMPLATE_EXPORT) && !defined (_STLP_NO_WCHAR_T)
436 _STLP_EXPORT_TEMPLATE_CLASS _Underflow<wchar_t, char_traits<wchar_t> >;
439 //----------------------------------------------------------------------
440 // Class basic_ifstream<>
442 template <class _CharT, class _Traits>
443 class basic_ifstream : public basic_istream<_CharT, _Traits> {
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;
451 typedef basic_ios<_CharT, _Traits> _Basic_ios;
452 typedef basic_istream<_CharT, _Traits> _Base;
453 typedef basic_filebuf<_CharT, _Traits> _Buf;
455 public: // Constructors, destructor.
458 basic_ios<_CharT, _Traits>(), basic_istream<_CharT, _Traits>(0), _M_buf() {
462 explicit basic_ifstream(const char* __s, ios_base::openmode __mod = ios_base::in) :
463 basic_ios<_CharT, _Traits>(), basic_istream<_CharT, _Traits>(0),
466 if (!_M_buf.open(__s, __mod | ios_base::in))
467 this->setstate(ios_base::failbit);
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() {
474 if (!_M_buf.open(__id, __mod | ios_base::in))
475 this->setstate(ios_base::failbit);
477 basic_ifstream(const char* __s, ios_base::openmode __m,
479 basic_ios<_CharT, _Traits>(), basic_istream<_CharT, _Traits>(0), _M_buf() {
481 if (!_M_buf.open(__s, __m | ios_base::in, __protection))
482 this->setstate(ios_base::failbit);
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() {
489 if (!_M_buf.open(__id, __mod | ios_base::in))
490 this->setstate(ios_base::failbit);
492 # endif /* _STLP_USE_WIN32_IO */
497 public: // File and buffer operations.
498 basic_filebuf<_CharT, _Traits>* rdbuf() const
499 { return __CONST_CAST(_Buf*,&_M_buf); }
502 return this->rdbuf()->is_open();
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);
511 if (!this->rdbuf()->close())
512 this->setstate(ios_base::failbit);
516 basic_filebuf<_CharT, _Traits> _M_buf;
520 //----------------------------------------------------------------------
521 // Class basic_ofstream<>
523 template <class _CharT, class _Traits>
524 class basic_ofstream : public basic_ostream<_CharT, _Traits> {
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;
532 typedef basic_ios<_CharT, _Traits> _Basic_ios;
533 typedef basic_ostream<_CharT, _Traits> _Base;
534 typedef basic_filebuf<_CharT, _Traits> _Buf;
536 public: // Constructors, destructor.
538 basic_ios<_CharT, _Traits>(),
539 basic_ostream<_CharT, _Traits>(0), _M_buf() {
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() {
545 if (!_M_buf.open(__s, __mod | ios_base::out))
546 this->setstate(ios_base::failbit);
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),
554 if (!_M_buf.open(__id, __mod | ios_base::out))
555 this->setstate(ios_base::failbit);
557 basic_ofstream(const char* __s, ios_base::openmode __m, long __protection) :
558 basic_ios<_CharT, _Traits>(), basic_ostream<_CharT, _Traits>(0), _M_buf() {
560 if (!_M_buf.open(__s, __m | ios_base::out, __protection))
561 this->setstate(ios_base::failbit);
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),
568 if (!_M_buf.open(__id, __mod | ios_base::out))
569 this->setstate(ios_base::failbit);
571 # endif /* _STLP_USE_WIN32_IO */
576 public: // File and buffer operations.
577 basic_filebuf<_CharT, _Traits>* rdbuf() const
578 { return __CONST_CAST(_Buf*,&_M_buf); }
581 return this->rdbuf()->is_open();
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);
590 if (!this->rdbuf()->close())
591 this->setstate(ios_base::failbit);
595 basic_filebuf<_CharT, _Traits> _M_buf;
599 //----------------------------------------------------------------------
600 // Class basic_fstream<>
602 template <class _CharT, class _Traits>
603 class basic_fstream : public basic_iostream<_CharT, _Traits> {
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;
611 typedef basic_ios<_CharT, _Traits> _Basic_ios;
612 typedef basic_iostream<_CharT, _Traits> _Base;
613 typedef basic_filebuf<_CharT, _Traits> _Buf;
615 public: // Constructors, destructor.
618 : basic_ios<_CharT, _Traits>(), basic_iostream<_CharT, _Traits>(0), _M_buf() {
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() {
626 if (!_M_buf.open(__s, __mod))
627 this->setstate(ios_base::failbit);
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() {
635 if (!_M_buf.open(__id, __mod))
636 this->setstate(ios_base::failbit);
638 basic_fstream(const char* __s, ios_base::openmode __m, long __protection) :
639 basic_ios<_CharT, _Traits>(), basic_iostream<_CharT, _Traits>(0), _M_buf() {
641 if (!_M_buf.open(__s, __m, __protection))
642 this->setstate(ios_base::failbit);
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() {
649 if (!_M_buf.open(__id, __mod))
650 this->setstate(ios_base::failbit);
652 # endif /* _STLP_USE_WIN32_IO */
656 public: // File and buffer operations.
658 basic_filebuf<_CharT, _Traits>* rdbuf() const
659 { return __CONST_CAST(_Buf*,&_M_buf); }
662 return this->rdbuf()->is_open();
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);
673 if (!this->rdbuf()->close())
674 this->setstate(ios_base::failbit);
678 basic_filebuf<_CharT, _Traits> _M_buf;
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&);
690 #if defined (_STLP_EXPOSE_STREAM_IMPLEMENTATION) && !defined (_STLP_LINK_TIME_INSTANTIATION)
691 # include <stl/_fstream.c>
694 _STLP_BEGIN_NAMESPACE
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> >;
705 #endif /* _STLP_USE_TEMPLATE_EXPORT */
709 #endif /* _STLP_FSTREAM */