]> gcc.gnu.org Git - gcc.git/blob - libstdc++-v3/config/c_io_stdio.cc
basic_file.h (_M_open_mode): Change signature, move specializations to..
[gcc.git] / libstdc++-v3 / config / c_io_stdio.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
36 namespace std {
37
38 // Generic definitions for __basic_file
39 template<typename _CharT>
40 __basic_file<_CharT>::__basic_file(__c_lock* /*__lock*/)
41 : _M_fileno(-1), _M_cfile(NULL) { }
42
43 template<typename _CharT>
44 int
45 __basic_file<_CharT>::get_fileno(void)
46 { return _M_fileno; }
47
48 template<typename _CharT>
49 __basic_file<_CharT>::~__basic_file()
50 {
51 if (this->is_open())
52 {
53 fflush(_M_cfile);
54 this->close();
55 }
56 }
57
58 template<typename _CharT>
59 void
60 __basic_file<_CharT>::_M_open_mode(ios_base::openmode __mode,
61 int& /*__p_mode*/, int& /*__rw_mode*/,
62 char* __c_mode)
63 {
64 bool __testb = __mode & ios_base::binary;
65 bool __testi = __mode & ios_base::in;
66 bool __testo = __mode & ios_base::out;
67 bool __testt = __mode & ios_base::trunc;
68 bool __testa = __mode & ios_base::app;
69
70 if (!__testi && __testo && !__testt && !__testa)
71 strcpy(__c_mode, "w");
72 if (!__testi && __testo && !__testt && __testa)
73 strcpy(__c_mode, "a");
74 if (!__testi && __testo && __testt && !__testa)
75 strcpy(__c_mode, "w");
76 if (__testi && !__testo && !__testt && !__testa)
77 strcpy(__c_mode, "r");
78 if (__testi && __testo && !__testt && !__testa)
79 strcpy(__c_mode, "r+");
80 if (__testi && __testo && __testt && !__testa)
81 strcpy(__c_mode, "w+");
82 if (__testb)
83 strcat(__c_mode, "b");
84 }
85
86 template<typename _CharT>
87 __basic_file<_CharT>*
88 __basic_file<_CharT>::sys_open(int __fd, ios_base::openmode __mode)
89 {
90 __basic_file* __ret = NULL;
91 int __p_mode = 0;
92 int __rw_mode = 0;
93 char __c_mode[4];
94
95 _M_open_mode(__mode, __p_mode, __rw_mode, __c_mode);
96
97 if (!this->is_open())
98 {
99 if ((_M_cfile = fdopen(__fd, __c_mode)))
100 {
101 _M_fileno = __fd;
102 __ret = this;
103 }
104 }
105 return __ret;
106 }
107
108 template<typename _CharT>
109 __basic_file<_CharT>*
110 __basic_file<_CharT>::open(const char* __name, ios_base::openmode __mode,
111 int /*__prot*/)
112 {
113 __basic_file* __ret = NULL;
114 int __p_mode = 0;
115 int __rw_mode = 0;
116 char __c_mode[4];
117
118 _M_open_mode(__mode, __p_mode, __rw_mode, __c_mode);
119
120 if (!this->is_open())
121 {
122 if ((_M_cfile = fopen(__name, __c_mode)))
123 {
124 _M_fileno = fileno(_M_cfile);
125 __ret = this;
126 }
127 }
128 return __ret;
129 }
130
131 template<typename _CharT>
132 bool
133 __basic_file<_CharT>::is_open() { return _M_fileno >= 0; }
134
135 template<typename _CharT>
136 __basic_file<_CharT>*
137 __basic_file<_CharT>::close()
138 {
139 __basic_file* __retval = static_cast<__basic_file*>(NULL);
140 bool __testopen = fclose(_M_cfile);
141 if (!__testopen)
142 {
143 __retval = this;
144 _M_fileno = -1;
145 }
146 return __retval;
147 }
148
149 template<typename _CharT>
150 streamsize
151 __basic_file<_CharT>::xsgetn(_CharT* __s, streamsize __n)
152 { return fread(__s, 1, __n, _M_cfile); }
153
154 template<typename _CharT>
155 streamsize
156 __basic_file<_CharT>::xsputn(const _CharT* __s, streamsize __n)
157 { return fwrite(__s, 1, __n, _M_cfile); }
158
159 template<typename _CharT>
160 streamoff
161 __basic_file<_CharT>::seekoff(streamoff __off, ios_base::seekdir __way,
162 ios_base::openmode /*__mode*/)
163 { return fseek(_M_cfile, __off, __way); }
164
165 template<typename _CharT>
166 streamoff
167 __basic_file<_CharT>::seekpos(streamoff __pos,
168 ios_base::openmode /*__mode*/)
169 { return fseek(_M_cfile, __pos, ios_base::beg); }
170
171 template<typename _CharT>
172 int
173 __basic_file<_CharT>::sync()
174 { return fflush(_M_cfile); }
175
176 // NB: Unused.
177 template<typename _CharT>
178 int
179 __basic_file<_CharT>::overflow(int /*__c*/)
180 { return EOF; }
181
182 // NB: Unused.
183 template<typename _CharT>
184 int
185 __basic_file<_CharT>::underflow()
186 { return EOF; }
187
188 // NB: Unused.
189 template<typename _CharT>
190 int
191 __basic_file<_CharT>::uflow()
192 { return EOF; }
193
194 // NB: Unused.
195 template<typename _CharT>
196 int
197 __basic_file<_CharT>::pbackfail(int /*__c*/)
198 { return EOF; }
199
200 // NB: Unused.
201 template<typename _CharT>
202 streambuf*
203 __basic_file<_CharT>::setbuf(_CharT* /*__b*/, int /*__len*/)
204 { return reinterpret_cast<streambuf*>(this); }
205
206 // NB: Unused.
207 template<typename _CharT>
208 int
209 __basic_file<_CharT>::doallocate()
210 { return EOF; }
211
212 // NB: Unused.
213 template<typename _CharT>
214 streamsize
215 __basic_file<_CharT>::sys_read(_CharT* __s, streamsize __n)
216 { return fread(__s, 1, __n, _M_cfile); }
217
218 // NB: Unused.
219 template<typename _CharT>
220 streamsize
221 __basic_file<_CharT>::sys_write(const _CharT* __s, streamsize __n)
222 { return fwrite(__s, 1, __n, _M_cfile); }
223
224 // NB: Unused.
225 template<typename _CharT>
226 streamoff
227 __basic_file<_CharT>::sys_seek(streamoff __pos, ios_base::seekdir __way)
228 { return fseek(_M_cfile, __pos, __way); }
229
230 // NB: Unused.
231 template<typename _CharT>
232 int
233 __basic_file<_CharT>::sys_close()
234 { return fclose(_M_cfile); }
235
236 // NB: Unused.
237 template<typename _CharT>
238 int
239 __basic_file<_CharT>::sys_stat(void* /*__v*/)
240 { return EOF; }
241
242 // NB: Unused.
243 template<typename _CharT>
244 int
245 __basic_file<_CharT>::showmanyc()
246 { return EOF; }
247
248 // NB: Unused.
249 template<typename _CharT>
250 void
251 __basic_file<_CharT>::imbue(void* /*__v*/) { }
252
253 // Need to instantiate base class here for type-info bits, etc
254 template struct __basic_file_base<char>;
255 template class __basic_file<char>;
256 #ifdef _GLIBCPP_USE_WCHAR_T
257 template struct __basic_file_base<wchar_t>;
258 template class __basic_file<wchar_t>;
259 #endif
260 } // namespace std
261
262
This page took 0.055904 seconds and 6 git commands to generate.