1 // Wrapper of C-language FILE struct -*- C++ -*-
3 // Copyright (C) 2000 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 #include <bits/basic_file.h>
36 #include <fcntl.h> // Solaris needs for O_* macros
40 // Need to instantiate base class here for type-info bits, etc
41 template struct __basic_file_base
<char>;
42 template struct __basic_file_base
<wchar_t>;
44 // Generic definitions for __basic_file
45 template<typename _CharT
>
47 __basic_file
<_CharT
>::get_fileno(void)
50 template<typename _CharT
>
51 __basic_file
<_CharT
>::~__basic_file()
52 { _IO_file_finish(this, 0); }
54 template<typename _CharT
>
56 __basic_file
<_CharT
>::_M_open_mode(ios_base::openmode __mode
,
57 int& __p_mode
, int& __rw_mode
)
60 bool __testb
= __mode
& ios_base::binary
;
62 bool __testi
= __mode
& ios_base::in
;
63 bool __testo
= __mode
& ios_base::out
;
64 bool __testt
= __mode
& ios_base::trunc
;
65 bool __testa
= __mode
& ios_base::app
;
67 if (!__testi
&& __testo
&& !__testt
&& !__testa
)
69 __p_mode
= O_WRONLY
| O_TRUNC
| O_CREAT
;
70 __rw_mode
= _IO_NO_READS
;
72 if (!__testi
&& __testo
&& !__testt
&& __testa
)
74 __p_mode
= O_WRONLY
| O_APPEND
| O_CREAT
;
75 __rw_mode
= _IO_NO_READS
| _IO_IS_APPENDING
;
77 if (!__testi
&& __testo
&& __testt
&& !__testa
)
79 __p_mode
= O_WRONLY
| O_TRUNC
| O_CREAT
;
80 __rw_mode
= _IO_NO_READS
;
82 if (__testi
&& !__testo
&& !__testt
&& !__testa
)
85 __rw_mode
= _IO_NO_WRITES
;
87 if (__testi
&& __testo
&& !__testt
&& !__testa
)
92 if (__testi
&& __testo
&& __testt
&& !__testa
)
94 __p_mode
= O_RDWR
| O_TRUNC
| O_CREAT
;
103 template<typename _CharT
>
104 __basic_file
<_CharT
>*
105 __basic_file
<_CharT
>::sys_open(int __fd
, ios_base::openmode __mode
)
107 __basic_file
* __ret
= NULL
;
109 int __rw_mode
= _IO_NO_READS
+ _IO_NO_WRITES
;
111 _M_open_mode(__mode
, __p_mode
, __rw_mode
);
113 if (!_IO_file_is_open(this))
116 _flags
&= ~(_IO_NO_READS
+_IO_NO_WRITES
);
117 _flags
|= _IO_DELETE_DONT_CLOSE
;
118 _offset
= _IO_pos_BAD
;
119 int __mask
= _IO_NO_READS
+ _IO_NO_WRITES
+ _IO_IS_APPENDING
;
120 _IO_mask_flags(this, __rw_mode
, __mask
);
126 template<typename _CharT
>
127 __basic_file
<_CharT
>*
128 __basic_file
<_CharT
>::open(const char* __name
, ios_base::openmode __mode
,
131 __basic_file
* __ret
= NULL
;
133 int __rw_mode
= _IO_NO_READS
+ _IO_NO_WRITES
;
135 _M_open_mode(__mode
, __p_mode
, __rw_mode
);
136 if (!_IO_file_is_open(this))
139 __f
= _IO_file_open(this, __name
, __p_mode
, __prot
, __rw_mode
, 0);
140 __ret
= __f
? this: NULL
;
145 template<typename _CharT
>
147 __basic_file
<_CharT
>::is_open() { return _fileno
>= 0; }
149 template<typename _CharT
>
150 __basic_file
<_CharT
>*
151 __basic_file
<_CharT
>::close()
153 return _IO_file_close_it(this) ? static_cast<__basic_file
*>(NULL
) : this;
156 template<typename _CharT
>
158 __basic_file
<_CharT
>::xsgetn(_CharT
* __s
, streamsize __n
)
159 { return _IO_file_xsgetn(this, __s
, __n
); }
162 template<typename _CharT
>
164 __basic_file
<_CharT
>::sys_read(_CharT
* __s
, streamsize __n
)
165 { return _IO_file_read(this, __s
, __n
); }
168 template<typename _CharT
>
170 __basic_file
<_CharT
>::sys_write(const _CharT
* __s
, streamsize __n
)
171 { return _IO_file_write(this, __s
, __n
); }
174 template<typename _CharT
>
176 __basic_file
<_CharT
>::sys_seek(streamoff __pos
, ios_base::seekdir __way
)
177 { return _IO_file_seek(this, __pos
, __way
); }
180 template<typename _CharT
>
182 __basic_file
<_CharT
>::sys_close()
183 { return _IO_file_close(this); }
186 template<typename _CharT
>
188 __basic_file
<_CharT
>::sys_stat(void* __v
)
189 { return _IO_file_stat(this, __v
); }
192 template<typename _CharT
>
194 __basic_file
<_CharT
>::showmanyc() { return EOF
; }
197 template<typename _CharT
>
199 __basic_file
<_CharT
>::imbue(void* /*__v*/) { }
201 // __basic_file<char> definitions
202 __basic_file
<char>::__basic_file(__c_lock
* __lock
)
207 // Don't set the orientation of the stream when initializing.
208 _IO_no_init(this, 0, 0, &_M_wfile
, 0);
209 _IO_JUMPS((_IO_FILE_plus
*) this) = &_IO_file_jumps
;
210 _IO_file_init((_IO_FILE_plus
*)this);
215 __basic_file
<char>::overflow(int __c
)
216 { return _IO_file_overflow(this, __c
); }
220 __basic_file
<char>::underflow()
221 { return _IO_file_underflow(this); }
225 __basic_file
<char>::uflow()
226 { return _IO_default_uflow(this); }
230 __basic_file
<char>::pbackfail(int __c
)
231 { return _IO_default_pbackfail(this, __c
); }
234 __basic_file
<char>::xsputn(const char* __s
, streamsize __n
)
235 { return _IO_file_xsputn(this, __s
, __n
); }
238 __basic_file
<char>::seekoff(streamoff __off
, ios_base::seekdir __way
,
239 ios_base::openmode __mode
)
240 { return _IO_file_seekoff(this, __off
, __way
, __mode
); }
243 __basic_file
<char>::seekpos(streamoff __pos
, ios_base::openmode __mode
)
244 { return _IO_file_seekoff(this, __pos
, ios_base::beg
, __mode
); }
248 __basic_file
<char>::setbuf(char* __b
, int __len
)
249 { return (streambuf
*) _IO_file_setbuf(this,__b
, __len
); }
252 __basic_file
<char>::sync()
253 { return _IO_file_sync(this); }
257 __basic_file
<char>::doallocate()
258 { return _IO_file_doallocate(this); }
260 template class __basic_file
<char>;
262 // __basic_file<wchar_t> definitions
263 #ifdef _GLIBCPP_USE_WCHAR_T
264 __basic_file
<wchar_t>::__basic_file(__c_lock
* __lock
)
269 // Don't set the orientation of the stream when initializing.
270 _IO_no_init(this, 0, 0, &_M_wfile
, &_IO_wfile_jumps
);
271 _IO_JUMPS((_IO_FILE_plus
*) this) = &_IO_wfile_jumps
;
272 _IO_file_init((_IO_FILE_plus
*)this);
274 // In addition, need to allocate the buffer...
275 _IO_wdoallocbuf(this);
276 // Setup initial positions for this buffer...
277 // if (!(_flags & _IO_NO_READS))
278 _IO_wsetg(this, _wide_data
->_IO_buf_base
, _wide_data
->_IO_buf_base
,
279 _wide_data
->_IO_buf_base
);
280 // if (!(_flags & _IO_NO_WRITES))
281 _IO_wsetp(this, _wide_data
->_IO_buf_base
, _wide_data
->_IO_buf_base
);
283 // Setup codecvt bits...
284 _codecvt
= &__c_libio_codecvt
;
286 // Do the same for narrow bits...
287 if (_IO_write_base
== NULL
)
289 _IO_doallocbuf(this);
290 // if (!(_flags & _IO_NO_READS))
291 _IO_setg(this, _IO_buf_base
, _IO_buf_base
, _IO_buf_base
);
292 // if (!(_flags & _IO_NO_WRITES))
293 _IO_setp(this, _IO_buf_base
, _IO_buf_base
);
298 __basic_file
<wchar_t>::overflow(int __c
)
299 { return _IO_wfile_overflow(this, __c
); }
302 __basic_file
<wchar_t>::underflow()
303 { return _IO_wfile_underflow(this); }
307 __basic_file
<wchar_t>::uflow()
308 { return _IO_wdefault_uflow(this); }
312 __basic_file
<wchar_t>::pbackfail(int __c
)
313 { return _IO_wdefault_pbackfail(this, __c
); }
316 __basic_file
<wchar_t>::xsputn(const wchar_t* __s
, streamsize __n
)
317 { return _IO_wfile_xsputn(this, __s
, __n
); }
320 __basic_file
<wchar_t>::seekoff(streamoff __off
, ios_base::seekdir __way
,
321 ios_base::openmode __mode
)
322 { return _IO_wfile_seekoff(this, __off
, __way
, __mode
); }
325 __basic_file
<wchar_t>::seekpos(streamoff __pos
, ios_base::openmode __mode
)
326 { return _IO_wfile_seekoff(this, __pos
, ios_base::beg
, __mode
); }
329 __basic_file
<wchar_t>::setbuf(wchar_t* __b
, int __len
)
330 { return (streambuf
*) _IO_wfile_setbuf(this,__b
, __len
); }
333 __basic_file
<wchar_t>::sync()
334 { return _IO_wfile_sync(this); }
337 __basic_file
<wchar_t>::doallocate()
338 { return _IO_wfile_doallocate(this); }
340 template class __basic_file
<wchar_t>;