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