xref: /aosp_15_r20/external/libcxx/include/__std_stream (revision 58b9f456b02922dfdb1fad8a988d5fd8765ecb80)
1*58b9f456SAndroid Build Coastguard Worker// -*- C++ -*-
2*58b9f456SAndroid Build Coastguard Worker//===----------------------------------------------------------------------===//
3*58b9f456SAndroid Build Coastguard Worker//
4*58b9f456SAndroid Build Coastguard Worker//                     The LLVM Compiler Infrastructure
5*58b9f456SAndroid Build Coastguard Worker//
6*58b9f456SAndroid Build Coastguard Worker// This file is dual licensed under the MIT and the University of Illinois Open
7*58b9f456SAndroid Build Coastguard Worker// Source Licenses. See LICENSE.TXT for details.
8*58b9f456SAndroid Build Coastguard Worker//
9*58b9f456SAndroid Build Coastguard Worker//===----------------------------------------------------------------------===//
10*58b9f456SAndroid Build Coastguard Worker
11*58b9f456SAndroid Build Coastguard Worker#ifndef _LIBCPP___STD_STREAM
12*58b9f456SAndroid Build Coastguard Worker#define _LIBCPP___STD_STREAM
13*58b9f456SAndroid Build Coastguard Worker
14*58b9f456SAndroid Build Coastguard Worker#include <__config>
15*58b9f456SAndroid Build Coastguard Worker#include <ostream>
16*58b9f456SAndroid Build Coastguard Worker#include <istream>
17*58b9f456SAndroid Build Coastguard Worker#include <__locale>
18*58b9f456SAndroid Build Coastguard Worker#include <cstdio>
19*58b9f456SAndroid Build Coastguard Worker
20*58b9f456SAndroid Build Coastguard Worker#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
21*58b9f456SAndroid Build Coastguard Worker#pragma GCC system_header
22*58b9f456SAndroid Build Coastguard Worker#endif
23*58b9f456SAndroid Build Coastguard Worker
24*58b9f456SAndroid Build Coastguard Worker_LIBCPP_PUSH_MACROS
25*58b9f456SAndroid Build Coastguard Worker#include <__undef_macros>
26*58b9f456SAndroid Build Coastguard Worker
27*58b9f456SAndroid Build Coastguard Worker
28*58b9f456SAndroid Build Coastguard Worker_LIBCPP_BEGIN_NAMESPACE_STD
29*58b9f456SAndroid Build Coastguard Worker
30*58b9f456SAndroid Build Coastguard Workerstatic const int __limit = 8;
31*58b9f456SAndroid Build Coastguard Worker
32*58b9f456SAndroid Build Coastguard Worker// __stdinbuf
33*58b9f456SAndroid Build Coastguard Worker
34*58b9f456SAndroid Build Coastguard Workertemplate <class _CharT>
35*58b9f456SAndroid Build Coastguard Workerclass _LIBCPP_HIDDEN __stdinbuf
36*58b9f456SAndroid Build Coastguard Worker    : public basic_streambuf<_CharT, char_traits<_CharT> >
37*58b9f456SAndroid Build Coastguard Worker{
38*58b9f456SAndroid Build Coastguard Workerpublic:
39*58b9f456SAndroid Build Coastguard Worker    typedef _CharT                           char_type;
40*58b9f456SAndroid Build Coastguard Worker    typedef char_traits<char_type>           traits_type;
41*58b9f456SAndroid Build Coastguard Worker    typedef typename traits_type::int_type   int_type;
42*58b9f456SAndroid Build Coastguard Worker    typedef typename traits_type::pos_type   pos_type;
43*58b9f456SAndroid Build Coastguard Worker    typedef typename traits_type::off_type   off_type;
44*58b9f456SAndroid Build Coastguard Worker    typedef typename traits_type::state_type state_type;
45*58b9f456SAndroid Build Coastguard Worker
46*58b9f456SAndroid Build Coastguard Worker    __stdinbuf(FILE* __fp, state_type* __st);
47*58b9f456SAndroid Build Coastguard Worker
48*58b9f456SAndroid Build Coastguard Workerprotected:
49*58b9f456SAndroid Build Coastguard Worker    virtual int_type underflow();
50*58b9f456SAndroid Build Coastguard Worker    virtual int_type uflow();
51*58b9f456SAndroid Build Coastguard Worker    virtual int_type pbackfail(int_type __c = traits_type::eof());
52*58b9f456SAndroid Build Coastguard Worker    virtual void imbue(const locale& __loc);
53*58b9f456SAndroid Build Coastguard Worker
54*58b9f456SAndroid Build Coastguard Workerprivate:
55*58b9f456SAndroid Build Coastguard Worker
56*58b9f456SAndroid Build Coastguard Worker    FILE* __file_;
57*58b9f456SAndroid Build Coastguard Worker    const codecvt<char_type, char, state_type>* __cv_;
58*58b9f456SAndroid Build Coastguard Worker    state_type* __st_;
59*58b9f456SAndroid Build Coastguard Worker    int __encoding_;
60*58b9f456SAndroid Build Coastguard Worker    int_type __last_consumed_;
61*58b9f456SAndroid Build Coastguard Worker    bool __last_consumed_is_next_;
62*58b9f456SAndroid Build Coastguard Worker    bool __always_noconv_;
63*58b9f456SAndroid Build Coastguard Worker
64*58b9f456SAndroid Build Coastguard Worker    __stdinbuf(const __stdinbuf&);
65*58b9f456SAndroid Build Coastguard Worker    __stdinbuf& operator=(const __stdinbuf&);
66*58b9f456SAndroid Build Coastguard Worker
67*58b9f456SAndroid Build Coastguard Worker    int_type __getchar(bool __consume);
68*58b9f456SAndroid Build Coastguard Worker};
69*58b9f456SAndroid Build Coastguard Worker
70*58b9f456SAndroid Build Coastguard Workertemplate <class _CharT>
71*58b9f456SAndroid Build Coastguard Worker__stdinbuf<_CharT>::__stdinbuf(FILE* __fp, state_type* __st)
72*58b9f456SAndroid Build Coastguard Worker    : __file_(__fp),
73*58b9f456SAndroid Build Coastguard Worker      __st_(__st),
74*58b9f456SAndroid Build Coastguard Worker      __last_consumed_(traits_type::eof()),
75*58b9f456SAndroid Build Coastguard Worker      __last_consumed_is_next_(false)
76*58b9f456SAndroid Build Coastguard Worker{
77*58b9f456SAndroid Build Coastguard Worker    imbue(this->getloc());
78*58b9f456SAndroid Build Coastguard Worker}
79*58b9f456SAndroid Build Coastguard Worker
80*58b9f456SAndroid Build Coastguard Workertemplate <class _CharT>
81*58b9f456SAndroid Build Coastguard Workervoid
82*58b9f456SAndroid Build Coastguard Worker__stdinbuf<_CharT>::imbue(const locale& __loc)
83*58b9f456SAndroid Build Coastguard Worker{
84*58b9f456SAndroid Build Coastguard Worker    __cv_ = &use_facet<codecvt<char_type, char, state_type> >(__loc);
85*58b9f456SAndroid Build Coastguard Worker    __encoding_ = __cv_->encoding();
86*58b9f456SAndroid Build Coastguard Worker    __always_noconv_ = __cv_->always_noconv();
87*58b9f456SAndroid Build Coastguard Worker    if (__encoding_ > __limit)
88*58b9f456SAndroid Build Coastguard Worker        __throw_runtime_error("unsupported locale for standard input");
89*58b9f456SAndroid Build Coastguard Worker}
90*58b9f456SAndroid Build Coastguard Worker
91*58b9f456SAndroid Build Coastguard Workertemplate <class _CharT>
92*58b9f456SAndroid Build Coastguard Workertypename __stdinbuf<_CharT>::int_type
93*58b9f456SAndroid Build Coastguard Worker__stdinbuf<_CharT>::underflow()
94*58b9f456SAndroid Build Coastguard Worker{
95*58b9f456SAndroid Build Coastguard Worker    return __getchar(false);
96*58b9f456SAndroid Build Coastguard Worker}
97*58b9f456SAndroid Build Coastguard Worker
98*58b9f456SAndroid Build Coastguard Workertemplate <class _CharT>
99*58b9f456SAndroid Build Coastguard Workertypename __stdinbuf<_CharT>::int_type
100*58b9f456SAndroid Build Coastguard Worker__stdinbuf<_CharT>::uflow()
101*58b9f456SAndroid Build Coastguard Worker{
102*58b9f456SAndroid Build Coastguard Worker    return __getchar(true);
103*58b9f456SAndroid Build Coastguard Worker}
104*58b9f456SAndroid Build Coastguard Worker
105*58b9f456SAndroid Build Coastguard Workertemplate <class _CharT>
106*58b9f456SAndroid Build Coastguard Workertypename __stdinbuf<_CharT>::int_type
107*58b9f456SAndroid Build Coastguard Worker__stdinbuf<_CharT>::__getchar(bool __consume)
108*58b9f456SAndroid Build Coastguard Worker{
109*58b9f456SAndroid Build Coastguard Worker    if (__last_consumed_is_next_)
110*58b9f456SAndroid Build Coastguard Worker    {
111*58b9f456SAndroid Build Coastguard Worker        int_type __result = __last_consumed_;
112*58b9f456SAndroid Build Coastguard Worker        if (__consume)
113*58b9f456SAndroid Build Coastguard Worker        {
114*58b9f456SAndroid Build Coastguard Worker            __last_consumed_ = traits_type::eof();
115*58b9f456SAndroid Build Coastguard Worker            __last_consumed_is_next_ = false;
116*58b9f456SAndroid Build Coastguard Worker        }
117*58b9f456SAndroid Build Coastguard Worker        return __result;
118*58b9f456SAndroid Build Coastguard Worker    }
119*58b9f456SAndroid Build Coastguard Worker    char __extbuf[__limit];
120*58b9f456SAndroid Build Coastguard Worker    int __nread = _VSTD::max(1, __encoding_);
121*58b9f456SAndroid Build Coastguard Worker    for (int __i = 0; __i < __nread; ++__i)
122*58b9f456SAndroid Build Coastguard Worker    {
123*58b9f456SAndroid Build Coastguard Worker        int __c = getc(__file_);
124*58b9f456SAndroid Build Coastguard Worker        if (__c == EOF)
125*58b9f456SAndroid Build Coastguard Worker            return traits_type::eof();
126*58b9f456SAndroid Build Coastguard Worker        __extbuf[__i] = static_cast<char>(__c);
127*58b9f456SAndroid Build Coastguard Worker    }
128*58b9f456SAndroid Build Coastguard Worker    char_type __1buf;
129*58b9f456SAndroid Build Coastguard Worker    if (__always_noconv_)
130*58b9f456SAndroid Build Coastguard Worker        __1buf = static_cast<char_type>(__extbuf[0]);
131*58b9f456SAndroid Build Coastguard Worker    else
132*58b9f456SAndroid Build Coastguard Worker    {
133*58b9f456SAndroid Build Coastguard Worker        const char* __enxt;
134*58b9f456SAndroid Build Coastguard Worker        char_type* __inxt;
135*58b9f456SAndroid Build Coastguard Worker        codecvt_base::result __r;
136*58b9f456SAndroid Build Coastguard Worker        do
137*58b9f456SAndroid Build Coastguard Worker        {
138*58b9f456SAndroid Build Coastguard Worker            state_type __sv_st = *__st_;
139*58b9f456SAndroid Build Coastguard Worker            __r = __cv_->in(*__st_, __extbuf, __extbuf + __nread, __enxt,
140*58b9f456SAndroid Build Coastguard Worker                                   &__1buf, &__1buf + 1, __inxt);
141*58b9f456SAndroid Build Coastguard Worker            switch (__r)
142*58b9f456SAndroid Build Coastguard Worker            {
143*58b9f456SAndroid Build Coastguard Worker            case _VSTD::codecvt_base::ok:
144*58b9f456SAndroid Build Coastguard Worker                break;
145*58b9f456SAndroid Build Coastguard Worker            case codecvt_base::partial:
146*58b9f456SAndroid Build Coastguard Worker                *__st_ = __sv_st;
147*58b9f456SAndroid Build Coastguard Worker                if (__nread == sizeof(__extbuf))
148*58b9f456SAndroid Build Coastguard Worker                    return traits_type::eof();
149*58b9f456SAndroid Build Coastguard Worker                {
150*58b9f456SAndroid Build Coastguard Worker                    int __c = getc(__file_);
151*58b9f456SAndroid Build Coastguard Worker                    if (__c == EOF)
152*58b9f456SAndroid Build Coastguard Worker                        return traits_type::eof();
153*58b9f456SAndroid Build Coastguard Worker                    __extbuf[__nread] = static_cast<char>(__c);
154*58b9f456SAndroid Build Coastguard Worker                }
155*58b9f456SAndroid Build Coastguard Worker                ++__nread;
156*58b9f456SAndroid Build Coastguard Worker                break;
157*58b9f456SAndroid Build Coastguard Worker            case codecvt_base::error:
158*58b9f456SAndroid Build Coastguard Worker                return traits_type::eof();
159*58b9f456SAndroid Build Coastguard Worker            case _VSTD::codecvt_base::noconv:
160*58b9f456SAndroid Build Coastguard Worker                __1buf = static_cast<char_type>(__extbuf[0]);
161*58b9f456SAndroid Build Coastguard Worker                break;
162*58b9f456SAndroid Build Coastguard Worker            }
163*58b9f456SAndroid Build Coastguard Worker        } while (__r == _VSTD::codecvt_base::partial);
164*58b9f456SAndroid Build Coastguard Worker    }
165*58b9f456SAndroid Build Coastguard Worker    if (!__consume)
166*58b9f456SAndroid Build Coastguard Worker    {
167*58b9f456SAndroid Build Coastguard Worker        for (int __i = __nread; __i > 0;)
168*58b9f456SAndroid Build Coastguard Worker        {
169*58b9f456SAndroid Build Coastguard Worker            if (ungetc(traits_type::to_int_type(__extbuf[--__i]), __file_) == EOF)
170*58b9f456SAndroid Build Coastguard Worker                return traits_type::eof();
171*58b9f456SAndroid Build Coastguard Worker        }
172*58b9f456SAndroid Build Coastguard Worker    }
173*58b9f456SAndroid Build Coastguard Worker    else
174*58b9f456SAndroid Build Coastguard Worker        __last_consumed_ = traits_type::to_int_type(__1buf);
175*58b9f456SAndroid Build Coastguard Worker    return traits_type::to_int_type(__1buf);
176*58b9f456SAndroid Build Coastguard Worker}
177*58b9f456SAndroid Build Coastguard Worker
178*58b9f456SAndroid Build Coastguard Workertemplate <class _CharT>
179*58b9f456SAndroid Build Coastguard Workertypename __stdinbuf<_CharT>::int_type
180*58b9f456SAndroid Build Coastguard Worker__stdinbuf<_CharT>::pbackfail(int_type __c)
181*58b9f456SAndroid Build Coastguard Worker{
182*58b9f456SAndroid Build Coastguard Worker    if (traits_type::eq_int_type(__c, traits_type::eof()))
183*58b9f456SAndroid Build Coastguard Worker    {
184*58b9f456SAndroid Build Coastguard Worker        if (!__last_consumed_is_next_)
185*58b9f456SAndroid Build Coastguard Worker        {
186*58b9f456SAndroid Build Coastguard Worker            __c = __last_consumed_;
187*58b9f456SAndroid Build Coastguard Worker            __last_consumed_is_next_ = !traits_type::eq_int_type(__last_consumed_,
188*58b9f456SAndroid Build Coastguard Worker                                                                 traits_type::eof());
189*58b9f456SAndroid Build Coastguard Worker        }
190*58b9f456SAndroid Build Coastguard Worker        return __c;
191*58b9f456SAndroid Build Coastguard Worker    }
192*58b9f456SAndroid Build Coastguard Worker    if (__last_consumed_is_next_)
193*58b9f456SAndroid Build Coastguard Worker    {
194*58b9f456SAndroid Build Coastguard Worker        char __extbuf[__limit];
195*58b9f456SAndroid Build Coastguard Worker        char* __enxt;
196*58b9f456SAndroid Build Coastguard Worker        const char_type __ci = traits_type::to_char_type(__last_consumed_);
197*58b9f456SAndroid Build Coastguard Worker        const char_type* __inxt;
198*58b9f456SAndroid Build Coastguard Worker        switch (__cv_->out(*__st_, &__ci, &__ci + 1, __inxt,
199*58b9f456SAndroid Build Coastguard Worker                                  __extbuf, __extbuf + sizeof(__extbuf), __enxt))
200*58b9f456SAndroid Build Coastguard Worker        {
201*58b9f456SAndroid Build Coastguard Worker        case _VSTD::codecvt_base::ok:
202*58b9f456SAndroid Build Coastguard Worker            break;
203*58b9f456SAndroid Build Coastguard Worker        case _VSTD::codecvt_base::noconv:
204*58b9f456SAndroid Build Coastguard Worker            __extbuf[0] = static_cast<char>(__last_consumed_);
205*58b9f456SAndroid Build Coastguard Worker            __enxt = __extbuf + 1;
206*58b9f456SAndroid Build Coastguard Worker            break;
207*58b9f456SAndroid Build Coastguard Worker        case codecvt_base::partial:
208*58b9f456SAndroid Build Coastguard Worker        case codecvt_base::error:
209*58b9f456SAndroid Build Coastguard Worker            return traits_type::eof();
210*58b9f456SAndroid Build Coastguard Worker        }
211*58b9f456SAndroid Build Coastguard Worker        while (__enxt > __extbuf)
212*58b9f456SAndroid Build Coastguard Worker            if (ungetc(*--__enxt, __file_) == EOF)
213*58b9f456SAndroid Build Coastguard Worker                return traits_type::eof();
214*58b9f456SAndroid Build Coastguard Worker    }
215*58b9f456SAndroid Build Coastguard Worker    __last_consumed_ = __c;
216*58b9f456SAndroid Build Coastguard Worker    __last_consumed_is_next_ = true;
217*58b9f456SAndroid Build Coastguard Worker    return __c;
218*58b9f456SAndroid Build Coastguard Worker}
219*58b9f456SAndroid Build Coastguard Worker
220*58b9f456SAndroid Build Coastguard Worker// __stdoutbuf
221*58b9f456SAndroid Build Coastguard Worker
222*58b9f456SAndroid Build Coastguard Workertemplate <class _CharT>
223*58b9f456SAndroid Build Coastguard Workerclass _LIBCPP_HIDDEN __stdoutbuf
224*58b9f456SAndroid Build Coastguard Worker    : public basic_streambuf<_CharT, char_traits<_CharT> >
225*58b9f456SAndroid Build Coastguard Worker{
226*58b9f456SAndroid Build Coastguard Workerpublic:
227*58b9f456SAndroid Build Coastguard Worker    typedef _CharT                           char_type;
228*58b9f456SAndroid Build Coastguard Worker    typedef char_traits<char_type>           traits_type;
229*58b9f456SAndroid Build Coastguard Worker    typedef typename traits_type::int_type   int_type;
230*58b9f456SAndroid Build Coastguard Worker    typedef typename traits_type::pos_type   pos_type;
231*58b9f456SAndroid Build Coastguard Worker    typedef typename traits_type::off_type   off_type;
232*58b9f456SAndroid Build Coastguard Worker    typedef typename traits_type::state_type state_type;
233*58b9f456SAndroid Build Coastguard Worker
234*58b9f456SAndroid Build Coastguard Worker    __stdoutbuf(FILE* __fp, state_type* __st);
235*58b9f456SAndroid Build Coastguard Worker
236*58b9f456SAndroid Build Coastguard Workerprotected:
237*58b9f456SAndroid Build Coastguard Worker    virtual int_type overflow (int_type __c = traits_type::eof());
238*58b9f456SAndroid Build Coastguard Worker    virtual streamsize xsputn(const char_type* __s, streamsize __n);
239*58b9f456SAndroid Build Coastguard Worker    virtual int sync();
240*58b9f456SAndroid Build Coastguard Worker    virtual void imbue(const locale& __loc);
241*58b9f456SAndroid Build Coastguard Worker
242*58b9f456SAndroid Build Coastguard Workerprivate:
243*58b9f456SAndroid Build Coastguard Worker    FILE* __file_;
244*58b9f456SAndroid Build Coastguard Worker    const codecvt<char_type, char, state_type>* __cv_;
245*58b9f456SAndroid Build Coastguard Worker    state_type* __st_;
246*58b9f456SAndroid Build Coastguard Worker    bool __always_noconv_;
247*58b9f456SAndroid Build Coastguard Worker
248*58b9f456SAndroid Build Coastguard Worker    __stdoutbuf(const __stdoutbuf&);
249*58b9f456SAndroid Build Coastguard Worker    __stdoutbuf& operator=(const __stdoutbuf&);
250*58b9f456SAndroid Build Coastguard Worker};
251*58b9f456SAndroid Build Coastguard Worker
252*58b9f456SAndroid Build Coastguard Workertemplate <class _CharT>
253*58b9f456SAndroid Build Coastguard Worker__stdoutbuf<_CharT>::__stdoutbuf(FILE* __fp, state_type* __st)
254*58b9f456SAndroid Build Coastguard Worker    : __file_(__fp),
255*58b9f456SAndroid Build Coastguard Worker      __cv_(&use_facet<codecvt<char_type, char, state_type> >(this->getloc())),
256*58b9f456SAndroid Build Coastguard Worker      __st_(__st),
257*58b9f456SAndroid Build Coastguard Worker      __always_noconv_(__cv_->always_noconv())
258*58b9f456SAndroid Build Coastguard Worker{
259*58b9f456SAndroid Build Coastguard Worker}
260*58b9f456SAndroid Build Coastguard Worker
261*58b9f456SAndroid Build Coastguard Workertemplate <class _CharT>
262*58b9f456SAndroid Build Coastguard Workertypename __stdoutbuf<_CharT>::int_type
263*58b9f456SAndroid Build Coastguard Worker__stdoutbuf<_CharT>::overflow(int_type __c)
264*58b9f456SAndroid Build Coastguard Worker{
265*58b9f456SAndroid Build Coastguard Worker    char __extbuf[__limit];
266*58b9f456SAndroid Build Coastguard Worker    char_type __1buf;
267*58b9f456SAndroid Build Coastguard Worker    if (!traits_type::eq_int_type(__c, traits_type::eof()))
268*58b9f456SAndroid Build Coastguard Worker    {
269*58b9f456SAndroid Build Coastguard Worker        __1buf = traits_type::to_char_type(__c);
270*58b9f456SAndroid Build Coastguard Worker        if (__always_noconv_)
271*58b9f456SAndroid Build Coastguard Worker        {
272*58b9f456SAndroid Build Coastguard Worker            if (fwrite(&__1buf, sizeof(char_type), 1, __file_) != 1)
273*58b9f456SAndroid Build Coastguard Worker                return traits_type::eof();
274*58b9f456SAndroid Build Coastguard Worker        }
275*58b9f456SAndroid Build Coastguard Worker        else
276*58b9f456SAndroid Build Coastguard Worker        {
277*58b9f456SAndroid Build Coastguard Worker            char* __extbe = __extbuf;
278*58b9f456SAndroid Build Coastguard Worker            codecvt_base::result __r;
279*58b9f456SAndroid Build Coastguard Worker            char_type* pbase = &__1buf;
280*58b9f456SAndroid Build Coastguard Worker            char_type* pptr = pbase + 1;
281*58b9f456SAndroid Build Coastguard Worker            do
282*58b9f456SAndroid Build Coastguard Worker            {
283*58b9f456SAndroid Build Coastguard Worker                const char_type* __e;
284*58b9f456SAndroid Build Coastguard Worker                __r = __cv_->out(*__st_, pbase, pptr, __e,
285*58b9f456SAndroid Build Coastguard Worker                                        __extbuf,
286*58b9f456SAndroid Build Coastguard Worker                                        __extbuf + sizeof(__extbuf),
287*58b9f456SAndroid Build Coastguard Worker                                        __extbe);
288*58b9f456SAndroid Build Coastguard Worker                if (__e == pbase)
289*58b9f456SAndroid Build Coastguard Worker                    return traits_type::eof();
290*58b9f456SAndroid Build Coastguard Worker                if (__r == codecvt_base::noconv)
291*58b9f456SAndroid Build Coastguard Worker                {
292*58b9f456SAndroid Build Coastguard Worker                    if (fwrite(pbase, 1, 1, __file_) != 1)
293*58b9f456SAndroid Build Coastguard Worker                        return traits_type::eof();
294*58b9f456SAndroid Build Coastguard Worker                }
295*58b9f456SAndroid Build Coastguard Worker                else if (__r == codecvt_base::ok || __r == codecvt_base::partial)
296*58b9f456SAndroid Build Coastguard Worker                {
297*58b9f456SAndroid Build Coastguard Worker                    size_t __nmemb = static_cast<size_t>(__extbe - __extbuf);
298*58b9f456SAndroid Build Coastguard Worker                    if (fwrite(__extbuf, 1, __nmemb, __file_) != __nmemb)
299*58b9f456SAndroid Build Coastguard Worker                        return traits_type::eof();
300*58b9f456SAndroid Build Coastguard Worker                    if (__r == codecvt_base::partial)
301*58b9f456SAndroid Build Coastguard Worker                    {
302*58b9f456SAndroid Build Coastguard Worker                        pbase = const_cast<char_type*>(__e);
303*58b9f456SAndroid Build Coastguard Worker                    }
304*58b9f456SAndroid Build Coastguard Worker                }
305*58b9f456SAndroid Build Coastguard Worker                else
306*58b9f456SAndroid Build Coastguard Worker                    return traits_type::eof();
307*58b9f456SAndroid Build Coastguard Worker            } while (__r == codecvt_base::partial);
308*58b9f456SAndroid Build Coastguard Worker        }
309*58b9f456SAndroid Build Coastguard Worker    }
310*58b9f456SAndroid Build Coastguard Worker    return traits_type::not_eof(__c);
311*58b9f456SAndroid Build Coastguard Worker}
312*58b9f456SAndroid Build Coastguard Worker
313*58b9f456SAndroid Build Coastguard Workertemplate <class _CharT>
314*58b9f456SAndroid Build Coastguard Workerstreamsize
315*58b9f456SAndroid Build Coastguard Worker__stdoutbuf<_CharT>::xsputn(const char_type* __s, streamsize __n)
316*58b9f456SAndroid Build Coastguard Worker{
317*58b9f456SAndroid Build Coastguard Worker    if (__always_noconv_)
318*58b9f456SAndroid Build Coastguard Worker        return fwrite(__s, sizeof(char_type), __n, __file_);
319*58b9f456SAndroid Build Coastguard Worker    streamsize __i = 0;
320*58b9f456SAndroid Build Coastguard Worker    for (; __i < __n; ++__i, ++__s)
321*58b9f456SAndroid Build Coastguard Worker        if (overflow(traits_type::to_int_type(*__s)) == traits_type::eof())
322*58b9f456SAndroid Build Coastguard Worker            break;
323*58b9f456SAndroid Build Coastguard Worker    return __i;
324*58b9f456SAndroid Build Coastguard Worker}
325*58b9f456SAndroid Build Coastguard Worker
326*58b9f456SAndroid Build Coastguard Workertemplate <class _CharT>
327*58b9f456SAndroid Build Coastguard Workerint
328*58b9f456SAndroid Build Coastguard Worker__stdoutbuf<_CharT>::sync()
329*58b9f456SAndroid Build Coastguard Worker{
330*58b9f456SAndroid Build Coastguard Worker    char __extbuf[__limit];
331*58b9f456SAndroid Build Coastguard Worker    codecvt_base::result __r;
332*58b9f456SAndroid Build Coastguard Worker    do
333*58b9f456SAndroid Build Coastguard Worker    {
334*58b9f456SAndroid Build Coastguard Worker        char* __extbe;
335*58b9f456SAndroid Build Coastguard Worker        __r = __cv_->unshift(*__st_, __extbuf,
336*58b9f456SAndroid Build Coastguard Worker                                    __extbuf + sizeof(__extbuf),
337*58b9f456SAndroid Build Coastguard Worker                                    __extbe);
338*58b9f456SAndroid Build Coastguard Worker        size_t __nmemb = static_cast<size_t>(__extbe - __extbuf);
339*58b9f456SAndroid Build Coastguard Worker        if (fwrite(__extbuf, 1, __nmemb, __file_) != __nmemb)
340*58b9f456SAndroid Build Coastguard Worker            return -1;
341*58b9f456SAndroid Build Coastguard Worker    } while (__r == codecvt_base::partial);
342*58b9f456SAndroid Build Coastguard Worker    if (__r == codecvt_base::error)
343*58b9f456SAndroid Build Coastguard Worker        return -1;
344*58b9f456SAndroid Build Coastguard Worker    if (fflush(__file_))
345*58b9f456SAndroid Build Coastguard Worker        return -1;
346*58b9f456SAndroid Build Coastguard Worker    return 0;
347*58b9f456SAndroid Build Coastguard Worker}
348*58b9f456SAndroid Build Coastguard Worker
349*58b9f456SAndroid Build Coastguard Workertemplate <class _CharT>
350*58b9f456SAndroid Build Coastguard Workervoid
351*58b9f456SAndroid Build Coastguard Worker__stdoutbuf<_CharT>::imbue(const locale& __loc)
352*58b9f456SAndroid Build Coastguard Worker{
353*58b9f456SAndroid Build Coastguard Worker    sync();
354*58b9f456SAndroid Build Coastguard Worker    __cv_ = &use_facet<codecvt<char_type, char, state_type> >(__loc);
355*58b9f456SAndroid Build Coastguard Worker    __always_noconv_ = __cv_->always_noconv();
356*58b9f456SAndroid Build Coastguard Worker}
357*58b9f456SAndroid Build Coastguard Worker
358*58b9f456SAndroid Build Coastguard Worker_LIBCPP_END_NAMESPACE_STD
359*58b9f456SAndroid Build Coastguard Worker
360*58b9f456SAndroid Build Coastguard Worker_LIBCPP_POP_MACROS
361*58b9f456SAndroid Build Coastguard Worker
362*58b9f456SAndroid Build Coastguard Worker#endif  // _LIBCPP___STD_STREAM
363