111cd02dfb91661c65134cac258cf5924270e9d2Dan Albert//===------------------------ iostream.cpp --------------------------------===//
211cd02dfb91661c65134cac258cf5924270e9d2Dan Albert//
311cd02dfb91661c65134cac258cf5924270e9d2Dan Albert//                     The LLVM Compiler Infrastructure
411cd02dfb91661c65134cac258cf5924270e9d2Dan Albert//
511cd02dfb91661c65134cac258cf5924270e9d2Dan Albert// This file is dual licensed under the MIT and the University of Illinois Open
611cd02dfb91661c65134cac258cf5924270e9d2Dan Albert// Source Licenses. See LICENSE.TXT for details.
711cd02dfb91661c65134cac258cf5924270e9d2Dan Albert//
811cd02dfb91661c65134cac258cf5924270e9d2Dan Albert//===----------------------------------------------------------------------===//
911cd02dfb91661c65134cac258cf5924270e9d2Dan Albert
1011cd02dfb91661c65134cac258cf5924270e9d2Dan Albert#include "__std_stream"
1111cd02dfb91661c65134cac258cf5924270e9d2Dan Albert#include "string"
1211cd02dfb91661c65134cac258cf5924270e9d2Dan Albert#include "new"
1311cd02dfb91661c65134cac258cf5924270e9d2Dan Albert
1411cd02dfb91661c65134cac258cf5924270e9d2Dan Albert_LIBCPP_BEGIN_NAMESPACE_STD
1511cd02dfb91661c65134cac258cf5924270e9d2Dan Albert
1611cd02dfb91661c65134cac258cf5924270e9d2Dan Albert#ifndef _LIBCPP_HAS_NO_STDIN
1711cd02dfb91661c65134cac258cf5924270e9d2Dan Albert_ALIGNAS_TYPE (istream)  _LIBCPP_FUNC_VIS char cin [sizeof(istream)];
1811cd02dfb91661c65134cac258cf5924270e9d2Dan Albert_ALIGNAS_TYPE (__stdinbuf<char> ) static char __cin [sizeof(__stdinbuf <char>)];
1911cd02dfb91661c65134cac258cf5924270e9d2Dan Albertstatic mbstate_t mb_cin;
2011cd02dfb91661c65134cac258cf5924270e9d2Dan Albert_ALIGNAS_TYPE (wistream) _LIBCPP_FUNC_VIS char wcin [sizeof(wistream)];
2111cd02dfb91661c65134cac258cf5924270e9d2Dan Albert_ALIGNAS_TYPE (__stdinbuf<wchar_t> ) static char __wcin [sizeof(__stdinbuf <wchar_t>)];
2211cd02dfb91661c65134cac258cf5924270e9d2Dan Albertstatic mbstate_t mb_wcin;
2311cd02dfb91661c65134cac258cf5924270e9d2Dan Albert#endif
2411cd02dfb91661c65134cac258cf5924270e9d2Dan Albert
2511cd02dfb91661c65134cac258cf5924270e9d2Dan Albert#ifndef _LIBCPP_HAS_NO_STDOUT
2611cd02dfb91661c65134cac258cf5924270e9d2Dan Albert_ALIGNAS_TYPE (ostream)  _LIBCPP_FUNC_VIS char cout[sizeof(ostream)];
2711cd02dfb91661c65134cac258cf5924270e9d2Dan Albert_ALIGNAS_TYPE (__stdoutbuf<char>) static char __cout[sizeof(__stdoutbuf<char>)];
2811cd02dfb91661c65134cac258cf5924270e9d2Dan Albertstatic mbstate_t mb_cout;
2911cd02dfb91661c65134cac258cf5924270e9d2Dan Albert_ALIGNAS_TYPE (wostream) _LIBCPP_FUNC_VIS char wcout[sizeof(wostream)];
3011cd02dfb91661c65134cac258cf5924270e9d2Dan Albert_ALIGNAS_TYPE (__stdoutbuf<wchar_t>) static char __wcout[sizeof(__stdoutbuf<wchar_t>)];
3111cd02dfb91661c65134cac258cf5924270e9d2Dan Albertstatic mbstate_t mb_wcout;
3211cd02dfb91661c65134cac258cf5924270e9d2Dan Albert#endif
3311cd02dfb91661c65134cac258cf5924270e9d2Dan Albert
3411cd02dfb91661c65134cac258cf5924270e9d2Dan Albert_ALIGNAS_TYPE (ostream)  _LIBCPP_FUNC_VIS char cerr[sizeof(ostream)];
3511cd02dfb91661c65134cac258cf5924270e9d2Dan Albert_ALIGNAS_TYPE (__stdoutbuf<char>) static char __cerr[sizeof(__stdoutbuf<char>)];
3611cd02dfb91661c65134cac258cf5924270e9d2Dan Albertstatic mbstate_t mb_cerr;
3711cd02dfb91661c65134cac258cf5924270e9d2Dan Albert_ALIGNAS_TYPE (wostream) _LIBCPP_FUNC_VIS char wcerr[sizeof(wostream)];
3811cd02dfb91661c65134cac258cf5924270e9d2Dan Albert_ALIGNAS_TYPE (__stdoutbuf<wchar_t>) static char __wcerr[sizeof(__stdoutbuf<wchar_t>)];
3911cd02dfb91661c65134cac258cf5924270e9d2Dan Albertstatic mbstate_t mb_wcerr;
4011cd02dfb91661c65134cac258cf5924270e9d2Dan Albert
4111cd02dfb91661c65134cac258cf5924270e9d2Dan Albert_ALIGNAS_TYPE (ostream)  _LIBCPP_FUNC_VIS char clog[sizeof(ostream)];
4211cd02dfb91661c65134cac258cf5924270e9d2Dan Albert_ALIGNAS_TYPE (wostream) _LIBCPP_FUNC_VIS char wclog[sizeof(wostream)];
4311cd02dfb91661c65134cac258cf5924270e9d2Dan Albert
4411cd02dfb91661c65134cac258cf5924270e9d2Dan Albertios_base::Init __start_std_streams;
4511cd02dfb91661c65134cac258cf5924270e9d2Dan Albert
4611cd02dfb91661c65134cac258cf5924270e9d2Dan Albertios_base::Init::Init()
4711cd02dfb91661c65134cac258cf5924270e9d2Dan Albert{
4811cd02dfb91661c65134cac258cf5924270e9d2Dan Albert#ifndef _LIBCPP_HAS_NO_STDIN
4911cd02dfb91661c65134cac258cf5924270e9d2Dan Albert    istream* cin_ptr  = ::new(cin)  istream(::new(__cin)  __stdinbuf <char>(stdin, &mb_cin));
5011cd02dfb91661c65134cac258cf5924270e9d2Dan Albert    wistream* wcin_ptr  = ::new(wcin)  wistream(::new(__wcin)  __stdinbuf <wchar_t>(stdin, &mb_wcin));
5111cd02dfb91661c65134cac258cf5924270e9d2Dan Albert#endif
5211cd02dfb91661c65134cac258cf5924270e9d2Dan Albert#ifndef _LIBCPP_HAS_NO_STDOUT
5311cd02dfb91661c65134cac258cf5924270e9d2Dan Albert    ostream* cout_ptr = ::new(cout) ostream(::new(__cout) __stdoutbuf<char>(stdout, &mb_cout));
5411cd02dfb91661c65134cac258cf5924270e9d2Dan Albert    wostream* wcout_ptr = ::new(wcout) wostream(::new(__wcout) __stdoutbuf<wchar_t>(stdout, &mb_wcout));
5511cd02dfb91661c65134cac258cf5924270e9d2Dan Albert#endif
5611cd02dfb91661c65134cac258cf5924270e9d2Dan Albert    ostream* cerr_ptr = ::new(cerr) ostream(::new(__cerr) __stdoutbuf<char>(stderr, &mb_cerr));
5711cd02dfb91661c65134cac258cf5924270e9d2Dan Albert                        ::new(clog) ostream(cerr_ptr->rdbuf());
5811cd02dfb91661c65134cac258cf5924270e9d2Dan Albert    wostream* wcerr_ptr = ::new(wcerr) wostream(::new(__wcerr) __stdoutbuf<wchar_t>(stderr, &mb_wcerr));
5911cd02dfb91661c65134cac258cf5924270e9d2Dan Albert                          ::new(wclog) wostream(wcerr_ptr->rdbuf());
6011cd02dfb91661c65134cac258cf5924270e9d2Dan Albert
6111cd02dfb91661c65134cac258cf5924270e9d2Dan Albert#if !defined(_LIBCPP_HAS_NO_STDIN) && !defined(_LIBCPP_HAS_NO_STDOUT)
6211cd02dfb91661c65134cac258cf5924270e9d2Dan Albert    cin_ptr->tie(cout_ptr);
6311cd02dfb91661c65134cac258cf5924270e9d2Dan Albert    wcin_ptr->tie(wcout_ptr);
6411cd02dfb91661c65134cac258cf5924270e9d2Dan Albert#endif
6511cd02dfb91661c65134cac258cf5924270e9d2Dan Albert    _VSTD::unitbuf(*cerr_ptr);
6611cd02dfb91661c65134cac258cf5924270e9d2Dan Albert    _VSTD::unitbuf(*wcerr_ptr);
6711cd02dfb91661c65134cac258cf5924270e9d2Dan Albert#ifndef _LIBCPP_HAS_NO_STDOUT
6811cd02dfb91661c65134cac258cf5924270e9d2Dan Albert    cerr_ptr->tie(cout_ptr);
6911cd02dfb91661c65134cac258cf5924270e9d2Dan Albert    wcerr_ptr->tie(wcout_ptr);
7011cd02dfb91661c65134cac258cf5924270e9d2Dan Albert#endif
7111cd02dfb91661c65134cac258cf5924270e9d2Dan Albert}
7211cd02dfb91661c65134cac258cf5924270e9d2Dan Albert
7311cd02dfb91661c65134cac258cf5924270e9d2Dan Albertios_base::Init::~Init()
7411cd02dfb91661c65134cac258cf5924270e9d2Dan Albert{
7511cd02dfb91661c65134cac258cf5924270e9d2Dan Albert#ifndef _LIBCPP_HAS_NO_STDOUT
7611cd02dfb91661c65134cac258cf5924270e9d2Dan Albert    ostream* cout_ptr = reinterpret_cast<ostream*>(cout);
7711cd02dfb91661c65134cac258cf5924270e9d2Dan Albert    wostream* wcout_ptr = reinterpret_cast<wostream*>(wcout);
7811cd02dfb91661c65134cac258cf5924270e9d2Dan Albert    cout_ptr->flush();
7911cd02dfb91661c65134cac258cf5924270e9d2Dan Albert    wcout_ptr->flush();
8011cd02dfb91661c65134cac258cf5924270e9d2Dan Albert#endif
8111cd02dfb91661c65134cac258cf5924270e9d2Dan Albert
8211cd02dfb91661c65134cac258cf5924270e9d2Dan Albert    ostream* clog_ptr = reinterpret_cast<ostream*>(clog);
8311cd02dfb91661c65134cac258cf5924270e9d2Dan Albert    wostream* wclog_ptr = reinterpret_cast<wostream*>(wclog);
8411cd02dfb91661c65134cac258cf5924270e9d2Dan Albert    clog_ptr->flush();
8511cd02dfb91661c65134cac258cf5924270e9d2Dan Albert    wclog_ptr->flush();
8611cd02dfb91661c65134cac258cf5924270e9d2Dan Albert}
8711cd02dfb91661c65134cac258cf5924270e9d2Dan Albert
8811cd02dfb91661c65134cac258cf5924270e9d2Dan Albert_LIBCPP_END_NAMESPACE_STD
89