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