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