ios.cc

00001 // Iostreams base classes -*- C++ -*- 00002 00003 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002 00004 // Free Software Foundation, Inc. 00005 // 00006 // This file is part of the GNU ISO C++ Library. This library is free 00007 // software; you can redistribute it and/or modify it under the 00008 // terms of the GNU General Public License as published by the 00009 // Free Software Foundation; either version 2, or (at your option) 00010 // any later version. 00011 00012 // This library is distributed in the hope that it will be useful, 00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 // GNU General Public License for more details. 00016 00017 // You should have received a copy of the GNU General Public License along 00018 // with this library; see the file COPYING. If not, write to the Free 00019 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, 00020 // USA. 00021 00022 // As a special exception, you may use this file as part of a free software 00023 // library without restriction. Specifically, if other files instantiate 00024 // templates or use macros or inline functions from this file, or you compile 00025 // this file and link it with other files to produce an executable, this 00026 // file does not by itself cause the resulting executable to be covered by 00027 // the GNU General Public License. This exception does not however 00028 // invalidate any other reasons why the executable file might be covered by 00029 // the GNU General Public License. 00030 00031 // 00032 // ISO C++ 14882: 27.4 Iostreams base classes 00033 // 00034 00035 #include <ios> 00036 #include <ostream> 00037 #include <istream> 00038 #include <fstream> 00039 #include <bits/atomicity.h> 00040 #include <ext/stdio_filebuf.h> 00041 #ifdef _GLIBCPP_HAVE_UNISTD_H 00042 #include <unistd.h> 00043 #endif 00044 00045 namespace __gnu_cxx 00046 { 00047 // Extern declarations for global objects in src/globals.cc. 00048 extern stdio_filebuf<char> buf_cout; 00049 extern stdio_filebuf<char> buf_cin; 00050 extern stdio_filebuf<char> buf_cerr; 00051 00052 #ifdef _GLIBCPP_USE_WCHAR_T 00053 extern stdio_filebuf<wchar_t> buf_wcout; 00054 extern stdio_filebuf<wchar_t> buf_wcin; 00055 extern stdio_filebuf<wchar_t> buf_wcerr; 00056 #endif 00057 } // namespace __gnu_cxx 00058 00059 namespace std 00060 { 00061 using namespace __gnu_cxx; 00062 00063 extern istream cin; 00064 extern ostream cout; 00065 extern ostream cerr; 00066 extern ostream clog; 00067 00068 #ifdef _GLIBCPP_USE_WCHAR_T 00069 extern wistream wcin; 00070 extern wostream wcout; 00071 extern wostream wcerr; 00072 extern wostream wclog; 00073 #endif 00074 00075 // Definitions for static const data members of __ios_flags. 00076 const __ios_flags::__int_type __ios_flags::_S_boolalpha; 00077 const __ios_flags::__int_type __ios_flags::_S_dec; 00078 const __ios_flags::__int_type __ios_flags::_S_fixed; 00079 const __ios_flags::__int_type __ios_flags::_S_hex; 00080 const __ios_flags::__int_type __ios_flags::_S_internal; 00081 const __ios_flags::__int_type __ios_flags::_S_left; 00082 const __ios_flags::__int_type __ios_flags::_S_oct; 00083 const __ios_flags::__int_type __ios_flags::_S_right; 00084 const __ios_flags::__int_type __ios_flags::_S_scientific; 00085 const __ios_flags::__int_type __ios_flags::_S_showbase; 00086 const __ios_flags::__int_type __ios_flags::_S_showpoint; 00087 const __ios_flags::__int_type __ios_flags::_S_showpos; 00088 const __ios_flags::__int_type __ios_flags::_S_skipws; 00089 const __ios_flags::__int_type __ios_flags::_S_unitbuf; 00090 const __ios_flags::__int_type __ios_flags::_S_uppercase; 00091 const __ios_flags::__int_type __ios_flags::_S_adjustfield; 00092 const __ios_flags::__int_type __ios_flags::_S_basefield; 00093 const __ios_flags::__int_type __ios_flags::_S_floatfield; 00094 00095 const __ios_flags::__int_type __ios_flags::_S_badbit; 00096 const __ios_flags::__int_type __ios_flags::_S_eofbit; 00097 const __ios_flags::__int_type __ios_flags::_S_failbit; 00098 00099 const __ios_flags::__int_type __ios_flags::_S_app; 00100 const __ios_flags::__int_type __ios_flags::_S_ate; 00101 const __ios_flags::__int_type __ios_flags::_S_bin; 00102 const __ios_flags::__int_type __ios_flags::_S_in; 00103 const __ios_flags::__int_type __ios_flags::_S_out; 00104 const __ios_flags::__int_type __ios_flags::_S_trunc; 00105 00106 // Definitions for static const members of ios_base. 00107 const ios_base::fmtflags ios_base::boolalpha; 00108 const ios_base::fmtflags ios_base::dec; 00109 const ios_base::fmtflags ios_base::fixed; 00110 const ios_base::fmtflags ios_base::hex; 00111 const ios_base::fmtflags ios_base::internal; 00112 const ios_base::fmtflags ios_base::left; 00113 const ios_base::fmtflags ios_base::oct; 00114 const ios_base::fmtflags ios_base::right; 00115 const ios_base::fmtflags ios_base::scientific; 00116 const ios_base::fmtflags ios_base::showbase; 00117 const ios_base::fmtflags ios_base::showpoint; 00118 const ios_base::fmtflags ios_base::showpos; 00119 const ios_base::fmtflags ios_base::skipws; 00120 const ios_base::fmtflags ios_base::unitbuf; 00121 const ios_base::fmtflags ios_base::uppercase; 00122 const ios_base::fmtflags ios_base::adjustfield; 00123 const ios_base::fmtflags ios_base::basefield; 00124 const ios_base::fmtflags ios_base::floatfield; 00125 00126 const ios_base::iostate ios_base::badbit; 00127 const ios_base::iostate ios_base::eofbit; 00128 const ios_base::iostate ios_base::failbit; 00129 const ios_base::iostate ios_base::goodbit; 00130 00131 const ios_base::openmode ios_base::app; 00132 const ios_base::openmode ios_base::ate; 00133 const ios_base::openmode ios_base::binary; 00134 const ios_base::openmode ios_base::in; 00135 const ios_base::openmode ios_base::out; 00136 const ios_base::openmode ios_base::trunc; 00137 00138 const ios_base::seekdir ios_base::beg; 00139 const ios_base::seekdir ios_base::cur; 00140 const ios_base::seekdir ios_base::end; 00141 00142 const int ios_base::_S_local_word_size; 00143 int ios_base::Init::_S_ios_base_init = 0; 00144 bool ios_base::Init::_S_synced_with_stdio = true; 00145 00146 ios_base::failure::failure(const string& __str) throw() 00147 { 00148 strncpy(_M_name, __str.c_str(), _M_bufsize); 00149 _M_name[_M_bufsize - 1] = '\0'; 00150 } 00151 00152 ios_base::failure::~failure() throw() 00153 { } 00154 00155 const char* 00156 ios_base::failure::what() const throw() 00157 { return _M_name; } 00158 00159 void 00160 ios_base::Init::_S_ios_create(bool __sync) 00161 { 00162 int __out_size = __sync ? 0 : static_cast<int>(BUFSIZ); 00163 #ifdef _GLIBCPP_HAVE_ISATTY 00164 int __in_size = (__sync || isatty (0)) ? 1 : static_cast<int>(BUFSIZ); 00165 #else 00166 int __in_size = 1; 00167 #endif 00168 00169 // NB: The file globals.cc creates the four standard files 00170 // with NULL buffers. At this point, we swap out the dummy NULL 00171 // [io]stream objects and buffers with the real deal. 00172 new (&buf_cout) stdio_filebuf<char>(stdout, ios_base::out, __out_size); 00173 new (&buf_cin) stdio_filebuf<char>(stdin, ios_base::in, __in_size); 00174 new (&buf_cerr) stdio_filebuf<char>(stderr, ios_base::out, __out_size); 00175 new (&cout) ostream(&buf_cout); 00176 new (&cin) istream(&buf_cin); 00177 new (&cerr) ostream(&buf_cerr); 00178 new (&clog) ostream(&buf_cerr); 00179 cin.tie(&cout); 00180 cerr.flags(ios_base::unitbuf); 00181 00182 #ifdef _GLIBCPP_USE_WCHAR_T 00183 new (&buf_wcout) stdio_filebuf<wchar_t>(stdout, ios_base::out, __out_size); 00184 new (&buf_wcin) stdio_filebuf<wchar_t>(stdin, ios_base::in, __in_size); 00185 new (&buf_wcerr) stdio_filebuf<wchar_t>(stderr, ios_base::out, __out_size); 00186 new (&wcout) wostream(&buf_wcout); 00187 new (&wcin) wistream(&buf_wcin); 00188 new (&wcerr) wostream(&buf_wcerr); 00189 new (&wclog) wostream(&buf_wcerr); 00190 wcin.tie(&wcout); 00191 wcerr.flags(ios_base::unitbuf); 00192 #endif 00193 } 00194 00195 void 00196 ios_base::Init::_S_ios_destroy() 00197 { 00198 // Explicitly call dtors to free any memory that is dynamically 00199 // allocated by filebuf ctor or member functions, but don't 00200 // deallocate all memory by calling operator delete. 00201 buf_cout.~stdio_filebuf(); 00202 buf_cin.~stdio_filebuf(); 00203 buf_cerr.~stdio_filebuf(); 00204 00205 #ifdef _GLIBCPP_USE_WCHAR_T 00206 buf_wcout.~stdio_filebuf(); 00207 buf_wcin.~stdio_filebuf(); 00208 buf_wcerr.~stdio_filebuf(); 00209 #endif 00210 } 00211 00212 ios_base::Init::Init() 00213 { 00214 if (_S_ios_base_init == 0) 00215 { 00216 // Standard streams default to synced with "C" operations. 00217 ios_base::Init::_S_synced_with_stdio = true; 00218 _S_ios_create(ios_base::Init::_S_synced_with_stdio); 00219 } 00220 ++_S_ios_base_init; 00221 } 00222 00223 ios_base::Init::~Init() 00224 { 00225 if (--_S_ios_base_init == 0) 00226 _S_ios_destroy(); 00227 } 00228 00229 // 27.4.2.5 ios_base storage functions 00230 int 00231 ios_base::xalloc() throw() 00232 { 00233 // Implementation note: Initialize top to zero to ensure that 00234 // initialization occurs before main() is started. 00235 static _Atomic_word _S_top = 0; 00236 return __exchange_and_add(&_S_top, 1) + 4; 00237 } 00238 00239 // 27.4.2.5 iword/pword storage 00240 ios_base::_Words& 00241 ios_base::_M_grow_words(int ix) 00242 { 00243 // Precondition: _M_word_size <= ix 00244 int newsize = _S_local_word_size; 00245 _Words* words = _M_local_word; 00246 if (ix > _S_local_word_size - 1) 00247 { 00248 if (ix < numeric_limits<int>::max()) 00249 { 00250 newsize = ix + 1; 00251 try 00252 { words = new _Words[newsize]; } 00253 catch (...) 00254 { 00255 delete [] _M_word; 00256 _M_word = 0; 00257 _M_streambuf_state |= badbit; 00258 if (_M_streambuf_state & _M_exception) 00259 __throw_ios_failure("ios_base::_M_grow_words failure"); 00260 return _M_word_zero; 00261 } 00262 for (int i = 0; i < _M_word_size; i++) 00263 words[i] = _M_word[i]; 00264 if (_M_word && _M_word != _M_local_word) 00265 { 00266 delete [] _M_word; 00267 _M_word = 0; 00268 } 00269 } 00270 else 00271 { 00272 _M_streambuf_state |= badbit; 00273 return _M_word_zero; 00274 } 00275 } 00276 _M_word = words; 00277 _M_word_size = newsize; 00278 return _M_word[ix]; 00279 } 00280 00281 // Called only by basic_ios<>::init. 00282 void 00283 ios_base::_M_init() 00284 { 00285 // NB: May be called more than once 00286 _M_precision = 6; 00287 _M_width = 0; 00288 _M_flags = skipws | dec; 00289 _M_callbacks = 0; 00290 _M_word_size = 0; 00291 _M_ios_locale = locale(); 00292 } 00293 00294 // 27.4.2.3 ios_base locale functions 00295 locale 00296 ios_base::imbue(const locale& __loc) 00297 { 00298 locale __old = _M_ios_locale; 00299 _M_ios_locale = __loc; 00300 _M_call_callbacks(imbue_event); 00301 return __old; 00302 } 00303 00304 ios_base::ios_base() : _M_callbacks(0), _M_word(0) 00305 { 00306 // Do nothing: basic_ios::init() does it. 00307 // NB: _M_callbacks and _M_word must be zero for non-initialized 00308 // ios_base to go through ~ios_base gracefully. 00309 } 00310 00311 // 27.4.2.7 ios_base constructors/destructors 00312 ios_base::~ios_base() 00313 { 00314 _M_call_callbacks(erase_event); 00315 _M_dispose_callbacks(); 00316 if (_M_word && _M_word != _M_local_word) 00317 { 00318 delete [] _M_word; 00319 _M_word = 0; 00320 } 00321 } 00322 00323 void 00324 ios_base::register_callback(event_callback __fn, int __index) 00325 { _M_callbacks = new _Callback_list(__fn, __index, _M_callbacks); } 00326 00327 void 00328 ios_base::_M_call_callbacks(event __e) throw() 00329 { 00330 _Callback_list* __p = _M_callbacks; 00331 while (__p) 00332 { 00333 try 00334 { (*__p->_M_fn) (__e, *this, __p->_M_index); } 00335 catch (...) 00336 { } 00337 __p = __p->_M_next; 00338 } 00339 } 00340 00341 void 00342 ios_base::_M_dispose_callbacks(void) 00343 { 00344 _Callback_list* __p = _M_callbacks; 00345 while (__p && __p->_M_remove_reference() == 0) 00346 { 00347 _Callback_list* __next = __p->_M_next; 00348 delete __p; 00349 __p = __next; 00350 } 00351 _M_callbacks = 0; 00352 } 00353 00354 bool 00355 ios_base::sync_with_stdio(bool __sync) 00356 { 00357 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS 00358 // 49. Underspecification of ios_base::sync_with_stdio 00359 bool __ret = ios_base::Init::_S_synced_with_stdio; 00360 #endif 00361 00362 // Turn off sync with C FILE* for cin, cout, cerr, clog iff 00363 // currently synchronized. 00364 if (!__sync && __ret) 00365 { 00366 ios_base::Init::_S_synced_with_stdio = false; 00367 ios_base::Init::_S_ios_destroy(); 00368 ios_base::Init::_S_ios_create(ios_base::Init::_S_synced_with_stdio); 00369 } 00370 return __ret; 00371 } 00372 } // namespace std

Generated on Wed Sep 29 13:54:49 2004 for libstdc++-v3 Source by doxygen 1.3.7