1132720Skan// Iostreams base classes -*- C++ -*-
2132720Skan
3169691Skan// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
4132720Skan// Free Software Foundation, Inc.
5132720Skan//
6132720Skan// This file is part of the GNU ISO C++ Library.  This library is free
7132720Skan// software; you can redistribute it and/or modify it under the
8132720Skan// terms of the GNU General Public License as published by the
9132720Skan// Free Software Foundation; either version 2, or (at your option)
10132720Skan// any later version.
11132720Skan
12132720Skan// This library is distributed in the hope that it will be useful,
13132720Skan// but WITHOUT ANY WARRANTY; without even the implied warranty of
14132720Skan// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15132720Skan// GNU General Public License for more details.
16132720Skan
17132720Skan// You should have received a copy of the GNU General Public License along
18132720Skan// with this library; see the file COPYING.  If not, write to the Free
19169691Skan// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20132720Skan// USA.
21132720Skan
22132720Skan// As a special exception, you may use this file as part of a free software
23132720Skan// library without restriction.  Specifically, if other files instantiate
24132720Skan// templates or use macros or inline functions from this file, or you compile
25132720Skan// this file and link it with other files to produce an executable, this
26132720Skan// file does not by itself cause the resulting executable to be covered by
27132720Skan// the GNU General Public License.  This exception does not however
28132720Skan// invalidate any other reasons why the executable file might be covered by
29132720Skan// the GNU General Public License.
30132720Skan
31132720Skan//
32132720Skan// ISO C++ 14882: 27.4  Iostreams base classes
33132720Skan//
34132720Skan
35132720Skan#include <ios>
36132720Skan#include <ostream>
37132720Skan#include <istream>
38132720Skan#include <fstream>
39132720Skan#include <ext/stdio_filebuf.h>
40132720Skan#include <ext/stdio_sync_filebuf.h>
41132720Skan
42169691Skannamespace __gnu_internal _GLIBCXX_VISIBILITY(hidden)
43132720Skan{
44132720Skan  using namespace __gnu_cxx;
45132720Skan
46132720Skan  // Extern declarations for global objects in src/globals.cc.
47132720Skan  extern stdio_sync_filebuf<char> buf_cout_sync;
48132720Skan  extern stdio_sync_filebuf<char> buf_cin_sync;
49132720Skan  extern stdio_sync_filebuf<char> buf_cerr_sync;
50132720Skan
51132720Skan  extern stdio_filebuf<char> buf_cout;
52132720Skan  extern stdio_filebuf<char> buf_cin;
53132720Skan  extern stdio_filebuf<char> buf_cerr;
54132720Skan
55132720Skan#ifdef _GLIBCXX_USE_WCHAR_T
56132720Skan  extern stdio_sync_filebuf<wchar_t> buf_wcout_sync;
57132720Skan  extern stdio_sync_filebuf<wchar_t> buf_wcin_sync;
58132720Skan  extern stdio_sync_filebuf<wchar_t> buf_wcerr_sync;
59132720Skan
60132720Skan  extern stdio_filebuf<wchar_t> buf_wcout;
61132720Skan  extern stdio_filebuf<wchar_t> buf_wcin;
62132720Skan  extern stdio_filebuf<wchar_t> buf_wcerr;
63132720Skan#endif
64132720Skan} // namespace __gnu_internal
65132720Skan
66169691Skan_GLIBCXX_BEGIN_NAMESPACE(std)
67169691Skan
68132720Skan  using namespace __gnu_internal;
69132720Skan
70132720Skan  extern istream cin;
71132720Skan  extern ostream cout;
72132720Skan  extern ostream cerr;
73132720Skan  extern ostream clog;
74132720Skan
75132720Skan#ifdef _GLIBCXX_USE_WCHAR_T
76132720Skan  extern wistream wcin;
77132720Skan  extern wostream wcout;
78132720Skan  extern wostream wcerr;
79132720Skan  extern wostream wclog;
80132720Skan#endif
81132720Skan
82132720Skan  ios_base::Init::Init()
83132720Skan  {
84169691Skan    if (__gnu_cxx::__exchange_and_add_dispatch(&_S_refcount, 1) == 0)
85132720Skan      {
86132720Skan	// Standard streams default to synced with "C" operations.
87132720Skan	_S_synced_with_stdio = true;
88132720Skan
89132720Skan	new (&buf_cout_sync) stdio_sync_filebuf<char>(stdout);
90132720Skan	new (&buf_cin_sync) stdio_sync_filebuf<char>(stdin);
91132720Skan	new (&buf_cerr_sync) stdio_sync_filebuf<char>(stderr);
92132720Skan
93132720Skan	// The standard streams are constructed once only and never
94132720Skan	// destroyed.
95132720Skan	new (&cout) ostream(&buf_cout_sync);
96132720Skan	new (&cin) istream(&buf_cin_sync);
97132720Skan	new (&cerr) ostream(&buf_cerr_sync);
98132720Skan	new (&clog) ostream(&buf_cerr_sync);
99132720Skan	cin.tie(&cout);
100132720Skan	cerr.flags(ios_base::unitbuf);
101169691Skan	// _GLIBCXX_RESOLVE_LIB_DEFECTS
102169691Skan	// 455. cerr::tie() and wcerr::tie() are overspecified.
103169691Skan	cerr.tie(&cout);
104169691Skan
105132720Skan#ifdef _GLIBCXX_USE_WCHAR_T
106132720Skan	new (&buf_wcout_sync) stdio_sync_filebuf<wchar_t>(stdout);
107132720Skan	new (&buf_wcin_sync) stdio_sync_filebuf<wchar_t>(stdin);
108132720Skan	new (&buf_wcerr_sync) stdio_sync_filebuf<wchar_t>(stderr);
109132720Skan
110132720Skan	new (&wcout) wostream(&buf_wcout_sync);
111132720Skan	new (&wcin) wistream(&buf_wcin_sync);
112132720Skan	new (&wcerr) wostream(&buf_wcerr_sync);
113132720Skan	new (&wclog) wostream(&buf_wcerr_sync);
114132720Skan	wcin.tie(&wcout);
115132720Skan	wcerr.flags(ios_base::unitbuf);
116169691Skan	wcerr.tie(&wcout);
117132720Skan#endif
118132720Skan
119132720Skan	// NB: Have to set refcount above one, so that standard
120132720Skan	// streams are not re-initialized with uses of ios_base::Init
121132720Skan	// besides <iostream> static object, ie just using <ios> with
122132720Skan	// ios_base::Init objects.
123169691Skan	__gnu_cxx::__atomic_add_dispatch(&_S_refcount, 1);
124132720Skan      }
125132720Skan  }
126132720Skan
127132720Skan  ios_base::Init::~Init()
128132720Skan  {
129169691Skan    if (__gnu_cxx::__exchange_and_add_dispatch(&_S_refcount, -1) == 2)
130132720Skan      {
131132720Skan	// Catch any exceptions thrown by basic_ostream::flush()
132132720Skan	try
133132720Skan	  {
134132720Skan	    // Flush standard output streams as required by 27.4.2.1.6
135132720Skan	    cout.flush();
136132720Skan	    cerr.flush();
137132720Skan	    clog.flush();
138132720Skan
139132720Skan#ifdef _GLIBCXX_USE_WCHAR_T
140132720Skan	    wcout.flush();
141132720Skan	    wcerr.flush();
142132720Skan	    wclog.flush();
143132720Skan#endif
144132720Skan	  }
145132720Skan	catch (...)
146132720Skan	  { }
147132720Skan      }
148132720Skan  }
149132720Skan
150132720Skan  bool
151132720Skan  ios_base::sync_with_stdio(bool __sync)
152132720Skan  {
153132720Skan    // _GLIBCXX_RESOLVE_LIB_DEFECTS
154132720Skan    // 49.  Underspecification of ios_base::sync_with_stdio
155132720Skan    bool __ret = ios_base::Init::_S_synced_with_stdio;
156132720Skan
157132720Skan    // Turn off sync with C FILE* for cin, cout, cerr, clog iff
158132720Skan    // currently synchronized.
159132720Skan    if (!__sync && __ret)
160132720Skan      {
161146897Skan	// Make sure the standard streams are constructed.
162146897Skan	ios_base::Init __init;
163146897Skan
164132720Skan	ios_base::Init::_S_synced_with_stdio = __sync;
165132720Skan
166132720Skan	// Explicitly call dtors to free any memory that is
167132720Skan	// dynamically allocated by filebuf ctor or member functions,
168132720Skan	// but don't deallocate all memory by calling operator delete.
169132720Skan	buf_cout_sync.~stdio_sync_filebuf<char>();
170132720Skan	buf_cin_sync.~stdio_sync_filebuf<char>();
171132720Skan	buf_cerr_sync.~stdio_sync_filebuf<char>();
172132720Skan
173132720Skan#ifdef _GLIBCXX_USE_WCHAR_T
174132720Skan	buf_wcout_sync.~stdio_sync_filebuf<wchar_t>();
175132720Skan	buf_wcin_sync.~stdio_sync_filebuf<wchar_t>();
176132720Skan	buf_wcerr_sync.~stdio_sync_filebuf<wchar_t>();
177132720Skan#endif
178132720Skan
179132720Skan	// Create stream buffers for the standard streams and use
180132720Skan	// those buffers without destroying and recreating the
181132720Skan	// streams.
182132720Skan	new (&buf_cout) stdio_filebuf<char>(stdout, ios_base::out);
183132720Skan	new (&buf_cin) stdio_filebuf<char>(stdin, ios_base::in);
184132720Skan	new (&buf_cerr) stdio_filebuf<char>(stderr, ios_base::out);
185132720Skan	cout.rdbuf(&buf_cout);
186132720Skan	cin.rdbuf(&buf_cin);
187132720Skan	cerr.rdbuf(&buf_cerr);
188132720Skan	clog.rdbuf(&buf_cerr);
189132720Skan
190132720Skan#ifdef _GLIBCXX_USE_WCHAR_T
191132720Skan	new (&buf_wcout) stdio_filebuf<wchar_t>(stdout, ios_base::out);
192132720Skan	new (&buf_wcin) stdio_filebuf<wchar_t>(stdin, ios_base::in);
193132720Skan	new (&buf_wcerr) stdio_filebuf<wchar_t>(stderr, ios_base::out);
194132720Skan	wcout.rdbuf(&buf_wcout);
195132720Skan	wcin.rdbuf(&buf_wcin);
196132720Skan	wcerr.rdbuf(&buf_wcerr);
197132720Skan	wclog.rdbuf(&buf_wcerr);
198132720Skan#endif
199132720Skan      }
200132720Skan    return __ret;
201132720Skan  }
202169691Skan
203169691Skan_GLIBCXX_END_NAMESPACE
204