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 
00042 namespace std 
00043 {
00044   // Extern declarations for global objects in src/globals.cc.
00045   extern istream cin;
00046   extern ostream cout;
00047   extern ostream cerr;
00048   extern ostream clog;
00049 
00050   using __gnu_cxx::stdio_filebuf;
00051   extern stdio_filebuf<char> buf_cout;
00052   extern stdio_filebuf<char> buf_cin;
00053   extern stdio_filebuf<char> buf_cerr;
00054 
00055 #ifdef _GLIBCPP_USE_WCHAR_T
00056   extern wistream wcin;
00057   extern wostream wcout;
00058   extern wostream wcerr;
00059   extern wostream wclog;
00060 
00061   extern stdio_filebuf<wchar_t> buf_wcout;
00062   extern stdio_filebuf<wchar_t> buf_wcin;
00063   extern stdio_filebuf<wchar_t> buf_wcerr;
00064 #endif
00065 
00066   // Definitions for static const data members of __ios_flags.
00067   const __ios_flags::__int_type __ios_flags::_S_boolalpha;
00068   const __ios_flags::__int_type __ios_flags::_S_dec;
00069   const __ios_flags::__int_type __ios_flags::_S_fixed;
00070   const __ios_flags::__int_type __ios_flags::_S_hex;
00071   const __ios_flags::__int_type __ios_flags::_S_internal;
00072   const __ios_flags::__int_type __ios_flags::_S_left;
00073   const __ios_flags::__int_type __ios_flags::_S_oct;
00074   const __ios_flags::__int_type __ios_flags::_S_right;
00075   const __ios_flags::__int_type __ios_flags::_S_scientific;
00076   const __ios_flags::__int_type __ios_flags::_S_showbase;
00077   const __ios_flags::__int_type __ios_flags::_S_showpoint;
00078   const __ios_flags::__int_type __ios_flags::_S_showpos;
00079   const __ios_flags::__int_type __ios_flags::_S_skipws;
00080   const __ios_flags::__int_type __ios_flags::_S_unitbuf;
00081   const __ios_flags::__int_type __ios_flags::_S_uppercase;
00082   const __ios_flags::__int_type __ios_flags::_S_adjustfield;
00083   const __ios_flags::__int_type __ios_flags::_S_basefield;
00084   const __ios_flags::__int_type __ios_flags::_S_floatfield;
00085 
00086   const __ios_flags::__int_type __ios_flags::_S_badbit;
00087   const __ios_flags::__int_type __ios_flags::_S_eofbit;
00088   const __ios_flags::__int_type __ios_flags::_S_failbit;
00089 
00090   const __ios_flags::__int_type __ios_flags::_S_app;
00091   const __ios_flags::__int_type __ios_flags::_S_ate;
00092   const __ios_flags::__int_type __ios_flags::_S_bin;
00093   const __ios_flags::__int_type __ios_flags::_S_in;
00094   const __ios_flags::__int_type __ios_flags::_S_out;
00095   const __ios_flags::__int_type __ios_flags::_S_trunc;
00096 
00097   // Definitions for static const members of ios_base.
00098   const ios_base::fmtflags ios_base::boolalpha;
00099   const ios_base::fmtflags ios_base::dec;
00100   const ios_base::fmtflags ios_base::fixed;
00101   const ios_base::fmtflags ios_base::hex;
00102   const ios_base::fmtflags ios_base::internal;
00103   const ios_base::fmtflags ios_base::left;
00104   const ios_base::fmtflags ios_base::oct;
00105   const ios_base::fmtflags ios_base::right;
00106   const ios_base::fmtflags ios_base::scientific;
00107   const ios_base::fmtflags ios_base::showbase;
00108   const ios_base::fmtflags ios_base::showpoint;
00109   const ios_base::fmtflags ios_base::showpos;
00110   const ios_base::fmtflags ios_base::skipws;
00111   const ios_base::fmtflags ios_base::unitbuf;
00112   const ios_base::fmtflags ios_base::uppercase;
00113   const ios_base::fmtflags ios_base::adjustfield;
00114   const ios_base::fmtflags ios_base::basefield;
00115   const ios_base::fmtflags ios_base::floatfield;
00116 
00117   const ios_base::iostate ios_base::badbit;
00118   const ios_base::iostate ios_base::eofbit;
00119   const ios_base::iostate ios_base::failbit;
00120   const ios_base::iostate ios_base::goodbit;
00121 
00122   const ios_base::openmode ios_base::app;
00123   const ios_base::openmode ios_base::ate;
00124   const ios_base::openmode ios_base::binary;
00125   const ios_base::openmode ios_base::in;
00126   const ios_base::openmode ios_base::out;
00127   const ios_base::openmode ios_base::trunc;
00128 
00129   const ios_base::seekdir ios_base::beg;
00130   const ios_base::seekdir ios_base::cur;
00131   const ios_base::seekdir ios_base::end;
00132 
00133   const int ios_base::_S_local_word_size;
00134   int ios_base::Init::_S_ios_base_init = 0;
00135   bool ios_base::Init::_S_synced_with_stdio = true;
00136 
00137   ios_base::failure::failure(const string& __str) throw()
00138   {
00139     strncpy(_M_name, __str.c_str(), _M_bufsize);
00140     _M_name[_M_bufsize - 1] = '\0';
00141   }
00142 
00143   ios_base::failure::~failure() throw()
00144   { }
00145 
00146   const char*
00147   ios_base::failure::what() const throw()
00148   { return _M_name; }
00149 
00150   void
00151   ios_base::Init::_S_ios_create(bool __sync)
00152   {
00153     int __out_size = __sync ? 0 : static_cast<int>(BUFSIZ);
00154     int __in_size = __sync ? 1 : static_cast<int>(BUFSIZ);
00155 
00156     // NB: The file globals.cc creates the four standard files
00157     // with NULL buffers. At this point, we swap out the dummy NULL
00158     // [io]stream objects and buffers with the real deal.
00159     new (&buf_cout) stdio_filebuf<char>(stdout, ios_base::out, __out_size);
00160     new (&buf_cin) stdio_filebuf<char>(stdin, ios_base::in, __in_size);
00161     new (&buf_cerr) stdio_filebuf<char>(stderr, ios_base::out, __out_size);
00162     new (&cout) ostream(&buf_cout);
00163     new (&cin) istream(&buf_cin);
00164     new (&cerr) ostream(&buf_cerr);
00165     new (&clog) ostream(&buf_cerr);
00166     cin.tie(&cout);
00167     cerr.flags(ios_base::unitbuf);
00168     
00169 #ifdef _GLIBCPP_USE_WCHAR_T
00170     new (&buf_wcout) stdio_filebuf<wchar_t>(stdout, ios_base::out, __out_size);
00171     new (&buf_wcin) stdio_filebuf<wchar_t>(stdin, ios_base::in, __in_size);
00172     new (&buf_wcerr) stdio_filebuf<wchar_t>(stderr, ios_base::out, __out_size);
00173     new (&wcout) wostream(&buf_wcout);
00174     new (&wcin) wistream(&buf_wcin);
00175     new (&wcerr) wostream(&buf_wcerr);
00176     new (&wclog) wostream(&buf_wcerr);
00177     wcin.tie(&wcout);
00178     wcerr.flags(ios_base::unitbuf);
00179 #endif
00180   }
00181 
00182   void
00183   ios_base::Init::_S_ios_destroy()
00184   {
00185     // Explicitly call dtors to free any memory that is dynamically
00186     // allocated by filebuf ctor or member functions, but don't
00187     // deallocate all memory by calling operator delete.
00188     buf_cout.~stdio_filebuf();
00189     buf_cin.~stdio_filebuf();
00190     buf_cerr.~stdio_filebuf();
00191 
00192 #ifdef _GLIBCPP_USE_WCHAR_T
00193     buf_wcout.~stdio_filebuf();
00194     buf_wcin.~stdio_filebuf();
00195     buf_wcerr.~stdio_filebuf();
00196 #endif
00197   }
00198 
00199   ios_base::Init::Init()
00200   {
00201     if (_S_ios_base_init == 0)
00202       {
00203     // Standard streams default to synced with "C" operations.
00204     ios_base::Init::_S_synced_with_stdio = true;
00205     _S_ios_create(ios_base::Init::_S_synced_with_stdio);
00206       }
00207     ++_S_ios_base_init;
00208   }
00209 
00210   ios_base::Init::~Init()
00211   {
00212     if (--_S_ios_base_init == 0)
00213       _S_ios_destroy();
00214   } 
00215 
00216   // 27.4.2.5  ios_base storage functions
00217   int 
00218   ios_base::xalloc() throw()
00219   {
00220     // Implementation note: Initialize top to zero to ensure that
00221     // initialization occurs before main() is started.
00222     static _Atomic_word _S_top = 0; 
00223     return __exchange_and_add(&_S_top, 1) + 4;
00224   }
00225 
00226   // 27.4.2.5  iword/pword storage
00227   ios_base::_Words&
00228   ios_base::_M_grow_words(int ix)
00229   {
00230     // Precondition: _M_word_size <= ix
00231     int newsize = _S_local_word_size;
00232     _Words* words = _M_local_word;
00233     if (ix > _S_local_word_size - 1)
00234       {
00235     if (ix < numeric_limits<int>::max())
00236       {
00237         newsize = ix + 1;
00238         try
00239           { words = new _Words[newsize]; }
00240         catch (...)
00241           {
00242         delete [] _M_word;
00243         _M_word = 0;
00244         _M_streambuf_state |= badbit;
00245         if (_M_streambuf_state & _M_exception)
00246           __throw_ios_failure("ios_base::_M_grow_words failure");
00247         return _M_word_zero;
00248           }
00249         for (int i = 0; i < _M_word_size; i++) 
00250           words[i] = _M_word[i];
00251         if (_M_word && _M_word != _M_local_word) 
00252           {
00253         delete [] _M_word;
00254         _M_word = 0;
00255           }
00256       }
00257     else
00258       {
00259         _M_streambuf_state |= badbit;
00260         return _M_word_zero;
00261       }
00262       }
00263     _M_word = words;
00264     _M_word_size = newsize;
00265     return _M_word[ix];
00266   }
00267   
00268   // Called only by basic_ios<>::init.
00269   void 
00270   ios_base::_M_init()   
00271   {
00272     // NB: May be called more than once
00273     _M_precision = 6;
00274     _M_width = 0;
00275     _M_flags = skipws | dec;
00276     _M_callbacks = 0;
00277     _M_word_size = 0;
00278     _M_ios_locale = locale();
00279   }  
00280   
00281   // 27.4.2.3  ios_base locale functions
00282   locale
00283   ios_base::imbue(const locale& __loc)
00284   {
00285     locale __old = _M_ios_locale;
00286     _M_ios_locale = __loc;
00287     _M_call_callbacks(imbue_event);
00288     return __old;
00289   }
00290 
00291   ios_base::ios_base() : _M_callbacks(0), _M_word(0)
00292   {
00293     // Do nothing: basic_ios::init() does it.  
00294     // NB: _M_callbacks and _M_word must be zero for non-initialized
00295     // ios_base to go through ~ios_base gracefully.
00296   }
00297   
00298   // 27.4.2.7  ios_base constructors/destructors
00299   ios_base::~ios_base()
00300   {
00301     _M_call_callbacks(erase_event);
00302     _M_dispose_callbacks();
00303     if (_M_word && _M_word != _M_local_word) 
00304       {
00305     delete [] _M_word;
00306     _M_word = 0;
00307       }
00308   }
00309 
00310   void 
00311   ios_base::register_callback(event_callback __fn, int __index)
00312   { _M_callbacks = new _Callback_list(__fn, __index, _M_callbacks); }
00313 
00314   void 
00315   ios_base::_M_call_callbacks(event __e) throw()
00316   {
00317     _Callback_list* __p = _M_callbacks;
00318     while (__p)
00319       {
00320     try 
00321       { (*__p->_M_fn) (__e, *this, __p->_M_index); } 
00322     catch (...) 
00323       { }
00324     __p = __p->_M_next;
00325       }
00326   }
00327 
00328   void 
00329   ios_base::_M_dispose_callbacks(void)
00330   {
00331     _Callback_list* __p = _M_callbacks;
00332     while (__p && __p->_M_remove_reference() == 0)
00333       {
00334     _Callback_list* __next = __p->_M_next;
00335     delete __p;
00336     __p = __next;
00337       }
00338     _M_callbacks = 0;
00339   }
00340 
00341   bool 
00342   ios_base::sync_with_stdio(bool __sync)
00343   { 
00344 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
00345     // 49.  Underspecification of ios_base::sync_with_stdio
00346     bool __ret = ios_base::Init::_S_synced_with_stdio;
00347 #endif
00348 
00349     // Turn off sync with C FILE* for cin, cout, cerr, clog iff
00350     // currently synchronized.
00351     if (!__sync && __ret)
00352       {
00353     ios_base::Init::_S_synced_with_stdio = false;
00354     ios_base::Init::_S_ios_destroy();
00355     ios_base::Init::_S_ios_create(ios_base::Init::_S_synced_with_stdio);
00356       }
00357     return __ret; 
00358   }
00359 }  // namespace std

Generated on Wed May 1 19:19:31 2002 for libstdc++-v3 Source by doxygen1.2.15