1 // Iostreams base classes -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
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)
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.
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,
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.
31 // ISO C++ 14882: 27.4 Iostreams base classes
34 #include <bits/std_ios.h>
35 #include <bits/std_ostream.h>
36 #include <bits/std_istream.h>
37 #include <bits/std_fstream.h>
41 // Extern declarations for global objects in src/globals.cc.
46 extern filebuf buf_cout
;
47 extern filebuf buf_cin
;
48 extern filebuf buf_cerr
;
50 #ifdef _GLIBCPP_USE_WCHAR_T
52 extern wostream wcout
;
53 extern wostream wcerr
;
54 extern wostream wclog
;
55 extern wfilebuf buf_wcout
;
56 extern wfilebuf buf_wcin
;
57 extern wfilebuf buf_wcerr
;
60 // Definitions for static const data members of __ios_flags.
61 const __ios_flags::__int_type
__ios_flags::_S_boolalpha
;
62 const __ios_flags::__int_type
__ios_flags::_S_dec
;
63 const __ios_flags::__int_type
__ios_flags::_S_fixed
;
64 const __ios_flags::__int_type
__ios_flags::_S_hex
;
65 const __ios_flags::__int_type
__ios_flags::_S_internal
;
66 const __ios_flags::__int_type
__ios_flags::_S_left
;
67 const __ios_flags::__int_type
__ios_flags::_S_oct
;
68 const __ios_flags::__int_type
__ios_flags::_S_right
;
69 const __ios_flags::__int_type
__ios_flags::_S_scientific
;
70 const __ios_flags::__int_type
__ios_flags::_S_showbase
;
71 const __ios_flags::__int_type
__ios_flags::_S_showpoint
;
72 const __ios_flags::__int_type
__ios_flags::_S_showpos
;
73 const __ios_flags::__int_type
__ios_flags::_S_skipws
;
74 const __ios_flags::__int_type
__ios_flags::_S_unitbuf
;
75 const __ios_flags::__int_type
__ios_flags::_S_uppercase
;
76 const __ios_flags::__int_type
__ios_flags::_S_adjustfield
;
77 const __ios_flags::__int_type
__ios_flags::_S_basefield
;
78 const __ios_flags::__int_type
__ios_flags::_S_floatfield
;
80 const __ios_flags::__int_type
__ios_flags::_S_badbit
;
81 const __ios_flags::__int_type
__ios_flags::_S_eofbit
;
82 const __ios_flags::__int_type
__ios_flags::_S_failbit
;
84 const __ios_flags::__int_type
__ios_flags::_S_app
;
85 const __ios_flags::__int_type
__ios_flags::_S_ate
;
86 const __ios_flags::__int_type
__ios_flags::_S_bin
;
87 const __ios_flags::__int_type
__ios_flags::_S_in
;
88 const __ios_flags::__int_type
__ios_flags::_S_out
;
89 const __ios_flags::__int_type
__ios_flags::_S_trunc
;
91 // Definitions for static const members of ios_base.
92 const ios_base::fmtflags
ios_base::boolalpha
;
93 const ios_base::fmtflags
ios_base::dec
;
94 const ios_base::fmtflags
ios_base::fixed
;
95 const ios_base::fmtflags
ios_base::hex
;
96 const ios_base::fmtflags
ios_base::internal
;
97 const ios_base::fmtflags
ios_base::left
;
98 const ios_base::fmtflags
ios_base::oct
;
99 const ios_base::fmtflags
ios_base::right
;
100 const ios_base::fmtflags
ios_base::scientific
;
101 const ios_base::fmtflags
ios_base::showbase
;
102 const ios_base::fmtflags
ios_base::showpoint
;
103 const ios_base::fmtflags
ios_base::showpos
;
104 const ios_base::fmtflags
ios_base::skipws
;
105 const ios_base::fmtflags
ios_base::unitbuf
;
106 const ios_base::fmtflags
ios_base::uppercase
;
107 const ios_base::fmtflags
ios_base::adjustfield
;
108 const ios_base::fmtflags
ios_base::basefield
;
109 const ios_base::fmtflags
ios_base::floatfield
;
111 const ios_base::iostate
ios_base::badbit
;
112 const ios_base::iostate
ios_base::eofbit
;
113 const ios_base::iostate
ios_base::failbit
;
114 const ios_base::iostate
ios_base::goodbit
;
116 const ios_base::openmode
ios_base::app
;
117 const ios_base::openmode
ios_base::ate
;
118 const ios_base::openmode
ios_base::binary
;
119 const ios_base::openmode
ios_base::in
;
120 const ios_base::openmode
ios_base::out
;
121 const ios_base::openmode
ios_base::trunc
;
123 const ios_base::seekdir
ios_base::beg
;
124 const ios_base::seekdir
ios_base::cur
;
125 const ios_base::seekdir
ios_base::end
;
127 const int ios_base::_S_local_words
;
128 int ios_base::Init::_S_ios_base_init
= 0;
129 bool ios_base::Init::_S_synced_with_stdio
= true;
131 ios_base::failure::failure(const string
& __str
) throw()
133 strncpy(_M_name
, __str
.c_str(), _M_bufsize
);
134 _M_name
[_M_bufsize
- 1] = '\0';
137 ios_base::failure::~failure() throw()
141 ios_base::failure::what() const throw()
145 ios_base::Init::_S_ios_create(bool __sync
)
147 int __bufsize
= __sync
? 0 : static_cast<int>(BUFSIZ
);
149 // NB: The file globals.cc creates the four standard files
150 // with NULL buffers. At this point, we swap out the dummy NULL
151 // [io]stream objects and buffers with the real deal.
152 new (&buf_cout
) filebuf(stdout
, ios_base::out
, __bufsize
);
153 new (&buf_cin
) filebuf(stdin
, ios_base::in
, 1);
154 new (&buf_cerr
) filebuf(stderr
, ios_base::out
, __bufsize
);
155 new (&cout
) ostream(&buf_cout
);
156 new (&cin
) istream(&buf_cin
);
157 new (&cerr
) ostream(&buf_cerr
);
158 new (&clog
) ostream(&buf_cerr
);
160 cerr
.flags(ios_base::unitbuf
);
162 #ifdef _GLIBCPP_USE_WCHAR_T
163 new (&buf_wcout
) wfilebuf(stdout
, ios_base::out
, __bufsize
);
164 new (&buf_wcin
) wfilebuf(stdin
, ios_base::in
, 1);
165 new (&buf_wcerr
) wfilebuf(stderr
, ios_base::out
, __bufsize
);
166 new (&wcout
) wostream(&buf_wcout
);
167 new (&wcin
) wistream(&buf_wcin
);
168 new (&wcerr
) wostream(&buf_wcerr
);
169 new (&wclog
) wostream(&buf_wcerr
);
171 wcerr
.flags(ios_base::unitbuf
);
176 ios_base::Init::_S_ios_destroy()
178 // Explicitly call dtors to free any memory that is dynamically
179 // allocated by filebuf ctor or member functions, but don't
180 // deallocate all memory by calling operator delete.
187 #ifdef _GLIBCPP_USE_WCHAR_T
191 buf_wcout
.~wfilebuf();
192 buf_wcin
.~wfilebuf();
193 buf_wcerr
.~wfilebuf();
197 ios_base::Init::Init()
199 if (++_S_ios_base_init
== 1)
201 // Standard streams default to synced with "C" operations.
202 ios_base::Init::_S_synced_with_stdio
= true;
203 _S_ios_create(ios_base::Init::_S_synced_with_stdio
);
207 ios_base::Init::~Init()
209 if (--_S_ios_base_init
== 0)
213 // 27.4.2.5 ios_base storage functions
215 ios_base::xalloc() throw()
218 // XXX should be a symbol. (Reserve 0..3 for builtins.)
223 // 27.4.2.5 iword/pword storage
225 ios_base::_M_grow_words(int ix
)
227 // Precondition: _M_word_limit <= ix
228 _Words zero
= { 0, 0 };
229 int newlimit
= _S_local_words
;
230 _Words
* words
= _M_word_array
;
232 if (_S_local_words
<= ix
)
236 { words
= new _Words
[ix
+1]; }
239 _M_dummy
= zero
; // XXX MT? Not on "normal" machines.
240 // XXX now in basic_ios
241 // _M_clear(_M_rdstate() | badbit); // may throw
244 for (; i
< _M_word_limit
; i
++)
245 words
[i
] = _M_words
[i
];
246 if (_M_words
!= _M_word_array
)
250 do { words
[i
] = zero
; } while (++i
< newlimit
);
252 _M_word_limit
= newlimit
;
256 // Called only by basic_ios<>::init.
260 // NB: May be called more than once
263 _M_flags
= skipws
| dec
;
267 _M_ios_locale
= locale();
268 // No init needed for _M_word_array or _M_dummy.
271 // 27.4.2.3 ios_base locale functions
273 ios_base::imbue(const locale
& __loc
)
275 locale __old
= _M_ios_locale
;
276 _M_ios_locale
= __loc
;
277 // Make sure there's a callback for the format caches so they will be
279 _Format_cache
<char>::_S_get(*this);
280 #ifdef _GLIBCPP_USE_WCHAR_T
281 _Format_cache
<wchar_t>::_S_get(*this);
283 _M_call_callbacks(imbue_event
);
289 // Do nothing; init() does it. Static init to 0 makes everything sane.
292 // 27.4.2.7 ios_base constructors/destructors
293 ios_base::~ios_base()
295 _M_call_callbacks(erase_event
);
296 _M_dispose_callbacks();
297 if (_M_words
!= _M_word_array
)
303 ios_base::register_callback(event_callback __fn
, int __index
)
304 { _M_callbacks
= new _Callback_list(__fn
, __index
, _M_callbacks
); }
307 ios_base::_M_call_callbacks(event __e
) throw()
309 for (_Callback_list
* __p
= _M_callbacks
; __p
; __p
= __p
->_M_next
)
312 (*__p
->_M_fn
) (__e
, *this, __p
->_M_index
);
320 ios_base::_M_dispose_callbacks(void)
322 _Callback_list
* __p
= _M_callbacks
;
323 while (__p
&& __p
->_M_remove_reference() == 0)
325 _Callback_list
* __next
= __p
->_M_next
;
333 ios_base::sync_with_stdio(bool __sync
)
335 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
336 // 49. Underspecification of ios_base::sync_with_stdio
337 bool __ret
= ios_base::Init::_S_synced_with_stdio
;
340 // Turn off sync with C FILE* for cin, cout, cerr, clog iff
341 // currently synchronized.
342 if (!__sync
&& __ret
)
344 ios_base::Init::_S_synced_with_stdio
= false;
345 ios_base::Init::_S_ios_destroy();
346 ios_base::Init::_S_ios_create(ios_base::Init::_S_synced_with_stdio
);