]> gcc.gnu.org Git - gcc.git/blob - libstdc++-v3/config/c_io_libio.cc
1747ddb3e6d0beeb2dfde8900a1d832a8a98199c
[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 // __basic_file<char> specializations
40 template<>
41 __basic_file<char>::__basic_file(__c_lock* __lock);
42
43 template<>
44 int
45 __basic_file<char>::overflow(int __c);
46
47 template<>
48 int
49 __basic_file<char>::underflow();
50
51 template<>
52 int
53 __basic_file<char>::uflow();
54
55 template<>
56 int
57 __basic_file<char>::pbackfail(int __c);
58
59 template<>
60 streamsize
61 __basic_file<char>::xsputn(const char* __s, streamsize __n);
62
63 template<>
64 streamoff
65 __basic_file<char>::seekoff(streamoff __off, ios_base::seekdir __way,
66 ios_base::openmode __mode);
67
68 template<>
69 streamoff
70 __basic_file<char>::seekpos(streamoff __pos, ios_base::openmode __mode);
71
72 template<>
73 streambuf*
74 __basic_file<char>::setbuf(char* __b, int __len);
75
76 template<>
77 int
78 __basic_file<char>::sync();
79
80 template<>
81 int
82 __basic_file<char>::doallocate();
83
84 // __basic_file<wchar_t> specializations
85 #ifdef _GLIBCPP_USE_WCHAR_T
86 template<>
87 __basic_file<wchar_t>::__basic_file(__c_lock* __lock);
88
89 template<>
90 int
91 __basic_file<wchar_t>::overflow(int __c);
92
93 template<>
94 int
95 __basic_file<wchar_t>::underflow();
96
97 template<>
98 int
99 __basic_file<wchar_t>::uflow();
100
101 template<>
102 int
103 __basic_file<wchar_t>::pbackfail(int __c);
104
105 template<>
106 streamsize
107 __basic_file<wchar_t>::xsputn(const wchar_t* __s, streamsize __n);
108
109 template<>
110 streamoff
111 __basic_file<wchar_t>::seekoff(streamoff __off, ios_base::seekdir __way,
112 ios_base::openmode __mode);
113
114 template<>
115 streamoff
116 __basic_file<wchar_t>::seekpos(streamoff __pos, ios_base::openmode __mode);
117
118 template<>
119 streambuf*
120 __basic_file<wchar_t>::setbuf(wchar_t* __b, int __len);
121
122 template<>
123 int
124 __basic_file<wchar_t>::sync();
125
126 template<>
127 int
128 __basic_file<wchar_t>::doallocate();
129 #endif
130
131 // Generic definitions for __basic_file
132 template<typename _CharT>
133 int
134 __basic_file<_CharT>::get_fileno(void)
135 { return _fileno; }
136
137 template<typename _CharT>
138 __basic_file<_CharT>::~__basic_file()
139 { _IO_file_finish(this, 0); }
140
141 template<typename _CharT>
142 void
143 __basic_file<_CharT>::_M_open_mode(ios_base::openmode __mode,
144 int& __p_mode, int& __rw_mode,
145 char* /*__c_mode*/)
146 {
147 #ifdef O_BINARY
148 bool __testb = __mode & ios_base::binary;
149 #endif
150 bool __testi = __mode & ios_base::in;
151 bool __testo = __mode & ios_base::out;
152 bool __testt = __mode & ios_base::trunc;
153 bool __testa = __mode & ios_base::app;
154
155 if (!__testi && __testo && !__testt && !__testa)
156 {
157 __p_mode = O_WRONLY | O_TRUNC | O_CREAT;
158 __rw_mode = _IO_NO_READS;
159 }
160 if (!__testi && __testo && !__testt && __testa)
161 {
162 __p_mode = O_WRONLY | O_APPEND | O_CREAT;
163 __rw_mode = _IO_NO_READS | _IO_IS_APPENDING;
164 }
165 if (!__testi && __testo && __testt && !__testa)
166 {
167 __p_mode = O_WRONLY | O_TRUNC | O_CREAT;
168 __rw_mode = _IO_NO_READS;
169 }
170 if (__testi && !__testo && !__testt && !__testa)
171 {
172 __p_mode = O_RDONLY;
173 __rw_mode = _IO_NO_WRITES;
174 }
175 if (__testi && __testo && !__testt && !__testa)
176 {
177 __p_mode = O_RDWR;
178 __rw_mode = 0;
179 }
180 if (__testi && __testo && __testt && !__testa)
181 {
182 __p_mode = O_RDWR | O_TRUNC | O_CREAT;
183 __rw_mode = 0;
184 }
185 #ifdef O_BINARY
186 if (__testb)
187 __p_mode |= O_BINARY;
188 #endif
189 }
190
191 template<typename _CharT>
192 __basic_file<_CharT>*
193 __basic_file<_CharT>::sys_open(int __fd, ios_base::openmode __mode)
194 {
195 __basic_file* __ret = NULL;
196 int __p_mode = 0;
197 int __rw_mode = _IO_NO_READS + _IO_NO_WRITES;
198 char __c_mode[4];
199
200 _M_open_mode(__mode, __p_mode, __rw_mode, __c_mode);
201
202 if (!_IO_file_is_open(this))
203 {
204 _fileno = __fd;
205 _flags &= ~(_IO_NO_READS+_IO_NO_WRITES);
206 _flags |= _IO_DELETE_DONT_CLOSE;
207 _offset = _IO_pos_BAD;
208 int __mask = _IO_NO_READS + _IO_NO_WRITES + _IO_IS_APPENDING;
209 _IO_mask_flags(this, __rw_mode, __mask);
210 }
211
212 return __ret;
213 }
214
215 template<typename _CharT>
216 __basic_file<_CharT>*
217 __basic_file<_CharT>::open(const char* __name, ios_base::openmode __mode,
218 int __prot)
219 {
220 __basic_file* __ret = NULL;
221 int __p_mode = 0;
222 int __rw_mode = _IO_NO_READS + _IO_NO_WRITES;
223 char __c_mode[4];
224
225 _M_open_mode(__mode, __p_mode, __rw_mode, __c_mode);
226 if (!_IO_file_is_open(this))
227 {
228 __c_file_type* __f;
229 __f = _IO_file_open(this, __name, __p_mode, __prot, __rw_mode, 0);
230 __ret = __f ? this: NULL;
231 }
232 return __ret;
233 }
234
235 template<typename _CharT>
236 bool
237 __basic_file<_CharT>::is_open() { return _fileno >= 0; }
238
239 template<typename _CharT>
240 __basic_file<_CharT>*
241 __basic_file<_CharT>::close()
242 {
243 return _IO_file_close_it(this) ? static_cast<__basic_file*>(NULL) : this;
244 }
245
246 template<typename _CharT>
247 streamsize
248 __basic_file<_CharT>::xsgetn(_CharT* __s, streamsize __n)
249 { return _IO_file_xsgetn(this, __s, __n); }
250
251 // NB: Unused.
252 template<typename _CharT>
253 streamsize
254 __basic_file<_CharT>::sys_read(_CharT* __s, streamsize __n)
255 { return _IO_file_read(this, __s, __n); }
256
257 // NB: Unused.
258 template<typename _CharT>
259 streamsize
260 __basic_file<_CharT>::sys_write(const _CharT* __s, streamsize __n)
261 { return _IO_file_write(this, __s, __n); }
262
263 // NB: Unused.
264 template<typename _CharT>
265 streamoff
266 __basic_file<_CharT>::sys_seek(streamoff __pos, ios_base::seekdir __way)
267 { return _IO_file_seek(this, __pos, __way); }
268
269 // NB: Unused.
270 template<typename _CharT>
271 int
272 __basic_file<_CharT>::sys_close()
273 { return _IO_file_close(this); }
274
275 // NB: Unused.
276 template<typename _CharT>
277 int
278 __basic_file<_CharT>::sys_stat(void* __v)
279 { return _IO_file_stat(this, __v); }
280
281 // NB: Unused.
282 template<typename _CharT>
283 int
284 __basic_file<_CharT>::showmanyc() { return EOF; }
285
286 // NB: Unused.
287 template<typename _CharT>
288 void
289 __basic_file<_CharT>::imbue(void* /*__v*/) { }
290
291 // __basic_file<char> definitions
292 __basic_file<char>::__basic_file(__c_lock* __lock)
293 {
294 #ifdef _IO_MTSAFE_IO
295 _lock = __lock;
296 #endif
297 // Don't set the orientation of the stream when initializing.
298 #ifdef _GLIBCPP_USE_WCHAR_T
299 _IO_no_init(this, 0, 0, &_M_wfile, 0);
300 #else /* !defined(_GLIBCPP_USE_WCHAR_T) */
301 _IO_no_init(this, 0, 0, NULL, 0);
302 #endif /* !defined(_GLIBCPP_USE_WCHAR_T) */
303 _IO_JUMPS((_IO_FILE_plus *) this) = &_IO_file_jumps;
304 _IO_file_init((_IO_FILE_plus*)this);
305 }
306
307 // NB: Unused.
308 int
309 __basic_file<char>::overflow(int __c)
310 { return _IO_file_overflow(this, __c); }
311
312 // NB: Unused.
313 int
314 __basic_file<char>::underflow()
315 { return _IO_file_underflow(this); }
316
317 // NB: Unused.
318 int
319 __basic_file<char>::uflow()
320 { return _IO_default_uflow(this); }
321
322 // NB: Unused.
323 int
324 __basic_file<char>::pbackfail(int __c)
325 { return _IO_default_pbackfail(this, __c); }
326
327 streamsize
328 __basic_file<char>::xsputn(const char* __s, streamsize __n)
329 { return _IO_file_xsputn(this, __s, __n); }
330
331 streamoff
332 __basic_file<char>::seekoff(streamoff __off, ios_base::seekdir __way,
333 ios_base::openmode __mode)
334 { return _IO_file_seekoff(this, __off, __way, __mode); }
335
336 streamoff
337 __basic_file<char>::seekpos(streamoff __pos, ios_base::openmode __mode)
338 { return _IO_file_seekoff(this, __pos, ios_base::beg, __mode); }
339
340 // NB: Unused.
341 streambuf*
342 __basic_file<char>::setbuf(char* __b, int __len)
343 { return (streambuf*) _IO_file_setbuf(this,__b, __len); }
344
345 int
346 __basic_file<char>::sync()
347 { return _IO_file_sync(this); }
348
349 // NB: Unused.
350 int
351 __basic_file<char>::doallocate()
352 { return _IO_file_doallocate(this); }
353
354 // __basic_file<wchar_t> definitions
355 #ifdef _GLIBCPP_USE_WCHAR_T
356 __basic_file<wchar_t>::__basic_file(__c_lock* __lock)
357 {
358 #ifdef _IO_MTSAFE_IO
359 _lock = __lock;
360 #endif
361 // Don't set the orientation of the stream when initializing.
362 _IO_no_init(this, 0, 0, &_M_wfile, &_IO_wfile_jumps);
363 _IO_JUMPS((_IO_FILE_plus *) this) = &_IO_wfile_jumps;
364 _IO_file_init((_IO_FILE_plus*)this);
365
366 // In addition, need to allocate the buffer...
367 _IO_wdoallocbuf(this);
368 // Setup initial positions for this buffer...
369 // if (!(_flags & _IO_NO_READS))
370 _IO_wsetg(this, _wide_data->_IO_buf_base, _wide_data->_IO_buf_base,
371 _wide_data->_IO_buf_base);
372 // if (!(_flags & _IO_NO_WRITES))
373 _IO_wsetp(this, _wide_data->_IO_buf_base, _wide_data->_IO_buf_base);
374
375 // Setup codecvt bits...
376 _codecvt = &__c_libio_codecvt;
377
378 // Do the same for narrow bits...
379 if (_IO_write_base == NULL)
380 {
381 _IO_doallocbuf(this);
382 // if (!(_flags & _IO_NO_READS))
383 _IO_setg(this, _IO_buf_base, _IO_buf_base, _IO_buf_base);
384 // if (!(_flags & _IO_NO_WRITES))
385 _IO_setp(this, _IO_buf_base, _IO_buf_base);
386 }
387 }
388
389 int
390 __basic_file<wchar_t>::overflow(int __c)
391 { return _IO_wfile_overflow(this, __c); }
392
393 int
394 __basic_file<wchar_t>::underflow()
395 { return _IO_wfile_underflow(this); }
396
397 // NB: Unused.
398 int
399 __basic_file<wchar_t>::uflow()
400 { return _IO_wdefault_uflow(this); }
401
402 // NB: Unused.
403 int
404 __basic_file<wchar_t>::pbackfail(int __c)
405 { return _IO_wdefault_pbackfail(this, __c); }
406
407 streamsize
408 __basic_file<wchar_t>::xsputn(const wchar_t* __s, streamsize __n)
409 { return _IO_wfile_xsputn(this, __s, __n); }
410
411 streamoff
412 __basic_file<wchar_t>::seekoff(streamoff __off, ios_base::seekdir __way,
413 ios_base::openmode __mode)
414 { return _IO_wfile_seekoff(this, __off, __way, __mode); }
415
416 streamoff
417 __basic_file<wchar_t>::seekpos(streamoff __pos, ios_base::openmode __mode)
418 { return _IO_wfile_seekoff(this, __pos, ios_base::beg, __mode); }
419
420 streambuf*
421 __basic_file<wchar_t>::setbuf(wchar_t* __b, int __len)
422 { return (streambuf*) _IO_wfile_setbuf(this,__b, __len); }
423
424 int
425 __basic_file<wchar_t>::sync()
426 { return _IO_wfile_sync(this); }
427
428 int
429 __basic_file<wchar_t>::doallocate()
430 { return _IO_wfile_doallocate(this); }
431 #endif
432
433 // Need to instantiate base class here for type-info bits, etc
434 template struct __basic_file_base<char>;
435 template class __basic_file<char>;
436 #ifdef _GLIBCPP_USE_WCHAR_T
437 template struct __basic_file_base<wchar_t>;
438 template class __basic_file<wchar_t>;
439 #endif
440 } // namespace std
441
442
443
444
445
446
447
This page took 0.057562 seconds and 5 git commands to generate.