Path: blob/main/contrib/llvm-project/libcxx/src/std_stream.h
35147 views
// -*- C++ -*-1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//89#ifndef _LIBCPP_STD_STREAM_H10#define _LIBCPP_STD_STREAM_H1112#include <__config>13#include <__locale>14#include <cstdio>15#include <istream>16#include <ostream>1718#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)19# pragma GCC system_header20#endif2122_LIBCPP_PUSH_MACROS23#include <__undef_macros>2425_LIBCPP_BEGIN_NAMESPACE_STD2627static const int __limit = 8;2829// __stdinbuf3031template <class _CharT>32class _LIBCPP_HIDDEN __stdinbuf : public basic_streambuf<_CharT, char_traits<_CharT> > {33public:34typedef _CharT char_type;35typedef char_traits<char_type> traits_type;36typedef typename traits_type::int_type int_type;37typedef typename traits_type::pos_type pos_type;38typedef typename traits_type::off_type off_type;39typedef typename traits_type::state_type state_type;4041__stdinbuf(FILE* __fp, state_type* __st);4243protected:44virtual int_type underflow();45virtual int_type uflow();46virtual int_type pbackfail(int_type __c = traits_type::eof());47virtual void imbue(const locale& __loc);4849private:50FILE* __file_;51const codecvt<char_type, char, state_type>* __cv_;52state_type* __st_;53int __encoding_;54int_type __last_consumed_;55bool __last_consumed_is_next_;56bool __always_noconv_;5758#if defined(_LIBCPP_WIN32API)59static constexpr bool __is_win32api_wide_char = !is_same_v<_CharT, char>;60#else61static constexpr bool __is_win32api_wide_char = false;62#endif6364__stdinbuf(const __stdinbuf&);65__stdinbuf& operator=(const __stdinbuf&);6667int_type __getchar(bool __consume);68};6970template <class _CharT>71__stdinbuf<_CharT>::__stdinbuf(FILE* __fp, state_type* __st)72: __file_(__fp), __st_(__st), __last_consumed_(traits_type::eof()), __last_consumed_is_next_(false) {73imbue(this->getloc());74// On Windows, in wchar_t mode, ignore the codecvt from the locale by75// default and assume noconv; this passes wchar_t through unmodified from76// getwc. If the user sets a custom locale with imbue(), that gets honored,77// the IO is done with getc() and converted with the provided codecvt.78if constexpr (__is_win32api_wide_char)79__always_noconv_ = true;80}8182template <class _CharT>83void __stdinbuf<_CharT>::imbue(const locale& __loc) {84__cv_ = &use_facet<codecvt<char_type, char, state_type> >(__loc);85__encoding_ = __cv_->encoding();86__always_noconv_ = __cv_->always_noconv();87if (__encoding_ > __limit)88__throw_runtime_error("unsupported locale for standard input");89}9091template <class _CharT>92typename __stdinbuf<_CharT>::int_type __stdinbuf<_CharT>::underflow() {93return __getchar(false);94}9596template <class _CharT>97typename __stdinbuf<_CharT>::int_type __stdinbuf<_CharT>::uflow() {98return __getchar(true);99}100101inline bool __do_getc(FILE* __fp, char* __pbuf) {102int __c = getc(__fp);103if (__c == EOF)104return false;105*__pbuf = static_cast<char>(__c);106return true;107}108#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS109inline bool __do_getc(FILE* __fp, wchar_t* __pbuf) {110wint_t __c = getwc(__fp);111if (__c == WEOF)112return false;113*__pbuf = static_cast<wchar_t>(__c);114return true;115}116#endif117118inline bool __do_ungetc(int __c, FILE* __fp, char __dummy) {119if (ungetc(__c, __fp) == EOF)120return false;121return true;122}123#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS124inline bool __do_ungetc(std::wint_t __c, FILE* __fp, wchar_t __dummy) {125if (ungetwc(__c, __fp) == WEOF)126return false;127return true;128}129#endif130131template <class _CharT>132typename __stdinbuf<_CharT>::int_type __stdinbuf<_CharT>::__getchar(bool __consume) {133if (__last_consumed_is_next_) {134int_type __result = __last_consumed_;135if (__consume) {136__last_consumed_ = traits_type::eof();137__last_consumed_is_next_ = false;138}139return __result;140}141if (__always_noconv_) {142char_type __1buf;143if (!__do_getc(__file_, &__1buf))144return traits_type::eof();145if (!__consume) {146if (!__do_ungetc(traits_type::to_int_type(__1buf), __file_, __1buf))147return traits_type::eof();148} else149__last_consumed_ = traits_type::to_int_type(__1buf);150return traits_type::to_int_type(__1buf);151}152153char __extbuf[__limit];154int __nread = std::max(1, __encoding_);155for (int __i = 0; __i < __nread; ++__i) {156int __c = getc(__file_);157if (__c == EOF)158return traits_type::eof();159__extbuf[__i] = static_cast<char>(__c);160}161char_type __1buf;162const char* __enxt;163char_type* __inxt;164codecvt_base::result __r;165do {166state_type __sv_st = *__st_;167__r = __cv_->in(*__st_, __extbuf, __extbuf + __nread, __enxt, &__1buf, &__1buf + 1, __inxt);168switch (__r) {169case std::codecvt_base::ok:170break;171case codecvt_base::partial:172*__st_ = __sv_st;173if (__nread == sizeof(__extbuf))174return traits_type::eof();175{176int __c = getc(__file_);177if (__c == EOF)178return traits_type::eof();179__extbuf[__nread] = static_cast<char>(__c);180}181++__nread;182break;183case codecvt_base::error:184return traits_type::eof();185case std::codecvt_base::noconv:186__1buf = static_cast<char_type>(__extbuf[0]);187break;188}189} while (__r == std::codecvt_base::partial);190if (!__consume) {191for (int __i = __nread; __i > 0;) {192if (ungetc(traits_type::to_int_type(__extbuf[--__i]), __file_) == EOF)193return traits_type::eof();194}195} else196__last_consumed_ = traits_type::to_int_type(__1buf);197return traits_type::to_int_type(__1buf);198}199200template <class _CharT>201typename __stdinbuf<_CharT>::int_type __stdinbuf<_CharT>::pbackfail(int_type __c) {202if (traits_type::eq_int_type(__c, traits_type::eof())) {203if (!__last_consumed_is_next_) {204__c = __last_consumed_;205__last_consumed_is_next_ = !traits_type::eq_int_type(__last_consumed_, traits_type::eof());206}207return __c;208}209if (__always_noconv_ && __last_consumed_is_next_) {210if (!__do_ungetc(__last_consumed_, __file_, traits_type::to_char_type(__last_consumed_)))211return traits_type::eof();212} else if (__last_consumed_is_next_) {213char __extbuf[__limit];214char* __enxt;215const char_type __ci = traits_type::to_char_type(__last_consumed_);216const char_type* __inxt;217switch (__cv_->out(*__st_, &__ci, &__ci + 1, __inxt, __extbuf, __extbuf + sizeof(__extbuf), __enxt)) {218case std::codecvt_base::ok:219break;220case std::codecvt_base::noconv:221__extbuf[0] = static_cast<char>(__last_consumed_);222__enxt = __extbuf + 1;223break;224case codecvt_base::partial:225case codecvt_base::error:226return traits_type::eof();227}228while (__enxt > __extbuf)229if (ungetc(*--__enxt, __file_) == EOF)230return traits_type::eof();231}232__last_consumed_ = __c;233__last_consumed_is_next_ = true;234return __c;235}236237// __stdoutbuf238239template <class _CharT>240class _LIBCPP_HIDDEN __stdoutbuf : public basic_streambuf<_CharT, char_traits<_CharT> > {241public:242typedef _CharT char_type;243typedef char_traits<char_type> traits_type;244typedef typename traits_type::int_type int_type;245typedef typename traits_type::pos_type pos_type;246typedef typename traits_type::off_type off_type;247typedef typename traits_type::state_type state_type;248249__stdoutbuf(FILE* __fp, state_type* __st);250251protected:252virtual int_type overflow(int_type __c = traits_type::eof());253virtual streamsize xsputn(const char_type* __s, streamsize __n);254virtual int sync();255virtual void imbue(const locale& __loc);256257private:258FILE* __file_;259const codecvt<char_type, char, state_type>* __cv_;260state_type* __st_;261bool __always_noconv_;262263#if defined(_LIBCPP_WIN32API)264static constexpr bool __is_win32api_wide_char = !is_same_v<_CharT, char>;265#else266static constexpr bool __is_win32api_wide_char = false;267#endif268269__stdoutbuf(const __stdoutbuf&);270__stdoutbuf& operator=(const __stdoutbuf&);271272_LIBCPP_EXPORTED_FROM_ABI friend FILE* __get_ostream_file(ostream&);273};274275template <class _CharT>276__stdoutbuf<_CharT>::__stdoutbuf(FILE* __fp, state_type* __st)277: __file_(__fp),278__cv_(&use_facet<codecvt<char_type, char, state_type> >(this->getloc())),279__st_(__st),280__always_noconv_(__cv_->always_noconv()) {281// On Windows, in wchar_t mode, ignore the codecvt from the locale by282// default and assume noconv; this passes wchar_t through unmodified to283// fputwc, which handles it correctly depending on the actual mode of the284// output stream. If the user sets a custom locale with imbue(), that285// gets honored.286if constexpr (__is_win32api_wide_char)287__always_noconv_ = true;288}289290inline bool __do_fputc(char __c, FILE* __fp) {291if (fwrite(&__c, sizeof(__c), 1, __fp) != 1)292return false;293return true;294}295#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS296inline bool __do_fputc(wchar_t __c, FILE* __fp) {297// fputwc works regardless of wide/narrow mode of stdout, while298// fwrite of wchar_t only works if the stream actually has been set299// into wide mode.300if (fputwc(__c, __fp) == WEOF)301return false;302return true;303}304#endif305306template <class _CharT>307typename __stdoutbuf<_CharT>::int_type __stdoutbuf<_CharT>::overflow(int_type __c) {308char __extbuf[__limit];309char_type __1buf;310if (!traits_type::eq_int_type(__c, traits_type::eof())) {311__1buf = traits_type::to_char_type(__c);312if (__always_noconv_) {313if (!__do_fputc(__1buf, __file_))314return traits_type::eof();315} else {316char* __extbe = __extbuf;317codecvt_base::result __r;318char_type* pbase = &__1buf;319char_type* pptr = pbase + 1;320do {321const char_type* __e;322__r = __cv_->out(*__st_, pbase, pptr, __e, __extbuf, __extbuf + sizeof(__extbuf), __extbe);323if (__e == pbase)324return traits_type::eof();325if (__r == codecvt_base::noconv) {326if (fwrite(pbase, 1, 1, __file_) != 1)327return traits_type::eof();328} else if (__r == codecvt_base::ok || __r == codecvt_base::partial) {329size_t __nmemb = static_cast<size_t>(__extbe - __extbuf);330if (fwrite(__extbuf, 1, __nmemb, __file_) != __nmemb)331return traits_type::eof();332if (__r == codecvt_base::partial) {333pbase = const_cast<char_type*>(__e);334}335} else336return traits_type::eof();337} while (__r == codecvt_base::partial);338}339}340return traits_type::not_eof(__c);341}342343template <class _CharT>344streamsize __stdoutbuf<_CharT>::xsputn(const char_type* __s, streamsize __n) {345// For wchar_t on Windows, don't call fwrite(), but write characters one346// at a time with fputwc(); that works both when stdout is in the default347// mode and when it is set to Unicode mode.348if (__always_noconv_ && !__is_win32api_wide_char)349return fwrite(__s, sizeof(char_type), __n, __file_);350streamsize __i = 0;351for (; __i < __n; ++__i, ++__s)352if (overflow(traits_type::to_int_type(*__s)) == traits_type::eof())353break;354return __i;355}356357template <class _CharT>358int __stdoutbuf<_CharT>::sync() {359char __extbuf[__limit];360codecvt_base::result __r;361do {362char* __extbe;363__r = __cv_->unshift(*__st_, __extbuf, __extbuf + sizeof(__extbuf), __extbe);364size_t __nmemb = static_cast<size_t>(__extbe - __extbuf);365if (fwrite(__extbuf, 1, __nmemb, __file_) != __nmemb)366return -1;367} while (__r == codecvt_base::partial);368if (__r == codecvt_base::error)369return -1;370if (fflush(__file_))371return -1;372return 0;373}374375template <class _CharT>376void __stdoutbuf<_CharT>::imbue(const locale& __loc) {377sync();378__cv_ = &use_facet<codecvt<char_type, char, state_type> >(__loc);379__always_noconv_ = __cv_->always_noconv();380}381382_LIBCPP_END_NAMESPACE_STD383384_LIBCPP_POP_MACROS385386#endif // _LIBCPP_STD_STREAM_H387388389