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   // Definitions for static const data members of __ios_flags.
00042   const __ios_flags::__int_type __ios_flags::_S_boolalpha;
00043   const __ios_flags::__int_type __ios_flags::_S_dec;
00044   const __ios_flags::__int_type __ios_flags::_S_fixed;
00045   const __ios_flags::__int_type __ios_flags::_S_hex;
00046   const __ios_flags::__int_type __ios_flags::_S_internal;
00047   const __ios_flags::__int_type __ios_flags::_S_left;
00048   const __ios_flags::__int_type __ios_flags::_S_oct;
00049   const __ios_flags::__int_type __ios_flags::_S_right;
00050   const __ios_flags::__int_type __ios_flags::_S_scientific;
00051   const __ios_flags::__int_type __ios_flags::_S_showbase;
00052   const __ios_flags::__int_type __ios_flags::_S_showpoint;
00053   const __ios_flags::__int_type __ios_flags::_S_showpos;
00054   const __ios_flags::__int_type __ios_flags::_S_skipws;
00055   const __ios_flags::__int_type __ios_flags::_S_unitbuf;
00056   const __ios_flags::__int_type __ios_flags::_S_uppercase;
00057   const __ios_flags::__int_type __ios_flags::_S_adjustfield;
00058   const __ios_flags::__int_type __ios_flags::_S_basefield;
00059   const __ios_flags::__int_type __ios_flags::_S_floatfield;
00060 
00061   const __ios_flags::__int_type __ios_flags::_S_badbit;
00062   const __ios_flags::__int_type __ios_flags::_S_eofbit;
00063   const __ios_flags::__int_type __ios_flags::_S_failbit;
00064 
00065   const __ios_flags::__int_type __ios_flags::_S_app;
00066   const __ios_flags::__int_type __ios_flags::_S_ate;
00067   const __ios_flags::__int_type __ios_flags::_S_bin;
00068   const __ios_flags::__int_type __ios_flags::_S_in;
00069   const __ios_flags::__int_type __ios_flags::_S_out;
00070   const __ios_flags::__int_type __ios_flags::_S_trunc;
00071 
00072   // Definitions for static const members of ios_base.
00073   const ios_base::fmtflags ios_base::boolalpha;
00074   const ios_base::fmtflags ios_base::dec;
00075   const ios_base::fmtflags ios_base::fixed;
00076   const ios_base::fmtflags ios_base::hex;
00077   const ios_base::fmtflags ios_base::internal;
00078   const ios_base::fmtflags ios_base::left;
00079   const ios_base::fmtflags ios_base::oct;
00080   const ios_base::fmtflags ios_base::right;
00081   const ios_base::fmtflags ios_base::scientific;
00082   const ios_base::fmtflags ios_base::showbase;
00083   const ios_base::fmtflags ios_base::showpoint;
00084   const ios_base::fmtflags ios_base::showpos;
00085   const ios_base::fmtflags ios_base::skipws;
00086   const ios_base::fmtflags ios_base::unitbuf;
00087   const ios_base::fmtflags ios_base::uppercase;
00088   const ios_base::fmtflags ios_base::adjustfield;
00089   const ios_base::fmtflags ios_base::basefield;
00090   const ios_base::fmtflags ios_base::floatfield;
00091 
00092   const ios_base::iostate ios_base::badbit;
00093   const ios_base::iostate ios_base::eofbit;
00094   const ios_base::iostate ios_base::failbit;
00095   const ios_base::iostate ios_base::goodbit;
00096 
00097   const ios_base::openmode ios_base::app;
00098   const ios_base::openmode ios_base::ate;
00099   const ios_base::openmode ios_base::binary;
00100   const ios_base::openmode ios_base::in;
00101   const ios_base::openmode ios_base::out;
00102   const ios_base::openmode ios_base::trunc;
00103 
00104   const ios_base::seekdir ios_base::beg;
00105   const ios_base::seekdir ios_base::cur;
00106   const ios_base::seekdir ios_base::end;
00107 
00108   const int ios_base::_S_local_words;
00109   int ios_base::Init::_S_ios_base_init = 0;
00110   bool ios_base::Init::_S_synced_with_stdio = true;
00111 
00112   extern istream cin;
00113   extern ostream cout;
00114   extern ostream cerr;
00115   extern ostream clog;
00116 #ifdef _GLIBCPP_USE_WCHAR_T
00117   extern wistream wcin;
00118   extern wostream wcout;
00119   extern wostream wcerr;
00120   extern wostream wclog;
00121 #endif
00122 
00123   ios_base::failure::failure(const string& __str) throw()
00124   {
00125     strncpy(_M_name, __str.c_str(), _M_bufsize);
00126     _M_name[_M_bufsize - 1] = '\0';
00127   }
00128 
00129   ios_base::failure::~failure() throw()
00130   { }
00131 
00132   const char*
00133   ios_base::failure::what() const throw()
00134   { return _M_name; }
00135 
00136   void
00137   ios_base::Init::_S_ios_create(bool __sync)
00138   {
00139     // NB: The file std_iostream.h creates the four standard files
00140     // with NULL buffers. At this point, we swap out these
00141     new (&cout) ostream(new filebuf(stdout, __sync, ios_base::out));
00142     new (&cin) istream(new filebuf(stdin, __sync, ios_base::in));
00143     new (&cerr) ostream(new filebuf(stderr, __sync, ios_base::out));
00144     new (&clog) ostream(cerr.rdbuf());
00145     cin.tie(&cout);
00146     cerr.flags(ios_base::unitbuf);
00147     
00148 #ifdef _GLIBCPP_USE_WCHAR_T
00149     new (&wcout) wostream( new wfilebuf(stdout, __sync, ios_base::out));
00150     new (&wcin) wistream(new wfilebuf(stdin, __sync, ios_base::in));
00151     new (&wcerr) wostream(new wfilebuf(stderr, __sync, ios_base::out));
00152     new (&wclog) wostream(wcerr.rdbuf());
00153     wcin.tie(&wcout);
00154     wcerr.flags(ios_base::unitbuf);
00155 #endif
00156   }
00157 
00158   ios_base::Init::Init()
00159   {
00160     if (++_S_ios_base_init == 1)
00161       {
00162     // Standard streams default to synced with "C" operations.
00163     ios_base::Init::_S_synced_with_stdio = true;
00164     _S_ios_create(ios_base::Init::_S_synced_with_stdio);
00165       }
00166   }
00167 
00168   void
00169   ios_base::Init::_S_ios_destroy()
00170   {
00171     cout.flush();
00172     cerr.flush();
00173     clog.flush();
00174     delete cout.rdbuf();
00175     delete cin.rdbuf();
00176     delete cerr.rdbuf();
00177 #ifdef _GLIBCPP_USE_WCHAR_T
00178     wcout.flush();
00179     wcerr.flush();
00180     wclog.flush();
00181     delete wcout.rdbuf();
00182     delete wcin.rdbuf();
00183     delete wcerr.rdbuf();
00184 #endif
00185   }
00186 
00187   ios_base::Init::~Init()
00188   {
00189     if (--_S_ios_base_init == 0)
00190       _S_ios_destroy();
00191   } 
00192 
00193   // 27.4.2.5  ios_base storage functions
00194   int 
00195   ios_base::xalloc() throw()
00196   {
00197     // XXX MT
00198     // XXX should be a symbol. (Reserve 0..3 for builtins.)
00199     static int top = 4; 
00200     return top++;
00201   }
00202 
00203   // 27.4.2.5  iword/pword storage
00204   ios_base::_Words&
00205   ios_base::_M_grow_words(int ix)
00206   {
00207     // Precondition: _M_word_limit <= ix
00208     _Words zero = { 0, 0 };
00209     int newlimit = _S_local_words;
00210     _Words* words = _M_word_array;
00211     int i = 0;
00212     if (_S_local_words <= ix)
00213       {
00214     newlimit = ix+1;
00215     try
00216       { words = new _Words[ix+1]; }
00217     catch (...)
00218       {
00219         _M_dummy = zero;  // XXX MT? Not on "normal" machines.
00220         // XXX now in basic_ios
00221         // _M_clear(_M_rdstate() | badbit);  // may throw
00222         return _M_dummy;
00223       }
00224     for (; i < _M_word_limit; i++) 
00225       words[i] = _M_words[i];
00226     if (_M_words != _M_word_array) 
00227       delete [] _M_words;
00228       }
00229     
00230     do { words[i] = zero; } while (++i < newlimit);
00231     _M_words = words;
00232     _M_word_limit = newlimit;
00233     return words[ix];
00234   }
00235   
00236   // Called only by basic_ios<>::init.
00237   void 
00238   ios_base::_M_init()   
00239   {
00240     // NB: May be called more than once
00241     _M_precision = 6;
00242     _M_width = 0;
00243     _M_flags = skipws | dec;
00244     _M_callbacks = 0;
00245     _M_words = 0;
00246     _M_word_limit = 0;
00247     _M_ios_locale = locale();
00248     // No init needed for _M_word_array or _M_dummy.
00249   }  
00250   
00251   // 27.4.2.3  ios_base locale functions
00252   locale
00253   ios_base::imbue(const locale& __loc)
00254   {
00255     locale __old = _M_ios_locale;
00256     _M_ios_locale = __loc;
00257     // Make sure there's a callback for the format caches so they will be
00258     // marked dirty.
00259     _Format_cache<char>::_S_get(*this);
00260 #ifdef _GLIBCPP_USE_WCHAR_T
00261     _Format_cache<wchar_t>::_S_get(*this);
00262 #endif
00263     _M_call_callbacks(imbue_event);
00264     return __old;
00265   }
00266 
00267   ios_base::ios_base()
00268   {
00269     // Do nothing; init() does it.  Static init to 0 makes everything sane.
00270   }
00271   
00272   // 27.4.2.7  ios_base constructors/destructors
00273   ios_base::~ios_base()
00274   {
00275     _M_call_callbacks(erase_event);
00276     _M_dispose_callbacks();
00277     if (_M_words != _M_word_array) 
00278       delete [] _M_words;
00279     // XXX done?
00280   }
00281 
00282   void 
00283   ios_base::register_callback(event_callback __fn, int __index)
00284   { _M_callbacks = new _Callback_list(__fn, __index, _M_callbacks); }
00285 
00286   void 
00287   ios_base::_M_call_callbacks(event __e) throw()
00288   {
00289     for (_Callback_list* __p = _M_callbacks; __p; __p = __p->_M_next)
00290       {
00291     try { 
00292       (*__p->_M_fn) (__e, *this, __p->_M_index); 
00293     } 
00294     catch (...) { 
00295     }
00296       }
00297   }
00298 
00299   void 
00300   ios_base::_M_dispose_callbacks(void)
00301   {
00302     _Callback_list* __p = _M_callbacks;
00303     while (__p && __p->_M_remove_reference() == 0)
00304       {
00305     _Callback_list* __next = __p->_M_next;
00306     delete __p;
00307     __p = __next;
00308       }
00309     _M_callbacks = 0;
00310   }
00311 
00312   bool 
00313   ios_base::sync_with_stdio(bool __sync)
00314   { 
00315 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
00316     // 49.  Underspecification of ios_base::sync_with_stdio
00317     bool __ret = ios_base::Init::_S_synced_with_stdio;
00318 #endif
00319 
00320     // Turn off sync with C FILE* for cin, cout, cerr, clog iff
00321     // currently synchronized.
00322     if (!__sync && __ret)
00323       {
00324     ios_base::Init::_S_synced_with_stdio = false;
00325     ios_base::Init::_S_ios_destroy();
00326     ios_base::Init::_S_ios_create(ios_base::Init::_S_synced_with_stdio);
00327       }
00328     return __ret; 
00329   }
00330 }  // namespace std
00331 

Generated at Tue May 1 16:28:38 2001 for libstdc++-v3 by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001