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