* loop.c (strength_reduce): Don't do biv increment -> DEST_REG giv
[official-gcc.git] / libio / stdiostream.cc
blobbc9bef08ab6736af7fb3a2bc1da34c1633c5d146
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)
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 #ifdef __GNUG__
28 #pragma implementation
29 #endif
31 #include <stdiostream.h>
32 #include "libioP.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.
45 #ifndef SEEK_SET
46 #define SEEK_SET 0
47 #endif
48 #ifndef SEEK_CUR
49 #define SEEK_CUR 1
50 #endif
51 #ifndef SEEK_END
52 #define SEEK_END 2
53 #endif
55 stdiobuf::stdiobuf(FILE *f) : filebuf(fileno(f))
57 _file = f;
58 // Turn off buffer in stdiobuf. Instead, rely on buffering in (FILE).
59 // Thus the stdiobuf will be synchronized with the FILE.
60 setbuf(NULL, 0);
63 stdiobuf::~stdiobuf()
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
75 if (size == 1)
77 register int ch = getc(_file);
78 if (ch == EOF)
79 return 0;
80 *buf = (char)ch;
81 return 1;
83 else
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);
90 if (_offset >= 0)
91 _offset += n;
92 return count;
95 streampos stdiobuf::sys_seek(streamoff offset, _seek_dir dir)
97 // Normally, equivalent to: fdir=dir
98 int fdir =
99 (dir == ios::beg) ? SEEK_SET :
100 (dir == ios::cur) ? SEEK_CUR :
101 (dir == ios::end) ? SEEK_END :
102 dir;
103 return fseek(_file, offset, fdir);
106 int stdiobuf::sys_close()
108 int status = fclose(_file);
109 _file = NULL;
110 return status;
113 int stdiobuf::sync()
115 if (_IO_do_flush((_IO_FILE*)this))
116 return EOF;
117 if (!(xflags() & _IO_NO_WRITES))
118 if (fflush(_file))
119 return EOF;
120 return 0;
123 int stdiobuf::overflow(int c /* = EOF*/)
125 if (filebuf::overflow(c) == EOF)
126 return EOF;
127 if (c != EOF)
128 return c;
129 return fflush(_file);
132 streamsize stdiobuf::xsputn(const char* s, streamsize n)
134 if (buffered ())
136 // The filebuf implementation of sputn loses.
137 return streambuf::xsputn(s, n);
139 else
140 return fwrite (s, 1, n, _file);
143 void stdiobuf::buffered (int b)
145 if (b)
147 if (_flags & _IO_UNBUFFERED)
148 { /* Was unbuffered, make it buffered. */
149 _flags &= ~_IO_UNBUFFERED;
152 else
154 if (!(_flags & _IO_UNBUFFERED))
155 { /* Was buffered, make it unbuffered. */
156 setbuf(NULL, 0);