OSDN Git Service

2000-12-06 Benjamin Kosnik <bkoz@kredhat.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / src / ios.cc
1 // Iostreams base classes -*- C++ -*-
2
3 // Copyright (C) 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library.  This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING.  If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // USA.
20
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction.  Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License.  This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
29
30 //
31 // ISO C++ 14882: 27.4  Iostreams base classes
32 //
33
34 #include <bits/std_ios.h>
35 #include <bits/std_iostream.h>
36 #include <bits/std_fstream.h>
37
38 namespace std {
39
40   // Definitions for static const data members of __ios_flags.
41   const __ios_flags::__int_type __ios_flags::_S_boolalpha;
42   const __ios_flags::__int_type __ios_flags::_S_dec;
43   const __ios_flags::__int_type __ios_flags::_S_fixed;
44   const __ios_flags::__int_type __ios_flags::_S_hex;
45   const __ios_flags::__int_type __ios_flags::_S_internal;
46   const __ios_flags::__int_type __ios_flags::_S_left;
47   const __ios_flags::__int_type __ios_flags::_S_oct;
48   const __ios_flags::__int_type __ios_flags::_S_right;
49   const __ios_flags::__int_type __ios_flags::_S_scientific;
50   const __ios_flags::__int_type __ios_flags::_S_showbase;
51   const __ios_flags::__int_type __ios_flags::_S_showpoint;
52   const __ios_flags::__int_type __ios_flags::_S_showpos;
53   const __ios_flags::__int_type __ios_flags::_S_skipws;
54   const __ios_flags::__int_type __ios_flags::_S_unitbuf;
55   const __ios_flags::__int_type __ios_flags::_S_uppercase;
56   const __ios_flags::__int_type __ios_flags::_S_adjustfield;
57   const __ios_flags::__int_type __ios_flags::_S_basefield;
58   const __ios_flags::__int_type __ios_flags::_S_floatfield;
59
60   const __ios_flags::__int_type __ios_flags::_S_badbit;
61   const __ios_flags::__int_type __ios_flags::_S_eofbit;
62   const __ios_flags::__int_type __ios_flags::_S_failbit;
63
64   const __ios_flags::__int_type __ios_flags::_S_app;
65   const __ios_flags::__int_type __ios_flags::_S_ate;
66   const __ios_flags::__int_type __ios_flags::_S_bin;
67   const __ios_flags::__int_type __ios_flags::_S_in;
68   const __ios_flags::__int_type __ios_flags::_S_out;
69   const __ios_flags::__int_type __ios_flags::_S_trunc;
70
71   // Definitions for static const members of ios_base.
72   const ios_base::fmtflags ios_base::boolalpha;
73   const ios_base::fmtflags ios_base::dec;
74   const ios_base::fmtflags ios_base::fixed;
75   const ios_base::fmtflags ios_base::hex;
76   const ios_base::fmtflags ios_base::internal;
77   const ios_base::fmtflags ios_base::left;
78   const ios_base::fmtflags ios_base::oct;
79   const ios_base::fmtflags ios_base::right;
80   const ios_base::fmtflags ios_base::scientific;
81   const ios_base::fmtflags ios_base::showbase;
82   const ios_base::fmtflags ios_base::showpoint;
83   const ios_base::fmtflags ios_base::showpos;
84   const ios_base::fmtflags ios_base::skipws;
85   const ios_base::fmtflags ios_base::unitbuf;
86   const ios_base::fmtflags ios_base::uppercase;
87   const ios_base::fmtflags ios_base::adjustfield;
88   const ios_base::fmtflags ios_base::basefield;
89   const ios_base::fmtflags ios_base::floatfield;
90
91   const ios_base::iostate ios_base::badbit;
92   const ios_base::iostate ios_base::eofbit;
93   const ios_base::iostate ios_base::failbit;
94   const ios_base::iostate ios_base::goodbit;
95
96   const ios_base::openmode ios_base::app;
97   const ios_base::openmode ios_base::ate;
98   const ios_base::openmode ios_base::binary;
99   const ios_base::openmode ios_base::in;
100   const ios_base::openmode ios_base::out;
101   const ios_base::openmode ios_base::trunc;
102
103   const ios_base::seekdir ios_base::beg;
104   const ios_base::seekdir ios_base::cur;
105   const ios_base::seekdir ios_base::end;
106
107   int ios_base::Init::_S_ios_base_init = 0;
108
109   const int ios_base::_S_local_words;
110
111   ios_base::failure::failure(const string& __str)
112   {
113     strncpy(_M_name, __str.c_str(), _M_bufsize);
114     _M_name[_M_bufsize - 1] = '\0';
115   }
116
117   ios_base::failure::~failure()
118   { }
119
120   const char*
121   ios_base::failure::what() const throw()
122   { return _M_name; }
123
124   ios_base::Init::Init()
125   {
126     if (++_S_ios_base_init == 1)
127       {
128         // NB: std_iostream.h creates the four standard files with
129         // default buffers. At this point, we swap out the default
130         // buffers for the properly-constructed ones, and dispose of
131         // the default buffers.
132         streambuf* __old;
133         _M_cout = new filebuf(1, "stdout", ios_base::out);
134         _M_cin = new filebuf(0, "stdin", ios_base::in);
135         _M_cerr = new filebuf(2, "stderr", ios_base::out);
136         __old = cout.rdbuf(_M_cout);
137         __old->~streambuf();
138         __old = cin.rdbuf(_M_cin);
139         __old->~streambuf();
140         cin.tie(&cout);
141         __old = cerr.rdbuf(_M_cerr);
142         __old->~streambuf();
143         cerr.flags(ios_base::unitbuf);
144         __old = clog.rdbuf(_M_cerr);
145         __old->~streambuf();
146 #ifdef _GLIBCPP_USE_WCHAR_T
147         wstreambuf* __wold;
148         _M_wcout = new wfilebuf(1, "stdout", ios_base::out);
149         _M_wcin = new wfilebuf(0, "stdin", ios_base::in);
150         _M_wcerr = new wfilebuf(2, "stderr", ios_base::out);
151         __wold = wcout.rdbuf(_M_wcout);
152         __wold->~wstreambuf();
153         __wold = wcin.rdbuf(_M_wcin);
154         __wold->~wstreambuf();
155         wcin.tie(&wcout);
156         __wold = wcerr.rdbuf(_M_wcerr);
157         __wold->~wstreambuf();
158         wcerr.flags(ios_base::unitbuf);
159         __wold = wclog.rdbuf(_M_wcerr);
160         __wold->~wstreambuf();
161 #endif
162       }
163   }
164
165   ios_base::Init::~Init()
166   {
167     if (--_S_ios_base_init == 0)
168       {
169         cout.flush();
170         cerr.flush();
171         clog.flush();
172         delete _M_cout;
173         delete _M_cin;
174         delete _M_cerr;
175         _M_cout = NULL;
176         _M_cin = NULL;
177         _M_cerr = NULL;
178 #ifdef _GLIBCPP_USE_WCHAR_T
179         wcout.flush();
180         wcerr.flush();
181         wclog.flush();
182         delete _M_wcout;
183         delete _M_wcin;
184         delete _M_wcerr;
185         _M_wcout = NULL;
186         _M_wcin = NULL;
187         _M_wcerr = NULL;
188 #endif
189       }
190   } 
191
192   // 27.4.2.5  ios_base storage functions
193   int 
194   ios_base::xalloc() throw()
195   {
196     // XXX MT
197     // XXX should be a symbol. (Reserve 0..3 for builtins.)
198     static int top = 4; 
199     return top++;
200   }
201
202   // 27.4.2.5  iword/pword storage
203   ios_base::_Words&
204   ios_base::_M_grow_words(int ix)
205   {
206     // Precondition: _M_word_limit <= ix
207     _Words zero = { 0, 0 };
208     int newlimit = _S_local_words;
209     _Words* words = _M_word_array;
210     int i = 0;
211     if (_S_local_words <= ix)
212       {
213         newlimit = ix+1;
214         try
215           { words = new _Words[ix+1]; }
216         catch (...)
217           {
218             _M_dummy = zero;  // XXX MT? Not on "normal" machines.
219             // XXX now in basic_ios
220             // _M_clear(_M_rdstate() | badbit);  // may throw
221             return _M_dummy;
222           }
223         do { words[i] = _M_words[i]; } while (++i < _M_word_limit);
224         if (_M_words != _M_word_array) delete [] _M_words;
225       }
226     
227     do { words[i] = zero; } while (++i < newlimit);
228     _M_words = words;
229     _M_word_limit = newlimit;
230     return words[ix];
231   }
232   
233   // Called only by basic_ios<>::init.
234   void 
235   ios_base::_M_init()   
236   {
237     // NB: May be called more than once
238     _M_flags = skipws | dec;
239     _M_width = 0;
240     _M_precision = 6;
241     _M_callbacks = 0;
242     _M_words = 0;
243     _M_word_limit = 0;
244     _M_ios_locale = locale();
245     // No init needed for _M_word_array or _M_dummy.
246   }  
247   
248   // 27.4.2.3  ios_base locale functions
249   locale
250   ios_base::imbue(const locale& __loc)
251   {
252     locale __old = _M_ios_locale;
253     _M_ios_locale = __loc;
254     // Make sure there's a callback for the format caches so they will be
255     // marked dirty.
256     _Format_cache<char>::_S_get(*this);
257 #ifdef _GLIBCPP_USE_WCHAR_T
258     _Format_cache<wchar_t>::_S_get(*this);
259 #endif
260     _M_call_callbacks(imbue_event);
261     return __old;
262   }
263
264   ios_base::ios_base()
265   {
266     // Do nothing; init() does it.  Static init to 0 makes everything sane.
267   }
268   
269   // 27.4.2.7  ios_base constructors/destructors
270   ios_base::~ios_base()
271   {
272     _M_call_callbacks(erase_event);
273     _M_dispose_callbacks();
274     if (_M_words != _M_word_array) 
275       delete [] _M_words;
276     // XXX done?
277   }
278
279   void 
280   ios_base::register_callback(event_callback __fn, int __index)
281   { _M_callbacks = new _Callback_list(__fn, __index, _M_callbacks); }
282
283   void 
284   ios_base::_M_call_callbacks(event __e) throw()
285   {
286     for (_Callback_list* __p = _M_callbacks; __p; __p = __p->_M_next)
287       {
288         try { 
289           (*__p->_M_fn) (__e, *this, __p->_M_index); 
290         } 
291         catch (...) { 
292         }
293       }
294   }
295
296   void 
297   ios_base::_M_dispose_callbacks(void)
298   {
299     _Callback_list* __p = _M_callbacks;
300     while (__p && __p->_M_remove_reference() == 0)
301       {
302         _Callback_list* __next = __p->_M_next;
303         delete __p;
304         __p = __next;
305       }
306     _M_callbacks = 0;
307   }
308
309   bool 
310   ios_base::sync_with_stdio(bool __sync)
311   { 
312 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
313     // 49.  Underspecification of ios_base::sync_with_stdio
314     bool __ret = __ioinit._M_cin->_M_file->get_fileno() == 0;
315
316     // Turn off sync with C FILE* for cin, cout, cerr, clog.
317     if (!__sync && __ret)
318       {
319         // Need to dispose of the buffers created at initialization.
320         __ioinit._M_cout->~filebuf();
321         __ioinit._M_cin->~filebuf();
322         __ioinit._M_cerr->~filebuf();
323         __ioinit._M_cout = new filebuf();
324         __ioinit._M_cin = new filebuf();
325         __ioinit._M_cerr = new filebuf();
326         __ioinit._M_cout->open("stdout", ios_base::out);
327         __ioinit._M_cin->open("stdin", ios_base::in);
328         __ioinit._M_cerr->open("stderr", ios_base::out);
329         cout.rdbuf(__ioinit._M_cout);
330         cin.rdbuf(__ioinit._M_cin);
331         cerr.rdbuf(__ioinit._M_cerr);
332         cerr.flags(ios_base::unitbuf);
333         clog.rdbuf(__ioinit._M_cerr);
334 #ifdef _GLIBCPP_USE_WCHAR_T
335         __ioinit._M_wcout->~wfilebuf();
336         __ioinit._M_wcin->~wfilebuf();
337         __ioinit._M_wcerr->~wfilebuf();
338         __ioinit._M_wcout = new wfilebuf();
339         __ioinit._M_wcin = new wfilebuf();
340         __ioinit._M_wcerr = new wfilebuf();
341         __ioinit._M_wcout->open("wstdout", ios_base::out);
342         __ioinit._M_wcin->open("wstdin", ios_base::in);
343         __ioinit._M_wcerr->open("wstderr", ios_base::out);
344         wcout.rdbuf(__ioinit._M_wcout);
345         wcin.rdbuf(__ioinit._M_wcin);
346         wcerr.rdbuf(__ioinit._M_wcerr);
347         wcerr.flags(ios_base::unitbuf);
348         wclog.rdbuf(__ioinit._M_wcerr);
349 #endif
350       }
351     
352     return __ret; 
353 #endif
354   }
355
356 }  // namespace std
357
358
359
360
361
362
363
364
365
366
367
368