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_H
34
#include <stl/_istream.h>
37
#ifndef _STLP_INTERNAL_CODECVT_H
38
#include <stl/_codecvt.h>
41
#ifndef _STLP_STDIO_FILE_H
42
#include <stl/_stdio_file.h>
45
#if !defined (_STLP_USE_UNIX_IO) && !defined(_STLP_USE_WIN32_IO) \
46
&& ! defined (_STLP_USE_UNIX_EMULATION_IO) && !defined (_STLP_USE_STDIO_IO)
48
# if defined (_STLP_UNIX) || defined (__CYGWIN__) || defined (__amigaos__) || defined (__EMX__)
49
// open/close/read/write
50
# define _STLP_USE_UNIX_IO
51
# elif defined (_STLP_WIN32) && ! defined (__CYGWIN__)
52
// CreateFile/ReadFile/WriteFile
53
# define _STLP_USE_WIN32_IO
54
# elif defined (_STLP_WIN16) || defined (_STLP_WIN32) || defined (_STLP_MAC)
56
# define _STLP_USE_UNIX_EMULATION_IO
59
# define _STLP_USE_STDIO_IO
60
# endif /* _STLP_UNIX */
62
#endif /* mode selection */
65
#if defined (_STLP_USE_WIN32_IO)
66
typedef void* _STLP_fd;
67
#elif defined (_STLP_USE_UNIX_EMULATION_IO) || defined (_STLP_USE_STDIO_IO) || defined (_STLP_USE_UNIX_IO)
70
#error "Configure i/o !"
76
//----------------------------------------------------------------------
77
// Class _Filebuf_base, a private base class to factor out the system-
78
// dependent code from basic_filebuf<>.
80
class _STLP_CLASS_DECLSPEC _Filebuf_base {
81
public: // Opening and closing files.
84
bool _M_open(const char*, ios_base::openmode, long __protection);
85
bool _M_open(const char*, ios_base::openmode);
86
bool _M_open(int __id, ios_base::openmode = ios_base::__default_mode);
89
public: // Low-level I/O, like Unix read/write
90
ptrdiff_t _M_read(char* __buf, ptrdiff_t __n);
91
streamoff _M_seek(streamoff __offset, ios_base::seekdir __dir);
92
streamoff _M_file_size();
93
bool _M_write(char* __buf, ptrdiff_t __n);
95
public: // Memory-mapped I/O.
96
void* _M_mmap(streamoff __offset, streamoff __len);
97
void _M_unmap(void* __mmap_base, streamoff __len);
100
// Returns a value n such that, if pos is the file pointer at the
101
// beginning of the range [first, last), pos + n is the file pointer at
102
// the end. On many operating systems n == __last - __first.
103
// In Unix, writing n characters always bumps the file position by n.
104
// In Windows text mode, however, it bumps the file position by n + m,
105
// where m is the number of newlines in the range. That's because an
106
// internal \n corresponds to an external two-character sequence.
107
streamoff _M_get_offset(char* __first, char* __last) {
108
#if defined (_STLP_UNIX) || defined (_STLP_MAC)
109
return __last - __first;
110
#else // defined (_STLP_WIN32) || defined (_STLP_WIN16) || defined (_STLP_DOS)
111
return ( (_M_openmode & ios_base::binary) != 0 )
113
: count(__first, __last, '\n') + (__last - __first);
117
// Returns true if we're in binary mode or if we're using an OS or file
118
// system where there is no distinction between text and binary mode.
119
bool _M_in_binary_mode() const {
120
# if defined (_STLP_UNIX) || defined (_STLP_MAC) || defined(__BEOS__) || defined (__amigaos__)
122
# elif defined (_STLP_WIN32) || defined (_STLP_WIN16) || defined (_STLP_DOS) || defined (_STLP_VM) || defined (__EMX__)
123
return (_M_openmode & ios_base::binary) != 0;
129
protected: // Static data members.
130
static size_t _M_page_size;
132
protected: // Data members.
134
# ifdef _STLP_USE_STDIO_IO
135
// for stdio, the whole FILE* is being kept here
138
# ifdef _STLP_USE_WIN32_IO
142
ios_base::openmode _M_openmode ;
143
unsigned char _M_is_open ;
144
unsigned char _M_should_close ;
145
unsigned char _M_regular_file ;
148
static size_t _STLP_CALL __page_size() { return _M_page_size; }
149
int __o_mode() const { return (int)_M_openmode; }
150
bool __is_open() const { return (_M_is_open !=0 ); }
151
bool __should_close() const { return (_M_should_close != 0); }
152
bool __regular_file() const { return (_M_regular_file != 0); }
153
_STLP_fd __get_fd() const { return _M_file_id; }
159
//----------------------------------------------------------------------
160
// Class basic_filebuf<>.
162
// Forward declaration of two helper classes.
163
template <class _Traits> class _Noconv_input;
165
class _Noconv_input<char_traits<char> >;
167
template <class _Traits> class _Noconv_output;
169
class _Noconv_output< char_traits<char> >;
171
// There is a specialized version of underflow, for basic_filebuf<char>,
174
template <class _CharT, class _Traits>
177
_STLP_TEMPLATE_NULL class _Underflow< char, char_traits<char> >;
179
template <class _CharT, class _Traits>
180
class basic_filebuf : public basic_streambuf<_CharT, _Traits>
183
typedef _CharT char_type;
184
typedef typename _Traits::int_type int_type;
185
typedef typename _Traits::pos_type pos_type;
186
typedef typename _Traits::off_type off_type;
187
typedef _Traits traits_type;
189
typedef typename _Traits::state_type _State_type;
190
typedef basic_streambuf<_CharT, _Traits> _Base;
191
typedef basic_filebuf<_CharT, _Traits> _Self;
193
public: // Constructors, destructor.
197
public: // Opening and closing files.
198
bool is_open() const { return _M_base.__is_open(); }
200
_Self* open(const char* __s, ios_base::openmode __m) {
201
return _M_base._M_open(__s, __m) ? this : 0;
204
# ifndef _STLP_NO_EXTENSIONS
205
// These two version of open() and file descriptor getter are extensions.
206
_Self* open(const char* __s, ios_base::openmode __m,
208
return _M_base._M_open(__s, __m, __protection) ? this : 0;
211
_STLP_fd fd() const { return _M_base.__get_fd(); }
213
_Self* open(int __id, ios_base::openmode _Init_mode = ios_base::__default_mode) {
214
return this->_M_open(__id, _Init_mode);
218
_Self* _M_open(int __id, ios_base::openmode _Init_mode = ios_base::__default_mode) {
219
return _M_base._M_open(__id, _Init_mode) ? this : 0;
224
protected: // Virtual functions from basic_streambuf.
225
virtual streamsize showmanyc();
226
virtual int_type underflow();
228
virtual int_type pbackfail(int_type = traits_type::eof());
229
virtual int_type overflow(int_type = traits_type::eof());
231
virtual basic_streambuf<_CharT, _Traits>* setbuf(char_type*, streamsize);
232
virtual pos_type seekoff(off_type, ios_base::seekdir,
233
ios_base::openmode = ios_base::in | ios_base::out);
234
virtual pos_type seekpos(pos_type,
235
ios_base::openmode = ios_base::in | ios_base::out);
238
virtual void imbue(const locale&);
240
private: // Helper functions.
242
// Precondition: we are currently in putback input mode. Effect:
243
// switches back to ordinary input mode.
244
void _M_exit_putback_mode() {
245
this->setg(_M_saved_eback, _M_saved_gptr, _M_saved_egptr);
246
_M_in_putback_mode = false;
248
bool _M_switch_to_input_mode();
249
void _M_exit_input_mode();
250
bool _M_switch_to_output_mode();
252
int_type _M_input_error();
253
int_type _M_underflow_aux();
254
// friend class _Noconv_input<_Traits>;
255
// friend class _Noconv_output<_Traits>;
256
friend class _Underflow<_CharT, _Traits>;
258
int_type _M_output_error();
261
bool _M_allocate_buffers(_CharT* __buf, streamsize __n);
262
bool _M_allocate_buffers();
263
void _M_deallocate_buffers();
265
pos_type _M_seek_return(off_type __off, _State_type __state) {
267
if (_M_in_input_mode)
268
_M_exit_input_mode();
269
_M_in_input_mode = false;
270
_M_in_output_mode = false;
271
_M_in_putback_mode = false;
272
_M_in_error_mode = false;
277
pos_type __result(__off);
278
__result.state(__state);
282
bool _M_seek_init(bool __do_unshift);
284
void _M_setup_codecvt(const locale&);
286
private: // Data members used in all modes.
288
_Filebuf_base _M_base;
290
private: // Locale-related information.
292
unsigned char _M_constant_width;
293
unsigned char _M_always_noconv;
295
// private: // Mode flags.
296
unsigned char _M_int_buf_dynamic; // True if internal buffer is heap allocated,
297
// false if it was supplied by the user.
298
unsigned char _M_in_input_mode;
299
unsigned char _M_in_output_mode;
300
unsigned char _M_in_error_mode;
301
unsigned char _M_in_putback_mode;
303
// Internal buffer: characters seen by the filebuf's clients.
305
_CharT* _M_int_buf_EOS;
307
// External buffer: characters corresponding to the external file.
309
char* _M_ext_buf_EOS;
311
// The range [_M_ext_buf, _M_ext_buf_converted) contains the external
312
// characters corresponding to the sequence in the internal buffer. The
313
// range [_M_ext_buf_converted, _M_ext_buf_end) contains characters that
314
// have been read into the external buffer but have not been converted
315
// to an internal sequence.
316
char* _M_ext_buf_converted;
317
char* _M_ext_buf_end;
319
// State corresponding to beginning of internal buffer.
320
_State_type _M_state;
322
private: // Data members used only in input mode.
324
// Similar to _M_state except that it corresponds to
325
// the end of the internal buffer instead of the beginning.
326
_State_type _M_end_state;
328
// This is a null pointer unless we are in mmap input mode.
330
streamoff _M_mmap_len;
332
private: // Data members used only in putback mode.
333
_CharT* _M_saved_eback;
334
_CharT* _M_saved_gptr;
335
_CharT* _M_saved_egptr;
337
typedef codecvt<_CharT, char, _State_type> _Codecvt;
338
const _Codecvt* _M_codecvt;
340
int _M_width; // Width of the encoding (if constant), else 1
341
int _M_max_width; // Largest possible width of single character.
344
enum { _S_pback_buf_size = 8 };
345
_CharT _M_pback_buf[_S_pback_buf_size];
347
// for _Noconv_output
349
bool _M_write(char* __buf, ptrdiff_t __n) {return _M_base._M_write(__buf, __n); }
353
_M_do_noconv_input() {
354
_M_ext_buf_converted = _M_ext_buf_end;
355
this->setg((char_type*)_M_ext_buf, (char_type*)_M_ext_buf, (char_type*)_M_ext_buf_end);
356
return traits_type::to_int_type(*_M_ext_buf);
360
# if defined (_STLP_USE_TEMPLATE_EXPORT)
361
_STLP_EXPORT_TEMPLATE_CLASS basic_filebuf<char, char_traits<char> >;
362
# if ! defined (_STLP_NO_WCHAR_T)
363
_STLP_EXPORT_TEMPLATE_CLASS basic_filebuf<wchar_t, char_traits<wchar_t> >;
365
# endif /* _STLP_USE_TEMPLATE_EXPORT */
369
template <class _CharT>
370
struct _Filebuf_Tmp_Buf
373
_Filebuf_Tmp_Buf(ptrdiff_t __n) : _M_ptr(0) { _M_ptr = new _CharT[__n]; }
374
~_Filebuf_Tmp_Buf() { delete[] _M_ptr; }
379
// This class had to be designed very carefully to work
382
template <class _Traits>
383
class _Noconv_output {
385
typedef typename _Traits::char_type char_type;
386
static bool _STLP_CALL _M_doit(basic_filebuf<char_type, _Traits >*,
387
char_type*, char_type*)
394
class _STLP_CLASS_DECLSPEC _Noconv_output< char_traits<char> > {
396
static bool _STLP_CALL
397
_M_doit(basic_filebuf<char, char_traits<char> >* __buf,
398
char* __first, char* __last)
400
ptrdiff_t __n = __last - __first;
401
if (__buf->_M_write(__first, __n)) {
409
//----------------------------------------------------------------------
410
// basic_filebuf<> helper functions.
413
//----------------------------------------
414
// Helper functions for switching between modes.
417
// This class had to be designed very carefully to work
420
template <class _Traits>
421
class _Noconv_input {
423
typedef typename _Traits::int_type int_type;
424
typedef typename _Traits::char_type char_type;
426
static inline int_type _STLP_CALL
427
_M_doit(basic_filebuf<char_type, _Traits>*)
434
class _Noconv_input<char_traits<char> > {
436
static inline int _STLP_CALL
437
_M_doit(basic_filebuf<char, char_traits<char> >* __buf) {
438
return __buf->_M_do_noconv_input();
442
// underflow() may be called for one of two reasons. (1) We've
443
// been going through the special putback buffer, and we need to move back
444
// to the regular internal buffer. (2) We've exhausted the internal buffer,
445
// and we need to replentish it.
446
template <class _CharT, class _Traits>
449
typedef typename _Traits::int_type int_type;
450
typedef _Traits traits_type;
452
static int_type _STLP_CALL _M_doit(basic_filebuf<_CharT, _Traits>* __this);
456
// Specialization of underflow: if the character type is char, maybe
457
// we can use mmap instead of read.
459
class _STLP_CLASS_DECLSPEC _Underflow< char, char_traits<char> > {
461
typedef char_traits<char>::int_type int_type;
462
typedef char_traits<char> traits_type;
463
static int _STLP_CALL _M_doit(basic_filebuf<char, traits_type >* __this);
466
// There is a specialized version of underflow, for basic_filebuf<char>,
469
template <class _CharT, class _Traits>
470
_STLP_TYPENAME_ON_RETURN_TYPE _Underflow<_CharT, _Traits>::int_type // _STLP_CALL
471
_Underflow<_CharT, _Traits>::_M_doit(basic_filebuf<_CharT, _Traits>* __this)
473
if (!__this->_M_in_input_mode) {
474
if (!__this->_M_switch_to_input_mode())
475
return traits_type::eof();
478
else if (__this->_M_in_putback_mode) {
479
__this->_M_exit_putback_mode();
480
if (__this->gptr() != __this->egptr()) {
481
int_type __c = traits_type::to_int_type(*__this->gptr());
486
return __this->_M_underflow_aux();
489
#if defined( _STLP_USE_TEMPLATE_EXPORT ) && ! defined (_STLP_NO_WCHAR_T)
490
_STLP_EXPORT_TEMPLATE_CLASS _Underflow<wchar_t, char_traits<wchar_t> >;
494
//----------------------------------------------------------------------
495
// Class basic_ifstream<>
497
template <class _CharT, class _Traits>
498
class basic_ifstream : public basic_istream<_CharT, _Traits>
501
typedef _CharT char_type;
502
typedef typename _Traits::int_type int_type;
503
typedef typename _Traits::pos_type pos_type;
504
typedef typename _Traits::off_type off_type;
505
typedef _Traits traits_type;
507
typedef basic_ios<_CharT, _Traits> _Basic_ios;
508
typedef basic_istream<_CharT, _Traits> _Base;
509
typedef basic_filebuf<_CharT, _Traits> _Buf;
511
public: // Constructors, destructor.
514
basic_ios<_CharT, _Traits>(), basic_istream<_CharT, _Traits>(0), _M_buf() {
518
explicit basic_ifstream(const char* __s, ios_base::openmode __mod = ios_base::in) :
519
basic_ios<_CharT, _Traits>(), basic_istream<_CharT, _Traits>(0),
522
if (!_M_buf.open(__s, __mod | ios_base::in))
523
this->setstate(ios_base::failbit);
526
# ifndef _STLP_NO_EXTENSIONS
527
explicit basic_ifstream(int __id, ios_base::openmode __mod = ios_base::in) :
528
basic_ios<_CharT, _Traits>(), basic_istream<_CharT, _Traits>(0), _M_buf() {
530
if (!_M_buf.open(__id, __mod | ios_base::in))
531
this->setstate(ios_base::failbit);
533
basic_ifstream(const char* __s, ios_base::openmode __m,
535
basic_ios<_CharT, _Traits>(), basic_istream<_CharT, _Traits>(0), _M_buf() {
537
if (!_M_buf.open(__s, __m | ios_base::in, __protection))
538
this->setstate(ios_base::failbit);
545
public: // File and buffer operations.
546
basic_filebuf<_CharT, _Traits>* rdbuf() const
547
{ return __CONST_CAST(_Buf*,&_M_buf); }
550
return this->rdbuf()->is_open();
553
void open(const char* __s, ios_base::openmode __mod = ios_base::in) {
554
if (!this->rdbuf()->open(__s, __mod | ios_base::in))
555
this->setstate(ios_base::failbit);
559
if (!this->rdbuf()->close())
560
this->setstate(ios_base::failbit);
565
basic_filebuf<_CharT, _Traits> _M_buf;
569
//----------------------------------------------------------------------
570
// Class basic_ofstream<>
572
template <class _CharT, class _Traits>
573
class basic_ofstream : public basic_ostream<_CharT, _Traits>
576
typedef _CharT char_type;
577
typedef typename _Traits::int_type int_type;
578
typedef typename _Traits::pos_type pos_type;
579
typedef typename _Traits::off_type off_type;
580
typedef _Traits traits_type;
582
typedef basic_ios<_CharT, _Traits> _Basic_ios;
583
typedef basic_ostream<_CharT, _Traits> _Base;
584
typedef basic_filebuf<_CharT, _Traits> _Buf;
586
public: // Constructors, destructor.
588
basic_ios<_CharT, _Traits>(),
589
basic_ostream<_CharT, _Traits>(0), _M_buf() {
592
explicit basic_ofstream(const char* __s, ios_base::openmode __mod = ios_base::out)
593
: basic_ios<_CharT, _Traits>(), basic_ostream<_CharT, _Traits>(0),
596
if (!_M_buf.open(__s, __mod | ios_base::out))
597
this->setstate(ios_base::failbit);
600
# ifndef _STLP_NO_EXTENSIONS
601
explicit basic_ofstream(int __id, ios_base::openmode __mod = ios_base::out)
602
: basic_ios<_CharT, _Traits>(), basic_ostream<_CharT, _Traits>(0),
605
if (!_M_buf.open(__id, __mod | ios_base::out))
606
this->setstate(ios_base::failbit);
608
basic_ofstream(const char* __s, ios_base::openmode __m, long __protection) :
609
basic_ios<_CharT, _Traits>(), basic_ostream<_CharT, _Traits>(0), _M_buf() {
611
if (!_M_buf.open(__s, __m | ios_base::out, __protection))
612
this->setstate(ios_base::failbit);
618
public: // File and buffer operations.
619
basic_filebuf<_CharT, _Traits>* rdbuf() const
620
{ return __CONST_CAST(_Buf*,&_M_buf); }
623
return this->rdbuf()->is_open();
626
void open(const char* __s, ios_base::openmode __mod= ios_base::out) {
627
if (!this->rdbuf()->open(__s, __mod | ios_base::out))
628
this->setstate(ios_base::failbit);
632
if (!this->rdbuf()->close())
633
this->setstate(ios_base::failbit);
637
basic_filebuf<_CharT, _Traits> _M_buf;
641
//----------------------------------------------------------------------
642
// Class basic_fstream<>
644
template <class _CharT, class _Traits>
645
class basic_fstream : public basic_iostream<_CharT, _Traits>
648
typedef _CharT char_type;
649
typedef typename _Traits::int_type int_type;
650
typedef typename _Traits::pos_type pos_type;
651
typedef typename _Traits::off_type off_type;
652
typedef _Traits traits_type;
654
typedef basic_ios<_CharT, _Traits> _Basic_ios;
655
typedef basic_iostream<_CharT, _Traits> _Base;
656
typedef basic_filebuf<_CharT, _Traits> _Buf;
658
public: // Constructors, destructor.
661
: basic_ios<_CharT, _Traits>(), basic_iostream<_CharT, _Traits>(0), _M_buf() {
665
explicit basic_fstream(const char* __s,
666
ios_base::openmode __mod = ios_base::in | ios_base::out) :
667
basic_ios<_CharT, _Traits>(), basic_iostream<_CharT, _Traits>(0), _M_buf() {
669
if (!_M_buf.open(__s, __mod))
670
this->setstate(ios_base::failbit);
673
# ifndef _STLP_NO_EXTENSIONS
674
explicit basic_fstream(int __id,
675
ios_base::openmode __mod = ios_base::in | ios_base::out) :
676
basic_ios<_CharT, _Traits>(), basic_iostream<_CharT, _Traits>(0), _M_buf() {
678
if (!_M_buf.open(__id, __mod))
679
this->setstate(ios_base::failbit);
681
basic_fstream(const char* __s, ios_base::openmode __m, long __protection) :
682
basic_ios<_CharT, _Traits>(), basic_iostream<_CharT, _Traits>(0), _M_buf() {
684
if (!_M_buf.open(__s, __m, __protection))
685
this->setstate(ios_base::failbit);
690
public: // File and buffer operations.
692
basic_filebuf<_CharT, _Traits>* rdbuf() const
693
{ return __CONST_CAST(_Buf*,&_M_buf); }
696
return this->rdbuf()->is_open();
699
void open(const char* __s,
700
ios_base::openmode __mod =
701
ios_base::in | ios_base::out) {
702
if (!this->rdbuf()->open(__s, __mod))
703
this->setstate(ios_base::failbit);
707
if (!this->rdbuf()->close())
708
this->setstate(ios_base::failbit);
712
basic_filebuf<_CharT, _Traits> _M_buf;
717
# if !defined (_STLP_LINK_TIME_INSTANTIATION)
718
# include <stl/_fstream.c>
721
_STLP_BEGIN_NAMESPACE
723
# if defined (_STLP_USE_TEMPLATE_EXPORT)
724
_STLP_EXPORT_TEMPLATE_CLASS basic_ifstream<char, char_traits<char> >;
725
_STLP_EXPORT_TEMPLATE_CLASS basic_ofstream<char, char_traits<char> >;
726
_STLP_EXPORT_TEMPLATE_CLASS basic_fstream<char, char_traits<char> >;
727
# if ! defined (_STLP_NO_WCHAR_T)
728
_STLP_EXPORT_TEMPLATE_CLASS basic_ifstream<wchar_t, char_traits<wchar_t> >;
729
_STLP_EXPORT_TEMPLATE_CLASS basic_ofstream<wchar_t, char_traits<wchar_t> >;
730
_STLP_EXPORT_TEMPLATE_CLASS basic_fstream<wchar_t, char_traits<wchar_t> >;
732
# endif /* _STLP_USE_TEMPLATE_EXPORT */
736
#endif /* _STLP_FSTREAM */