1 // Wrapper of C-language FILE struct -*- C++ -*-
3 // Copyright (C) 2000, 2001 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
38 // Generic definitions for __basic_file
39 template<typename _CharT>
40 __basic_file<_CharT>::__basic_file(__c_lock* /*__lock*/)
41 : _M_cfile(NULL), _M_cfile_created(false) { }
43 template<typename _CharT>
44 __basic_file<_CharT>::~__basic_file()
53 template<typename _CharT>
55 __basic_file<_CharT>::_M_open_mode(ios_base::openmode __mode,
56 int& /*__p_mode*/, int& /*__rw_mode*/,
59 bool __testb = __mode & ios_base::binary;
60 bool __testi = __mode & ios_base::in;
61 bool __testo = __mode & ios_base::out;
62 bool __testt = __mode & ios_base::trunc;
63 bool __testa = __mode & ios_base::app;
65 if (!__testi && __testo && !__testt && !__testa)
66 strcpy(__c_mode, "w");
67 if (!__testi && __testo && !__testt && __testa)
68 strcpy(__c_mode, "a");
69 if (!__testi && __testo && __testt && !__testa)
70 strcpy(__c_mode, "w");
71 if (__testi && !__testo && !__testt && !__testa)
72 strcpy(__c_mode, "r");
73 if (__testi && __testo && !__testt && !__testa)
74 strcpy(__c_mode, "r+");
75 if (__testi && __testo && __testt && !__testa)
76 strcpy(__c_mode, "w+");
78 strcat(__c_mode, "b");
81 template<typename _CharT>
83 __basic_file<_CharT>::sys_open(__c_file_type* __file, ios_base::openmode)
85 __basic_file* __ret = NULL;
87 if (!this->is_open() && __file)
90 _M_cfile_created = false;
97 template<typename _CharT>
99 __basic_file<_CharT>::open(const char* __name, ios_base::openmode __mode,
102 __basic_file* __ret = NULL;
107 _M_open_mode(__mode, __p_mode, __rw_mode, __c_mode);
109 if (!this->is_open())
111 if ((_M_cfile = fopen(__name, __c_mode)))
113 _M_cfile_created = true;
120 template<typename _CharT>
122 __basic_file<_CharT>::is_open() { return _M_cfile != 0; }
124 template<typename _CharT>
125 __basic_file<_CharT>*
126 __basic_file<_CharT>::close()
128 __basic_file* __retval = static_cast<__basic_file*>(NULL);
129 if (_M_cfile_created && fclose(_M_cfile))
134 template<typename _CharT>
136 __basic_file<_CharT>::xsgetn(_CharT* __s, streamsize __n)
137 { return fread(__s, 1, __n, _M_cfile); }
139 template<typename _CharT>
141 __basic_file<_CharT>::xsputn(const _CharT* __s, streamsize __n)
142 { return fwrite(__s, 1, __n, _M_cfile); }
144 template<typename _CharT>
146 __basic_file<_CharT>::seekoff(streamoff __off, ios_base::seekdir __way,
147 ios_base::openmode /*__mode*/)
148 { fseek(_M_cfile, __off, __way); return ftell(_M_cfile); }
150 template<typename _CharT>
152 __basic_file<_CharT>::seekpos(streamoff __pos,
153 ios_base::openmode /*__mode*/)
154 { fseek(_M_cfile, __pos, ios_base::beg); return ftell(_M_cfile); }
156 template<typename _CharT>
158 __basic_file<_CharT>::sync()
159 { return fflush(_M_cfile); }
162 template<typename _CharT>
164 __basic_file<_CharT>::overflow(int /*__c*/)
168 template<typename _CharT>
170 __basic_file<_CharT>::underflow()
174 template<typename _CharT>
176 __basic_file<_CharT>::uflow()
180 template<typename _CharT>
182 __basic_file<_CharT>::pbackfail(int /*__c*/)
186 template<typename _CharT>
188 __basic_file<_CharT>::setbuf(_CharT* /*__b*/, int /*__len*/)
189 { return reinterpret_cast<streambuf*>(this); }
192 template<typename _CharT>
194 __basic_file<_CharT>::doallocate()
198 template<typename _CharT>
200 __basic_file<_CharT>::sys_read(_CharT* __s, streamsize __n)
201 { return fread(__s, 1, __n, _M_cfile); }
204 template<typename _CharT>
206 __basic_file<_CharT>::sys_write(const _CharT* __s, streamsize __n)
207 { return fwrite(__s, 1, __n, _M_cfile); }
210 template<typename _CharT>
212 __basic_file<_CharT>::sys_seek(streamoff __pos, ios_base::seekdir __way)
214 fseek(_M_cfile, __pos, __way);
215 return ftell(_M_cfile);
219 template<typename _CharT>
221 __basic_file<_CharT>::sys_close()
222 { return fclose(_M_cfile); }
225 template<typename _CharT>
227 __basic_file<_CharT>::sys_stat(void* /*__v*/)
231 template<typename _CharT>
233 __basic_file<_CharT>::showmanyc()
237 template<typename _CharT>
239 __basic_file<_CharT>::imbue(void* /*__v*/) { }