1 /* This is part of libio/iostream, providing -*- C++ -*- input/output.
2 Copyright (C) 1993, 1999 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)
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). */
28 #pragma implementation
32 # define _POSIX_SOURCE
34 #include <stdiostream.h>
37 // A stdiobuf is "tied" to a FILE object (as used by the stdio package).
38 // Thus a stdiobuf is always synchronized with the corresponding FILE,
39 // though at the cost of some overhead. (If you use the implementation
40 // of stdio supplied with this library, you don't need stdiobufs.)
41 // This implementation inherits from filebuf, but implement the virtual
42 // functions sys_read/..., using the stdio functions fread/... instead
43 // of the low-level read/... system calls. This has the advantage that
44 // we get all of the nice filebuf semantics automatically, though
45 // with some overhead.
58 stdiobuf::stdiobuf(FILE *f) : filebuf(fileno(f))
61 // Turn off buffer in stdiobuf. Instead, rely on buffering in (FILE).
62 // Thus the stdiobuf will be synchronized with the FILE.
68 /* Only needed if we're buffered. Not buffered is the default. */
69 _IO_do_flush((_IO_FILE*)this);
72 streamsize stdiobuf::sys_read(char* buf, streamsize size)
74 // A minor optimization, but it makes a noticable difference.
75 // A bigger optimization would be to write stdiobuf::underflow,
76 // but that has some modularity disadvantages. Re-evaluate that
77 // after we have gotten rid of the double indirection. FIXME
80 register int ch = getc(_file);
87 return fread(buf, 1, size, _file);
90 streamsize stdiobuf::sys_write(const char *buf, streamsize n)
92 _IO_ssize_t count = fwrite(buf, 1, n, _file);
98 streampos stdiobuf::sys_seek(streamoff offset, _seek_dir dir)
100 // Normally, equivalent to: fdir=dir
102 (dir == ios::beg) ? SEEK_SET :
103 (dir == ios::cur) ? SEEK_CUR :
104 (dir == ios::end) ? SEEK_END :
106 return fseek(_file, offset, fdir);
109 int stdiobuf::sys_close()
111 int status = fclose(_file);
118 if (_IO_do_flush((_IO_FILE*)this))
120 if (!(xflags() & _IO_NO_WRITES))
126 int stdiobuf::overflow(int c /* = EOF*/)
128 if (filebuf::overflow(c) == EOF)
132 return fflush(_file);
135 streamsize stdiobuf::xsputn(const char* s, streamsize n)
139 // The filebuf implementation of sputn loses.
140 return streambuf::xsputn(s, n);
143 return fwrite (s, 1, n, _file);
146 void stdiobuf::buffered (int b)
150 if (_flags & _IO_UNBUFFERED)
151 { /* Was unbuffered, make it buffered. */
152 _flags &= ~_IO_UNBUFFERED;
157 if (!(_flags & _IO_UNBUFFERED))
158 { /* Was buffered, make it unbuffered. */