OSDN Git Service

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