1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
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
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_STD_STREAM_H
11#define _LIBCPP_STD_STREAM_H
12
13#include <__config>
14#include <__locale_dir/codecvt.h>
15#include <__locale_dir/locale.h>
16#include <cstdio>
17#include <istream>
18#include <ostream>
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_LIBCPP_BEGIN_NAMESPACE_STD
28_LIBCPP_BEGIN_EXPLICIT_ABI_ANNOTATIONS
29
30static const int __limit = 8;
31
32// __stdinbuf
33
34template <class _CharT>
35class _LIBCPP_HIDDEN __stdinbuf : public basic_streambuf<_CharT, char_traits<_CharT> > {
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 FILE* __file_;
54 const codecvt<char_type, char, state_type>* __cv_;
55 state_type* __st_;
56 int __encoding_;
57 int_type __last_consumed_;
58 bool __last_consumed_is_next_;
59 bool __always_noconv_;
60
61#ifdef _WIN32
62 static constexpr bool __is_win32api_wide_char = !is_same_v<_CharT, char>;
63#else
64 static constexpr bool __is_win32api_wide_char = false;
65#endif
66
67 __stdinbuf(const __stdinbuf&);
68 __stdinbuf& operator=(const __stdinbuf&);
69
70 int_type __getchar(bool __consume);
71};
72
73template <class _CharT>
74__stdinbuf<_CharT>::__stdinbuf(FILE* __fp, state_type* __st)
75 : __file_(__fp), __st_(__st), __last_consumed_(traits_type::eof()), __last_consumed_is_next_(false) {
76 imbue(loc: this->getloc());
77 // On Windows, in wchar_t mode, ignore the codecvt from the locale by
78 // default and assume noconv; this passes wchar_t through unmodified from
79 // getwc. If the user sets a custom locale with imbue(), that gets honored,
80 // the IO is done with getc() and converted with the provided codecvt.
81 if constexpr (__is_win32api_wide_char)
82 __always_noconv_ = true;
83}
84
85template <class _CharT>
86void __stdinbuf<_CharT>::imbue(const locale& __loc) {
87 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(__loc);
88 __encoding_ = __cv_->encoding();
89 __always_noconv_ = __cv_->always_noconv();
90 if (__encoding_ > __limit)
91 std::__throw_runtime_error("unsupported locale for standard input");
92}
93
94template <class _CharT>
95typename __stdinbuf<_CharT>::int_type __stdinbuf<_CharT>::underflow() {
96 return __getchar(consume: false);
97}
98
99template <class _CharT>
100typename __stdinbuf<_CharT>::int_type __stdinbuf<_CharT>::uflow() {
101 return __getchar(consume: true);
102}
103
104inline bool __do_getc(FILE* __fp, char* __pbuf) {
105 int __c = getc(stream: __fp);
106 if (__c == EOF)
107 return false;
108 *__pbuf = static_cast<char>(__c);
109 return true;
110}
111#if _LIBCPP_HAS_WIDE_CHARACTERS
112inline bool __do_getc(FILE* __fp, wchar_t* __pbuf) {
113 wint_t __c = getwc(stream: __fp);
114 if (__c == WEOF)
115 return false;
116 *__pbuf = static_cast<wchar_t>(__c);
117 return true;
118}
119#endif
120
121inline bool __do_ungetc(int __c, FILE* __fp, char __dummy) {
122 if (ungetc(__c, stream: __fp) == EOF)
123 return false;
124 return true;
125}
126#if _LIBCPP_HAS_WIDE_CHARACTERS
127inline bool __do_ungetc(std::wint_t __c, FILE* __fp, wchar_t __dummy) {
128 if (ungetwc(wc: __c, stream: __fp) == WEOF)
129 return false;
130 return true;
131}
132#endif
133
134template <class _CharT>
135typename __stdinbuf<_CharT>::int_type __stdinbuf<_CharT>::__getchar(bool __consume) {
136 if (__last_consumed_is_next_) {
137 int_type __result = __last_consumed_;
138 if (__consume) {
139 __last_consumed_ = traits_type::eof();
140 __last_consumed_is_next_ = false;
141 }
142 return __result;
143 }
144 if (__always_noconv_) {
145 char_type __1buf;
146 if (!__do_getc(__file_, &__1buf))
147 return traits_type::eof();
148 if (!__consume) {
149 if (!__do_ungetc(traits_type::to_int_type(__1buf), __file_, __1buf))
150 return traits_type::eof();
151 } else
152 __last_consumed_ = traits_type::to_int_type(__1buf);
153 return traits_type::to_int_type(__1buf);
154 }
155
156 char __extbuf[__limit];
157 int __nread = std::max(a: 1, b: __encoding_);
158 for (int __i = 0; __i < __nread; ++__i) {
159 int __c = getc(stream: __file_);
160 if (__c == EOF)
161 return traits_type::eof();
162 __extbuf[__i] = static_cast<char>(__c);
163 }
164 char_type __1buf;
165 const char* __enxt;
166 char_type* __inxt;
167 codecvt_base::result __r;
168 do {
169 state_type __sv_st = *__st_;
170 __r = __cv_->in(*__st_, __extbuf, __extbuf + __nread, __enxt, &__1buf, &__1buf + 1, __inxt);
171 switch (__r) {
172 case std::codecvt_base::ok:
173 break;
174 case codecvt_base::partial:
175 *__st_ = __sv_st;
176 if (__nread == sizeof(__extbuf))
177 return traits_type::eof();
178 {
179 int __c = getc(stream: __file_);
180 if (__c == EOF)
181 return traits_type::eof();
182 __extbuf[__nread] = static_cast<char>(__c);
183 }
184 ++__nread;
185 break;
186 case codecvt_base::error:
187 return traits_type::eof();
188 case std::codecvt_base::noconv:
189 __1buf = static_cast<char_type>(__extbuf[0]);
190 break;
191 }
192 } while (__r == std::codecvt_base::partial);
193 if (!__consume) {
194 for (int __i = __nread; __i > 0;) {
195 if (ungetc(traits_type::to_int_type(__extbuf[--__i]), __file_) == EOF)
196 return traits_type::eof();
197 }
198 } else
199 __last_consumed_ = traits_type::to_int_type(__1buf);
200 return traits_type::to_int_type(__1buf);
201}
202
203template <class _CharT>
204typename __stdinbuf<_CharT>::int_type __stdinbuf<_CharT>::pbackfail(int_type __c) {
205 if (traits_type::eq_int_type(__c, traits_type::eof())) {
206 if (!__last_consumed_is_next_) {
207 __c = __last_consumed_;
208 __last_consumed_is_next_ = !traits_type::eq_int_type(__last_consumed_, traits_type::eof());
209 }
210 return __c;
211 }
212 if (__always_noconv_ && __last_consumed_is_next_) {
213 if (!__do_ungetc(__last_consumed_, __file_, traits_type::to_char_type(__last_consumed_)))
214 return traits_type::eof();
215 } else if (__last_consumed_is_next_) {
216 char __extbuf[__limit];
217 char* __enxt;
218 const char_type __ci = traits_type::to_char_type(__last_consumed_);
219 const char_type* __inxt;
220 switch (__cv_->out(*__st_, &__ci, &__ci + 1, __inxt, __extbuf, __extbuf + sizeof(__extbuf), __enxt)) {
221 case std::codecvt_base::ok:
222 break;
223 case std::codecvt_base::noconv:
224 __extbuf[0] = static_cast<char>(__last_consumed_);
225 __enxt = __extbuf + 1;
226 break;
227 case codecvt_base::partial:
228 case codecvt_base::error:
229 return traits_type::eof();
230 }
231 while (__enxt > __extbuf)
232 if (ungetc(c: *--__enxt, stream: __file_) == EOF)
233 return traits_type::eof();
234 }
235 __last_consumed_ = __c;
236 __last_consumed_is_next_ = true;
237 return __c;
238}
239
240// __stdoutbuf
241
242template <class _CharT>
243class _LIBCPP_HIDDEN __stdoutbuf : public basic_streambuf<_CharT, char_traits<_CharT> > {
244public:
245 typedef _CharT char_type;
246 typedef char_traits<char_type> traits_type;
247 typedef typename traits_type::int_type int_type;
248 typedef typename traits_type::pos_type pos_type;
249 typedef typename traits_type::off_type off_type;
250 typedef typename traits_type::state_type state_type;
251
252 __stdoutbuf(FILE* __fp, state_type* __st);
253
254protected:
255 virtual int_type overflow(int_type __c = traits_type::eof());
256 virtual streamsize xsputn(const char_type* __s, streamsize __n);
257 virtual int sync();
258 virtual void imbue(const locale& __loc);
259
260private:
261 FILE* __file_;
262 const codecvt<char_type, char, state_type>* __cv_;
263 state_type* __st_;
264 bool __always_noconv_;
265
266#ifdef _WIN32
267 static constexpr bool __is_win32api_wide_char = !is_same_v<_CharT, char>;
268#else
269 static constexpr bool __is_win32api_wide_char = false;
270#endif
271
272 __stdoutbuf(const __stdoutbuf&);
273 __stdoutbuf& operator=(const __stdoutbuf&);
274
275 _LIBCPP_EXPORTED_FROM_ABI friend FILE* __get_ostream_file(ostream&);
276};
277
278template <class _CharT>
279__stdoutbuf<_CharT>::__stdoutbuf(FILE* __fp, state_type* __st)
280 : __file_(__fp),
281 __cv_(&use_facet<codecvt<char_type, char, state_type> >(this->getloc())),
282 __st_(__st),
283 __always_noconv_(__cv_->always_noconv()) {
284 // On Windows, in wchar_t mode, ignore the codecvt from the locale by
285 // default and assume noconv; this passes wchar_t through unmodified to
286 // fputwc, which handles it correctly depending on the actual mode of the
287 // output stream. If the user sets a custom locale with imbue(), that
288 // gets honored.
289 if constexpr (__is_win32api_wide_char)
290 __always_noconv_ = true;
291}
292
293inline bool __do_fputc(char __c, FILE* __fp) {
294 if (fwrite(ptr: &__c, size: sizeof(__c), n: 1, s: __fp) != 1)
295 return false;
296 return true;
297}
298#if _LIBCPP_HAS_WIDE_CHARACTERS
299inline bool __do_fputc(wchar_t __c, FILE* __fp) {
300 // fputwc works regardless of wide/narrow mode of stdout, while
301 // fwrite of wchar_t only works if the stream actually has been set
302 // into wide mode.
303 if (fputwc(wc: __c, stream: __fp) == WEOF)
304 return false;
305 return true;
306}
307#endif
308
309template <class _CharT>
310typename __stdoutbuf<_CharT>::int_type __stdoutbuf<_CharT>::overflow(int_type __c) {
311 char __extbuf[__limit];
312 char_type __1buf;
313 if (!traits_type::eq_int_type(__c, traits_type::eof())) {
314 __1buf = traits_type::to_char_type(__c);
315 if (__always_noconv_) {
316 if (!__do_fputc(__1buf, __file_))
317 return traits_type::eof();
318 } else {
319 char* __extbe = __extbuf;
320 codecvt_base::result __r;
321 char_type* pbase = &__1buf;
322 char_type* pptr = pbase + 1;
323 do {
324 const char_type* __e;
325 __r = __cv_->out(*__st_, pbase, pptr, __e, __extbuf, __extbuf + sizeof(__extbuf), __extbe);
326 if (__e == pbase)
327 return traits_type::eof();
328 if (__r == codecvt_base::noconv) {
329 if (fwrite(pbase, 1, 1, __file_) != 1)
330 return traits_type::eof();
331 } else if (__r == codecvt_base::ok || __r == codecvt_base::partial) {
332 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf);
333 if (fwrite(ptr: __extbuf, size: 1, n: __nmemb, s: __file_) != __nmemb)
334 return traits_type::eof();
335 if (__r == codecvt_base::partial) {
336 pbase = const_cast<char_type*>(__e);
337 }
338 } else
339 return traits_type::eof();
340 } while (__r == codecvt_base::partial);
341 }
342 }
343 return traits_type::not_eof(__c);
344}
345
346template <class _CharT>
347streamsize __stdoutbuf<_CharT>::xsputn(const char_type* __s, streamsize __n) {
348 // For wchar_t on Windows, don't call fwrite(), but write characters one
349 // at a time with fputwc(); that works both when stdout is in the default
350 // mode and when it is set to Unicode mode.
351 if (__always_noconv_ && !__is_win32api_wide_char)
352 return fwrite(__s, sizeof(char_type), __n, __file_);
353 streamsize __i = 0;
354 for (; __i < __n; ++__i, ++__s)
355 if (overflow(c: traits_type::to_int_type(*__s)) == traits_type::eof())
356 break;
357 return __i;
358}
359
360template <class _CharT>
361int __stdoutbuf<_CharT>::sync() {
362 char __extbuf[__limit];
363 codecvt_base::result __r;
364 do {
365 char* __extbe;
366 __r = __cv_->unshift(*__st_, __extbuf, __extbuf + sizeof(__extbuf), __extbe);
367 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf);
368 if (fwrite(ptr: __extbuf, size: 1, n: __nmemb, s: __file_) != __nmemb)
369 return -1;
370 } while (__r == codecvt_base::partial);
371 if (__r == codecvt_base::error)
372 return -1;
373 if (fflush(stream: __file_))
374 return -1;
375 return 0;
376}
377
378template <class _CharT>
379void __stdoutbuf<_CharT>::imbue(const locale& __loc) {
380 sync();
381 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(__loc);
382 __always_noconv_ = __cv_->always_noconv();
383}
384
385_LIBCPP_END_EXPLICIT_ABI_ANNOTATIONS
386_LIBCPP_END_NAMESPACE_STD
387
388_LIBCPP_POP_MACROS
389
390#endif // _LIBCPP_STD_STREAM_H
391