Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes15kdownloads
__std_stream366 linesDownload Raw Back to include
1// -*- C++ -*-2//===----------------------------------------------------------------------===//3//4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.5// See https://llvm.org/LICENSE.txt for license information.6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception7//8//===----------------------------------------------------------------------===//9 10#ifndef _LIBCUDACXX___STD_STREAM11#define _LIBCUDACXX___STD_STREAM12 13#include <__config>14#include <ostream>15#include <istream>16#include <__locale>17#include <cstdio>18 19#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC)20#  pragma GCC system_header21#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG)22#  pragma clang system_header23#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC)24#  pragma system_header25#endif // no system header26 27_LIBCUDACXX_PUSH_MACROS28#include <__undef_macros>29 30 31_LIBCUDACXX_BEGIN_NAMESPACE_STD32 33static const int __limit = 8;34 35// __stdinbuf36 37template <class _CharT>38class _LIBCUDACXX_HIDDEN __stdinbuf39    : public basic_streambuf<_CharT, char_traits<_CharT> >40{41public:42    typedef _CharT                           char_type;43    typedef char_traits<char_type>           traits_type;44    typedef typename traits_type::int_type   int_type;45    typedef typename traits_type::pos_type   pos_type;46    typedef typename traits_type::off_type   off_type;47    typedef typename traits_type::state_type state_type;48 49    __stdinbuf(FILE* __fp, state_type* __st);50 51protected:52    virtual int_type underflow();53    virtual int_type uflow();54    virtual int_type pbackfail(int_type __c = traits_type::eof());55    virtual void imbue(const locale& __loc);56 57private:58 59    FILE* __file_;60    const codecvt<char_type, char, state_type>* __cv_;61    state_type* __st_;62    int __encoding_;63    int_type __last_consumed_;64    bool __last_consumed_is_next_;65    bool __always_noconv_;66 67    __stdinbuf(const __stdinbuf&);68    __stdinbuf& operator=(const __stdinbuf&);69 70    int_type __getchar(bool __consume);71};72 73template <class _CharT>74__stdinbuf<_CharT>::__stdinbuf(FILE* __fp, state_type* __st)75    : __file_(__fp),76      __st_(__st),77      __last_consumed_(traits_type::eof()),78      __last_consumed_is_next_(false)79{80    imbue(this->getloc());81}82 83template <class _CharT>84void85__stdinbuf<_CharT>::imbue(const locale& __loc)86{87    __cv_ = &use_facet<codecvt<char_type, char, state_type> >(__loc);88    __encoding_ = __cv_->encoding();89    __always_noconv_ = __cv_->always_noconv();90    if (__encoding_ > __limit)91        __throw_runtime_error("unsupported locale for standard input");92}93 94template <class _CharT>95typename __stdinbuf<_CharT>::int_type96__stdinbuf<_CharT>::underflow()97{98    return __getchar(false);99}100 101template <class _CharT>102typename __stdinbuf<_CharT>::int_type103__stdinbuf<_CharT>::uflow()104{105    return __getchar(true);106}107 108template <class _CharT>109typename __stdinbuf<_CharT>::int_type110__stdinbuf<_CharT>::__getchar(bool __consume)111{112    if (__last_consumed_is_next_)113    {114        int_type __result = __last_consumed_;115        if (__consume)116        {117            __last_consumed_ = traits_type::eof();118            __last_consumed_is_next_ = false;119        }120        return __result;121    }122    char __extbuf[__limit];123    int __nread = _CUDA_VSTD::max(1, __encoding_);124    for (int __i = 0; __i < __nread; ++__i)125    {126        int __c = getc(__file_);127        if (__c == EOF)128            return traits_type::eof();129        __extbuf[__i] = static_cast<char>(__c);130    }131    char_type __1buf;132    if (__always_noconv_)133        __1buf = static_cast<char_type>(__extbuf[0]);134    else135    {136        const char* __enxt;137        char_type* __inxt;138        codecvt_base::result __r;139        do140        {141            state_type __sv_st = *__st_;142            __r = __cv_->in(*__st_, __extbuf, __extbuf + __nread, __enxt,143                                   &__1buf, &__1buf + 1, __inxt);144            switch (__r)145            {146            case _CUDA_VSTD::codecvt_base::ok:147                break;148            case codecvt_base::partial:149                *__st_ = __sv_st;150                if (__nread == sizeof(__extbuf))151                    return traits_type::eof();152                {153                    int __c = getc(__file_);154                    if (__c == EOF)155                        return traits_type::eof();156                    __extbuf[__nread] = static_cast<char>(__c);157                }158                ++__nread;159                break;160            case codecvt_base::error:161                return traits_type::eof();162            case _CUDA_VSTD::codecvt_base::noconv:163                __1buf = static_cast<char_type>(__extbuf[0]);164                break;165            }166        } while (__r == _CUDA_VSTD::codecvt_base::partial);167    }168    if (!__consume)169    {170        for (int __i = __nread; __i > 0;)171        {172            if (ungetc(traits_type::to_int_type(__extbuf[--__i]), __file_) == EOF)173                return traits_type::eof();174        }175    }176    else177        __last_consumed_ = traits_type::to_int_type(__1buf);178    return traits_type::to_int_type(__1buf);179}180 181template <class _CharT>182typename __stdinbuf<_CharT>::int_type183__stdinbuf<_CharT>::pbackfail(int_type __c)184{185    if (traits_type::eq_int_type(__c, traits_type::eof()))186    {187        if (!__last_consumed_is_next_)188        {189            __c = __last_consumed_;190            __last_consumed_is_next_ = !traits_type::eq_int_type(__last_consumed_,191                                                                 traits_type::eof());192        }193        return __c;194    }195    if (__last_consumed_is_next_)196    {197        char __extbuf[__limit];198        char* __enxt;199        const char_type __ci = traits_type::to_char_type(__last_consumed_);200        const char_type* __inxt;201        switch (__cv_->out(*__st_, &__ci, &__ci + 1, __inxt,202                                  __extbuf, __extbuf + sizeof(__extbuf), __enxt))203        {204        case _CUDA_VSTD::codecvt_base::ok:205            break;206        case _CUDA_VSTD::codecvt_base::noconv:207            __extbuf[0] = static_cast<char>(__last_consumed_);208            __enxt = __extbuf + 1;209            break;210        case codecvt_base::partial:211        case codecvt_base::error:212            return traits_type::eof();213        }214        while (__enxt > __extbuf)215            if (ungetc(*--__enxt, __file_) == EOF)216                return traits_type::eof();217    }218    __last_consumed_ = __c;219    __last_consumed_is_next_ = true;220    return __c;221}222 223// __stdoutbuf224 225template <class _CharT>226class _LIBCUDACXX_HIDDEN __stdoutbuf227    : public basic_streambuf<_CharT, char_traits<_CharT> >228{229public:230    typedef _CharT                           char_type;231    typedef char_traits<char_type>           traits_type;232    typedef typename traits_type::int_type   int_type;233    typedef typename traits_type::pos_type   pos_type;234    typedef typename traits_type::off_type   off_type;235    typedef typename traits_type::state_type state_type;236 237    __stdoutbuf(FILE* __fp, state_type* __st);238 239protected:240    virtual int_type overflow (int_type __c = traits_type::eof());241    virtual streamsize xsputn(const char_type* __s, streamsize __n);242    virtual int sync();243    virtual void imbue(const locale& __loc);244 245private:246    FILE* __file_;247    const codecvt<char_type, char, state_type>* __cv_;248    state_type* __st_;249    bool __always_noconv_;250 251    __stdoutbuf(const __stdoutbuf&);252    __stdoutbuf& operator=(const __stdoutbuf&);253};254 255template <class _CharT>256__stdoutbuf<_CharT>::__stdoutbuf(FILE* __fp, state_type* __st)257    : __file_(__fp),258      __cv_(&use_facet<codecvt<char_type, char, state_type> >(this->getloc())),259      __st_(__st),260      __always_noconv_(__cv_->always_noconv())261{262}263 264template <class _CharT>265typename __stdoutbuf<_CharT>::int_type266__stdoutbuf<_CharT>::overflow(int_type __c)267{268    char __extbuf[__limit];269    char_type __1buf;270    if (!traits_type::eq_int_type(__c, traits_type::eof()))271    {272        __1buf = traits_type::to_char_type(__c);273        if (__always_noconv_)274        {275            if (fwrite(&__1buf, sizeof(char_type), 1, __file_) != 1)276                return traits_type::eof();277        }278        else279        {280            char* __extbe = __extbuf;281            codecvt_base::result __r;282            char_type* pbase = &__1buf;283            char_type* pptr = pbase + 1;284            do285            {286                const char_type* __e;287                __r = __cv_->out(*__st_, pbase, pptr, __e,288                                        __extbuf,289                                        __extbuf + sizeof(__extbuf),290                                        __extbe);291                if (__e == pbase)292                    return traits_type::eof();293                if (__r == codecvt_base::noconv)294                {295                    if (fwrite(pbase, 1, 1, __file_) != 1)296                        return traits_type::eof();297                }298                else if (__r == codecvt_base::ok || __r == codecvt_base::partial)299                {300                    size_t __nmemb = static_cast<size_t>(__extbe - __extbuf);301                    if (fwrite(__extbuf, 1, __nmemb, __file_) != __nmemb)302                        return traits_type::eof();303                    if (__r == codecvt_base::partial)304                    {305                        pbase = const_cast<char_type*>(__e);306                    }307                }308                else309                    return traits_type::eof();310            } while (__r == codecvt_base::partial);311        }312    }313    return traits_type::not_eof(__c);314}315 316template <class _CharT>317streamsize318__stdoutbuf<_CharT>::xsputn(const char_type* __s, streamsize __n)319{320    if (__always_noconv_)321        return fwrite(__s, sizeof(char_type), __n, __file_);322    streamsize __i = 0;323    for (; __i < __n; ++__i, ++__s)324        if (overflow(traits_type::to_int_type(*__s)) == traits_type::eof())325            break;326    return __i;327}328 329template <class _CharT>330int331__stdoutbuf<_CharT>::sync()332{333    char __extbuf[__limit];334    codecvt_base::result __r;335    do336    {337        char* __extbe;338        __r = __cv_->unshift(*__st_, __extbuf,339                                    __extbuf + sizeof(__extbuf),340                                    __extbe);341        size_t __nmemb = static_cast<size_t>(__extbe - __extbuf);342        if (fwrite(__extbuf, 1, __nmemb, __file_) != __nmemb)343            return -1;344    } while (__r == codecvt_base::partial);345    if (__r == codecvt_base::error)346        return -1;347    if (fflush(__file_))348        return -1;349    return 0;350}351 352template <class _CharT>353void354__stdoutbuf<_CharT>::imbue(const locale& __loc)355{356    sync();357    __cv_ = &use_facet<codecvt<char_type, char, state_type> >(__loc);358    __always_noconv_ = __cv_->always_noconv();359}360 361_LIBCUDACXX_END_NAMESPACE_STD362 363_LIBCUDACXX_POP_MACROS364 365#endif  // _LIBCUDACXX___STD_STREAM366 
codekingpro/portable-devtools · Team Ai