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 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
21 #pragma GCC system_header
25 #include <__undef_macros>
28 _LIBCPP_BEGIN_NAMESPACE_STD
30 static const int __limit = 8;
34 template <class _CharT>
35 class _LIBCPP_HIDDEN __stdinbuf
36 : public basic_streambuf<_CharT, char_traits<_CharT> >
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;
46 __stdinbuf(FILE* __fp, state_type* __st);
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);
57 const codecvt<char_type, char, state_type>* __cv_;
60 int_type __last_consumed_;
61 bool __last_consumed_is_next_;
62 bool __always_noconv_;
64 __stdinbuf(const __stdinbuf&);
65 __stdinbuf& operator=(const __stdinbuf&);
67 int_type __getchar(bool __consume);
70 template <class _CharT>
71 __stdinbuf<_CharT>::__stdinbuf(FILE* __fp, state_type* __st)
74 __last_consumed_(traits_type::eof()),
75 __last_consumed_is_next_(false)
77 imbue(this->getloc());
80 template <class _CharT>
82 __stdinbuf<_CharT>::imbue(const locale& __loc)
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");
91 template <class _CharT>
92 typename __stdinbuf<_CharT>::int_type
93 __stdinbuf<_CharT>::underflow()
95 return __getchar(false);
98 template <class _CharT>
99 typename __stdinbuf<_CharT>::int_type
100 __stdinbuf<_CharT>::uflow()
102 return __getchar(true);
105 template <class _CharT>
106 typename __stdinbuf<_CharT>::int_type
107 __stdinbuf<_CharT>::__getchar(bool __consume)
109 if (__last_consumed_is_next_)
111 int_type __result = __last_consumed_;
114 __last_consumed_ = traits_type::eof();
115 __last_consumed_is_next_ = false;
119 char __extbuf[__limit];
120 int __nread = _VSTD::max(1, __encoding_);
121 for (int __i = 0; __i < __nread; ++__i)
123 int __c = getc(__file_);
125 return traits_type::eof();
126 __extbuf[__i] = static_cast<char>(__c);
129 if (__always_noconv_)
130 __1buf = static_cast<char_type>(__extbuf[0]);
135 codecvt_base::result __r;
138 state_type __sv_st = *__st_;
139 __r = __cv_->in(*__st_, __extbuf, __extbuf + __nread, __enxt,
140 &__1buf, &__1buf + 1, __inxt);
143 case _VSTD::codecvt_base::ok:
145 case codecvt_base::partial:
147 if (__nread == sizeof(__extbuf))
148 return traits_type::eof();
150 int __c = getc(__file_);
152 return traits_type::eof();
153 __extbuf[__nread] = static_cast<char>(__c);
157 case codecvt_base::error:
158 return traits_type::eof();
159 case _VSTD::codecvt_base::noconv:
160 __1buf = static_cast<char_type>(__extbuf[0]);
163 } while (__r == _VSTD::codecvt_base::partial);
167 for (int __i = __nread; __i > 0;)
169 if (ungetc(traits_type::to_int_type(__extbuf[--__i]), __file_) == EOF)
170 return traits_type::eof();
174 __last_consumed_ = traits_type::to_int_type(__1buf);
175 return traits_type::to_int_type(__1buf);
178 template <class _CharT>
179 typename __stdinbuf<_CharT>::int_type
180 __stdinbuf<_CharT>::pbackfail(int_type __c)
182 if (traits_type::eq_int_type(__c, traits_type::eof()))
184 if (!__last_consumed_is_next_)
186 __c = __last_consumed_;
187 __last_consumed_is_next_ = !traits_type::eq_int_type(__last_consumed_,
192 if (__last_consumed_is_next_)
194 char __extbuf[__limit];
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))
201 case _VSTD::codecvt_base::ok:
203 case _VSTD::codecvt_base::noconv:
204 __extbuf[0] = static_cast<char>(__last_consumed_);
205 __enxt = __extbuf + 1;
207 case codecvt_base::partial:
208 case codecvt_base::error:
209 return traits_type::eof();
211 while (__enxt > __extbuf)
212 if (ungetc(*--__enxt, __file_) == EOF)
213 return traits_type::eof();
215 __last_consumed_ = __c;
216 __last_consumed_is_next_ = true;
222 template <class _CharT>
223 class _LIBCPP_HIDDEN __stdoutbuf
224 : public basic_streambuf<_CharT, char_traits<_CharT> >
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;
234 __stdoutbuf(FILE* __fp, state_type* __st);
237 virtual int_type overflow (int_type __c = traits_type::eof());
238 virtual streamsize xsputn(const char_type* __s, streamsize __n);
240 virtual void imbue(const locale& __loc);
244 const codecvt<char_type, char, state_type>* __cv_;
246 bool __always_noconv_;
248 __stdoutbuf(const __stdoutbuf&);
249 __stdoutbuf& operator=(const __stdoutbuf&);
252 template <class _CharT>
253 __stdoutbuf<_CharT>::__stdoutbuf(FILE* __fp, state_type* __st)
255 __cv_(&use_facet<codecvt<char_type, char, state_type> >(this->getloc())),
257 __always_noconv_(__cv_->always_noconv())
261 template <class _CharT>
262 typename __stdoutbuf<_CharT>::int_type
263 __stdoutbuf<_CharT>::overflow(int_type __c)
265 char __extbuf[__limit];
267 if (!traits_type::eq_int_type(__c, traits_type::eof()))
269 __1buf = traits_type::to_char_type(__c);
270 if (__always_noconv_)
272 if (fwrite(&__1buf, sizeof(char_type), 1, __file_) != 1)
273 return traits_type::eof();
277 char* __extbe = __extbuf;
278 codecvt_base::result __r;
279 char_type* pbase = &__1buf;
280 char_type* pptr = pbase + 1;
283 const char_type* __e;
284 __r = __cv_->out(*__st_, pbase, pptr, __e,
286 __extbuf + sizeof(__extbuf),
289 return traits_type::eof();
290 if (__r == codecvt_base::noconv)
292 if (fwrite(pbase, 1, 1, __file_) != 1)
293 return traits_type::eof();
295 else if (__r == codecvt_base::ok || __r == codecvt_base::partial)
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)
302 pbase = const_cast<char_type*>(__e);
306 return traits_type::eof();
307 } while (__r == codecvt_base::partial);
310 return traits_type::not_eof(__c);
313 template <class _CharT>
315 __stdoutbuf<_CharT>::xsputn(const char_type* __s, streamsize __n)
317 if (__always_noconv_)
318 return fwrite(__s, sizeof(char_type), __n, __file_);
320 for (; __i < __n; ++__i, ++__s)
321 if (overflow(traits_type::to_int_type(*__s)) == traits_type::eof())
326 template <class _CharT>
328 __stdoutbuf<_CharT>::sync()
330 char __extbuf[__limit];
331 codecvt_base::result __r;
335 __r = __cv_->unshift(*__st_, __extbuf,
336 __extbuf + sizeof(__extbuf),
338 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf);
339 if (fwrite(__extbuf, 1, __nmemb, __file_) != __nmemb)
341 } while (__r == codecvt_base::partial);
342 if (__r == codecvt_base::error)
349 template <class _CharT>
351 __stdoutbuf<_CharT>::imbue(const locale& __loc)
354 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(__loc);
355 __always_noconv_ = __cv_->always_noconv();
358 _LIBCPP_END_NAMESPACE_STD
362 #endif // _LIBCPP___STD_STREAM