OSDN Git Service

5c7ab2e8322b993c5c6adf6506132c15154582ab
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / src / ios_init.cc
1 // Iostreams base classes -*- C++ -*-
2
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
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 <ext/stdio_filebuf.h>
40 #include <ext/stdio_sync_filebuf.h>
41
42 namespace __gnu_internal _GLIBCXX_VISIBILITY(hidden)
43 {
44   using namespace __gnu_cxx;
45
46   // Extern declarations for global objects in src/globals.cc.
47   extern stdio_sync_filebuf<char> buf_cout_sync;
48   extern stdio_sync_filebuf<char> buf_cin_sync;
49   extern stdio_sync_filebuf<char> buf_cerr_sync;
50
51   extern stdio_filebuf<char> buf_cout;
52   extern stdio_filebuf<char> buf_cin;
53   extern stdio_filebuf<char> buf_cerr;
54
55 #ifdef _GLIBCXX_USE_WCHAR_T
56   extern stdio_sync_filebuf<wchar_t> buf_wcout_sync;
57   extern stdio_sync_filebuf<wchar_t> buf_wcin_sync;
58   extern stdio_sync_filebuf<wchar_t> buf_wcerr_sync;
59
60   extern stdio_filebuf<wchar_t> buf_wcout;
61   extern stdio_filebuf<wchar_t> buf_wcin;
62   extern stdio_filebuf<wchar_t> buf_wcerr;
63 #endif
64 } // namespace __gnu_internal
65
66 _GLIBCXX_BEGIN_NAMESPACE(std)
67
68   using namespace __gnu_internal;
69   
70   extern istream cin;
71   extern ostream cout;
72   extern ostream cerr;
73   extern ostream clog;
74
75 #ifdef _GLIBCXX_USE_WCHAR_T
76   extern wistream wcin;
77   extern wostream wcout;
78   extern wostream wcerr;
79   extern wostream wclog;
80 #endif
81
82   ios_base::Init::Init()
83   {
84     if (__gnu_cxx::__exchange_and_add_dispatch(&_S_refcount, 1) == 0)
85       {
86         // Standard streams default to synced with "C" operations.
87         _S_synced_with_stdio = true;
88
89         new (&buf_cout_sync) stdio_sync_filebuf<char>(stdout);
90         new (&buf_cin_sync) stdio_sync_filebuf<char>(stdin);
91         new (&buf_cerr_sync) stdio_sync_filebuf<char>(stderr);
92
93         // The standard streams are constructed once only and never
94         // destroyed.
95         new (&cout) ostream(&buf_cout_sync);
96         new (&cin) istream(&buf_cin_sync);
97         new (&cerr) ostream(&buf_cerr_sync);
98         new (&clog) ostream(&buf_cerr_sync);
99         cin.tie(&cout);
100         cerr.flags(ios_base::unitbuf);
101         // _GLIBCXX_RESOLVE_LIB_DEFECTS
102         // 455. cerr::tie() and wcerr::tie() are overspecified.
103         cerr.tie(&cout);
104
105 #ifdef _GLIBCXX_USE_WCHAR_T
106         new (&buf_wcout_sync) stdio_sync_filebuf<wchar_t>(stdout);
107         new (&buf_wcin_sync) stdio_sync_filebuf<wchar_t>(stdin);
108         new (&buf_wcerr_sync) stdio_sync_filebuf<wchar_t>(stderr);
109
110         new (&wcout) wostream(&buf_wcout_sync);
111         new (&wcin) wistream(&buf_wcin_sync);
112         new (&wcerr) wostream(&buf_wcerr_sync);
113         new (&wclog) wostream(&buf_wcerr_sync);
114         wcin.tie(&wcout);
115         wcerr.flags(ios_base::unitbuf);
116         wcerr.tie(&wcout);      
117 #endif
118         
119         // NB: Have to set refcount above one, so that standard
120         // streams are not re-initialized with uses of ios_base::Init
121         // besides <iostream> static object, ie just using <ios> with
122         // ios_base::Init objects.
123         __gnu_cxx::__atomic_add_dispatch(&_S_refcount, 1);
124       }
125   }
126
127   ios_base::Init::~Init()
128   {
129     if (__gnu_cxx::__exchange_and_add_dispatch(&_S_refcount, -1) == 2)
130       {
131         // Catch any exceptions thrown by basic_ostream::flush()
132         try
133           { 
134             // Flush standard output streams as required by 27.4.2.1.6
135             cout.flush();
136             cerr.flush();
137             clog.flush();
138     
139 #ifdef _GLIBCXX_USE_WCHAR_T
140             wcout.flush();
141             wcerr.flush();
142             wclog.flush();    
143 #endif
144           }
145         catch (...)
146           { }
147       }
148   } 
149
150   bool 
151   ios_base::sync_with_stdio(bool __sync)
152   { 
153     // _GLIBCXX_RESOLVE_LIB_DEFECTS
154     // 49.  Underspecification of ios_base::sync_with_stdio
155     bool __ret = ios_base::Init::_S_synced_with_stdio;
156
157     // Turn off sync with C FILE* for cin, cout, cerr, clog iff
158     // currently synchronized.
159     if (!__sync && __ret)
160       {
161         // Make sure the standard streams are constructed.
162         ios_base::Init __init;
163
164         ios_base::Init::_S_synced_with_stdio = __sync;
165
166         // Explicitly call dtors to free any memory that is
167         // dynamically allocated by filebuf ctor or member functions,
168         // but don't deallocate all memory by calling operator delete.
169         buf_cout_sync.~stdio_sync_filebuf<char>();
170         buf_cin_sync.~stdio_sync_filebuf<char>();
171         buf_cerr_sync.~stdio_sync_filebuf<char>();
172
173 #ifdef _GLIBCXX_USE_WCHAR_T
174         buf_wcout_sync.~stdio_sync_filebuf<wchar_t>();
175         buf_wcin_sync.~stdio_sync_filebuf<wchar_t>();
176         buf_wcerr_sync.~stdio_sync_filebuf<wchar_t>();
177 #endif
178
179         // Create stream buffers for the standard streams and use
180         // those buffers without destroying and recreating the
181         // streams.
182         new (&buf_cout) stdio_filebuf<char>(stdout, ios_base::out);
183         new (&buf_cin) stdio_filebuf<char>(stdin, ios_base::in);
184         new (&buf_cerr) stdio_filebuf<char>(stderr, ios_base::out);
185         cout.rdbuf(&buf_cout);
186         cin.rdbuf(&buf_cin);
187         cerr.rdbuf(&buf_cerr);
188         clog.rdbuf(&buf_cerr);
189     
190 #ifdef _GLIBCXX_USE_WCHAR_T
191         new (&buf_wcout) stdio_filebuf<wchar_t>(stdout, ios_base::out);
192         new (&buf_wcin) stdio_filebuf<wchar_t>(stdin, ios_base::in);
193         new (&buf_wcerr) stdio_filebuf<wchar_t>(stderr, ios_base::out);
194         wcout.rdbuf(&buf_wcout);
195         wcin.rdbuf(&buf_wcin);
196         wcerr.rdbuf(&buf_wcerr);
197         wclog.rdbuf(&buf_wcerr);
198 #endif
199       }
200     return __ret; 
201   }
202
203 _GLIBCXX_END_NAMESPACE