2 //===----------------------------------------------------------------------===//
4 // The LLVM Compiler Infrastructure
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 #ifndef _LIBCPP___STD_STREAM
12 #define _LIBCPP___STD_STREAM
20 #include <__undef_min_max>
22 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
23 #pragma GCC system_header
26 _LIBCPP_BEGIN_NAMESPACE_STD
28 static const int __limit = 8;
32 template <class _CharT>
33 class _LIBCPP_HIDDEN __stdinbuf
34 : public basic_streambuf<_CharT, char_traits<_CharT> >
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;
44 explicit __stdinbuf(FILE* __fp);
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);
55 const codecvt<char_type, char, state_type>* __cv_;
58 bool __always_noconv_;
60 __stdinbuf(const __stdinbuf&);
61 __stdinbuf& operator=(const __stdinbuf&);
63 int_type __getchar(bool __consume);
66 template <class _CharT>
67 __stdinbuf<_CharT>::__stdinbuf(FILE* __fp)
71 imbue(this->getloc());
74 template <class _CharT>
76 __stdinbuf<_CharT>::imbue(const locale& __loc)
78 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(__loc);
79 __encoding_ = __cv_->encoding();
80 __always_noconv_ = __cv_->always_noconv();
81 if (__encoding_ > __limit)
82 __throw_runtime_error("unsupported locale for standard input");
85 template <class _CharT>
86 typename __stdinbuf<_CharT>::int_type
87 __stdinbuf<_CharT>::underflow()
89 return __getchar(false);
92 template <class _CharT>
93 typename __stdinbuf<_CharT>::int_type
94 __stdinbuf<_CharT>::uflow()
96 return __getchar(true);
99 template <class _CharT>
100 typename __stdinbuf<_CharT>::int_type
101 __stdinbuf<_CharT>::__getchar(bool __consume)
103 char __extbuf[__limit];
104 int __nread = _VSTD::max(1, __encoding_);
105 for (int __i = 0; __i < __nread; ++__i)
107 int __c = getc(__file_);
109 return traits_type::eof();
110 __extbuf[__i] = static_cast<char>(__c);
113 if (__always_noconv_)
114 __1buf = static_cast<char_type>(__extbuf[0]);
119 codecvt_base::result __r;
122 state_type __sv_st = __st_;
123 __r = __cv_->in(__st_, __extbuf, __extbuf + __nread, __enxt,
124 &__1buf, &__1buf + 1, __inxt);
127 case _VSTD::codecvt_base::ok:
129 case codecvt_base::partial:
131 if (__nread == sizeof(__extbuf))
132 return traits_type::eof();
134 int __c = getc(__file_);
136 return traits_type::eof();
137 __extbuf[__nread] = static_cast<char>(__c);
141 case codecvt_base::error:
142 return traits_type::eof();
143 case _VSTD::codecvt_base::noconv:
144 __1buf = static_cast<char_type>(__extbuf[0]);
147 } while (__r == _VSTD::codecvt_base::partial);
151 for (int __i = __nread; __i > 0;)
153 if (ungetc(__extbuf[--__i], __file_) == EOF)
154 return traits_type::eof();
157 return traits_type::to_int_type(__1buf);
160 template <class _CharT>
161 typename __stdinbuf<_CharT>::int_type
162 __stdinbuf<_CharT>::pbackfail(int_type __c)
164 if (traits_type::eq_int_type(__c, traits_type::eof()))
166 char __extbuf[__limit];
168 const char_type __ci = traits_type::to_char_type(__c);
169 const char_type* __inxt;
170 switch (__cv_->out(__st_, &__ci, &__ci + 1, __inxt,
171 __extbuf, __extbuf + sizeof(__extbuf), __enxt))
173 case _VSTD::codecvt_base::ok:
175 case _VSTD::codecvt_base::noconv:
176 __extbuf[0] = static_cast<char>(__c);
177 __enxt = __extbuf + 1;
179 case codecvt_base::partial:
180 case codecvt_base::error:
181 return traits_type::eof();
183 while (__enxt > __extbuf)
184 if (ungetc(*--__enxt, __file_) == EOF)
185 return traits_type::eof();
186 return traits_type::not_eof(__c);
191 template <class _CharT>
192 class _LIBCPP_HIDDEN __stdoutbuf
193 : public basic_streambuf<_CharT, char_traits<_CharT> >
196 typedef _CharT char_type;
197 typedef char_traits<char_type> traits_type;
198 typedef typename traits_type::int_type int_type;
199 typedef typename traits_type::pos_type pos_type;
200 typedef typename traits_type::off_type off_type;
201 typedef typename traits_type::state_type state_type;
203 explicit __stdoutbuf(FILE* __fp);
206 virtual int_type overflow (int_type __c = traits_type::eof());
208 virtual void imbue(const locale& __loc);
212 const codecvt<char_type, char, state_type>* __cv_;
214 bool __always_noconv_;
216 __stdoutbuf(const __stdoutbuf&);
217 __stdoutbuf& operator=(const __stdoutbuf&);
220 template <class _CharT>
221 __stdoutbuf<_CharT>::__stdoutbuf(FILE* __fp)
223 __cv_(&use_facet<codecvt<char_type, char, state_type> >(this->getloc())),
225 __always_noconv_(__cv_->always_noconv())
229 template <class _CharT>
230 typename __stdoutbuf<_CharT>::int_type
231 __stdoutbuf<_CharT>::overflow(int_type __c)
233 char __extbuf[__limit];
235 if (!traits_type::eq_int_type(__c, traits_type::eof()))
237 this->setp(&__1buf, &__1buf+1);
238 *this->pptr() = traits_type::to_char_type(__c);
240 if (__always_noconv_)
242 if (fwrite(this->pbase(), sizeof(char_type), 1, __file_) != 1)
243 return traits_type::eof();
247 char* __extbe = __extbuf;
248 codecvt_base::result __r;
251 const char_type* __e;
252 __r = __cv_->out(__st_, this->pbase(), this->pptr(), __e,
254 __extbuf + sizeof(__extbuf),
256 if (__e == this->pbase())
257 return traits_type::eof();
258 if (__r == codecvt_base::noconv)
260 if (fwrite(this->pbase(), 1, 1, __file_) != 1)
261 return traits_type::eof();
263 else if (__r == codecvt_base::ok || __r == codecvt_base::partial)
265 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf);
266 if (fwrite(__extbuf, 1, __nmemb, __file_) != __nmemb)
267 return traits_type::eof();
268 if (__r == codecvt_base::partial)
270 this->setp((char_type*)__e, this->pptr());
271 this->pbump(static_cast<int>(this->epptr() - this->pbase()));
275 return traits_type::eof();
276 } while (__r == codecvt_base::partial);
280 return traits_type::not_eof(__c);
283 template <class _CharT>
285 __stdoutbuf<_CharT>::sync()
287 char __extbuf[__limit];
288 codecvt_base::result __r;
292 __r = __cv_->unshift(__st_, __extbuf,
293 __extbuf + sizeof(__extbuf),
295 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf);
296 if (fwrite(__extbuf, 1, __nmemb, __file_) != __nmemb)
298 } while (__r == codecvt_base::partial);
299 if (__r == codecvt_base::error)
306 template <class _CharT>
308 __stdoutbuf<_CharT>::imbue(const locale& __loc)
311 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(__loc);
312 __always_noconv_ = __cv_->always_noconv();
315 _LIBCPP_END_NAMESPACE_STD
317 #endif // _LIBCPP___STD_STREAM