1 // Wrapper of C-language FILE struct -*- C++ -*-
3 // Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
31 // ISO C++ 14882: 27.8 File-based streams
34 /** @file basic_file.h
35 * This is an internal header file, included by other library headers.
36 * You should not attempt to use it directly.
39 #ifndef _CPP_BASIC_FILE
40 #define _CPP_BASIC_FILE 1
42 #pragma GCC system_header
44 #include <bits/c++config.h>
50 // Ulrich is going to make some detailed comment here, explaining
51 // all this unpleasantness, providing detailed performance analysis
52 // as to why we have to do all this lame vtable hacking instead of a
53 // sane, function-based approach. This verbiage will provide a clear
54 // and detailed description of the whole object-layout,
55 // vtable-swapping, sordid history of this hack.
56 template<typename _CharT>
57 struct __basic_file_base: public __c_file_type
60 ~__basic_file_base() { };
63 overflow(int __c = EOF) = 0;
72 pbackfail(int __c) = 0;
75 xsputn(const _CharT* __s, streamsize __n) = 0;
78 xsgetn(_CharT* __s, streamsize __n) = 0;
81 seekoff(streamoff __off, ios_base::seekdir __way,
82 ios_base::openmode __mode = ios_base::in | ios_base::out) = 0;
85 seekpos(streamoff __pos,
86 ios_base::openmode __mode = ios_base::in | ios_base::out) = 0;
89 setbuf(_CharT* __b, int __len) = 0;
98 sys_read(_CharT* __s, streamsize __n) = 0;
101 sys_write(const _CharT* __s, streamsize __n) = 0;
104 sys_seek(streamoff __off, ios_base::seekdir __way) = 0;
110 sys_stat(void* __v) = 0;
116 imbue(void* __v) = 0;
119 // Some of these member functions are based on libio/filebuf.cc.
120 // Also note that the order and number of virtual functions has to precisely
121 // match the order and number in the _IO_jump_t struct defined in libioP.h.
122 template<typename _CharT>
123 class __basic_file: public __basic_file_base<_CharT>
125 # ifdef _GLIBCPP_USE_WCHAR_T
126 __c_wfile_type _M_wfile;
130 __basic_file(__c_lock* __lock = 0);
133 _M_open_mode(ios_base::openmode __mode, int& __p_mode, int& __rw_mode,
136 // Equivalent to the normal fopen function.
138 open(const char* __name, ios_base::openmode __mode, int __prot = 0664);
140 // Used for opening the standard streams, cin, cout, cerr, clog,
141 // and their wide-stream equivalents. Instead of calling open, it
143 // - for libio: __c_file_type->_fileno and the respective _flags bits
144 // - for stdio: _M_cfile = __file and some internal flags
147 sys_open(__c_file_type* __file, ios_base::openmode __mode);
164 // NB: Must match FILE specific jump table starting here--this
165 // means all virtual functions starting with the dtor must match,
166 // slot by slot. For glibc-based dystems, this means the _IO_FILE
167 // as the FILE struct and _IO_jump_t as the jump table.
169 ~__basic_file(); // Takes the place of __finish.
172 overflow(int __c = EOF);
183 // A complex "write" function that sets all of __c_file_type's
184 // pointers and associated data members correctly and manages its
185 // relation to the external byte sequence.
187 xsputn(const _CharT* __s, streamsize __n);
189 // A complex "read" function that sets all of __c_file_type's
190 // pointers and associated data members correctly and manages its
191 // relation to the external byte sequence.
193 xsgetn(_CharT* __s, streamsize __n);
195 // A complex "seekoff" function that sets all of __c_file_type's
196 // pointers and associated data members correctly and manages its
197 // relation to the external byte sequence.
199 seekoff(streamoff __off, ios_base::seekdir __way,
200 ios_base::openmode __mode = ios_base::in | ios_base::out);
202 // A complex "seekpos" function that sets all of __c_file_type's
203 // pointers and associated data members correctly and manages its
204 // relation to the external byte sequence.
206 seekpos(streamoff __pos,
207 ios_base::openmode __mode = ios_base::in | ios_base::out);
210 setbuf(_CharT* __b, int __len);
218 // A simple read function for the external byte sequence, that
219 // does no mucking around with or setting of the pointers or flags
222 sys_read(_CharT* __s, streamsize __n);
224 // A simple write function for the external byte sequence, that
225 // does no mucking around with or setting of the pointers or flags
228 sys_write(const _CharT* __s, streamsize __n);
230 // A simple seek function for the external byte sequence, that
231 // does no mucking around with or setting of the pointers or flags
234 sys_seek(streamoff __off, ios_base::seekdir __way);
249 // __basic_file<char> specializations
251 __basic_file<char>::__basic_file(__c_lock* __lock);
255 __basic_file<char>::overflow(int __c);
259 __basic_file<char>::underflow();
263 __basic_file<char>::uflow();
267 __basic_file<char>::pbackfail(int __c);
271 __basic_file<char>::xsputn(const char* __s, streamsize __n);
275 __basic_file<char>::seekoff(streamoff __off, ios_base::seekdir __way,
276 ios_base::openmode __mode);
280 __basic_file<char>::seekpos(streamoff __pos, ios_base::openmode __mode);
284 __basic_file<char>::setbuf(char* __b, int __len);
288 __basic_file<char>::sync();
292 __basic_file<char>::doallocate();
294 // __basic_file<wchar_t> specializations
295 #ifdef _GLIBCPP_USE_WCHAR_T
297 __basic_file<wchar_t>::__basic_file(__c_lock* __lock);
301 __basic_file<wchar_t>::overflow(int __c);
305 __basic_file<wchar_t>::underflow();
309 __basic_file<wchar_t>::uflow();
313 __basic_file<wchar_t>::pbackfail(int __c);
317 __basic_file<wchar_t>::xsputn(const wchar_t* __s, streamsize __n);
321 __basic_file<wchar_t>::seekoff(streamoff __off, ios_base::seekdir __way,
322 ios_base::openmode __mode);
326 __basic_file<wchar_t>::seekpos(streamoff __pos, ios_base::openmode __mode);
330 __basic_file<wchar_t>::setbuf(wchar_t* __b, int __len);
334 __basic_file<wchar_t>::sync();
338 __basic_file<wchar_t>::doallocate();
341 template<typename _CharT>
342 __basic_file<_CharT>::~__basic_file()
343 { _IO_file_finish(this, 0); }
345 template<typename _CharT>
347 __basic_file<_CharT>::_M_open_mode(ios_base::openmode __mode,
348 int& __p_mode, int& __rw_mode,
352 bool __testb = __mode & ios_base::binary;
354 bool __testi = __mode & ios_base::in;
355 bool __testo = __mode & ios_base::out;
356 bool __testt = __mode & ios_base::trunc;
357 bool __testa = __mode & ios_base::app;
359 if (!__testi && __testo && !__testt && !__testa)
361 __p_mode = O_WRONLY | O_TRUNC | O_CREAT;
362 __rw_mode = _IO_NO_READS;
364 if (!__testi && __testo && !__testt && __testa)
366 __p_mode = O_WRONLY | O_APPEND | O_CREAT;
367 __rw_mode = _IO_NO_READS | _IO_IS_APPENDING;
369 if (!__testi && __testo && __testt && !__testa)
371 __p_mode = O_WRONLY | O_TRUNC | O_CREAT;
372 __rw_mode = _IO_NO_READS;
374 if (__testi && !__testo && !__testt && !__testa)
377 __rw_mode = _IO_NO_WRITES;
379 if (__testi && __testo && !__testt && !__testa)
384 if (__testi && __testo && __testt && !__testa)
386 __p_mode = O_RDWR | O_TRUNC | O_CREAT;
391 __p_mode |= O_BINARY;
395 template<typename _CharT>
396 __basic_file<_CharT>*
397 __basic_file<_CharT>::sys_open(__c_file_type* __f,
398 ios_base::openmode __mode)
400 __basic_file* __ret = NULL;
401 int __fd = fileno(__f);
403 int __rw_mode = _IO_NO_READS + _IO_NO_WRITES;
406 _M_open_mode(__mode, __p_mode, __rw_mode, __c_mode);
408 if (!_IO_file_is_open(this))
411 _flags &= ~(_IO_NO_READS + _IO_NO_WRITES);
412 _flags |= _IO_DELETE_DONT_CLOSE;
413 _offset = _IO_pos_BAD;
414 int __mask = _IO_NO_READS + _IO_NO_WRITES + _IO_IS_APPENDING;
415 _IO_mask_flags(this, __rw_mode, __mask);
421 template<typename _CharT>
422 __basic_file<_CharT>*
423 __basic_file<_CharT>::open(const char* __name, ios_base::openmode __mode,
426 __basic_file* __ret = NULL;
428 int __rw_mode = _IO_NO_READS + _IO_NO_WRITES;
431 _M_open_mode(__mode, __p_mode, __rw_mode, __c_mode);
432 if (!_IO_file_is_open(this))
435 __f = _IO_file_open(this, __name, __p_mode, __prot, __rw_mode, 0);
436 __ret = __f ? this: NULL;
441 template<typename _CharT>
443 __basic_file<_CharT>::is_open() { return _fileno >= 0; }
445 template<typename _CharT>
446 __basic_file<_CharT>*
447 __basic_file<_CharT>::close()
449 return _IO_file_close_it(this) ? static_cast<__basic_file*>(NULL) : this;
452 template<typename _CharT>
454 __basic_file<_CharT>::xsgetn(_CharT* __s, streamsize __n)
455 { return _IO_file_xsgetn(this, __s, __n); }
458 template<typename _CharT>
460 __basic_file<_CharT>::sys_read(_CharT* __s, streamsize __n)
461 { return _IO_file_read(this, __s, __n); }
464 template<typename _CharT>
466 __basic_file<_CharT>::sys_write(const _CharT* __s, streamsize __n)
467 { return _IO_file_write(this, __s, __n); }
470 template<typename _CharT>
472 __basic_file<_CharT>::sys_seek(streamoff __pos, ios_base::seekdir __way)
473 { return _IO_file_seek(this, __pos, __way); }
476 template<typename _CharT>
478 __basic_file<_CharT>::sys_close()
479 { return _IO_file_close(this); }
482 template<typename _CharT>
484 __basic_file<_CharT>::sys_stat(void* __v)
485 { return _IO_file_stat(this, __v); }
488 template<typename _CharT>
490 __basic_file<_CharT>::showmanyc() { return EOF; }
493 template<typename _CharT>
495 __basic_file<_CharT>::imbue(void* /*__v*/) { }
498 #endif // _CPP_BASIC_FILE