blob: 5a9a470a97877b516a6aaa7f649b23fcacaf362c [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:161// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
Chandler Carruth7c3769d2019-01-19 10:56:404// 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
Howard Hinnantbc8d3f92010-05-11 19:42:167//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP___STD_STREAM
11#define _LIBCPP___STD_STREAM
12
13#include <__config>
14#include <ostream>
15#include <istream>
16#include <__locale>
17#include <cstdio>
18
Howard Hinnant08e17472011-10-17 20:05:1019#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:1620#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:1021#endif
Howard Hinnantbc8d3f92010-05-11 19:42:1622
Eric Fiselier018a3d52017-05-31 22:07:4923_LIBCPP_PUSH_MACROS
24#include <__undef_macros>
25
26
Howard Hinnantbc8d3f92010-05-11 19:42:1627_LIBCPP_BEGIN_NAMESPACE_STD
28
Howard Hinnantec3773c2011-12-01 20:21:0429static const int __limit = 8;
Howard Hinnantbc8d3f92010-05-11 19:42:1630
31// __stdinbuf
32
33template <class _CharT>
34class _LIBCPP_HIDDEN __stdinbuf
35 : public basic_streambuf<_CharT, char_traits<_CharT> >
36{
37public:
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;
44
Howard Hinnant903439f2013-03-19 21:34:4845 __stdinbuf(FILE* __fp, state_type* __st);
Howard Hinnantbc8d3f92010-05-11 19:42:1646
47protected:
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);
52
53private:
54
55 FILE* __file_;
56 const codecvt<char_type, char, state_type>* __cv_;
Howard Hinnant903439f2013-03-19 21:34:4857 state_type* __st_;
Howard Hinnantbc8d3f92010-05-11 19:42:1658 int __encoding_;
Howard Hinnanta7f5c1b2013-05-08 21:18:4259 int_type __last_consumed_;
60 bool __last_consumed_is_next_;
Howard Hinnantbc8d3f92010-05-11 19:42:1661 bool __always_noconv_;
62
63 __stdinbuf(const __stdinbuf&);
64 __stdinbuf& operator=(const __stdinbuf&);
65
66 int_type __getchar(bool __consume);
67};
68
69template <class _CharT>
Howard Hinnant903439f2013-03-19 21:34:4870__stdinbuf<_CharT>::__stdinbuf(FILE* __fp, state_type* __st)
Howard Hinnantbc8d3f92010-05-11 19:42:1671 : __file_(__fp),
Howard Hinnanta7f5c1b2013-05-08 21:18:4272 __st_(__st),
73 __last_consumed_(traits_type::eof()),
74 __last_consumed_is_next_(false)
Howard Hinnantbc8d3f92010-05-11 19:42:1675{
76 imbue(this->getloc());
77}
78
79template <class _CharT>
80void
81__stdinbuf<_CharT>::imbue(const locale& __loc)
82{
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");
88}
89
90template <class _CharT>
91typename __stdinbuf<_CharT>::int_type
92__stdinbuf<_CharT>::underflow()
93{
94 return __getchar(false);
95}
96
97template <class _CharT>
98typename __stdinbuf<_CharT>::int_type
99__stdinbuf<_CharT>::uflow()
100{
101 return __getchar(true);
102}
103
104template <class _CharT>
105typename __stdinbuf<_CharT>::int_type
106__stdinbuf<_CharT>::__getchar(bool __consume)
107{
Howard Hinnanta7f5c1b2013-05-08 21:18:42108 if (__last_consumed_is_next_)
109 {
110 int_type __result = __last_consumed_;
111 if (__consume)
112 {
113 __last_consumed_ = traits_type::eof();
114 __last_consumed_is_next_ = false;
115 }
116 return __result;
117 }
Howard Hinnantbc8d3f92010-05-11 19:42:16118 char __extbuf[__limit];
Howard Hinnant0949eed2011-06-30 21:18:19119 int __nread = _VSTD::max(1, __encoding_);
Howard Hinnantbc8d3f92010-05-11 19:42:16120 for (int __i = 0; __i < __nread; ++__i)
121 {
Howard Hinnantec3773c2011-12-01 20:21:04122 int __c = getc(__file_);
Howard Hinnantbc8d3f92010-05-11 19:42:16123 if (__c == EOF)
124 return traits_type::eof();
125 __extbuf[__i] = static_cast<char>(__c);
126 }
127 char_type __1buf;
128 if (__always_noconv_)
129 __1buf = static_cast<char_type>(__extbuf[0]);
130 else
131 {
132 const char* __enxt;
133 char_type* __inxt;
134 codecvt_base::result __r;
135 do
136 {
Howard Hinnant903439f2013-03-19 21:34:48137 state_type __sv_st = *__st_;
138 __r = __cv_->in(*__st_, __extbuf, __extbuf + __nread, __enxt,
Howard Hinnantbc8d3f92010-05-11 19:42:16139 &__1buf, &__1buf + 1, __inxt);
140 switch (__r)
141 {
Howard Hinnant0949eed2011-06-30 21:18:19142 case _VSTD::codecvt_base::ok:
Howard Hinnantbc8d3f92010-05-11 19:42:16143 break;
144 case codecvt_base::partial:
Howard Hinnant903439f2013-03-19 21:34:48145 *__st_ = __sv_st;
Howard Hinnantbc8d3f92010-05-11 19:42:16146 if (__nread == sizeof(__extbuf))
147 return traits_type::eof();
148 {
Howard Hinnantec3773c2011-12-01 20:21:04149 int __c = getc(__file_);
Howard Hinnantbc8d3f92010-05-11 19:42:16150 if (__c == EOF)
151 return traits_type::eof();
152 __extbuf[__nread] = static_cast<char>(__c);
153 }
154 ++__nread;
155 break;
156 case codecvt_base::error:
157 return traits_type::eof();
Howard Hinnant0949eed2011-06-30 21:18:19158 case _VSTD::codecvt_base::noconv:
Howard Hinnantbc8d3f92010-05-11 19:42:16159 __1buf = static_cast<char_type>(__extbuf[0]);
160 break;
161 }
Howard Hinnant0949eed2011-06-30 21:18:19162 } while (__r == _VSTD::codecvt_base::partial);
Howard Hinnantbc8d3f92010-05-11 19:42:16163 }
164 if (!__consume)
165 {
166 for (int __i = __nread; __i > 0;)
167 {
Howard Hinnantc7890252013-03-11 19:53:48168 if (ungetc(traits_type::to_int_type(__extbuf[--__i]), __file_) == EOF)
Howard Hinnantbc8d3f92010-05-11 19:42:16169 return traits_type::eof();
170 }
171 }
Howard Hinnanta7f5c1b2013-05-08 21:18:42172 else
173 __last_consumed_ = traits_type::to_int_type(__1buf);
Howard Hinnantbc8d3f92010-05-11 19:42:16174 return traits_type::to_int_type(__1buf);
175}
176
177template <class _CharT>
178typename __stdinbuf<_CharT>::int_type
179__stdinbuf<_CharT>::pbackfail(int_type __c)
180{
181 if (traits_type::eq_int_type(__c, traits_type::eof()))
Howard Hinnantbc8d3f92010-05-11 19:42:16182 {
Howard Hinnanta7f5c1b2013-05-08 21:18:42183 if (!__last_consumed_is_next_)
184 {
185 __c = __last_consumed_;
186 __last_consumed_is_next_ = !traits_type::eq_int_type(__last_consumed_,
187 traits_type::eof());
188 }
189 return __c;
Howard Hinnantbc8d3f92010-05-11 19:42:16190 }
Howard Hinnanta7f5c1b2013-05-08 21:18:42191 if (__last_consumed_is_next_)
192 {
193 char __extbuf[__limit];
194 char* __enxt;
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))
199 {
200 case _VSTD::codecvt_base::ok:
201 break;
202 case _VSTD::codecvt_base::noconv:
203 __extbuf[0] = static_cast<char>(__last_consumed_);
204 __enxt = __extbuf + 1;
205 break;
206 case codecvt_base::partial:
207 case codecvt_base::error:
Howard Hinnantbc8d3f92010-05-11 19:42:16208 return traits_type::eof();
Howard Hinnanta7f5c1b2013-05-08 21:18:42209 }
210 while (__enxt > __extbuf)
211 if (ungetc(*--__enxt, __file_) == EOF)
212 return traits_type::eof();
213 }
214 __last_consumed_ = __c;
215 __last_consumed_is_next_ = true;
216 return __c;
Howard Hinnantbc8d3f92010-05-11 19:42:16217}
218
219// __stdoutbuf
220
221template <class _CharT>
222class _LIBCPP_HIDDEN __stdoutbuf
223 : public basic_streambuf<_CharT, char_traits<_CharT> >
224{
225public:
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;
232
Howard Hinnant903439f2013-03-19 21:34:48233 __stdoutbuf(FILE* __fp, state_type* __st);
Howard Hinnantbc8d3f92010-05-11 19:42:16234
235protected:
236 virtual int_type overflow (int_type __c = traits_type::eof());
Howard Hinnant5ea5d312013-08-09 16:25:43237 virtual streamsize xsputn(const char_type* __s, streamsize __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16238 virtual int sync();
239 virtual void imbue(const locale& __loc);
240
241private:
242 FILE* __file_;
243 const codecvt<char_type, char, state_type>* __cv_;
Howard Hinnant903439f2013-03-19 21:34:48244 state_type* __st_;
Howard Hinnantbc8d3f92010-05-11 19:42:16245 bool __always_noconv_;
246
247 __stdoutbuf(const __stdoutbuf&);
248 __stdoutbuf& operator=(const __stdoutbuf&);
249};
250
251template <class _CharT>
Howard Hinnant903439f2013-03-19 21:34:48252__stdoutbuf<_CharT>::__stdoutbuf(FILE* __fp, state_type* __st)
Howard Hinnantbc8d3f92010-05-11 19:42:16253 : __file_(__fp),
254 __cv_(&use_facet<codecvt<char_type, char, state_type> >(this->getloc())),
Howard Hinnant903439f2013-03-19 21:34:48255 __st_(__st),
Howard Hinnantbc8d3f92010-05-11 19:42:16256 __always_noconv_(__cv_->always_noconv())
257{
258}
259
260template <class _CharT>
261typename __stdoutbuf<_CharT>::int_type
262__stdoutbuf<_CharT>::overflow(int_type __c)
263{
264 char __extbuf[__limit];
265 char_type __1buf;
266 if (!traits_type::eq_int_type(__c, traits_type::eof()))
267 {
Howard Hinnantab135d72013-06-28 21:40:28268 __1buf = traits_type::to_char_type(__c);
Howard Hinnantbc8d3f92010-05-11 19:42:16269 if (__always_noconv_)
270 {
Howard Hinnantab135d72013-06-28 21:40:28271 if (fwrite(&__1buf, sizeof(char_type), 1, __file_) != 1)
Howard Hinnantbc8d3f92010-05-11 19:42:16272 return traits_type::eof();
273 }
274 else
275 {
276 char* __extbe = __extbuf;
277 codecvt_base::result __r;
Howard Hinnantab135d72013-06-28 21:40:28278 char_type* pbase = &__1buf;
279 char_type* pptr = pbase + 1;
Howard Hinnantbc8d3f92010-05-11 19:42:16280 do
281 {
282 const char_type* __e;
Howard Hinnantab135d72013-06-28 21:40:28283 __r = __cv_->out(*__st_, pbase, pptr, __e,
Howard Hinnantbc8d3f92010-05-11 19:42:16284 __extbuf,
285 __extbuf + sizeof(__extbuf),
286 __extbe);
Howard Hinnantab135d72013-06-28 21:40:28287 if (__e == pbase)
Howard Hinnantbc8d3f92010-05-11 19:42:16288 return traits_type::eof();
289 if (__r == codecvt_base::noconv)
290 {
Howard Hinnantab135d72013-06-28 21:40:28291 if (fwrite(pbase, 1, 1, __file_) != 1)
Howard Hinnantbc8d3f92010-05-11 19:42:16292 return traits_type::eof();
293 }
294 else if (__r == codecvt_base::ok || __r == codecvt_base::partial)
295 {
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)
300 {
Saleem Abdulrasool5334ba72016-12-31 18:13:34301 pbase = const_cast<char_type*>(__e);
Howard Hinnantbc8d3f92010-05-11 19:42:16302 }
303 }
304 else
305 return traits_type::eof();
306 } while (__r == codecvt_base::partial);
307 }
Howard Hinnantbc8d3f92010-05-11 19:42:16308 }
309 return traits_type::not_eof(__c);
310}
311
312template <class _CharT>
Howard Hinnant5ea5d312013-08-09 16:25:43313streamsize
314__stdoutbuf<_CharT>::xsputn(const char_type* __s, streamsize __n)
315{
316 if (__always_noconv_)
317 return fwrite(__s, sizeof(char_type), __n, __file_);
318 streamsize __i = 0;
319 for (; __i < __n; ++__i, ++__s)
320 if (overflow(traits_type::to_int_type(*__s)) == traits_type::eof())
321 break;
322 return __i;
323}
324
325template <class _CharT>
Howard Hinnantbc8d3f92010-05-11 19:42:16326int
327__stdoutbuf<_CharT>::sync()
328{
329 char __extbuf[__limit];
330 codecvt_base::result __r;
331 do
332 {
333 char* __extbe;
Howard Hinnant903439f2013-03-19 21:34:48334 __r = __cv_->unshift(*__st_, __extbuf,
Howard Hinnantbc8d3f92010-05-11 19:42:16335 __extbuf + sizeof(__extbuf),
336 __extbe);
337 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf);
338 if (fwrite(__extbuf, 1, __nmemb, __file_) != __nmemb)
339 return -1;
340 } while (__r == codecvt_base::partial);
341 if (__r == codecvt_base::error)
342 return -1;
343 if (fflush(__file_))
344 return -1;
345 return 0;
346}
347
348template <class _CharT>
349void
350__stdoutbuf<_CharT>::imbue(const locale& __loc)
351{
352 sync();
353 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(__loc);
354 __always_noconv_ = __cv_->always_noconv();
355}
356
357_LIBCPP_END_NAMESPACE_STD
358
Eric Fiselier018a3d52017-05-31 22:07:49359_LIBCPP_POP_MACROS
360
Howard Hinnantbc8d3f92010-05-11 19:42:16361#endif // _LIBCPP___STD_STREAM