]> gcc.gnu.org Git - gcc.git/blob - libstdc++-v3/config/c_io_libio.cc
c_io_libio.cc (basic_file<char>::__basic_file): Don't pass a NULL _IO_wide_data to...
[gcc.git] / libstdc++-v3 / config / c_io_libio.cc
1 // Wrapper of C-language FILE struct -*- C++ -*-
2
3 // Copyright (C) 2000 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.
10
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.
15
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.
20
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.
29
30 //
31 // ISO C++ 14882: 27.8 File-based streams
32 //
33
34 #include <bits/basic_file.h>
35 #include <libioP.h>
36
37 namespace std {
38
39 // Need to instantiate base class here for type-info bits, etc
40 template struct __basic_file_base<char>;
41 template struct __basic_file_base<wchar_t>;
42
43 // Generic definitions for __basic_file
44 template<typename _CharT>
45 int
46 __basic_file<_CharT>::get_fileno(void)
47 { return _fileno; }
48
49 template<typename _CharT>
50 __basic_file<_CharT>::~__basic_file()
51 { _IO_file_finish(this, 0); }
52
53 template<typename _CharT>
54 void
55 __basic_file<_CharT>::_M_open_mode(ios_base::openmode __mode,
56 int& __p_mode, int& __rw_mode)
57 {
58 #ifdef O_BINARY
59 bool __testb = __mode & ios_base::binary;
60 #endif
61 bool __testi = __mode & ios_base::in;
62 bool __testo = __mode & ios_base::out;
63 bool __testt = __mode & ios_base::trunc;
64 bool __testa = __mode & ios_base::app;
65
66 if (!__testi && __testo && !__testt && !__testa)
67 {
68 __p_mode = O_WRONLY | O_TRUNC | O_CREAT;
69 __rw_mode = _IO_NO_READS;
70 }
71 if (!__testi && __testo && !__testt && __testa)
72 {
73 __p_mode = O_WRONLY | O_APPEND | O_CREAT;
74 __rw_mode = _IO_NO_READS | _IO_IS_APPENDING;
75 }
76 if (!__testi && __testo && __testt && !__testa)
77 {
78 __p_mode = O_WRONLY | O_TRUNC | O_CREAT;
79 __rw_mode = _IO_NO_READS;
80 }
81 if (__testi && !__testo && !__testt && !__testa)
82 {
83 __p_mode = O_RDONLY;
84 __rw_mode = _IO_NO_WRITES;
85 }
86 if (__testi && __testo && !__testt && !__testa)
87 {
88 __p_mode = O_RDWR;
89 __rw_mode = 0;
90 }
91 if (__testi && __testo && __testt && !__testa)
92 {
93 __p_mode = O_RDWR | O_TRUNC | O_CREAT;
94 __rw_mode = 0;
95 }
96 #ifdef O_BINARY
97 if (__testb)
98 __p_mode |= O_BINARY;
99 #endif
100 }
101
102 template<typename _CharT>
103 __basic_file<_CharT>*
104 __basic_file<_CharT>::sys_open(int __fd, ios_base::openmode __mode)
105 {
106 __basic_file* __ret = NULL;
107 int __p_mode = 0;
108 int __rw_mode = _IO_NO_READS + _IO_NO_WRITES;
109
110 _M_open_mode(__mode, __p_mode, __rw_mode);
111
112 if (!_IO_file_is_open(this))
113 {
114 _fileno = __fd;
115 _flags &= ~(_IO_NO_READS+_IO_NO_WRITES);
116 _flags |= _IO_DELETE_DONT_CLOSE;
117 _offset = _IO_pos_BAD;
118 int __mask = _IO_NO_READS + _IO_NO_WRITES + _IO_IS_APPENDING;
119 _IO_mask_flags(this, __rw_mode, __mask);
120 }
121
122 return __ret;
123 }
124
125 template<typename _CharT>
126 __basic_file<_CharT>*
127 __basic_file<_CharT>::open(const char* __name, ios_base::openmode __mode,
128 int __prot = 0664)
129 {
130 __basic_file* __ret = NULL;
131 int __p_mode = 0;
132 int __rw_mode = _IO_NO_READS + _IO_NO_WRITES;
133
134 _M_open_mode(__mode, __p_mode, __rw_mode);
135 if (!_IO_file_is_open(this))
136 {
137 __c_file_type* __f;
138 __f = _IO_file_open(this, __name, __p_mode, __prot, __rw_mode, 0);
139 __ret = __f ? this: NULL;
140 }
141 return __ret;
142 }
143
144 template<typename _CharT>
145 bool
146 __basic_file<_CharT>::is_open() { return _fileno >= 0; }
147
148 template<typename _CharT>
149 __basic_file<_CharT>*
150 __basic_file<_CharT>::close()
151 {
152 return _IO_file_close_it(this) ? static_cast<__basic_file*>(NULL) : this;
153 }
154
155 template<typename _CharT>
156 streamsize
157 __basic_file<_CharT>::xsgetn(_CharT* __s, streamsize __n)
158 { return _IO_file_xsgetn(this, __s, __n); }
159
160 // NB: Unused.
161 template<typename _CharT>
162 streamsize
163 __basic_file<_CharT>::sys_read(_CharT* __s, streamsize __n)
164 { return _IO_file_read(this, __s, __n); }
165
166 // NB: Unused.
167 template<typename _CharT>
168 streamsize
169 __basic_file<_CharT>::sys_write(const _CharT* __s, streamsize __n)
170 { return _IO_file_write(this, __s, __n); }
171
172 // NB: Unused.
173 template<typename _CharT>
174 streamoff
175 __basic_file<_CharT>::sys_seek(streamoff __pos, ios_base::seekdir __way)
176 { return _IO_file_seek(this, __pos, __way); }
177
178 // NB: Unused.
179 template<typename _CharT>
180 int
181 __basic_file<_CharT>::sys_close()
182 { return _IO_file_close(this); }
183
184 // NB: Unused.
185 template<typename _CharT>
186 int
187 __basic_file<_CharT>::sys_stat(void* __v)
188 { return _IO_file_stat(this, __v); }
189
190 // NB: Unused.
191 template<typename _CharT>
192 int
193 __basic_file<_CharT>::showmanyc() { return EOF; }
194
195 // NB: Unused.
196 template<typename _CharT>
197 void
198 __basic_file<_CharT>::imbue(void* /*__v*/) { }
199
200 // __basic_file<char> definitions
201 __basic_file<char>::__basic_file(__c_lock* __lock)
202 {
203 #ifdef _IO_MTSAFE_IO
204 _lock = __lock;
205 #endif
206 // Don't set the orientation of the stream when initializing.
207 #ifdef _GLIBCPP_USE_WCHAR_T
208 _IO_no_init(this, 0, 0, &_M_wfile, 0);
209 #else /* !defined(_GLIBCPP_USE_WCHAR_T) */
210 _IO_no_init(this, 0, 0, NULL, 0);
211 #endif /* !defined(_GLIBCPP_USE_WCHAR_T) */
212 _IO_JUMPS((_IO_FILE_plus *) this) = &_IO_file_jumps;
213 _IO_file_init((_IO_FILE_plus*)this);
214 }
215
216 // NB: Unused.
217 int
218 __basic_file<char>::overflow(int __c)
219 { return _IO_file_overflow(this, __c); }
220
221 // NB: Unused.
222 int
223 __basic_file<char>::underflow()
224 { return _IO_file_underflow(this); }
225
226 // NB: Unused.
227 int
228 __basic_file<char>::uflow()
229 { return _IO_default_uflow(this); }
230
231 // NB: Unused.
232 int
233 __basic_file<char>::pbackfail(int __c)
234 { return _IO_default_pbackfail(this, __c); }
235
236 streamsize
237 __basic_file<char>::xsputn(const char* __s, streamsize __n)
238 { return _IO_file_xsputn(this, __s, __n); }
239
240 streamoff
241 __basic_file<char>::seekoff(streamoff __off, ios_base::seekdir __way,
242 ios_base::openmode __mode)
243 { return _IO_file_seekoff(this, __off, __way, __mode); }
244
245 streamoff
246 __basic_file<char>::seekpos(streamoff __pos, ios_base::openmode __mode)
247 { return _IO_file_seekoff(this, __pos, ios_base::beg, __mode); }
248
249 // NB: Unused.
250 streambuf*
251 __basic_file<char>::setbuf(char* __b, int __len)
252 { return (streambuf*) _IO_file_setbuf(this,__b, __len); }
253
254 int
255 __basic_file<char>::sync()
256 { return _IO_file_sync(this); }
257
258 // NB: Unused.
259 int
260 __basic_file<char>::doallocate()
261 { return _IO_file_doallocate(this); }
262
263 template class __basic_file<char>;
264
265 // __basic_file<wchar_t> definitions
266 #ifdef _GLIBCPP_USE_WCHAR_T
267 __basic_file<wchar_t>::__basic_file(__c_lock* __lock)
268 {
269 #ifdef _IO_MTSAFE_IO
270 _lock = __lock;
271 #endif
272 // Don't set the orientation of the stream when initializing.
273 _IO_no_init(this, 0, 0, &_M_wfile, &_IO_wfile_jumps);
274 _IO_JUMPS((_IO_FILE_plus *) this) = &_IO_wfile_jumps;
275 _IO_file_init((_IO_FILE_plus*)this);
276
277 // In addition, need to allocate the buffer...
278 _IO_wdoallocbuf(this);
279 // Setup initial positions for this buffer...
280 // if (!(_flags & _IO_NO_READS))
281 _IO_wsetg(this, _wide_data->_IO_buf_base, _wide_data->_IO_buf_base,
282 _wide_data->_IO_buf_base);
283 // if (!(_flags & _IO_NO_WRITES))
284 _IO_wsetp(this, _wide_data->_IO_buf_base, _wide_data->_IO_buf_base);
285
286 // Setup codecvt bits...
287 _codecvt = &__c_libio_codecvt;
288
289 // Do the same for narrow bits...
290 if (_IO_write_base == NULL)
291 {
292 _IO_doallocbuf(this);
293 // if (!(_flags & _IO_NO_READS))
294 _IO_setg(this, _IO_buf_base, _IO_buf_base, _IO_buf_base);
295 // if (!(_flags & _IO_NO_WRITES))
296 _IO_setp(this, _IO_buf_base, _IO_buf_base);
297 }
298 }
299
300 int
301 __basic_file<wchar_t>::overflow(int __c)
302 { return _IO_wfile_overflow(this, __c); }
303
304 int
305 __basic_file<wchar_t>::underflow()
306 { return _IO_wfile_underflow(this); }
307
308 // NB: Unused.
309 int
310 __basic_file<wchar_t>::uflow()
311 { return _IO_wdefault_uflow(this); }
312
313 // NB: Unused.
314 int
315 __basic_file<wchar_t>::pbackfail(int __c)
316 { return _IO_wdefault_pbackfail(this, __c); }
317
318 streamsize
319 __basic_file<wchar_t>::xsputn(const wchar_t* __s, streamsize __n)
320 { return _IO_wfile_xsputn(this, __s, __n); }
321
322 streamoff
323 __basic_file<wchar_t>::seekoff(streamoff __off, ios_base::seekdir __way,
324 ios_base::openmode __mode)
325 { return _IO_wfile_seekoff(this, __off, __way, __mode); }
326
327 streamoff
328 __basic_file<wchar_t>::seekpos(streamoff __pos, ios_base::openmode __mode)
329 { return _IO_wfile_seekoff(this, __pos, ios_base::beg, __mode); }
330
331 streambuf*
332 __basic_file<wchar_t>::setbuf(wchar_t* __b, int __len)
333 { return (streambuf*) _IO_wfile_setbuf(this,__b, __len); }
334
335 int
336 __basic_file<wchar_t>::sync()
337 { return _IO_wfile_sync(this); }
338
339 int
340 __basic_file<wchar_t>::doallocate()
341 { return _IO_wfile_doallocate(this); }
342
343 template class __basic_file<wchar_t>;
344 #endif
345
346 } // namespace std
347
348
349
350
351
352
353
This page took 0.064664 seconds and 6 git commands to generate.