1 // Iostreams base classes -*- C++ -*-
3 // Copyright (C) 1997-1999 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_iostream.h>
36 #include <bits/std_fstream.h>
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;
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;
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;
72 const ios_base::seekdir ios_base::beg;
73 const ios_base::seekdir ios_base::cur;
74 const ios_base::seekdir ios_base::end;
76 ios_base::failure::failure(const string& __str)
78 strncpy(_M_name, __str.c_str(), _M_bufsize);
79 _M_name[_M_bufsize - 1] = '\0';
82 int ios_base::Init::_M_ios_base_init = 0;
84 ios_base::Init::Init()
86 if (++_M_ios_base_init == 1)
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.
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);
98 __old = cin.rdbuf(_M_cin);
101 __old = cerr.rdbuf(_M_cerr);
103 cerr.flags(ios_base::unitbuf);
104 __old = clog.rdbuf(_M_cerr);
106 #ifdef _GLIBCPP_USE_WCHAR_T
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();
116 __wold = wcerr.rdbuf(_M_wcerr);
117 __wold->~wstreambuf();
118 wcerr.flags(ios_base::unitbuf);
119 __wold = wclog.rdbuf(_M_wcerr);
120 __wold->~wstreambuf();
125 ios_base::Init::~Init()
127 if (--_M_ios_base_init == 0)
138 #ifdef _GLIBCPP_USE_WCHAR_T
152 // 27.4.2.5 ios_base storage functions
154 ios_base::xalloc() throw()
157 // XXX should be a symbol. (Reserve 0..3 for builtins.)
162 // 27.4.2.5 iword/pword storage
164 ios_base::_M_grow_words(int ix)
166 // Precondition: _M_word_limit <= ix
167 _Words zero = { 0, 0 };
168 int newlimit = _S_local_words;
169 _Words* words = _M_word_array;
171 if (_S_local_words <= ix)
175 { words = new _Words[ix+1]; }
178 _M_dummy = zero; // XXX MT? Not on "normal" machines.
179 // XXX now in basic_ios
180 // _M_clear(_M_rdstate() | badbit); // may throw
183 do { words[i] = _M_words[i]; } while (++i < _M_word_limit);
184 if (_M_words != _M_word_array) delete [] _M_words;
187 do { words[i] = zero; } while (++i < newlimit);
189 _M_word_limit = newlimit;
193 // Called only by basic_ios<>::init.
197 // NB: May be called more than once
198 _M_flags = skipws | dec;
204 _M_locale_ios = locale();
205 // No init needed for _M_word_array or _M_dummy.
208 // 27.4.2.3 ios_base locale functions
210 ios_base::imbue(const locale& __loc)
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
216 _Format_cache<char>::_S_get(*this);
217 #ifdef _GLIBCPP_USE_WCHAR_T
218 _Format_cache<wchar_t>::_S_get(*this);
220 _M_call_callbacks(imbue_event);
226 // Do nothing; init() does it. Static init to 0 makes everything sane.
229 // 27.4.2.7 ios_base constructors/destructors
230 ios_base::~ios_base()
232 _M_call_callbacks(erase_event);
233 _M_dispose_callbacks();
234 if (_M_words != _M_word_array)
240 ios_base::register_callback(event_callback __fn, int __index)
241 { _M_callbacks = new _Callback_list(__fn, __index, _M_callbacks); }
244 ios_base::_M_call_callbacks(event __e) throw()
246 for (_Callback_list* __p = _M_callbacks; __p; __p = __p->_M_next)
249 (*__p->_M_fn) (__e, *this, __p->_M_index);
257 ios_base::_M_dispose_callbacks(void)
259 _Callback_list* __p = _M_callbacks;
260 while (__p && __p->_M_remove_reference() == 0)
262 _Callback_list* __next = __p->_M_next;
270 ios_base::sync_with_stdio(bool __sync)
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;
276 // Turn off sync with C FILE* for cin, cout, cerr, clog.
277 if (!__sync && __retval)
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);