* config/avr/avr.md ("mcu_enhanced"): New attribute.
[official-gcc.git] / libstdc++-v3 / config / c_io_libio.cc
blob6f001b2930084485b20f3a21bfdb07d7115eac19
1 // Wrapper of C-language FILE struct -*- C++ -*-
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.
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 #include <bits/basic_file.h>
35 #include <libioP.h>
36 #include <fcntl.h> // Solaris needs for O_* macros
38 namespace std {
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>
46 int
47 __basic_file<_CharT>::get_fileno(void)
48 { return _fileno; }
50 template<typename _CharT>
51 __basic_file<_CharT>::~__basic_file()
52 { _IO_file_finish(this, 0); }
54 template<typename _CharT>
55 void
56 __basic_file<_CharT>::_M_open_mode(ios_base::openmode __mode,
57 int& __p_mode, int& __rw_mode)
59 #ifdef O_BINARY
60 bool __testb = __mode & ios_base::binary;
61 #endif
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)
84 __p_mode = O_RDONLY;
85 __rw_mode = _IO_NO_WRITES;
87 if (__testi && __testo && !__testt && !__testa)
89 __p_mode = O_RDWR;
90 __rw_mode = 0;
92 if (__testi && __testo && __testt && !__testa)
94 __p_mode = O_RDWR | O_TRUNC | O_CREAT;
95 __rw_mode = 0;
97 #ifdef O_BINARY
98 if (__testb)
99 __p_mode |= O_BINARY;
100 #endif
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;
108 int __p_mode = 0;
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))
115 _fileno = __fd;
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);
123 return __ret;
126 template<typename _CharT>
127 __basic_file<_CharT>*
128 __basic_file<_CharT>::open(const char* __name, ios_base::openmode __mode,
129 int __prot = 0664)
131 __basic_file* __ret = NULL;
132 int __p_mode = 0;
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))
138 __c_file_type* __f;
139 __f = _IO_file_open(this, __name, __p_mode, __prot, __rw_mode, 0);
140 __ret = __f ? this: NULL;
142 return __ret;
145 template<typename _CharT>
146 bool
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>
157 streamsize
158 __basic_file<_CharT>::xsgetn(_CharT* __s, streamsize __n)
159 { return _IO_file_xsgetn(this, __s, __n); }
161 // NB: Unused.
162 template<typename _CharT>
163 streamsize
164 __basic_file<_CharT>::sys_read(_CharT* __s, streamsize __n)
165 { return _IO_file_read(this, __s, __n); }
167 // NB: Unused.
168 template<typename _CharT>
169 streamsize
170 __basic_file<_CharT>::sys_write(const _CharT* __s, streamsize __n)
171 { return _IO_file_write(this, __s, __n); }
173 // NB: Unused.
174 template<typename _CharT>
175 streamoff
176 __basic_file<_CharT>::sys_seek(streamoff __pos, ios_base::seekdir __way)
177 { return _IO_file_seek(this, __pos, __way); }
179 // NB: Unused.
180 template<typename _CharT>
181 int
182 __basic_file<_CharT>::sys_close()
183 { return _IO_file_close(this); }
185 // NB: Unused.
186 template<typename _CharT>
187 int
188 __basic_file<_CharT>::sys_stat(void* __v)
189 { return _IO_file_stat(this, __v); }
191 // NB: Unused.
192 template<typename _CharT>
193 int
194 __basic_file<_CharT>::showmanyc() { return EOF; }
196 // NB: Unused.
197 template<typename _CharT>
198 void
199 __basic_file<_CharT>::imbue(void* /*__v*/) { }
201 // __basic_file<char> definitions
202 __basic_file<char>::__basic_file(__c_lock* __lock)
204 #ifdef _IO_MTSAFE_IO
205 _lock = __lock;
206 #endif
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);
213 // NB: Unused.
214 int
215 __basic_file<char>::overflow(int __c)
216 { return _IO_file_overflow(this, __c); }
218 // NB: Unused.
219 int
220 __basic_file<char>::underflow()
221 { return _IO_file_underflow(this); }
223 // NB: Unused.
224 int
225 __basic_file<char>::uflow()
226 { return _IO_default_uflow(this); }
228 // NB: Unused.
229 int
230 __basic_file<char>::pbackfail(int __c)
231 { return _IO_default_pbackfail(this, __c); }
233 streamsize
234 __basic_file<char>::xsputn(const char* __s, streamsize __n)
235 { return _IO_file_xsputn(this, __s, __n); }
237 streamoff
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); }
242 streamoff
243 __basic_file<char>::seekpos(streamoff __pos, ios_base::openmode __mode)
244 { return _IO_file_seekoff(this, __pos, ios_base::beg, __mode); }
246 // NB: Unused.
247 streambuf*
248 __basic_file<char>::setbuf(char* __b, int __len)
249 { return (streambuf*) _IO_file_setbuf(this,__b, __len); }
251 int
252 __basic_file<char>::sync()
253 { return _IO_file_sync(this); }
255 // NB: Unused.
256 int
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)
266 #ifdef _IO_MTSAFE_IO
267 _lock = __lock;
268 #endif
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);
297 int
298 __basic_file<wchar_t>::overflow(int __c)
299 { return _IO_wfile_overflow(this, __c); }
301 int
302 __basic_file<wchar_t>::underflow()
303 { return _IO_wfile_underflow(this); }
305 // NB: Unused.
306 int
307 __basic_file<wchar_t>::uflow()
308 { return _IO_wdefault_uflow(this); }
310 // NB: Unused.
311 int
312 __basic_file<wchar_t>::pbackfail(int __c)
313 { return _IO_wdefault_pbackfail(this, __c); }
315 streamsize
316 __basic_file<wchar_t>::xsputn(const wchar_t* __s, streamsize __n)
317 { return _IO_wfile_xsputn(this, __s, __n); }
319 streamoff
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); }
324 streamoff
325 __basic_file<wchar_t>::seekpos(streamoff __pos, ios_base::openmode __mode)
326 { return _IO_wfile_seekoff(this, __pos, ios_base::beg, __mode); }
328 streambuf*
329 __basic_file<wchar_t>::setbuf(wchar_t* __b, int __len)
330 { return (streambuf*) _IO_wfile_setbuf(this,__b, __len); }
332 int
333 __basic_file<wchar_t>::sync()
334 { return _IO_wfile_sync(this); }
336 int
337 __basic_file<wchar_t>::doallocate()
338 { return _IO_wfile_doallocate(this); }
340 template class __basic_file<wchar_t>;
341 #endif
343 } // namespace std