xref: /openbsd/gnu/gcc/libstdc++-v3/src/ios_init.cc (revision 404b540a)
1*404b540aSrobert // Iostreams base classes -*- C++ -*-
2*404b540aSrobert 
3*404b540aSrobert // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
4*404b540aSrobert // Free Software Foundation, Inc.
5*404b540aSrobert //
6*404b540aSrobert // This file is part of the GNU ISO C++ Library.  This library is free
7*404b540aSrobert // software; you can redistribute it and/or modify it under the
8*404b540aSrobert // terms of the GNU General Public License as published by the
9*404b540aSrobert // Free Software Foundation; either version 2, or (at your option)
10*404b540aSrobert // any later version.
11*404b540aSrobert 
12*404b540aSrobert // This library is distributed in the hope that it will be useful,
13*404b540aSrobert // but WITHOUT ANY WARRANTY; without even the implied warranty of
14*404b540aSrobert // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15*404b540aSrobert // GNU General Public License for more details.
16*404b540aSrobert 
17*404b540aSrobert // You should have received a copy of the GNU General Public License along
18*404b540aSrobert // with this library; see the file COPYING.  If not, write to the Free
19*404b540aSrobert // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20*404b540aSrobert // USA.
21*404b540aSrobert 
22*404b540aSrobert // As a special exception, you may use this file as part of a free software
23*404b540aSrobert // library without restriction.  Specifically, if other files instantiate
24*404b540aSrobert // templates or use macros or inline functions from this file, or you compile
25*404b540aSrobert // this file and link it with other files to produce an executable, this
26*404b540aSrobert // file does not by itself cause the resulting executable to be covered by
27*404b540aSrobert // the GNU General Public License.  This exception does not however
28*404b540aSrobert // invalidate any other reasons why the executable file might be covered by
29*404b540aSrobert // the GNU General Public License.
30*404b540aSrobert 
31*404b540aSrobert //
32*404b540aSrobert // ISO C++ 14882: 27.4  Iostreams base classes
33*404b540aSrobert //
34*404b540aSrobert 
35*404b540aSrobert #include <ios>
36*404b540aSrobert #include <ostream>
37*404b540aSrobert #include <istream>
38*404b540aSrobert #include <fstream>
39*404b540aSrobert #include <ext/stdio_filebuf.h>
40*404b540aSrobert #include <ext/stdio_sync_filebuf.h>
41*404b540aSrobert 
42*404b540aSrobert namespace __gnu_internal _GLIBCXX_VISIBILITY(hidden)
43*404b540aSrobert {
44*404b540aSrobert   using namespace __gnu_cxx;
45*404b540aSrobert 
46*404b540aSrobert   // Extern declarations for global objects in src/globals.cc.
47*404b540aSrobert   extern stdio_sync_filebuf<char> buf_cout_sync;
48*404b540aSrobert   extern stdio_sync_filebuf<char> buf_cin_sync;
49*404b540aSrobert   extern stdio_sync_filebuf<char> buf_cerr_sync;
50*404b540aSrobert 
51*404b540aSrobert   extern stdio_filebuf<char> buf_cout;
52*404b540aSrobert   extern stdio_filebuf<char> buf_cin;
53*404b540aSrobert   extern stdio_filebuf<char> buf_cerr;
54*404b540aSrobert 
55*404b540aSrobert #ifdef _GLIBCXX_USE_WCHAR_T
56*404b540aSrobert   extern stdio_sync_filebuf<wchar_t> buf_wcout_sync;
57*404b540aSrobert   extern stdio_sync_filebuf<wchar_t> buf_wcin_sync;
58*404b540aSrobert   extern stdio_sync_filebuf<wchar_t> buf_wcerr_sync;
59*404b540aSrobert 
60*404b540aSrobert   extern stdio_filebuf<wchar_t> buf_wcout;
61*404b540aSrobert   extern stdio_filebuf<wchar_t> buf_wcin;
62*404b540aSrobert   extern stdio_filebuf<wchar_t> buf_wcerr;
63*404b540aSrobert #endif
64*404b540aSrobert } // namespace __gnu_internal
65*404b540aSrobert 
66*404b540aSrobert _GLIBCXX_BEGIN_NAMESPACE(std)
67*404b540aSrobert 
68*404b540aSrobert   using namespace __gnu_internal;
69*404b540aSrobert 
70*404b540aSrobert   extern istream cin;
71*404b540aSrobert   extern ostream cout;
72*404b540aSrobert   extern ostream cerr;
73*404b540aSrobert   extern ostream clog;
74*404b540aSrobert 
75*404b540aSrobert #ifdef _GLIBCXX_USE_WCHAR_T
76*404b540aSrobert   extern wistream wcin;
77*404b540aSrobert   extern wostream wcout;
78*404b540aSrobert   extern wostream wcerr;
79*404b540aSrobert   extern wostream wclog;
80*404b540aSrobert #endif
81*404b540aSrobert 
Init()82*404b540aSrobert   ios_base::Init::Init()
83*404b540aSrobert   {
84*404b540aSrobert     if (__gnu_cxx::__exchange_and_add_dispatch(&_S_refcount, 1) == 0)
85*404b540aSrobert       {
86*404b540aSrobert 	// Standard streams default to synced with "C" operations.
87*404b540aSrobert 	_S_synced_with_stdio = true;
88*404b540aSrobert 
89*404b540aSrobert 	new (&buf_cout_sync) stdio_sync_filebuf<char>(stdout);
90*404b540aSrobert 	new (&buf_cin_sync) stdio_sync_filebuf<char>(stdin);
91*404b540aSrobert 	new (&buf_cerr_sync) stdio_sync_filebuf<char>(stderr);
92*404b540aSrobert 
93*404b540aSrobert 	// The standard streams are constructed once only and never
94*404b540aSrobert 	// destroyed.
95*404b540aSrobert 	new (&cout) ostream(&buf_cout_sync);
96*404b540aSrobert 	new (&cin) istream(&buf_cin_sync);
97*404b540aSrobert 	new (&cerr) ostream(&buf_cerr_sync);
98*404b540aSrobert 	new (&clog) ostream(&buf_cerr_sync);
99*404b540aSrobert 	cin.tie(&cout);
100*404b540aSrobert 	cerr.flags(ios_base::unitbuf);
101*404b540aSrobert 	// _GLIBCXX_RESOLVE_LIB_DEFECTS
102*404b540aSrobert 	// 455. cerr::tie() and wcerr::tie() are overspecified.
103*404b540aSrobert 	cerr.tie(&cout);
104*404b540aSrobert 
105*404b540aSrobert #ifdef _GLIBCXX_USE_WCHAR_T
106*404b540aSrobert 	new (&buf_wcout_sync) stdio_sync_filebuf<wchar_t>(stdout);
107*404b540aSrobert 	new (&buf_wcin_sync) stdio_sync_filebuf<wchar_t>(stdin);
108*404b540aSrobert 	new (&buf_wcerr_sync) stdio_sync_filebuf<wchar_t>(stderr);
109*404b540aSrobert 
110*404b540aSrobert 	new (&wcout) wostream(&buf_wcout_sync);
111*404b540aSrobert 	new (&wcin) wistream(&buf_wcin_sync);
112*404b540aSrobert 	new (&wcerr) wostream(&buf_wcerr_sync);
113*404b540aSrobert 	new (&wclog) wostream(&buf_wcerr_sync);
114*404b540aSrobert 	wcin.tie(&wcout);
115*404b540aSrobert 	wcerr.flags(ios_base::unitbuf);
116*404b540aSrobert 	wcerr.tie(&wcout);
117*404b540aSrobert #endif
118*404b540aSrobert 
119*404b540aSrobert 	// NB: Have to set refcount above one, so that standard
120*404b540aSrobert 	// streams are not re-initialized with uses of ios_base::Init
121*404b540aSrobert 	// besides <iostream> static object, ie just using <ios> with
122*404b540aSrobert 	// ios_base::Init objects.
123*404b540aSrobert 	__gnu_cxx::__atomic_add_dispatch(&_S_refcount, 1);
124*404b540aSrobert       }
125*404b540aSrobert   }
126*404b540aSrobert 
~Init()127*404b540aSrobert   ios_base::Init::~Init()
128*404b540aSrobert   {
129*404b540aSrobert     if (__gnu_cxx::__exchange_and_add_dispatch(&_S_refcount, -1) == 2)
130*404b540aSrobert       {
131*404b540aSrobert 	// Catch any exceptions thrown by basic_ostream::flush()
132*404b540aSrobert 	try
133*404b540aSrobert 	  {
134*404b540aSrobert 	    // Flush standard output streams as required by 27.4.2.1.6
135*404b540aSrobert 	    cout.flush();
136*404b540aSrobert 	    cerr.flush();
137*404b540aSrobert 	    clog.flush();
138*404b540aSrobert 
139*404b540aSrobert #ifdef _GLIBCXX_USE_WCHAR_T
140*404b540aSrobert 	    wcout.flush();
141*404b540aSrobert 	    wcerr.flush();
142*404b540aSrobert 	    wclog.flush();
143*404b540aSrobert #endif
144*404b540aSrobert 	  }
145*404b540aSrobert 	catch (...)
146*404b540aSrobert 	  { }
147*404b540aSrobert       }
148*404b540aSrobert   }
149*404b540aSrobert 
150*404b540aSrobert   bool
sync_with_stdio(bool __sync)151*404b540aSrobert   ios_base::sync_with_stdio(bool __sync)
152*404b540aSrobert   {
153*404b540aSrobert     // _GLIBCXX_RESOLVE_LIB_DEFECTS
154*404b540aSrobert     // 49.  Underspecification of ios_base::sync_with_stdio
155*404b540aSrobert     bool __ret = ios_base::Init::_S_synced_with_stdio;
156*404b540aSrobert 
157*404b540aSrobert     // Turn off sync with C FILE* for cin, cout, cerr, clog iff
158*404b540aSrobert     // currently synchronized.
159*404b540aSrobert     if (!__sync && __ret)
160*404b540aSrobert       {
161*404b540aSrobert 	// Make sure the standard streams are constructed.
162*404b540aSrobert 	ios_base::Init __init;
163*404b540aSrobert 
164*404b540aSrobert 	ios_base::Init::_S_synced_with_stdio = __sync;
165*404b540aSrobert 
166*404b540aSrobert 	// Explicitly call dtors to free any memory that is
167*404b540aSrobert 	// dynamically allocated by filebuf ctor or member functions,
168*404b540aSrobert 	// but don't deallocate all memory by calling operator delete.
169*404b540aSrobert 	buf_cout_sync.~stdio_sync_filebuf<char>();
170*404b540aSrobert 	buf_cin_sync.~stdio_sync_filebuf<char>();
171*404b540aSrobert 	buf_cerr_sync.~stdio_sync_filebuf<char>();
172*404b540aSrobert 
173*404b540aSrobert #ifdef _GLIBCXX_USE_WCHAR_T
174*404b540aSrobert 	buf_wcout_sync.~stdio_sync_filebuf<wchar_t>();
175*404b540aSrobert 	buf_wcin_sync.~stdio_sync_filebuf<wchar_t>();
176*404b540aSrobert 	buf_wcerr_sync.~stdio_sync_filebuf<wchar_t>();
177*404b540aSrobert #endif
178*404b540aSrobert 
179*404b540aSrobert 	// Create stream buffers for the standard streams and use
180*404b540aSrobert 	// those buffers without destroying and recreating the
181*404b540aSrobert 	// streams.
182*404b540aSrobert 	new (&buf_cout) stdio_filebuf<char>(stdout, ios_base::out);
183*404b540aSrobert 	new (&buf_cin) stdio_filebuf<char>(stdin, ios_base::in);
184*404b540aSrobert 	new (&buf_cerr) stdio_filebuf<char>(stderr, ios_base::out);
185*404b540aSrobert 	cout.rdbuf(&buf_cout);
186*404b540aSrobert 	cin.rdbuf(&buf_cin);
187*404b540aSrobert 	cerr.rdbuf(&buf_cerr);
188*404b540aSrobert 	clog.rdbuf(&buf_cerr);
189*404b540aSrobert 
190*404b540aSrobert #ifdef _GLIBCXX_USE_WCHAR_T
191*404b540aSrobert 	new (&buf_wcout) stdio_filebuf<wchar_t>(stdout, ios_base::out);
192*404b540aSrobert 	new (&buf_wcin) stdio_filebuf<wchar_t>(stdin, ios_base::in);
193*404b540aSrobert 	new (&buf_wcerr) stdio_filebuf<wchar_t>(stderr, ios_base::out);
194*404b540aSrobert 	wcout.rdbuf(&buf_wcout);
195*404b540aSrobert 	wcin.rdbuf(&buf_wcin);
196*404b540aSrobert 	wcerr.rdbuf(&buf_wcerr);
197*404b540aSrobert 	wclog.rdbuf(&buf_wcerr);
198*404b540aSrobert #endif
199*404b540aSrobert       }
200*404b540aSrobert     return __ret;
201*404b540aSrobert   }
202*404b540aSrobert 
203*404b540aSrobert _GLIBCXX_END_NAMESPACE
204