Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

ios.cc

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

Generated on Sat Apr 19 07:14:22 2003 for libstdc++-v3 Source by doxygen1.2.15