1 /* This is part of libio/iostream, providing -*- C++ -*- input/output.
2 Copyright (C) 1993 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
31 #include <stdiostream.h>
34 // A stdiobuf is "tied" to a FILE object (as used by the stdio package).
35 // Thus a stdiobuf is always synchronized with the corresponding FILE,
36 // though at the cost of some overhead. (If you use the implementation
37 // of stdio supplied with this library, you don't need stdiobufs.)
38 // This implementation inherits from filebuf, but implement the virtual
39 // functions sys_read/..., using the stdio functions fread/... instead
40 // of the low-level read/... system calls. This has the advantage that
41 // we get all of the nice filebuf semantics automatically, though
42 // with some overhead.
55 stdiobuf::stdiobuf(FILE *f
) : filebuf(fileno(f
))
58 // Turn off buffer in stdiobuf. Instead, rely on buffering in (FILE).
59 // Thus the stdiobuf will be synchronized with the FILE.
65 /* Only needed if we're buffered. Not buffered is the default. */
66 _IO_do_flush((_IO_FILE
*)this);
69 streamsize
stdiobuf::sys_read(char* buf
, streamsize size
)
71 // A minor optimization, but it makes a noticable difference.
72 // A bigger optimization would be to write stdiobuf::underflow,
73 // but that has some modularity disadvantages. Re-evaluate that
74 // after we have gotten rid of the double indirection. FIXME
77 register int ch
= getc(_file
);
84 return fread(buf
, 1, size
, _file
);
87 streamsize
stdiobuf::sys_write(const char *buf
, streamsize n
)
89 _IO_ssize_t count
= fwrite(buf
, 1, n
, _file
);
95 streampos
stdiobuf::sys_seek(streamoff offset
, _seek_dir dir
)
97 // Normally, equivalent to: fdir=dir
99 (dir
== ios::beg
) ? SEEK_SET
:
100 (dir
== ios::cur
) ? SEEK_CUR
:
101 (dir
== ios::end
) ? SEEK_END
:
103 return fseek(_file
, offset
, fdir
);
106 int stdiobuf::sys_close()
108 int status
= fclose(_file
);
115 if (_IO_do_flush((_IO_FILE
*)this))
117 if (!(xflags() & _IO_NO_WRITES
))
123 int stdiobuf::overflow(int c
/* = EOF*/)
125 if (filebuf::overflow(c
) == EOF
)
129 return fflush(_file
);
132 streamsize
stdiobuf::xsputn(const char* s
, streamsize n
)
136 // The filebuf implementation of sputn loses.
137 return streambuf::xsputn(s
, n
);
140 return fwrite (s
, 1, n
, _file
);
143 void stdiobuf::buffered (int b
)
147 if (_flags
& _IO_UNBUFFERED
)
148 { /* Was unbuffered, make it buffered. */
149 _flags
&= ~_IO_UNBUFFERED
;
154 if (!(_flags
& _IO_UNBUFFERED
))
155 { /* Was buffered, make it unbuffered. */