OSDN Git Service

2000-05-24 Benjamin Kosnik <bkoz@purist.soma.redhat.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / src / ios.cc
1 // Iostreams base classes -*- C++ -*-
2
3 // Copyright (C) 1997-1999 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   // Out-of-line definitions for static const ios_base members.
41   const ios_base::fmtflags ios_base::boolalpha;
42   const ios_base::fmtflags ios_base::dec;
43   const ios_base::fmtflags ios_base::fixed;
44   const ios_base::fmtflags ios_base::hex;
45   const ios_base::fmtflags ios_base::internal;
46   const ios_base::fmtflags ios_base::left;
47   const ios_base::fmtflags ios_base::oct;
48   const ios_base::fmtflags ios_base::right;
49   const ios_base::fmtflags ios_base::scientific;
50   const ios_base::fmtflags ios_base::showbase;
51   const ios_base::fmtflags ios_base::showpoint;
52   const ios_base::fmtflags ios_base::showpos;
53   const ios_base::fmtflags ios_base::skipws;
54   const ios_base::fmtflags ios_base::unitbuf;
55   const ios_base::fmtflags ios_base::uppercase;
56   const ios_base::fmtflags ios_base::adjustfield;
57   const ios_base::fmtflags ios_base::basefield;
58   const ios_base::fmtflags ios_base::floatfield;
59
60   const ios_base::iostate ios_base::badbit;
61   const ios_base::iostate ios_base::eofbit;
62   const ios_base::iostate ios_base::failbit;
63   const ios_base::iostate ios_base::goodbit;
64
65   const ios_base::openmode ios_base::app;
66   const ios_base::openmode ios_base::ate;
67   const ios_base::openmode ios_base::binary;
68   const ios_base::openmode ios_base::in;
69   const ios_base::openmode ios_base::out;
70   const ios_base::openmode ios_base::trunc;
71
72   const ios_base::seekdir ios_base::beg;
73   const ios_base::seekdir ios_base::cur;
74   const ios_base::seekdir ios_base::end;
75
76   ios_base::failure::failure(const string& __str)
77   {
78     strncpy(_M_name, __str.c_str(), _M_bufsize);
79     _M_name[_M_bufsize - 1] = '\0';
80   }
81
82   int ios_base::Init::_M_ios_base_init = 0;
83
84   ios_base::Init::Init()
85   {
86     if (++_M_ios_base_init == 1)
87       {
88         // NB: std_iostream.h creates the four standard files with
89         // default buffers. At this point, we swap out the default
90         // buffers for the properly-constructed ones, and dispose of
91         // the default buffers.
92         streambuf* __old;
93         _M_cout = new filebuf(1, "stdout", ios_base::out);
94         _M_cin = new filebuf(0, "stdin", ios_base::in);
95         _M_cerr = new filebuf(2, "stderr", ios_base::out);
96         __old = cout.rdbuf(_M_cout);
97         __old->~streambuf();
98         __old = cin.rdbuf(_M_cin);
99         __old->~streambuf();
100         cin.tie(&cout);
101         __old = cerr.rdbuf(_M_cerr);
102         __old->~streambuf();
103         cerr.flags(ios_base::unitbuf);
104         __old = clog.rdbuf(_M_cerr);
105         __old->~streambuf();
106 #ifdef _GLIBCPP_USE_WCHAR_T
107         wstreambuf* __wold;
108         _M_wcout = new wfilebuf(1, "stdout", ios_base::out);
109         _M_wcin = new wfilebuf(0, "stdin", ios_base::in);
110         _M_wcerr = new wfilebuf(2, "stderr", ios_base::out);
111         __wold = wcout.rdbuf(_M_wcout);
112         __wold->~wstreambuf();
113         __wold = wcin.rdbuf(_M_wcin);
114         __wold->~wstreambuf();
115         wcin.tie(&wcout);
116         __wold = wcerr.rdbuf(_M_wcerr);
117         __wold->~wstreambuf();
118         wcerr.flags(ios_base::unitbuf);
119         __wold = wclog.rdbuf(_M_wcerr);
120         __wold->~wstreambuf();
121 #endif
122       }
123   }
124
125   ios_base::Init::~Init()
126   {
127     if (--_M_ios_base_init == 0)
128       {
129         cout.flush();
130         cerr.flush();
131         clog.flush();
132         delete _M_cout;
133         delete _M_cin;
134         delete _M_cerr;
135         _M_cout = NULL;
136         _M_cin = NULL;
137         _M_cerr = NULL;
138 #ifdef _GLIBCPP_USE_WCHAR_T
139         wcout.flush();
140         wcerr.flush();
141         wclog.flush();
142         delete _M_wcout;
143         delete _M_wcin;
144         delete _M_wcerr;
145         _M_wcout = NULL;
146         _M_wcin = NULL;
147         _M_wcerr = NULL;
148 #endif
149       }
150   } 
151
152   // 27.4.2.5  ios_base storage functions
153   int 
154   ios_base::xalloc() throw()
155   {
156     // XXX MT
157     // XXX should be a symbol. (Reserve 0..3 for builtins.)
158     static int top = 4; 
159     return top++;
160   }
161
162   // 27.4.2.5  iword/pword storage
163   ios_base::_Words&
164   ios_base::_M_grow_words(int ix)
165   {
166     // Precondition: _M_word_limit <= ix
167     _Words zero = { 0, 0 };
168     int newlimit = _S_local_words;
169     _Words* words = _M_word_array;
170     int i = 0;
171     if (_S_local_words <= ix)
172       {
173         newlimit = ix+1;
174         try
175           { words = new _Words[ix+1]; }
176         catch (...)
177           {
178             _M_dummy = zero;  // XXX MT? Not on "normal" machines.
179             // XXX now in basic_ios
180             // _M_clear(_M_rdstate() | badbit);  // may throw
181             return _M_dummy;
182           }
183         do { words[i] = _M_words[i]; } while (++i < _M_word_limit);
184         if (_M_words != _M_word_array) delete [] _M_words;
185       }
186     
187     do { words[i] = zero; } while (++i < newlimit);
188     _M_words = words;
189     _M_word_limit = newlimit;
190     return words[ix];
191   }
192   
193   // Called only by basic_ios<>::init.
194   void 
195   ios_base::_M_init()   
196   {
197     // NB: May be called more than once
198     _M_flags = skipws | dec;
199     _M_width = 0;
200     _M_precision = 6;
201     _M_callbacks = 0;
202     _M_words = 0;
203     _M_word_limit = 0;
204     _M_locale_ios = locale();
205     // No init needed for _M_word_array or _M_dummy.
206   }  
207   
208   // 27.4.2.3  ios_base locale functions
209   locale
210   ios_base::imbue(const locale& __loc)
211   {
212     locale __old = _M_locale_ios;
213     _M_locale_ios = __loc;
214     // Make sure there's a callback for the format caches so they will be
215     // marked dirty.
216     _Format_cache<char>::_S_get(*this);
217 #ifdef _GLIBCPP_USE_WCHAR_T
218     _Format_cache<wchar_t>::_S_get(*this);
219 #endif
220     _M_call_callbacks(imbue_event);
221     return __old;
222   }
223
224   ios_base::ios_base()
225   {
226     // Do nothing; init() does it.  Static init to 0 makes everything sane.
227   }
228   
229   // 27.4.2.7  ios_base constructors/destructors
230   ios_base::~ios_base()
231   {
232     _M_call_callbacks(erase_event);
233     _M_dispose_callbacks();
234     if (_M_words != _M_word_array) 
235       delete [] _M_words;
236     // XXX done?
237   }
238
239   void 
240   ios_base::register_callback(event_callback __fn, int __index)
241   { _M_callbacks = new _Callback_list(__fn, __index, _M_callbacks); }
242
243   void 
244   ios_base::_M_call_callbacks(event __e) throw()
245   {
246     for (_Callback_list* __p = _M_callbacks; __p; __p = __p->_M_next)
247       {
248         try { 
249           (*__p->_M_fn) (__e, *this, __p->_M_index); 
250         } 
251         catch (...) { 
252         }
253       }
254   }
255
256   void 
257   ios_base::_M_dispose_callbacks(void)
258   {
259     _Callback_list* __p = _M_callbacks;
260     while (__p && __p->_M_remove_reference() == 0)
261       {
262         _Callback_list* __next = __p->_M_next;
263         delete __p;
264         __p = __next;
265       }
266     _M_callbacks = 0;
267   }
268
269   bool 
270   ios_base::sync_with_stdio(bool __sync)
271   { 
272 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
273     // 49.  Underspecification of ios_base::sync_with_stdio
274     bool __retval = __ioinit._M_cin->_M_file->get_fileno() == 0;
275     
276     // Turn off sync with C FILE* for cin, cout, cerr, clog.
277     if (!__sync && __retval)
278       {
279         // Need to dispose of the buffers created at initialization.
280         __ioinit._M_cout->~filebuf();
281         __ioinit._M_cin->~filebuf();
282         __ioinit._M_cerr->~filebuf();
283         __ioinit._M_cout = new filebuf();
284         __ioinit._M_cin = new filebuf();
285         __ioinit._M_cerr = new filebuf();
286         __ioinit._M_cout->open("stdout", ios_base::out);
287         __ioinit._M_cin->open("stdin", ios_base::in);
288         __ioinit._M_cerr->open("stderr", ios_base::out);
289         cout.rdbuf(__ioinit._M_cout);
290         cin.rdbuf(__ioinit._M_cin);
291         cerr.rdbuf(__ioinit._M_cerr);
292         cerr.flags(ios_base::unitbuf);
293         clog.rdbuf(__ioinit._M_cerr);
294 #ifdef _GLIBCPP_USE_WCHAR_T
295         __ioinit._M_wcout->~wfilebuf();
296         __ioinit._M_wcin->~wfilebuf();
297         __ioinit._M_wcerr->~wfilebuf();
298         __ioinit._M_wcout = new wfilebuf();
299         __ioinit._M_wcin = new wfilebuf();
300         __ioinit._M_wcerr = new wfilebuf();
301         __ioinit._M_wcout->open("wstdout", ios_base::out);
302         __ioinit._M_wcin->open("wstdin", ios_base::in);
303         __ioinit._M_wcerr->open("wstderr", ios_base::out);
304         wcout.rdbuf(__ioinit._M_wcout);
305         wcin.rdbuf(__ioinit._M_wcin);
306         wcerr.rdbuf(__ioinit._M_wcerr);
307         wcerr.flags(ios_base::unitbuf);
308         wclog.rdbuf(__ioinit._M_wcerr);
309 #endif
310       }
311     
312     return __retval; 
313 #endif
314   }
315
316 }  // namespace std
317
318
319
320
321
322
323
324
325
326
327
328