~ubuntu-branches/ubuntu/hardy/libterralib/hardy

« back to all changes in this revision

Viewing changes to src/STLport/stl/_fstream.h

  • Committer: Bazaar Package Importer
  • Author(s): Daniel T Chen
  • Date: 2005-11-25 22:32:59 UTC
  • Revision ID: james.westby@ubuntu.com-20051125223259-3zubal8ux4ki4fjg
Tags: upstream-3.0.3b2
ImportĀ upstreamĀ versionĀ 3.0.3b2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
// 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.
 
21
 
 
22
#ifndef _STLP_INTERNAL_FSTREAM_H
 
23
#define _STLP_INTERNAL_FSTREAM_H
 
24
 
 
25
#if defined(__sgi) && !defined(__GNUC__) && !defined(_STANDARD_C_PLUS_PLUS)
 
26
#error This header file requires the -LANG:std option
 
27
#endif
 
28
 
 
29
#ifndef _STLP_INTERNAL_STREAMBUF
 
30
# include <stl/_streambuf.h>
 
31
#endif
 
32
 
 
33
#ifndef _STLP_INTERNAL_ISTREAM_H
 
34
#include <stl/_istream.h>
 
35
#endif
 
36
 
 
37
#ifndef _STLP_INTERNAL_CODECVT_H
 
38
#include <stl/_codecvt.h>
 
39
#endif
 
40
 
 
41
#ifndef _STLP_STDIO_FILE_H
 
42
#include <stl/_stdio_file.h>
 
43
#endif
 
44
 
 
45
#if !defined (_STLP_USE_UNIX_IO) && !defined(_STLP_USE_WIN32_IO) \
 
46
    && ! defined (_STLP_USE_UNIX_EMULATION_IO) && !defined (_STLP_USE_STDIO_IO)
 
47
 
 
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)
 
55
// _open/_read/_write
 
56
#  define _STLP_USE_UNIX_EMULATION_IO
 
57
# else
 
58
// fopen/fread/fwrite
 
59
#  define _STLP_USE_STDIO_IO
 
60
# endif /* _STLP_UNIX */
 
61
 
 
62
#endif /* mode selection */
 
63
 
 
64
 
 
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)
 
68
typedef int _STLP_fd;
 
69
#else
 
70
#error "Configure i/o !"
 
71
#endif
 
72
 
 
73
 
 
74
_STLP_BEGIN_NAMESPACE
 
75
 
 
76
//----------------------------------------------------------------------
 
77
// Class _Filebuf_base, a private base class to factor out the system-
 
78
// dependent code from basic_filebuf<>.
 
79
 
 
80
class _STLP_CLASS_DECLSPEC _Filebuf_base {
 
81
public:                      // Opening and closing files.
 
82
  _Filebuf_base();
 
83
 
 
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);
 
87
  bool _M_close();
 
88
 
 
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);
 
94
 
 
95
public:                      // Memory-mapped I/O.
 
96
  void* _M_mmap(streamoff __offset, streamoff __len);
 
97
  void _M_unmap(void* __mmap_base, streamoff __len);
 
98
 
 
99
public:
 
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 )
 
112
      ? (__last - __first)
 
113
      : count(__first, __last, '\n') + (__last - __first);
 
114
#endif
 
115
  }
 
116
 
 
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__) 
 
121
    return true;
 
122
# elif defined (_STLP_WIN32) || defined (_STLP_WIN16) || defined (_STLP_DOS) || defined (_STLP_VM) || defined (__EMX__)
 
123
    return (_M_openmode & ios_base::binary) != 0;
 
124
# else 
 
125
#   error "Port!"
 
126
# endif
 
127
  }
 
128
 
 
129
protected:                      // Static data members.
 
130
  static size_t _M_page_size;
 
131
 
 
132
protected:                      // Data members.
 
133
  _STLP_fd _M_file_id;
 
134
# ifdef _STLP_USE_STDIO_IO
 
135
  // for stdio, the whole FILE* is being kept here
 
136
  FILE* _M_file;
 
137
# endif
 
138
# ifdef _STLP_USE_WIN32_IO
 
139
  void* _M_view_id; 
 
140
# endif
 
141
 
 
142
  ios_base::openmode _M_openmode     ;
 
143
  unsigned char      _M_is_open      ;
 
144
  unsigned char      _M_should_close ;
 
145
  unsigned char      _M_regular_file ;
 
146
 
 
147
public :
 
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; }
 
154
};
 
155
 
 
156
 
 
157
 
 
158
 
 
159
//----------------------------------------------------------------------
 
160
// Class basic_filebuf<>.
 
161
 
 
162
// Forward declaration of two helper classes.
 
163
template <class _Traits> class _Noconv_input;
 
164
_STLP_TEMPLATE_NULL
 
165
class _Noconv_input<char_traits<char> >;
 
166
 
 
167
template <class _Traits> class _Noconv_output;
 
168
_STLP_TEMPLATE_NULL
 
169
class _Noconv_output< char_traits<char> >;
 
170
 
 
171
// There is a specialized version of underflow, for basic_filebuf<char>,
 
172
// in fstream.cxx.
 
173
 
 
174
template <class _CharT, class _Traits>
 
175
class _Underflow;
 
176
 
 
177
 _STLP_TEMPLATE_NULL class _Underflow< char, char_traits<char> >;
 
178
 
 
179
template <class _CharT, class _Traits>
 
180
class basic_filebuf : public basic_streambuf<_CharT, _Traits>
 
181
{
 
182
public:                         // Types.
 
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;
 
188
 
 
189
  typedef typename _Traits::state_type _State_type;
 
190
  typedef basic_streambuf<_CharT, _Traits> _Base;
 
191
  typedef basic_filebuf<_CharT, _Traits> _Self;
 
192
 
 
193
public:                         // Constructors, destructor.
 
194
  basic_filebuf();  
 
195
  ~basic_filebuf();
 
196
  
 
197
public:                         // Opening and closing files.
 
198
  bool is_open() const { return _M_base.__is_open(); }
 
199
 
 
200
  _Self* open(const char* __s, ios_base::openmode __m) {
 
201
    return _M_base._M_open(__s, __m) ? this : 0;
 
202
  }
 
203
 
 
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,
 
207
                      long __protection) {
 
208
    return _M_base._M_open(__s, __m, __protection) ? this : 0;
 
209
  }
 
210
  
 
211
  _STLP_fd fd() const { return _M_base.__get_fd(); }
 
212
 
 
213
  _Self* open(int __id, ios_base::openmode _Init_mode = ios_base::__default_mode) {
 
214
    return this->_M_open(__id, _Init_mode);
 
215
  }
 
216
# endif
 
217
 
 
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;
 
220
  }
 
221
 
 
222
  _Self* close();
 
223
 
 
224
protected:                      // Virtual functions from basic_streambuf.
 
225
  virtual streamsize showmanyc();
 
226
  virtual int_type underflow();
 
227
 
 
228
  virtual int_type pbackfail(int_type = traits_type::eof());
 
229
  virtual int_type overflow(int_type = traits_type::eof());
 
230
 
 
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);
 
236
 
 
237
  virtual int sync();
 
238
  virtual void imbue(const locale&);
 
239
 
 
240
private:                        // Helper functions.
 
241
 
 
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;
 
247
  }
 
248
  bool _M_switch_to_input_mode();
 
249
  void _M_exit_input_mode();
 
250
  bool _M_switch_to_output_mode();
 
251
 
 
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>;
 
257
 
 
258
  int_type _M_output_error();
 
259
  bool _M_unshift();
 
260
 
 
261
  bool _M_allocate_buffers(_CharT* __buf, streamsize __n);
 
262
  bool _M_allocate_buffers();
 
263
  void _M_deallocate_buffers();
 
264
 
 
265
  pos_type _M_seek_return(off_type __off, _State_type __state) {
 
266
    if (__off != -1) {
 
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;
 
273
      this->setg(0, 0, 0);
 
274
      this->setp(0, 0);
 
275
    }
 
276
    
 
277
    pos_type __result(__off);
 
278
    __result.state(__state);
 
279
    return __result;
 
280
  }
 
281
  
 
282
  bool _M_seek_init(bool __do_unshift);
 
283
 
 
284
  void _M_setup_codecvt(const locale&);
 
285
 
 
286
private:                        // Data members used in all modes.
 
287
 
 
288
  _Filebuf_base _M_base;
 
289
 
 
290
private:                        // Locale-related information.
 
291
 
 
292
  unsigned char _M_constant_width;
 
293
  unsigned char _M_always_noconv;
 
294
  
 
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;
 
302
  
 
303
  // Internal buffer: characters seen by the filebuf's clients.
 
304
  _CharT* _M_int_buf;
 
305
  _CharT* _M_int_buf_EOS;
 
306
  
 
307
  // External buffer: characters corresponding to the external file.
 
308
  char* _M_ext_buf;
 
309
  char* _M_ext_buf_EOS;
 
310
 
 
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;
 
318
 
 
319
  // State corresponding to beginning of internal buffer.
 
320
  _State_type _M_state;
 
321
 
 
322
private:                        // Data members used only in input mode.
 
323
 
 
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;
 
327
 
 
328
  // This is a null pointer unless we are in mmap input mode.
 
329
  void*     _M_mmap_base;
 
330
  streamoff _M_mmap_len;
 
331
 
 
332
private:                        // Data members used only in putback mode.
 
333
  _CharT* _M_saved_eback;
 
334
  _CharT* _M_saved_gptr;
 
335
  _CharT* _M_saved_egptr;
 
336
 
 
337
  typedef codecvt<_CharT, char, _State_type> _Codecvt;
 
338
  const _Codecvt* _M_codecvt;
 
339
 
 
340
  int _M_width;                 // Width of the encoding (if constant), else 1
 
341
  int _M_max_width;             // Largest possible width of single character.
 
342
 
 
343
 
 
344
  enum { _S_pback_buf_size = 8 };
 
345
  _CharT _M_pback_buf[_S_pback_buf_size];
 
346
 
 
347
  // for _Noconv_output
 
348
public:
 
349
  bool _M_write(char* __buf,  ptrdiff_t __n) {return _M_base._M_write(__buf, __n); }
 
350
 
 
351
public:
 
352
  int_type
 
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);
 
357
  }
 
358
};
 
359
 
 
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> >;
 
364
#  endif
 
365
# endif /* _STLP_USE_TEMPLATE_EXPORT */
 
366
 
 
367
// public:
 
368
// helper class.
 
369
template <class _CharT>
 
370
struct _Filebuf_Tmp_Buf
 
371
{
 
372
  _CharT* _M_ptr;
 
373
  _Filebuf_Tmp_Buf(ptrdiff_t __n) : _M_ptr(0) { _M_ptr = new _CharT[__n]; }
 
374
  ~_Filebuf_Tmp_Buf() { delete[] _M_ptr; }
 
375
};
 
376
 
 
377
 
 
378
//
 
379
// This class had to be designed very carefully to work
 
380
// with Visual C++.
 
381
//
 
382
template <class _Traits>
 
383
class _Noconv_output {
 
384
public:
 
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*)
 
388
  {
 
389
      return false; 
 
390
  }
 
391
};
 
392
 
 
393
_STLP_TEMPLATE_NULL
 
394
class _STLP_CLASS_DECLSPEC _Noconv_output< char_traits<char> > {
 
395
public:
 
396
  static bool  _STLP_CALL
 
397
  _M_doit(basic_filebuf<char, char_traits<char> >* __buf, 
 
398
          char* __first, char* __last)
 
399
  {
 
400
    ptrdiff_t __n = __last - __first;
 
401
    if (__buf->_M_write(__first, __n)) {
 
402
      return true;
 
403
    }
 
404
    else
 
405
      return false; 
 
406
  }
 
407
};
 
408
 
 
409
//----------------------------------------------------------------------
 
410
// basic_filebuf<> helper functions.
 
411
 
 
412
 
 
413
//----------------------------------------
 
414
// Helper functions for switching between modes.
 
415
 
 
416
//
 
417
// This class had to be designed very carefully to work
 
418
// with Visual C++.
 
419
//
 
420
template <class _Traits>
 
421
class _Noconv_input {
 
422
public:
 
423
  typedef typename _Traits::int_type int_type;
 
424
  typedef typename _Traits::char_type char_type;
 
425
 
 
426
  static inline int_type _STLP_CALL
 
427
  _M_doit(basic_filebuf<char_type, _Traits>*) 
 
428
  {
 
429
    return 0;
 
430
  }
 
431
};
 
432
 
 
433
_STLP_TEMPLATE_NULL
 
434
class _Noconv_input<char_traits<char> > {
 
435
public:
 
436
  static inline int _STLP_CALL
 
437
  _M_doit(basic_filebuf<char, char_traits<char> >* __buf)  {
 
438
    return __buf->_M_do_noconv_input();
 
439
  }
 
440
};
 
441
 
 
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>
 
447
class _Underflow {
 
448
public:
 
449
  typedef typename _Traits::int_type int_type;
 
450
  typedef _Traits                    traits_type;
 
451
  
 
452
  static int_type _STLP_CALL _M_doit(basic_filebuf<_CharT, _Traits>* __this);
 
453
};
 
454
 
 
455
 
 
456
// Specialization of underflow: if the character type is char, maybe
 
457
// we can use mmap instead of read.
 
458
_STLP_TEMPLATE_NULL
 
459
class _STLP_CLASS_DECLSPEC _Underflow< char, char_traits<char> > {
 
460
public:
 
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);
 
464
};
 
465
 
 
466
// There is a specialized version of underflow, for basic_filebuf<char>,
 
467
// in fstream.cxx.
 
468
 
 
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)
 
472
{
 
473
  if (!__this->_M_in_input_mode) {
 
474
    if (!__this->_M_switch_to_input_mode())
 
475
      return traits_type::eof();
 
476
  }
 
477
  
 
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());
 
482
      return __c;
 
483
    }
 
484
  }
 
485
  
 
486
  return __this->_M_underflow_aux();
 
487
}
 
488
 
 
489
#if defined( _STLP_USE_TEMPLATE_EXPORT ) && ! defined (_STLP_NO_WCHAR_T)
 
490
_STLP_EXPORT_TEMPLATE_CLASS _Underflow<wchar_t, char_traits<wchar_t> >;
 
491
#endif
 
492
 
 
493
 
 
494
//----------------------------------------------------------------------
 
495
// Class basic_ifstream<>
 
496
 
 
497
template <class _CharT, class _Traits>
 
498
class basic_ifstream : public basic_istream<_CharT, _Traits>
 
499
{
 
500
public:                         // Types
 
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;
 
506
 
 
507
  typedef basic_ios<_CharT, _Traits>                _Basic_ios;
 
508
  typedef basic_istream<_CharT, _Traits>            _Base;
 
509
  typedef basic_filebuf<_CharT, _Traits>            _Buf;
 
510
 
 
511
public:                         // Constructors, destructor.
 
512
 
 
513
  basic_ifstream() : 
 
514
    basic_ios<_CharT, _Traits>(),  basic_istream<_CharT, _Traits>(0), _M_buf() {
 
515
      this->init(&_M_buf);
 
516
  }
 
517
 
 
518
  explicit basic_ifstream(const char* __s, ios_base::openmode __mod = ios_base::in) : 
 
519
    basic_ios<_CharT, _Traits>(),  basic_istream<_CharT, _Traits>(0),
 
520
    _M_buf() {
 
521
      this->init(&_M_buf);
 
522
      if (!_M_buf.open(__s, __mod | ios_base::in))
 
523
        this->setstate(ios_base::failbit);
 
524
  }
 
525
 
 
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() {
 
529
    this->init(&_M_buf);
 
530
    if (!_M_buf.open(__id, __mod | ios_base::in))
 
531
      this->setstate(ios_base::failbit);
 
532
  }
 
533
  basic_ifstream(const char* __s, ios_base::openmode __m,
 
534
                 long __protection) : 
 
535
    basic_ios<_CharT, _Traits>(),  basic_istream<_CharT, _Traits>(0), _M_buf() {
 
536
    this->init(&_M_buf);
 
537
    if (!_M_buf.open(__s, __m | ios_base::in, __protection))
 
538
      this->setstate(ios_base::failbit);  
 
539
  }
 
540
  
 
541
# endif
 
542
 
 
543
  ~basic_ifstream() {}
 
544
 
 
545
public:                         // File and buffer operations.
 
546
  basic_filebuf<_CharT, _Traits>* rdbuf() const
 
547
    { return __CONST_CAST(_Buf*,&_M_buf); }
 
548
 
 
549
  bool is_open() {
 
550
    return this->rdbuf()->is_open();
 
551
  }
 
552
 
 
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);
 
556
  }
 
557
 
 
558
  void close() {
 
559
    if (!this->rdbuf()->close())
 
560
      this->setstate(ios_base::failbit);
 
561
  }
 
562
 
 
563
 
 
564
private:
 
565
  basic_filebuf<_CharT, _Traits> _M_buf;
 
566
};
 
567
 
 
568
 
 
569
//----------------------------------------------------------------------
 
570
// Class basic_ofstream<>
 
571
 
 
572
template <class _CharT, class _Traits>
 
573
class basic_ofstream : public basic_ostream<_CharT, _Traits>
 
574
{
 
575
public:                         // Types
 
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;
 
581
 
 
582
  typedef basic_ios<_CharT, _Traits>                _Basic_ios;
 
583
  typedef basic_ostream<_CharT, _Traits>            _Base;
 
584
  typedef basic_filebuf<_CharT, _Traits>            _Buf;
 
585
 
 
586
public:                         // Constructors, destructor.
 
587
  basic_ofstream() : 
 
588
    basic_ios<_CharT, _Traits>(), 
 
589
    basic_ostream<_CharT, _Traits>(0), _M_buf() {
 
590
      this->init(&_M_buf);
 
591
  }
 
592
  explicit basic_ofstream(const char* __s, ios_base::openmode __mod = ios_base::out) 
 
593
    : basic_ios<_CharT, _Traits>(), basic_ostream<_CharT, _Traits>(0),
 
594
      _M_buf() {
 
595
        this->init(&_M_buf);
 
596
        if (!_M_buf.open(__s, __mod | ios_base::out))
 
597
          this->setstate(ios_base::failbit);
 
598
  }
 
599
 
 
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),
 
603
    _M_buf() {
 
604
        this->init(&_M_buf);
 
605
        if (!_M_buf.open(__id, __mod | ios_base::out))
 
606
          this->setstate(ios_base::failbit);
 
607
  }
 
608
  basic_ofstream(const char* __s, ios_base::openmode __m, long __protection) : 
 
609
    basic_ios<_CharT, _Traits>(),  basic_ostream<_CharT, _Traits>(0), _M_buf() {
 
610
    this->init(&_M_buf);
 
611
    if (!_M_buf.open(__s, __m | ios_base::out, __protection))
 
612
      this->setstate(ios_base::failbit);  
 
613
  }
 
614
# endif
 
615
  
 
616
  ~basic_ofstream() {}
 
617
 
 
618
public:                         // File and buffer operations.
 
619
  basic_filebuf<_CharT, _Traits>* rdbuf() const
 
620
    { return __CONST_CAST(_Buf*,&_M_buf); } 
 
621
 
 
622
  bool is_open() {
 
623
    return this->rdbuf()->is_open();
 
624
  }
 
625
 
 
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);
 
629
  }
 
630
 
 
631
  void close() {
 
632
    if (!this->rdbuf()->close())
 
633
      this->setstate(ios_base::failbit);
 
634
  }
 
635
 
 
636
private:
 
637
  basic_filebuf<_CharT, _Traits> _M_buf;
 
638
};
 
639
 
 
640
 
 
641
//----------------------------------------------------------------------
 
642
// Class basic_fstream<>
 
643
 
 
644
template <class _CharT, class _Traits>
 
645
class basic_fstream : public basic_iostream<_CharT, _Traits>
 
646
{
 
647
public:                         // Types
 
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;
 
653
 
 
654
  typedef basic_ios<_CharT, _Traits>                _Basic_ios;
 
655
  typedef basic_iostream<_CharT, _Traits>           _Base;
 
656
  typedef basic_filebuf<_CharT, _Traits>            _Buf;
 
657
 
 
658
public:                         // Constructors, destructor.
 
659
  
 
660
  basic_fstream()   
 
661
    : basic_ios<_CharT, _Traits>(), basic_iostream<_CharT, _Traits>(0), _M_buf() {
 
662
      this->init(&_M_buf);
 
663
  }
 
664
 
 
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() {
 
668
      this->init(&_M_buf);
 
669
      if (!_M_buf.open(__s, __mod))
 
670
        this->setstate(ios_base::failbit);
 
671
  }
 
672
 
 
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() {
 
677
    this->init(&_M_buf);
 
678
    if (!_M_buf.open(__id, __mod))
 
679
      this->setstate(ios_base::failbit);
 
680
  }
 
681
  basic_fstream(const char* __s, ios_base::openmode __m, long __protection) : 
 
682
    basic_ios<_CharT, _Traits>(),  basic_iostream<_CharT, _Traits>(0), _M_buf() {
 
683
    this->init(&_M_buf);
 
684
    if (!_M_buf.open(__s, __m, __protection))
 
685
      this->setstate(ios_base::failbit);  
 
686
  }
 
687
# endif    
 
688
  ~basic_fstream() {}
 
689
 
 
690
public:                         // File and buffer operations.
 
691
 
 
692
  basic_filebuf<_CharT, _Traits>* rdbuf() const
 
693
    { return __CONST_CAST(_Buf*,&_M_buf); } 
 
694
 
 
695
  bool is_open() {
 
696
    return this->rdbuf()->is_open();
 
697
  }
 
698
 
 
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);
 
704
  }
 
705
 
 
706
  void close() {
 
707
    if (!this->rdbuf()->close())
 
708
      this->setstate(ios_base::failbit);
 
709
  }
 
710
 
 
711
private:
 
712
  basic_filebuf<_CharT, _Traits> _M_buf;
 
713
};
 
714
 
 
715
_STLP_END_NAMESPACE
 
716
 
 
717
# if !defined (_STLP_LINK_TIME_INSTANTIATION)
 
718
#  include <stl/_fstream.c>
 
719
# endif
 
720
 
 
721
_STLP_BEGIN_NAMESPACE
 
722
 
 
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> >;
 
731
#  endif
 
732
# endif /* _STLP_USE_TEMPLATE_EXPORT */
 
733
 
 
734
_STLP_END_NAMESPACE
 
735
 
 
736
#endif /* _STLP_FSTREAM */
 
737
 
 
738
 
 
739
// Local Variables:
 
740
// mode:C++
 
741
// End: