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 #include "streambuf.h"
31 // The ANSI draft requires that operations on cin/cout/cerr can be
32 // mixed with operations on stdin/stdout/stderr on a character by
33 // character basis. This normally requires that the streambuf's
34 // used by cin/cout/cerr be stdiostreams. However, if the stdio
35 // implementation is the one that is built using this library,
36 // then we don't need to, since in that case stdin/stdout/stderr
37 // are identical to _IO_stdin/_IO_stdout/_IO_stderr.
41 #ifdef _STDIO_USES_IOSTREAM
42 #define CIN_SBUF _IO_stdin_
43 #define COUT_SBUF _IO_stdout_
44 #define CERR_SBUF _IO_stderr_
45 static int use_stdiobuf
= 0;
47 #define CIN_SBUF _IO_stdin_buf
48 #define COUT_SBUF _IO_stdout_buf
49 #define CERR_SBUF _IO_stderr_buf
50 static int use_stdiobuf
= 1;
64 #define PAD 0 /* g++ allows 0-length arrays. */
68 struct _fake_istream
{
71 _ios_fields
*vb
; /* pointer to virtual base class ios */
74 /* This is supposedly correct for cfront. */
77 _ios_fields
*vb
; /* pointer to virtual base class ios */
81 char filler
[sizeof(struct istream
)-sizeof(struct _ios_fields
)+PAD
];
83 struct _fake_ostream
{
88 _ios_fields
*vb
; /* pointer to virtual base class ios */
91 char filler
[sizeof(struct ostream
)-sizeof(struct _ios_fields
)+PAD
];
95 #ifdef _IO_NEW_STREAMS
96 #define STD_STR(SBUF, TIE, EXTRA_FLAGS) \
97 (streambuf*)&SBUF, TIE, 0, ios::skipws|ios::dec|EXTRA_FLAGS, ' ',0,0,6
99 #define STD_STR(SBUF, TIE, EXTRA_FLAGS) \
100 (streambuf*)&SBUF, TIE, 0, ios::dont_close|ios::dec|ios::skipws|EXTRA_FLAGS, ' ',0,0,6
104 #define OSTREAM_DEF(NAME, SBUF, TIE, EXTRA_FLAGS, ASM) \
105 _fake_ostream NAME ASM = { {&NAME.base}, {STD_STR(SBUF, TIE, EXTRA_FLAGS) }};
106 #define ISTREAM_DEF(NAME, SBUF, TIE, EXTRA_FLAGS) \
107 _fake_istream NAME = { {&NAME.base}, {STD_STR(SBUF, TIE, EXTRA_FLAGS) }};
109 #define OSTREAM_DEF(NAME, SBUF, TIE, EXTRA_FLAGS) \
110 _fake_ostream NAME = { {0, &NAME.base}, {STD_STR(SBUF, TIE, EXTRA_FLAGS) }};
111 #define ISTREAM_DEF(NAME, SBUF, TIE, EXTRA_FLAGS) \
112 _fake_istream NAME = {{0, 0, &NAME.base}, {STD_STR(SBUF, TIE, EXTRA_FLAGS)}};
115 OSTREAM_DEF(cout
, COUT_SBUF
, NULL
, 0, )
116 OSTREAM_DEF(cerr
, CERR_SBUF
,(ostream
*)&cout
, ios::unitbuf
, )
117 ISTREAM_DEF(cin
, CIN_SBUF
, (ostream
*)&cout
, 0)
119 /* Only for (partial) compatibility with AT&T's library. */
121 OSTREAM_DEF(clog
, CERR_SBUF
, (ostream
*)&cout
, 0, __asm__ ("__IO_clog"))
123 OSTREAM_DEF(clog
, CERR_SBUF
, (ostream
*)&cout
, 0, )
126 // Switches between using _IO_std{in,out,err} and __std{in,out,err}_buf
127 // for standard streams. This does not normally need to be called
128 // explicitly, but is provided for AT&T compatibility.
130 int ios::sync_with_stdio(int new_state
)
132 #ifdef _STDIO_USES_IOSTREAM
133 // It is always synced.
136 if (new_state
== use_stdiobuf
) // The usual case now.
139 cin
.base
._strbuf
= (streambuf
*)&_IO_stdin_buf
;
140 cout
.base
._strbuf
= (streambuf
*)&_IO_stdout_buf
;
141 cerr
.base
._strbuf
= (streambuf
*)&_IO_stderr_buf
;
142 clog
.base
._strbuf
= (streambuf
*)&_IO_stderr_buf
;
144 cin
.base
._strbuf
= (streambuf
*)_IO_stdin
;
145 cout
.base
._strbuf
= (streambuf
*)_IO_stdout
;
146 cerr
.base
._strbuf
= (streambuf
*)_IO_stderr
;
147 clog
.base
._strbuf
= (streambuf
*)_IO_stderr
;
149 int old_state
= use_stdiobuf
;
150 use_stdiobuf
= new_state
;