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