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