]> gcc.gnu.org Git - gcc.git/blob - libstdc++-v3/config/c_io_libio.cc
Sync libio to glibc-2.2 current CVS.
[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 #include <fcntl.h> // Solaris needs for O_* macros
37
38 namespace std {
39
40 __basic_file::__basic_file(__c_lock* __lock)
41 {
42 _lock = __lock;
43 _IO_init(this, 0);
44 _IO_file_init((_IO_FILE_plus*) this);
45 _IO_file_attach(this, -1);
46 }
47
48 int
49 __basic_file::get_fileno(void)
50 { return _fileno; }
51
52 __basic_file::~__basic_file()
53 {
54 if (this->is_open())
55 {
56 _IO_do_flush(this);
57 if (!(_flags & _IO_DELETE_DONT_CLOSE))
58 _IO_SYSCLOSE((_IO_FILE*)this);
59 }
60 _IO_default_finish(this, 0);
61 }
62
63 __basic_file*
64 __basic_file::sys_open(int __fd, ios_base::openmode __mode)
65 {
66 __basic_file* __retval = NULL;
67 bool __testi = __mode & ios_base::in;
68 bool __testo = __mode & ios_base::out;
69 #ifdef O_BINARY
70 bool __testb = __mode & ios_base::binary;
71 #endif
72 int __p_mode = 0;
73 int __rw_mode = _IO_NO_READS + _IO_NO_WRITES;
74
75 if (__testi)
76 {
77 __p_mode = O_RDONLY;
78 __rw_mode = _IO_NO_WRITES;
79 }
80 if (__testo)
81 {
82 __p_mode = O_WRONLY | O_TRUNC;
83 __rw_mode = _IO_NO_READS;
84 }
85 #ifdef O_BINARY
86 if (__testb)
87 __p_mode |= O_BINARY;
88 #endif
89
90 if (__fd >= 0)
91 {
92 _fileno = __fd;
93 int __mask = _IO_NO_READS + _IO_NO_WRITES + _IO_IS_APPENDING;
94 _flags = (_flags & ~__mask) | (__rw_mode & __mask);
95 _IO_link_in((_IO_FILE_plus*) this);
96 __retval = this;
97 }
98 return __retval;
99 }
100
101 __basic_file*
102 __basic_file::open(const char* __name, ios_base::openmode __mode,
103 int __prot = 0664)
104 {
105 __basic_file* __retval = NULL;
106 #ifdef O_BINARY
107 bool __testb = __mode & ios_base::binary;
108 #endif
109 bool __testi = __mode & ios_base::in;
110 bool __testo = __mode & ios_base::out;
111 bool __testt = __mode & ios_base::trunc;
112 bool __testa = __mode & ios_base::app;
113 int __p_mode = 0;
114 int __rw_mode = _IO_NO_READS + _IO_NO_WRITES;
115
116 if (!__testi && __testo && !__testt && !__testa)
117 {
118 __p_mode = O_WRONLY | O_TRUNC | O_CREAT;
119 __rw_mode = _IO_NO_READS;
120 }
121 if (!__testi && __testo && !__testt && __testa)
122 {
123 __p_mode = O_WRONLY | O_APPEND | O_CREAT;
124 __rw_mode = _IO_NO_READS | _IO_IS_APPENDING;
125 }
126 if (!__testi && __testo && __testt && !__testa)
127 {
128 __p_mode = O_WRONLY | O_TRUNC | O_CREAT;
129 __rw_mode = _IO_NO_READS;
130 }
131 if (__testi && !__testo && !__testt && !__testa)
132 {
133 __p_mode = O_RDONLY;
134 __rw_mode = _IO_NO_WRITES;
135 }
136 if (__testi && __testo && !__testt && !__testa)
137 {
138 __p_mode = O_RDWR;
139 __rw_mode = 0;
140 }
141 if (__testi && __testo && __testt && !__testa)
142 {
143 __p_mode = O_RDWR | O_TRUNC | O_CREAT;
144 __rw_mode = 0;
145 }
146 #ifdef O_BINARY
147 if (__testb)
148 __p_mode |= O_BINARY;
149 #endif
150 if (!_IO_file_is_open(this))
151 {
152 #if _G_HAVE_IO_FILE_OPEN
153 __c_file_type* __f;
154 __f = _IO_file_open(this, __name, __p_mode, __prot, __rw_mode, 0);
155 __retval = __f ? this: NULL;
156 #else
157 int __fd = ::open(__name, __p_mode, __prot);
158 if (__fd >= 0)
159 {
160 _fileno = __fd;
161 int __mask = _IO_NO_READS + _IO_NO_WRITES + _IO_IS_APPENDING;
162 _flags = (_flags & ~__mask) | (__rw_mode & __mask);
163 _IO_link_in(this);
164 __retval = this;
165 }
166 #endif
167 }
168 return __retval;
169 }
170
171 bool
172 __basic_file::is_open() { return _fileno >= 0; }
173
174 __basic_file*
175 __basic_file::close()
176 { return _IO_file_close_it(this) ? static_cast<__basic_file*>(NULL) : this; }
177
178 // NB: Unused.
179 int
180 __basic_file::overflow(int __c) { return _IO_file_overflow(this, __c); }
181
182 // NB: Unused.
183 int
184 __basic_file::underflow() { return _IO_file_underflow(this); }
185
186 // NB: Unused.
187 int
188 __basic_file::uflow() { return _IO_default_uflow(this); }
189
190 // NB: Unused.
191 int
192 __basic_file::pbackfail(int __c)
193 { return _IO_default_pbackfail(this, __c); }
194
195 streamsize
196 __basic_file::xsputn(const char* __s, streamsize __n)
197 { return _IO_file_xsputn(this, __s, __n); }
198
199 streamsize
200 __basic_file::xsgetn(char* __s, streamsize __n)
201 { return _IO_default_xsgetn(this, __s, __n); }
202
203 streamoff
204 __basic_file::seekoff(streamoff __off, ios_base::seekdir __way,
205 ios_base::openmode __mode)
206 { return _IO_file_seekoff(this, __off, __way, __mode); }
207
208 streamoff
209 __basic_file::seekpos(streamoff __pos, ios_base::openmode __mode)
210 { return _IO_file_seekoff(this, __pos, ios_base::beg, __mode); }
211
212 // NB: Unused.
213 streambuf*
214 __basic_file::setbuf(char* __b, int __len)
215 { return (streambuf*) _IO_file_setbuf(this,__b, __len); }
216
217 int
218 __basic_file::sync()
219 { return _IO_file_sync(this); }
220
221 // NB: Unused.
222 int
223 __basic_file::doallocate()
224 { return _IO_file_doallocate(this); }
225
226 // NB: Unused.
227 streamsize
228 __basic_file::sys_read(char* __s, streamsize __n)
229 { return _IO_file_read(this, __s, __n); }
230
231 // NB: Unused.
232 streamsize
233 __basic_file::sys_write(const char* __s, streamsize __n)
234 { return _IO_file_write(this, __s, __n); }
235
236 // NB: Unused.
237 streamoff
238 __basic_file::sys_seek(streamoff __pos, ios_base::seekdir __way)
239 { return _IO_file_seek(this, __pos, __way); }
240
241 // NB: Unused.
242 int
243 __basic_file::sys_close() { return _IO_file_close(this); }
244
245 // NB: Unused.
246 int
247 __basic_file::sys_stat(void* __v) { return _IO_file_stat(this, __v); }
248
249 // NB: Unused.
250 int
251 __basic_file::showmanyc() { return EOF; }
252
253 // NB: Unused.
254 void
255 __basic_file::imbue(void* /*__v*/) { }
256
257 } // namespace std
258
259
260
261
262
263
264
This page took 0.051641 seconds and 6 git commands to generate.