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