00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
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   
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   
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   
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     
00157     
00158     
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     
00186     
00187     
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     
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   
00217   int 
00218   ios_base::xalloc() throw()
00219   {
00220     
00221     
00222     static _Atomic_word _S_top = 0; 
00223     return __exchange_and_add(&_S_top, 1) + 4;
00224   }
00225 
00226   
00227   ios_base::_Words&
00228   ios_base::_M_grow_words(int ix)
00229   {
00230     
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   
00269   void 
00270   ios_base::_M_init()   
00271   {
00272     
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   
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     
00294     
00295     
00296   }
00297   
00298   
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     
00346     bool __ret = ios_base::Init::_S_synced_with_stdio;
00347 #endif
00348 
00349     
00350     
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 }