2 //===----------------------------------------------------------------------===//
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP___STD_STREAM
11 #define _LIBCPP___STD_STREAM
19 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
20 #pragma GCC system_header
24 #include <__undef_macros>
27 _LIBCPP_BEGIN_NAMESPACE_STD
29 static const int __limit = 8;
33 template <class _CharT>
34 class _LIBCPP_HIDDEN __stdinbuf
35 : public basic_streambuf<_CharT, char_traits<_CharT> >
38 typedef _CharT char_type;
39 typedef char_traits<char_type> traits_type;
40 typedef typename traits_type::int_type int_type;
41 typedef typename traits_type::pos_type pos_type;
42 typedef typename traits_type::off_type off_type;
43 typedef typename traits_type::state_type state_type;
45 __stdinbuf(FILE* __fp, state_type* __st);
48 virtual int_type underflow();
49 virtual int_type uflow();
50 virtual int_type pbackfail(int_type __c = traits_type::eof());
51 virtual void imbue(const locale& __loc);
56 const codecvt<char_type, char, state_type>* __cv_;
59 int_type __last_consumed_;
60 bool __last_consumed_is_next_;
61 bool __always_noconv_;
63 __stdinbuf(const __stdinbuf&);
64 __stdinbuf& operator=(const __stdinbuf&);
66 int_type __getchar(bool __consume);
69 template <class _CharT>
70 __stdinbuf<_CharT>::__stdinbuf(FILE* __fp, state_type* __st)
73 __last_consumed_(traits_type::eof()),
74 __last_consumed_is_next_(false)
76 imbue(this->getloc());
79 template <class _CharT>
81 __stdinbuf<_CharT>::imbue(const locale& __loc)
83 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(__loc);
84 __encoding_ = __cv_->encoding();
85 __always_noconv_ = __cv_->always_noconv();
86 if (__encoding_ > __limit)
87 __throw_runtime_error("unsupported locale for standard input");
90 template <class _CharT>
91 typename __stdinbuf<_CharT>::int_type
92 __stdinbuf<_CharT>::underflow()
94 return __getchar(false);
97 template <class _CharT>
98 typename __stdinbuf<_CharT>::int_type
99 __stdinbuf<_CharT>::uflow()
101 return __getchar(true);
104 template <class _CharT>
105 typename __stdinbuf<_CharT>::int_type
106 __stdinbuf<_CharT>::__getchar(bool __consume)
108 if (__last_consumed_is_next_)
110 int_type __result = __last_consumed_;
113 __last_consumed_ = traits_type::eof();
114 __last_consumed_is_next_ = false;
118 char __extbuf[__limit];
119 int __nread = _VSTD::max(1, __encoding_);
120 for (int __i = 0; __i < __nread; ++__i)
122 int __c = getc(__file_);
124 return traits_type::eof();
125 __extbuf[__i] = static_cast<char>(__c);
128 if (__always_noconv_)
129 __1buf = static_cast<char_type>(__extbuf[0]);
134 codecvt_base::result __r;
137 state_type __sv_st = *__st_;
138 __r = __cv_->in(*__st_, __extbuf, __extbuf + __nread, __enxt,
139 &__1buf, &__1buf + 1, __inxt);
142 case _VSTD::codecvt_base::ok:
144 case codecvt_base::partial:
146 if (__nread == sizeof(__extbuf))
147 return traits_type::eof();
149 int __c = getc(__file_);
151 return traits_type::eof();
152 __extbuf[__nread] = static_cast<char>(__c);
156 case codecvt_base::error:
157 return traits_type::eof();
158 case _VSTD::codecvt_base::noconv:
159 __1buf = static_cast<char_type>(__extbuf[0]);
162 } while (__r == _VSTD::codecvt_base::partial);
166 for (int __i = __nread; __i > 0;)
168 if (ungetc(traits_type::to_int_type(__extbuf[--__i]), __file_) == EOF)
169 return traits_type::eof();
173 __last_consumed_ = traits_type::to_int_type(__1buf);
174 return traits_type::to_int_type(__1buf);
177 template <class _CharT>
178 typename __stdinbuf<_CharT>::int_type
179 __stdinbuf<_CharT>::pbackfail(int_type __c)
181 if (traits_type::eq_int_type(__c, traits_type::eof()))
183 if (!__last_consumed_is_next_)
185 __c = __last_consumed_;
186 __last_consumed_is_next_ = !traits_type::eq_int_type(__last_consumed_,
191 if (__last_consumed_is_next_)
193 char __extbuf[__limit];
195 const char_type __ci = traits_type::to_char_type(__last_consumed_);
196 const char_type* __inxt;
197 switch (__cv_->out(*__st_, &__ci, &__ci + 1, __inxt,
198 __extbuf, __extbuf + sizeof(__extbuf), __enxt))
200 case _VSTD::codecvt_base::ok:
202 case _VSTD::codecvt_base::noconv:
203 __extbuf[0] = static_cast<char>(__last_consumed_);
204 __enxt = __extbuf + 1;
206 case codecvt_base::partial:
207 case codecvt_base::error:
208 return traits_type::eof();
210 while (__enxt > __extbuf)
211 if (ungetc(*--__enxt, __file_) == EOF)
212 return traits_type::eof();
214 __last_consumed_ = __c;
215 __last_consumed_is_next_ = true;
221 template <class _CharT>
222 class _LIBCPP_HIDDEN __stdoutbuf
223 : public basic_streambuf<_CharT, char_traits<_CharT> >
226 typedef _CharT char_type;
227 typedef char_traits<char_type> traits_type;
228 typedef typename traits_type::int_type int_type;
229 typedef typename traits_type::pos_type pos_type;
230 typedef typename traits_type::off_type off_type;
231 typedef typename traits_type::state_type state_type;
233 __stdoutbuf(FILE* __fp, state_type* __st);
236 virtual int_type overflow (int_type __c = traits_type::eof());
237 virtual streamsize xsputn(const char_type* __s, streamsize __n);
239 virtual void imbue(const locale& __loc);
243 const codecvt<char_type, char, state_type>* __cv_;
245 bool __always_noconv_;
247 __stdoutbuf(const __stdoutbuf&);
248 __stdoutbuf& operator=(const __stdoutbuf&);
251 template <class _CharT>
252 __stdoutbuf<_CharT>::__stdoutbuf(FILE* __fp, state_type* __st)
254 __cv_(&use_facet<codecvt<char_type, char, state_type> >(this->getloc())),
256 __always_noconv_(__cv_->always_noconv())
260 template <class _CharT>
261 typename __stdoutbuf<_CharT>::int_type
262 __stdoutbuf<_CharT>::overflow(int_type __c)
264 char __extbuf[__limit];
266 if (!traits_type::eq_int_type(__c, traits_type::eof()))
268 __1buf = traits_type::to_char_type(__c);
269 if (__always_noconv_)
271 if (fwrite(&__1buf, sizeof(char_type), 1, __file_) != 1)
272 return traits_type::eof();
276 char* __extbe = __extbuf;
277 codecvt_base::result __r;
278 char_type* pbase = &__1buf;
279 char_type* pptr = pbase + 1;
282 const char_type* __e;
283 __r = __cv_->out(*__st_, pbase, pptr, __e,
285 __extbuf + sizeof(__extbuf),
288 return traits_type::eof();
289 if (__r == codecvt_base::noconv)
291 if (fwrite(pbase, 1, 1, __file_) != 1)
292 return traits_type::eof();
294 else if (__r == codecvt_base::ok || __r == codecvt_base::partial)
296 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf);
297 if (fwrite(__extbuf, 1, __nmemb, __file_) != __nmemb)
298 return traits_type::eof();
299 if (__r == codecvt_base::partial)
301 pbase = const_cast<char_type*>(__e);
305 return traits_type::eof();
306 } while (__r == codecvt_base::partial);
309 return traits_type::not_eof(__c);
312 template <class _CharT>
314 __stdoutbuf<_CharT>::xsputn(const char_type* __s, streamsize __n)
316 if (__always_noconv_)
317 return fwrite(__s, sizeof(char_type), __n, __file_);
319 for (; __i < __n; ++__i, ++__s)
320 if (overflow(traits_type::to_int_type(*__s)) == traits_type::eof())
325 template <class _CharT>
327 __stdoutbuf<_CharT>::sync()
329 char __extbuf[__limit];
330 codecvt_base::result __r;
334 __r = __cv_->unshift(*__st_, __extbuf,
335 __extbuf + sizeof(__extbuf),
337 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf);
338 if (fwrite(__extbuf, 1, __nmemb, __file_) != __nmemb)
340 } while (__r == codecvt_base::partial);
341 if (__r == codecvt_base::error)
348 template <class _CharT>
350 __stdoutbuf<_CharT>::imbue(const locale& __loc)
353 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(__loc);
354 __always_noconv_ = __cv_->always_noconv();
357 _LIBCPP_END_NAMESPACE_STD
361 #endif // _LIBCPP___STD_STREAM