* cp-tree.h (struct lang_decl_flags): Add comdat.
[official-gcc.git] / libio / filebuf.cc
blob1e2682fbce9577293605a9383fff8800103ad986
1 /* This is part of libio/iostream, providing -*- C++ -*- input/output.
2 Copyright (C) 1993, 1995 Free Software Foundation
4 This file is part of the GNU IO Library. This library is free
5 software; you can redistribute it and/or modify it under the
6 terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 2, or (at your option)
8 any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this library; see the file COPYING. If not, write to the Free
17 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 As a special exception, if you link this library with files
20 compiled with a GNU compiler to produce an executable, this does not cause
21 the resulting executable to be covered by the GNU General Public License.
22 This exception does not however invalidate any other reasons why
23 the executable file might be covered by the GNU General Public License.
25 Written by Per Bothner (bothner@cygnus.com). */
27 #include "iostreamP.h"
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31 #include <errno.h>
32 #include "builtinbuf.h"
34 void filebuf::init()
36 _IO_file_init(this);
39 filebuf::filebuf()
41 _IO_file_init(this);
44 #if !_IO_UNIFIED_JUMPTABLES
45 /* This is like "new filebuf()", but it uses the _IO_file_jump jumptable,
46 for eficiency. */
48 filebuf* filebuf::__new()
50 filebuf *fb = new filebuf;
51 _IO_JUMPS(fb) = &_IO_file_jumps;
52 fb->_vtable() = builtinbuf_vtable;
53 return fb;
55 #endif
57 filebuf::filebuf(int fd)
59 _IO_file_init(this);
60 _IO_file_attach(this, fd);
63 filebuf::filebuf(int fd, char* p, int len)
65 _IO_file_init(this);
66 _IO_file_attach(this, fd);
67 setbuf(p, len);
70 filebuf::~filebuf()
72 if (_IO_file_is_open(this))
74 _IO_do_flush (this);
75 if (!(xflags() & _IO_DELETE_DONT_CLOSE))
76 _IO_SYSCLOSE (this);
80 filebuf* filebuf::open(const char *filename, ios::openmode mode, int prot)
82 if (_IO_file_is_open (this))
83 return NULL;
84 int posix_mode;
85 int read_write;
86 if (mode & ios::app)
87 mode |= ios::out;
88 if ((mode & (ios::in|ios::out)) == (ios::in|ios::out)) {
89 posix_mode = O_RDWR;
90 read_write = 0;
92 else if (mode & ios::out)
93 posix_mode = O_WRONLY, read_write = _IO_NO_READS;
94 else if (mode & (int)ios::in)
95 posix_mode = O_RDONLY, read_write = _IO_NO_WRITES;
96 else
97 posix_mode = 0, read_write = _IO_NO_READS+_IO_NO_WRITES;
98 if (mode & ios::binary)
100 mode &= ~ios::binary;
101 #ifdef O_BINARY
102 /* This is a (mis-)feature of DOS/Windows C libraries. */
103 posix_mode |= O_BINARY;
104 #endif
106 if ((mode & (int)ios::trunc) || mode == (int)ios::out)
107 posix_mode |= O_TRUNC;
108 if (mode & ios::app)
109 posix_mode |= O_APPEND, read_write |= _IO_IS_APPENDING;
110 if (!(mode & (int)ios::nocreate) && mode != ios::in)
111 posix_mode |= O_CREAT;
112 if (mode & (int)ios::noreplace)
113 posix_mode |= O_EXCL;
114 int fd = ::open(filename, posix_mode, prot);
115 if (fd < 0)
116 return NULL;
117 _fileno = fd;
118 xsetflags(read_write, _IO_NO_READS+_IO_NO_WRITES+_IO_IS_APPENDING);
119 if (mode & (ios::ate|ios::app)) {
120 if (pubseekoff(0, ios::end) == EOF)
121 return NULL;
123 _IO_link_in(this);
124 return this;
127 filebuf* filebuf::open(const char *filename, const char *mode)
129 return (filebuf*)_IO_file_fopen(this, filename, mode);
132 filebuf* filebuf::attach(int fd)
134 return (filebuf*)_IO_file_attach(this, fd);
137 streambuf* filebuf::setbuf(char* p, int len)
139 return (streambuf*)_IO_file_setbuf (this, p, len);
142 int filebuf::doallocate() { return _IO_file_doallocate(this); }
144 int filebuf::overflow(int c)
146 return _IO_file_overflow(this, c);
149 int filebuf::underflow()
151 return _IO_file_underflow(this);
154 int filebuf::sync()
156 return _IO_file_sync(this);
159 streampos filebuf::seekoff(streamoff offset, _seek_dir dir, int mode)
161 return _IO_file_seekoff (this, offset, dir, mode);
164 filebuf* filebuf::close()
166 return (_IO_file_close_it(this) ? (filebuf*)NULL : this);
169 streamsize filebuf::sys_read(char* buf, streamsize size)
171 return _IO_file_read(this, buf, size);
174 streampos filebuf::sys_seek(streamoff offset, _seek_dir dir)
176 return _IO_file_seek(this, offset, dir);
179 streamsize filebuf::sys_write(const char *buf, streamsize n)
181 return _IO_file_write (this, buf, n);
184 int filebuf::sys_stat(void* st)
186 return _IO_file_stat (this, st);
189 int filebuf::sys_close()
191 return _IO_file_close (this);
194 streamsize filebuf::xsputn(const char *s, streamsize n)
196 return _IO_file_xsputn(this, s, n);
199 streamsize filebuf::xsgetn(char *s, streamsize n)
201 // FIXME: OPTIMIZE THIS (specifically, when unbuffered()).
202 return streambuf::xsgetn(s, n);
205 // Non-ANSI AT&T-ism: Default open protection.
206 const int filebuf::openprot = 0644;