OSDN Git Service

2003-05-06 Benjamin Kosnik <bkoz@redhat.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / src / ios.cc
1 // Iostreams base classes -*- C++ -*-
2
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003
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
42 namespace __gnu_cxx
43 {
44   // Extern declarations for global objects in src/globals.cc.
45   extern stdio_filebuf<char> buf_cout;
46   extern stdio_filebuf<char> buf_cin;
47   extern stdio_filebuf<char> buf_cerr;
48
49 #ifdef _GLIBCPP_USE_WCHAR_T
50   extern stdio_filebuf<wchar_t> buf_wcout;
51   extern stdio_filebuf<wchar_t> buf_wcin;
52   extern stdio_filebuf<wchar_t> buf_wcerr;
53 #endif
54 } // namespace __gnu_cxx
55
56 namespace std 
57 {
58   using namespace __gnu_cxx;
59   
60   extern istream cin;
61   extern ostream cout;
62   extern ostream cerr;
63   extern ostream clog;
64
65 #ifdef _GLIBCPP_USE_WCHAR_T
66   extern wistream wcin;
67   extern wostream wcout;
68   extern wostream wcerr;
69   extern wostream wclog;
70 #endif
71
72   // Definitions for static const data members of __ios_flags.
73   const __ios_flags::__int_type __ios_flags::_S_boolalpha;
74   const __ios_flags::__int_type __ios_flags::_S_dec;
75   const __ios_flags::__int_type __ios_flags::_S_fixed;
76   const __ios_flags::__int_type __ios_flags::_S_hex;
77   const __ios_flags::__int_type __ios_flags::_S_internal;
78   const __ios_flags::__int_type __ios_flags::_S_left;
79   const __ios_flags::__int_type __ios_flags::_S_oct;
80   const __ios_flags::__int_type __ios_flags::_S_right;
81   const __ios_flags::__int_type __ios_flags::_S_scientific;
82   const __ios_flags::__int_type __ios_flags::_S_showbase;
83   const __ios_flags::__int_type __ios_flags::_S_showpoint;
84   const __ios_flags::__int_type __ios_flags::_S_showpos;
85   const __ios_flags::__int_type __ios_flags::_S_skipws;
86   const __ios_flags::__int_type __ios_flags::_S_unitbuf;
87   const __ios_flags::__int_type __ios_flags::_S_uppercase;
88   const __ios_flags::__int_type __ios_flags::_S_adjustfield;
89   const __ios_flags::__int_type __ios_flags::_S_basefield;
90   const __ios_flags::__int_type __ios_flags::_S_floatfield;
91
92   const __ios_flags::__int_type __ios_flags::_S_badbit;
93   const __ios_flags::__int_type __ios_flags::_S_eofbit;
94   const __ios_flags::__int_type __ios_flags::_S_failbit;
95
96   const __ios_flags::__int_type __ios_flags::_S_app;
97   const __ios_flags::__int_type __ios_flags::_S_ate;
98   const __ios_flags::__int_type __ios_flags::_S_bin;
99   const __ios_flags::__int_type __ios_flags::_S_in;
100   const __ios_flags::__int_type __ios_flags::_S_out;
101   const __ios_flags::__int_type __ios_flags::_S_trunc;
102
103   // Definitions for static const members of ios_base.
104   const ios_base::fmtflags ios_base::boolalpha;
105   const ios_base::fmtflags ios_base::dec;
106   const ios_base::fmtflags ios_base::fixed;
107   const ios_base::fmtflags ios_base::hex;
108   const ios_base::fmtflags ios_base::internal;
109   const ios_base::fmtflags ios_base::left;
110   const ios_base::fmtflags ios_base::oct;
111   const ios_base::fmtflags ios_base::right;
112   const ios_base::fmtflags ios_base::scientific;
113   const ios_base::fmtflags ios_base::showbase;
114   const ios_base::fmtflags ios_base::showpoint;
115   const ios_base::fmtflags ios_base::showpos;
116   const ios_base::fmtflags ios_base::skipws;
117   const ios_base::fmtflags ios_base::unitbuf;
118   const ios_base::fmtflags ios_base::uppercase;
119   const ios_base::fmtflags ios_base::adjustfield;
120   const ios_base::fmtflags ios_base::basefield;
121   const ios_base::fmtflags ios_base::floatfield;
122
123   const ios_base::iostate ios_base::badbit;
124   const ios_base::iostate ios_base::eofbit;
125   const ios_base::iostate ios_base::failbit;
126   const ios_base::iostate ios_base::goodbit;
127
128   const ios_base::openmode ios_base::app;
129   const ios_base::openmode ios_base::ate;
130   const ios_base::openmode ios_base::binary;
131   const ios_base::openmode ios_base::in;
132   const ios_base::openmode ios_base::out;
133   const ios_base::openmode ios_base::trunc;
134
135   const ios_base::seekdir ios_base::beg;
136   const ios_base::seekdir ios_base::cur;
137   const ios_base::seekdir ios_base::end;
138
139   const int ios_base::_S_local_word_size;
140   int ios_base::Init::_S_ios_base_init = 0;
141   bool ios_base::Init::_S_synced_with_stdio = true;
142
143   ios_base::failure::failure(const string& __str) throw()
144   {
145     strncpy(_M_name, __str.c_str(), _S_bufsize);
146     _M_name[_S_bufsize - 1] = '\0';
147   }
148
149   ios_base::failure::~failure() throw()
150   { }
151
152   const char*
153   ios_base::failure::what() const throw()
154   { return _M_name; }
155
156   void
157   ios_base::Init::_S_create_buffers(bool __sync)
158   {
159     size_t __out_size = __sync ? 0 : static_cast<size_t>(BUFSIZ);
160     size_t __in_size = __sync ? 1 : static_cast<size_t>(BUFSIZ);
161
162     // Create stream buffers for the standard streams and use those
163     // buffers without destroying and recreating the streams.
164     new (&buf_cout) stdio_filebuf<char>(stdout, ios_base::out, __out_size);
165     new (&buf_cin) stdio_filebuf<char>(stdin, ios_base::in, __in_size);
166     new (&buf_cerr) stdio_filebuf<char>(stderr, ios_base::out, __out_size);
167     cout.rdbuf(&buf_cout);
168     cin.rdbuf(&buf_cin);
169     cerr.rdbuf(&buf_cerr);
170     clog.rdbuf(&buf_cerr);
171     
172 #ifdef _GLIBCPP_USE_WCHAR_T
173     new (&buf_wcout) stdio_filebuf<wchar_t>(stdout, ios_base::out, __out_size);
174     new (&buf_wcin) stdio_filebuf<wchar_t>(stdin, ios_base::in, __in_size);
175     new (&buf_wcerr) stdio_filebuf<wchar_t>(stderr, ios_base::out, __out_size);
176     wcout.rdbuf(&buf_wcout);
177     wcin.rdbuf(&buf_wcin);
178     wcerr.rdbuf(&buf_wcerr);
179     wclog.rdbuf(&buf_wcerr);
180 #endif
181   }
182
183   void
184   ios_base::Init::_S_destroy_buffers()
185   {
186     // Explicitly call dtors to free any memory that is dynamically
187     // allocated by filebuf ctor or member functions, but don't
188     // deallocate all memory by calling operator delete.
189     buf_cout.~stdio_filebuf();
190     buf_cin.~stdio_filebuf();
191     buf_cerr.~stdio_filebuf();
192
193 #ifdef _GLIBCPP_USE_WCHAR_T
194     buf_wcout.~stdio_filebuf();
195     buf_wcin.~stdio_filebuf();
196     buf_wcerr.~stdio_filebuf();
197 #endif
198   }
199
200   ios_base::Init::Init()
201   {
202     if (_S_ios_base_init == 0)
203       {
204         // Standard streams default to synced with "C" operations.
205         ios_base::Init::_S_synced_with_stdio = true;
206
207         // The standard streams are constructed once only and never destroyed.
208         // The stream buffers are set in _S_create_buffers below.
209         new (&cout) ostream(NULL);
210         new (&cin) istream(NULL);
211         new (&cerr) ostream(NULL);
212         new (&clog) ostream(NULL);
213         cin.tie(&cout);
214         cerr.flags(ios_base::unitbuf);
215         
216 #ifdef _GLIBCPP_USE_WCHAR_T
217         new (&wcout) wostream(NULL);
218         new (&wcin) wistream(NULL);
219         new (&wcerr) wostream(NULL);
220         new (&wclog) wostream(NULL);
221         wcin.tie(&wcout);
222         wcerr.flags(ios_base::unitbuf);
223 #endif
224
225         _S_create_buffers(ios_base::Init::_S_synced_with_stdio);
226         _S_ios_base_init = 1;
227       }
228     ++_S_ios_base_init;
229   }
230
231   ios_base::Init::~Init()
232   {
233     if (--_S_ios_base_init == 1)
234       {
235         // Catch any exceptions thrown by basic_ostream::flush()
236         try
237           { 
238             // Flush standard output streams as required by 27.4.2.1.6
239             cout.flush();
240             cerr.flush();
241             clog.flush();
242     
243 #ifdef _GLIBCPP_USE_WCHAR_T
244             wcout.flush();
245             wcerr.flush();
246             wclog.flush();    
247 #endif
248           }
249         catch (...)
250           { }
251       }
252   } 
253
254   // 27.4.2.5  ios_base storage functions
255   int 
256   ios_base::xalloc() throw()
257   {
258     // Implementation note: Initialize top to zero to ensure that
259     // initialization occurs before main() is started.
260     static _Atomic_word _S_top = 0; 
261     return __exchange_and_add(&_S_top, 1) + 4;
262   }
263
264   // 27.4.2.5  iword/pword storage
265   ios_base::_Words&
266   ios_base::_M_grow_words(int ix)
267   {
268     // Precondition: _M_word_size <= ix
269     int newsize = _S_local_word_size;
270     _Words* words = _M_local_word;
271     if (ix > _S_local_word_size - 1)
272       {
273         if (ix < numeric_limits<int>::max())
274           {
275             newsize = ix + 1;
276             try
277               { words = new _Words[newsize]; }
278             catch (...)
279               {
280                 _M_streambuf_state |= badbit;
281                 if (_M_streambuf_state & _M_exception)
282                   __throw_ios_failure("ios_base::_M_grow_words "
283                                       "allocation failed");
284                 return _M_word_zero;
285               }
286             for (int i = 0; i < _M_word_size; i++) 
287               words[i] = _M_word[i];
288             if (_M_word && _M_word != _M_local_word) 
289               {
290                 delete [] _M_word;
291                 _M_word = 0;
292               }
293           }
294         else
295           {
296             _M_streambuf_state |= badbit;
297             if (_M_streambuf_state & _M_exception)
298               __throw_ios_failure("ios_base::_M_grow_words ix not valid");
299             return _M_word_zero;
300           }
301       }
302     _M_word = words;
303     _M_word_size = newsize;
304     return _M_word[ix];
305   }
306   
307   // Called only by basic_ios<>::init.
308   void 
309   ios_base::_M_init()   
310   {
311     // NB: May be called more than once
312     _M_precision = 6;
313     _M_width = 0;
314     _M_flags = skipws | dec;
315     _M_ios_locale = locale();
316   }  
317   
318   // 27.4.2.3  ios_base locale functions
319   locale
320   ios_base::imbue(const locale& __loc)
321   {
322     locale __old = _M_ios_locale;
323     _M_ios_locale = __loc;
324     _M_call_callbacks(imbue_event);
325     return __old;
326   }
327
328   ios_base::ios_base() : _M_callbacks(0), _M_word_size(_S_local_word_size),
329                          _M_word(_M_local_word), _M_locale_cache(0)
330   {
331     // Do nothing: basic_ios::init() does it.  
332     // NB: _M_callbacks and _M_word must be zero for non-initialized
333     // ios_base to go through ~ios_base gracefully.
334   }
335   
336   // 27.4.2.7  ios_base constructors/destructors
337   ios_base::~ios_base()
338   {
339     _M_call_callbacks(erase_event);
340     _M_dispose_callbacks();
341     if (_M_word != _M_local_word) 
342       {
343         delete [] _M_word;
344         _M_word = 0;
345       }
346   }
347
348   void 
349   ios_base::register_callback(event_callback __fn, int __index)
350   { _M_callbacks = new _Callback_list(__fn, __index, _M_callbacks); }
351
352   void 
353   ios_base::_M_call_callbacks(event __e) throw()
354   {
355     _Callback_list* __p = _M_callbacks;
356     while (__p)
357       {
358         try 
359           { (*__p->_M_fn) (__e, *this, __p->_M_index); } 
360         catch (...) 
361           { }
362         __p = __p->_M_next;
363       }
364   }
365
366   void 
367   ios_base::_M_dispose_callbacks(void)
368   {
369     _Callback_list* __p = _M_callbacks;
370     while (__p && __p->_M_remove_reference() == 0)
371       {
372         _Callback_list* __next = __p->_M_next;
373         delete __p;
374         __p = __next;
375       }
376     _M_callbacks = 0;
377   }
378
379   bool 
380   ios_base::sync_with_stdio(bool __sync)
381   { 
382 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
383     // 49.  Underspecification of ios_base::sync_with_stdio
384     bool __ret = ios_base::Init::_S_synced_with_stdio;
385 #endif
386
387     // Turn off sync with C FILE* for cin, cout, cerr, clog iff
388     // currently synchronized.
389     if (!__sync && __ret)
390       {
391         ios_base::Init::_S_synced_with_stdio = __sync;
392         ios_base::Init::_S_destroy_buffers();
393         ios_base::Init::_S_create_buffers(__sync);
394       }
395     return __ret; 
396   }
397 }  // namespace std