1 // Iostreams base classes -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003
4 // Free Software Foundation, Inc.
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)
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.
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,
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.
32 // ISO C++ 14882: 27.4 Iostreams base classes
39 #include <bits/atomicity.h>
40 #include <ext/stdio_filebuf.h>
41 #include <ext/stdio_sync_filebuf.h>
45 // Extern declarations for global objects in src/globals.cc.
46 extern stdio_sync_filebuf<char> buf_cout_sync;
47 extern stdio_sync_filebuf<char> buf_cin_sync;
48 extern stdio_sync_filebuf<char> buf_cerr_sync;
50 extern stdio_filebuf<char> buf_cout;
51 extern stdio_filebuf<char> buf_cin;
52 extern stdio_filebuf<char> buf_cerr;
54 #ifdef _GLIBCXX_USE_WCHAR_T
55 extern stdio_sync_filebuf<wchar_t> buf_wcout_sync;
56 extern stdio_sync_filebuf<wchar_t> buf_wcin_sync;
57 extern stdio_sync_filebuf<wchar_t> buf_wcerr_sync;
59 extern stdio_filebuf<wchar_t> buf_wcout;
60 extern stdio_filebuf<wchar_t> buf_wcin;
61 extern stdio_filebuf<wchar_t> buf_wcerr;
63 } // namespace __gnu_cxx
67 using namespace __gnu_cxx;
74 #ifdef _GLIBCXX_USE_WCHAR_T
76 extern wostream wcout;
77 extern wostream wcerr;
78 extern wostream wclog;
81 // Definitions for static const data members of __ios_flags.
82 const __ios_flags::__int_type __ios_flags::_S_boolalpha;
83 const __ios_flags::__int_type __ios_flags::_S_dec;
84 const __ios_flags::__int_type __ios_flags::_S_fixed;
85 const __ios_flags::__int_type __ios_flags::_S_hex;
86 const __ios_flags::__int_type __ios_flags::_S_internal;
87 const __ios_flags::__int_type __ios_flags::_S_left;
88 const __ios_flags::__int_type __ios_flags::_S_oct;
89 const __ios_flags::__int_type __ios_flags::_S_right;
90 const __ios_flags::__int_type __ios_flags::_S_scientific;
91 const __ios_flags::__int_type __ios_flags::_S_showbase;
92 const __ios_flags::__int_type __ios_flags::_S_showpoint;
93 const __ios_flags::__int_type __ios_flags::_S_showpos;
94 const __ios_flags::__int_type __ios_flags::_S_skipws;
95 const __ios_flags::__int_type __ios_flags::_S_unitbuf;
96 const __ios_flags::__int_type __ios_flags::_S_uppercase;
97 const __ios_flags::__int_type __ios_flags::_S_adjustfield;
98 const __ios_flags::__int_type __ios_flags::_S_basefield;
99 const __ios_flags::__int_type __ios_flags::_S_floatfield;
101 const __ios_flags::__int_type __ios_flags::_S_badbit;
102 const __ios_flags::__int_type __ios_flags::_S_eofbit;
103 const __ios_flags::__int_type __ios_flags::_S_failbit;
105 const __ios_flags::__int_type __ios_flags::_S_app;
106 const __ios_flags::__int_type __ios_flags::_S_ate;
107 const __ios_flags::__int_type __ios_flags::_S_bin;
108 const __ios_flags::__int_type __ios_flags::_S_in;
109 const __ios_flags::__int_type __ios_flags::_S_out;
110 const __ios_flags::__int_type __ios_flags::_S_trunc;
112 // Definitions for static const members of ios_base.
113 const ios_base::fmtflags ios_base::boolalpha;
114 const ios_base::fmtflags ios_base::dec;
115 const ios_base::fmtflags ios_base::fixed;
116 const ios_base::fmtflags ios_base::hex;
117 const ios_base::fmtflags ios_base::internal;
118 const ios_base::fmtflags ios_base::left;
119 const ios_base::fmtflags ios_base::oct;
120 const ios_base::fmtflags ios_base::right;
121 const ios_base::fmtflags ios_base::scientific;
122 const ios_base::fmtflags ios_base::showbase;
123 const ios_base::fmtflags ios_base::showpoint;
124 const ios_base::fmtflags ios_base::showpos;
125 const ios_base::fmtflags ios_base::skipws;
126 const ios_base::fmtflags ios_base::unitbuf;
127 const ios_base::fmtflags ios_base::uppercase;
128 const ios_base::fmtflags ios_base::adjustfield;
129 const ios_base::fmtflags ios_base::basefield;
130 const ios_base::fmtflags ios_base::floatfield;
132 const ios_base::iostate ios_base::badbit;
133 const ios_base::iostate ios_base::eofbit;
134 const ios_base::iostate ios_base::failbit;
135 const ios_base::iostate ios_base::goodbit;
137 const ios_base::openmode ios_base::app;
138 const ios_base::openmode ios_base::ate;
139 const ios_base::openmode ios_base::binary;
140 const ios_base::openmode ios_base::in;
141 const ios_base::openmode ios_base::out;
142 const ios_base::openmode ios_base::trunc;
144 const ios_base::seekdir ios_base::beg;
145 const ios_base::seekdir ios_base::cur;
146 const ios_base::seekdir ios_base::end;
148 const int ios_base::_S_local_word_size;
149 int ios_base::Init::_S_ios_base_init = 0;
150 bool ios_base::Init::_S_synced_with_stdio = true;
152 ios_base::Init::Init()
154 if (_S_ios_base_init == 0)
156 // Standard streams default to synced with "C" operations.
157 _S_synced_with_stdio = true;
159 new (&buf_cout_sync) stdio_sync_filebuf<char>(stdout);
160 new (&buf_cin_sync) stdio_sync_filebuf<char>(stdin);
161 new (&buf_cerr_sync) stdio_sync_filebuf<char>(stderr);
163 // The standard streams are constructed once only and never
165 new (&cout) ostream(&buf_cout_sync);
166 new (&cin) istream(&buf_cin_sync);
167 new (&cerr) ostream(&buf_cerr_sync);
168 new (&clog) ostream(&buf_cerr_sync);
170 cerr.flags(ios_base::unitbuf);
172 #ifdef _GLIBCXX_USE_WCHAR_T
173 new (&buf_wcout_sync) stdio_sync_filebuf<wchar_t>(stdout);
174 new (&buf_wcin_sync) stdio_sync_filebuf<wchar_t>(stdin);
175 new (&buf_wcerr_sync) stdio_sync_filebuf<wchar_t>(stderr);
177 new (&wcout) wostream(&buf_wcout_sync);
178 new (&wcin) wistream(&buf_wcin_sync);
179 new (&wcerr) wostream(&buf_wcerr_sync);
180 new (&wclog) wostream(&buf_wcerr_sync);
182 wcerr.flags(ios_base::unitbuf);
185 _S_ios_base_init = 1;
190 ios_base::Init::~Init()
192 if (--_S_ios_base_init == 1)
194 // Catch any exceptions thrown by basic_ostream::flush()
197 // Flush standard output streams as required by 27.4.2.1.6
202 #ifdef _GLIBCXX_USE_WCHAR_T
213 // 27.4.2.5 ios_base storage functions
215 ios_base::xalloc() throw()
217 // Implementation note: Initialize top to zero to ensure that
218 // initialization occurs before main() is started.
219 static _Atomic_word _S_top = 0;
220 return __exchange_and_add(&_S_top, 1) + 4;
223 // 27.4.2.5 iword/pword storage
225 ios_base::_M_grow_words(int ix)
227 // Precondition: _M_word_size <= ix
228 int newsize = _S_local_word_size;
229 _Words* words = _M_local_word;
230 if (ix > _S_local_word_size - 1)
232 if (ix < numeric_limits<int>::max())
236 { words = new _Words[newsize]; }
239 _M_streambuf_state |= badbit;
240 if (_M_streambuf_state & _M_exception)
241 __throw_ios_failure("ios_base::_M_grow_words "
242 "allocation failed");
245 for (int i = 0; i < _M_word_size; i++)
246 words[i] = _M_word[i];
247 if (_M_word && _M_word != _M_local_word)
255 _M_streambuf_state |= badbit;
256 if (_M_streambuf_state & _M_exception)
257 __throw_ios_failure("ios_base::_M_grow_words is not valid");
262 _M_word_size = newsize;
266 // Called only by basic_ios<>::init.
270 // NB: May be called more than once
273 _M_flags = skipws | dec;
274 _M_ios_locale = locale();
277 // 27.4.2.3 ios_base locale functions
279 ios_base::imbue(const locale& __loc)
281 locale __old = _M_ios_locale;
282 _M_ios_locale = __loc;
283 _M_call_callbacks(imbue_event);
288 : _M_callbacks(0), _M_word_size(_S_local_word_size), _M_word(_M_local_word)
290 // Do nothing: basic_ios::init() does it.
291 // NB: _M_callbacks and _M_word must be zero for non-initialized
292 // ios_base to go through ~ios_base gracefully.
295 // 27.4.2.7 ios_base constructors/destructors
296 ios_base::~ios_base()
298 _M_call_callbacks(erase_event);
299 _M_dispose_callbacks();
300 if (_M_word != _M_local_word)
308 ios_base::register_callback(event_callback __fn, int __index)
309 { _M_callbacks = new _Callback_list(__fn, __index, _M_callbacks); }
312 ios_base::_M_call_callbacks(event __e) throw()
314 _Callback_list* __p = _M_callbacks;
318 { (*__p->_M_fn) (__e, *this, __p->_M_index); }
326 ios_base::_M_dispose_callbacks(void)
328 _Callback_list* __p = _M_callbacks;
329 while (__p && __p->_M_remove_reference() == 0)
331 _Callback_list* __next = __p->_M_next;
339 ios_base::sync_with_stdio(bool __sync)
341 #ifdef _GLIBCXX_RESOLVE_LIB_DEFECTS
342 // 49. Underspecification of ios_base::sync_with_stdio
343 bool __ret = ios_base::Init::_S_synced_with_stdio;
346 // Turn off sync with C FILE* for cin, cout, cerr, clog iff
347 // currently synchronized.
348 if (!__sync && __ret)
350 ios_base::Init::_S_synced_with_stdio = __sync;
352 // Explicitly call dtors to free any memory that is
353 // dynamically allocated by filebuf ctor or member functions,
354 // but don't deallocate all memory by calling operator delete.
355 buf_cout_sync.~stdio_sync_filebuf<char>();
356 buf_cin_sync.~stdio_sync_filebuf<char>();
357 buf_cerr_sync.~stdio_sync_filebuf<char>();
359 #ifdef _GLIBCXX_USE_WCHAR_T
360 buf_wcout_sync.~stdio_sync_filebuf<wchar_t>();
361 buf_wcin_sync.~stdio_sync_filebuf<wchar_t>();
362 buf_wcerr_sync.~stdio_sync_filebuf<wchar_t>();
365 // Create stream buffers for the standard streams and use
366 // those buffers without destroying and recreating the
368 new (&buf_cout) stdio_filebuf<char>(stdout, ios_base::out);
369 new (&buf_cin) stdio_filebuf<char>(stdin, ios_base::in);
370 new (&buf_cerr) stdio_filebuf<char>(stderr, ios_base::out);
371 cout.rdbuf(&buf_cout);
373 cerr.rdbuf(&buf_cerr);
374 clog.rdbuf(&buf_cerr);
376 #ifdef _GLIBCXX_USE_WCHAR_T
377 new (&buf_wcout) stdio_filebuf<wchar_t>(stdout, ios_base::out);
378 new (&buf_wcin) stdio_filebuf<wchar_t>(stdin, ios_base::in);
379 new (&buf_wcerr) stdio_filebuf<wchar_t>(stderr, ios_base::out);
380 wcout.rdbuf(&buf_wcout);
381 wcin.rdbuf(&buf_wcin);
382 wcerr.rdbuf(&buf_wcerr);
383 wclog.rdbuf(&buf_wcerr);