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