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 #ifdef _GLIBCPP_HAVE_UNISTD_H
00042 #include <unistd.h>
00043 #endif
00044
00045 namespace __gnu_cxx
00046 {
00047
00048 extern stdio_filebuf<char> buf_cout;
00049 extern stdio_filebuf<char> buf_cin;
00050 extern stdio_filebuf<char> buf_cerr;
00051 extern std::__locale_cache<char> locale_cache_cout;
00052 extern std::__locale_cache<char> locale_cache_cin;
00053 extern std::__locale_cache<char> locale_cache_cerr;
00054 extern std::__locale_cache<char> locale_cache_clog;
00055
00056 #ifdef _GLIBCPP_USE_WCHAR_T
00057 extern stdio_filebuf<wchar_t> buf_wcout;
00058 extern stdio_filebuf<wchar_t> buf_wcin;
00059 extern stdio_filebuf<wchar_t> buf_wcerr;
00060 extern std::__locale_cache<wchar_t> locale_cache_wcout;
00061 extern std::__locale_cache<wchar_t> locale_cache_wcin;
00062 extern std::__locale_cache<wchar_t> locale_cache_wcerr;
00063 extern std::__locale_cache<wchar_t> locale_cache_wclog;
00064 #endif
00065 }
00066
00067 namespace std
00068 {
00069 using namespace __gnu_cxx;
00070
00071 extern istream cin;
00072 extern ostream cout;
00073 extern ostream cerr;
00074 extern ostream clog;
00075
00076 #ifdef _GLIBCPP_USE_WCHAR_T
00077 extern wistream wcin;
00078 extern wostream wcout;
00079 extern wostream wcerr;
00080 extern wostream wclog;
00081 #endif
00082
00083
00084 const __ios_flags::__int_type __ios_flags::_S_boolalpha;
00085 const __ios_flags::__int_type __ios_flags::_S_dec;
00086 const __ios_flags::__int_type __ios_flags::_S_fixed;
00087 const __ios_flags::__int_type __ios_flags::_S_hex;
00088 const __ios_flags::__int_type __ios_flags::_S_internal;
00089 const __ios_flags::__int_type __ios_flags::_S_left;
00090 const __ios_flags::__int_type __ios_flags::_S_oct;
00091 const __ios_flags::__int_type __ios_flags::_S_right;
00092 const __ios_flags::__int_type __ios_flags::_S_scientific;
00093 const __ios_flags::__int_type __ios_flags::_S_showbase;
00094 const __ios_flags::__int_type __ios_flags::_S_showpoint;
00095 const __ios_flags::__int_type __ios_flags::_S_showpos;
00096 const __ios_flags::__int_type __ios_flags::_S_skipws;
00097 const __ios_flags::__int_type __ios_flags::_S_unitbuf;
00098 const __ios_flags::__int_type __ios_flags::_S_uppercase;
00099 const __ios_flags::__int_type __ios_flags::_S_adjustfield;
00100 const __ios_flags::__int_type __ios_flags::_S_basefield;
00101 const __ios_flags::__int_type __ios_flags::_S_floatfield;
00102
00103 const __ios_flags::__int_type __ios_flags::_S_badbit;
00104 const __ios_flags::__int_type __ios_flags::_S_eofbit;
00105 const __ios_flags::__int_type __ios_flags::_S_failbit;
00106
00107 const __ios_flags::__int_type __ios_flags::_S_app;
00108 const __ios_flags::__int_type __ios_flags::_S_ate;
00109 const __ios_flags::__int_type __ios_flags::_S_bin;
00110 const __ios_flags::__int_type __ios_flags::_S_in;
00111 const __ios_flags::__int_type __ios_flags::_S_out;
00112 const __ios_flags::__int_type __ios_flags::_S_trunc;
00113
00114
00115 const ios_base::fmtflags ios_base::boolalpha;
00116 const ios_base::fmtflags ios_base::dec;
00117 const ios_base::fmtflags ios_base::fixed;
00118 const ios_base::fmtflags ios_base::hex;
00119 const ios_base::fmtflags ios_base::internal;
00120 const ios_base::fmtflags ios_base::left;
00121 const ios_base::fmtflags ios_base::oct;
00122 const ios_base::fmtflags ios_base::right;
00123 const ios_base::fmtflags ios_base::scientific;
00124 const ios_base::fmtflags ios_base::showbase;
00125 const ios_base::fmtflags ios_base::showpoint;
00126 const ios_base::fmtflags ios_base::showpos;
00127 const ios_base::fmtflags ios_base::skipws;
00128 const ios_base::fmtflags ios_base::unitbuf;
00129 const ios_base::fmtflags ios_base::uppercase;
00130 const ios_base::fmtflags ios_base::adjustfield;
00131 const ios_base::fmtflags ios_base::basefield;
00132 const ios_base::fmtflags ios_base::floatfield;
00133
00134 const ios_base::iostate ios_base::badbit;
00135 const ios_base::iostate ios_base::eofbit;
00136 const ios_base::iostate ios_base::failbit;
00137 const ios_base::iostate ios_base::goodbit;
00138
00139 const ios_base::openmode ios_base::app;
00140 const ios_base::openmode ios_base::ate;
00141 const ios_base::openmode ios_base::binary;
00142 const ios_base::openmode ios_base::in;
00143 const ios_base::openmode ios_base::out;
00144 const ios_base::openmode ios_base::trunc;
00145
00146 const ios_base::seekdir ios_base::beg;
00147 const ios_base::seekdir ios_base::cur;
00148 const ios_base::seekdir ios_base::end;
00149
00150 const int ios_base::_S_local_word_size;
00151 int ios_base::Init::_S_ios_base_init = 0;
00152 bool ios_base::Init::_S_synced_with_stdio = true;
00153
00154 ios_base::failure::failure(const string& __str) throw()
00155 {
00156 strncpy(_M_name, __str.c_str(), _M_bufsize);
00157 _M_name[_M_bufsize - 1] = '\0';
00158 }
00159
00160 ios_base::failure::~failure() throw()
00161 { }
00162
00163 const char*
00164 ios_base::failure::what() const throw()
00165 { return _M_name; }
00166
00167 void
00168 ios_base::Init::_S_ios_create(bool __sync)
00169 {
00170 size_t __out_size = __sync ? 0 : static_cast<size_t>(BUFSIZ);
00171 #ifdef _GLIBCPP_HAVE_ISATTY
00172 size_t __in_size =
00173 (__sync || isatty (0)) ? 1 : static_cast<size_t>(BUFSIZ);
00174 #else
00175 size_t __in_size = 1;
00176 #endif
00177
00178
00179
00180
00181 new (&buf_cout) stdio_filebuf<char>(stdout, ios_base::out, __out_size);
00182 new (&buf_cin) stdio_filebuf<char>(stdin, ios_base::in, __in_size);
00183 new (&buf_cerr) stdio_filebuf<char>(stderr, ios_base::out, __out_size);
00184
00185 new (&cout) ostream(&buf_cout);
00186 new (&cin) istream(&buf_cin);
00187 new (&cerr) ostream(&buf_cerr);
00188 new (&clog) ostream(&buf_cerr);
00189 cout.init(&buf_cout, &locale_cache_cout);
00190 cin.init(&buf_cin, &locale_cache_cin);
00191 cerr.init(&buf_cerr, &locale_cache_cerr);
00192 clog.init(&buf_cerr, &locale_cache_clog);
00193 cin.tie(&cout);
00194 cerr.flags(ios_base::unitbuf);
00195
00196 #ifdef _GLIBCPP_USE_WCHAR_T
00197 new (&buf_wcout) stdio_filebuf<wchar_t>(stdout, ios_base::out, __out_size);
00198 new (&buf_wcin) stdio_filebuf<wchar_t>(stdin, ios_base::in, __in_size);
00199 new (&buf_wcerr) stdio_filebuf<wchar_t>(stderr, ios_base::out, __out_size);
00200 new (&wcout) wostream(&buf_wcout);
00201 new (&wcin) wistream(&buf_wcin);
00202 new (&wcerr) wostream(&buf_wcerr);
00203 new (&wclog) wostream(&buf_wcerr);
00204 wcout.init(&buf_wcout, &locale_cache_wcout);
00205 wcin.init(&buf_wcin, &locale_cache_wcin);
00206 wcerr.init(&buf_wcerr, &locale_cache_wcerr);
00207 wclog.init(&buf_wcerr, &locale_cache_wclog);
00208 wcin.tie(&wcout);
00209 wcerr.flags(ios_base::unitbuf);
00210 #endif
00211 }
00212
00213 void
00214 ios_base::Init::_S_ios_destroy()
00215 {
00216
00217
00218
00219 buf_cout.~stdio_filebuf();
00220 buf_cin.~stdio_filebuf();
00221 buf_cerr.~stdio_filebuf();
00222
00223 #ifdef _GLIBCPP_USE_WCHAR_T
00224 buf_wcout.~stdio_filebuf();
00225 buf_wcin.~stdio_filebuf();
00226 buf_wcerr.~stdio_filebuf();
00227 #endif
00228 }
00229
00230 ios_base::Init::Init()
00231 {
00232 if (_S_ios_base_init == 0)
00233 {
00234
00235 ios_base::Init::_S_synced_with_stdio = true;
00236 _S_ios_create(ios_base::Init::_S_synced_with_stdio);
00237 }
00238 ++_S_ios_base_init;
00239 }
00240
00241 ios_base::Init::~Init()
00242 {
00243 if (--_S_ios_base_init == 0)
00244 _S_ios_destroy();
00245 }
00246
00247
00248 int
00249 ios_base::xalloc() throw()
00250 {
00251
00252
00253 static _Atomic_word _S_top = 0;
00254 return __exchange_and_add(&_S_top, 1) + 4;
00255 }
00256
00257
00258 ios_base::_Words&
00259 ios_base::_M_grow_words(int ix)
00260 {
00261
00262 int newsize = _S_local_word_size;
00263 _Words* words = _M_local_word;
00264 if (ix > _S_local_word_size - 1)
00265 {
00266 if (ix < numeric_limits<int>::max())
00267 {
00268 newsize = ix + 1;
00269 try
00270 { words = new _Words[newsize]; }
00271 catch (...)
00272 {
00273 _M_streambuf_state |= badbit;
00274 if (_M_streambuf_state & _M_exception)
00275 __throw_ios_failure("ios_base::_M_grow_words failure");
00276 return _M_word_zero;
00277 }
00278 for (int i = 0; i < _M_word_size; i++)
00279 words[i] = _M_word[i];
00280 if (_M_word && _M_word != _M_local_word)
00281 {
00282 delete [] _M_word;
00283 _M_word = 0;
00284 }
00285 }
00286 else
00287 {
00288 _M_streambuf_state |= badbit;
00289 if (_M_streambuf_state & _M_exception)
00290 __throw_ios_failure("ios_base::_M_grow_words failure");
00291 return _M_word_zero;
00292 }
00293 }
00294 _M_word = words;
00295 _M_word_size = newsize;
00296 return _M_word[ix];
00297 }
00298
00299
00300 void
00301 ios_base::_M_init()
00302 {
00303
00304 _M_precision = 6;
00305 _M_width = 0;
00306 _M_flags = skipws | dec;
00307 _M_ios_locale = locale();
00308 }
00309
00310
00311 locale
00312 ios_base::imbue(const locale& __loc)
00313 {
00314 locale __old = _M_ios_locale;
00315 _M_ios_locale = __loc;
00316 _M_call_callbacks(imbue_event);
00317 return __old;
00318 }
00319
00320 ios_base::ios_base() : _M_callbacks(0), _M_word_size(_S_local_word_size),
00321 _M_word(_M_local_word)
00322 {
00323
00324
00325
00326 }
00327
00328
00329 ios_base::~ios_base()
00330 {
00331 _M_call_callbacks(erase_event);
00332 _M_dispose_callbacks();
00333 if (_M_word != _M_local_word)
00334 {
00335 delete [] _M_word;
00336 _M_word = 0;
00337 }
00338 }
00339
00340 void
00341 ios_base::register_callback(event_callback __fn, int __index)
00342 { _M_callbacks = new _Callback_list(__fn, __index, _M_callbacks); }
00343
00344 void
00345 ios_base::_M_call_callbacks(event __e) throw()
00346 {
00347 _Callback_list* __p = _M_callbacks;
00348 while (__p)
00349 {
00350 try
00351 { (*__p->_M_fn) (__e, *this, __p->_M_index); }
00352 catch (...)
00353 { }
00354 __p = __p->_M_next;
00355 }
00356 }
00357
00358 void
00359 ios_base::_M_dispose_callbacks(void)
00360 {
00361 _Callback_list* __p = _M_callbacks;
00362 while (__p && __p->_M_remove_reference() == 0)
00363 {
00364 _Callback_list* __next = __p->_M_next;
00365 delete __p;
00366 __p = __next;
00367 }
00368 _M_callbacks = 0;
00369 }
00370
00371 bool
00372 ios_base::sync_with_stdio(bool __sync)
00373 {
00374 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
00375
00376 bool __ret = ios_base::Init::_S_synced_with_stdio;
00377 #endif
00378
00379
00380
00381 if (!__sync && __ret)
00382 {
00383 ios_base::Init::_S_synced_with_stdio = false;
00384 ios_base::Init::_S_ios_destroy();
00385 ios_base::Init::_S_ios_create(ios_base::Init::_S_synced_with_stdio);
00386 }
00387 return __ret;
00388 }
00389 }