2002-01-04 Benjamin Kosnik <bkoz@redhat.com>
[official-gcc.git] / libstdc++-v3 / include / bits / basic_file.h
blob51dde5bc4c3c8414d315668c3de3be957ae2ffaf
1 // Wrapper of C-language FILE struct -*- C++ -*-
3 // Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
4 //
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)
9 // any later version.
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,
19 // USA.
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>
45 #include <ios>
47 namespace std
49 // Ulrich is going to make some detailed comment here, explaining
50 // all this unpleasantness, providing detailed performance analysis
51 // as to why we have to do all this lame vtable hacking instead of a
52 // sane, function-based approach. This verbiage will provide a clear
53 // and detailed description of the whole object-layout,
54 // vtable-swapping, sordid history of this hack.
55 template<typename _CharT>
56 struct __basic_file_base: public __c_file_type
58 virtual
59 ~__basic_file_base() { };
61 virtual int
62 overflow(int __c = EOF) = 0;
64 virtual int
65 underflow() = 0;
67 virtual int
68 uflow() = 0;
70 virtual int
71 pbackfail(int __c) = 0;
73 virtual streamsize
74 xsputn(const _CharT* __s, streamsize __n) = 0;
76 virtual streamsize
77 xsgetn(_CharT* __s, streamsize __n) = 0;
79 virtual streamoff
80 seekoff(streamoff __off, ios_base::seekdir __way,
81 ios_base::openmode __mode = ios_base::in | ios_base::out) = 0;
83 virtual streamoff
84 seekpos(streamoff __pos,
85 ios_base::openmode __mode = ios_base::in | ios_base::out) = 0;
87 virtual streambuf*
88 setbuf(_CharT* __b, int __len) = 0;
90 virtual int
91 sync() = 0;
93 virtual int
94 doallocate() = 0;
96 virtual streamsize
97 sys_read(_CharT* __s, streamsize __n) = 0;
99 virtual streamsize
100 sys_write(const _CharT* __s, streamsize __n) = 0;
102 virtual streamoff
103 sys_seek(streamoff __off, ios_base::seekdir __way) = 0;
105 virtual int
106 sys_close() = 0;
108 virtual int
109 sys_stat(void* __v) = 0;
111 virtual int
112 showmanyc() = 0;
114 virtual void
115 imbue(void* __v) = 0;
118 // Some of these member functions are based on libio/filebuf.cc.
119 // Also note that the order and number of virtual functions has to precisely
120 // match the order and number in the _IO_jump_t struct defined in libioP.h.
121 template<typename _CharT>
122 #ifdef _GLIBCPP_BASIC_FILE_INHERITANCE
123 class __basic_file: public __basic_file_base<_CharT>
124 #else
125 class __basic_file
126 #endif
128 #if _GLIBCPP_BASIC_FILE_ENCAPSULATION
129 // underlying data source/sink
130 __c_file_type* _M_cfile;
131 // true iff we opened _M_cfile, and thus must close it ourselves
132 bool _M_cfile_created;
133 #else
134 # ifdef _GLIBCPP_USE_WCHAR_T
135 __c_wfile_type _M_wfile;
136 # endif
137 #endif
139 public:
140 __basic_file(__c_lock* __lock = 0);
142 void
143 _M_open_mode(ios_base::openmode __mode, int& __p_mode, int& __rw_mode,
144 char* __c_mode);
146 // Equivalent to the normal fopen function.
147 __basic_file*
148 open(const char* __name, ios_base::openmode __mode, int __prot = 0664);
150 // Used for opening the standard streams, cin, cout, cerr, clog,
151 // and their wide-stream equivalents. Instead of calling open, it
152 // just sets
153 // - for libio: __c_file_type->_fileno and the respective _flags bits
154 // - for stdio: _M_cfile = __file and some internal flags
155 // and returns.
156 __basic_file*
157 sys_open(__c_file_type* __file, ios_base::openmode __mode);
159 _CharT
160 sys_getc();
162 _CharT
163 sys_ungetc(_CharT);
165 __basic_file*
166 close();
168 bool
169 is_open();
171 int
172 fd();
174 // NB: Must match FILE specific jump table starting here--this
175 // means all virtual functions starting with the dtor must match,
176 // slot by slot. For glibc-based dystems, this means the _IO_FILE
177 // as the FILE struct and _IO_jump_t as the jump table.
178 virtual
179 ~__basic_file(); // Takes the place of __finish.
181 virtual int
182 overflow(int __c = EOF);
184 virtual int
185 underflow();
187 virtual int
188 uflow();
190 virtual int
191 pbackfail(int __c);
193 // A complex "write" function that sets all of __c_file_type's
194 // pointers and associated data members correctly and manages its
195 // relation to the external byte sequence.
196 virtual streamsize
197 xsputn(const _CharT* __s, streamsize __n);
199 // A complex "read" function that sets all of __c_file_type's
200 // pointers and associated data members correctly and manages its
201 // relation to the external byte sequence.
202 virtual streamsize
203 xsgetn(_CharT* __s, streamsize __n);
205 // A complex "seekoff" function that sets all of __c_file_type's
206 // pointers and associated data members correctly and manages its
207 // relation to the external byte sequence.
208 virtual streamoff
209 seekoff(streamoff __off, ios_base::seekdir __way,
210 ios_base::openmode __mode = ios_base::in | ios_base::out);
212 // A complex "seekpos" function that sets all of __c_file_type's
213 // pointers and associated data members correctly and manages its
214 // relation to the external byte sequence.
215 virtual streamoff
216 seekpos(streamoff __pos,
217 ios_base::openmode __mode = ios_base::in | ios_base::out);
219 virtual streambuf*
220 setbuf(_CharT* __b, int __len);
222 virtual int
223 sync();
225 virtual int
226 doallocate();
228 // A simple read function for the external byte sequence, that
229 // does no mucking around with or setting of the pointers or flags
230 // in __c_file_type.
231 virtual streamsize
232 sys_read(_CharT* __s, streamsize __n);
234 // A simple write function for the external byte sequence, that
235 // does no mucking around with or setting of the pointers or flags
236 // in __c_file_type.
237 virtual streamsize
238 sys_write(const _CharT* __s, streamsize __n);
240 // A simple seek function for the external byte sequence, that
241 // does no mucking around with or setting of the pointers or flags
242 // in __c_file_type.
243 virtual streamoff
244 sys_seek(streamoff __off, ios_base::seekdir __way);
246 virtual int
247 sys_close();
249 virtual int
250 sys_stat(void* __v);
252 virtual int
253 showmanyc();
255 virtual void
256 imbue(void* __v);
258 } // namespace std
260 // Now include the bits that are dependent on the underlying I/O
261 // model chosen at configure time.
262 #include <bits/basic_file_model.h>
264 #endif // _CPP_BASIC_FILE