blob: db90795f66fcbac1749818b40bfba12e70284c3d [file] [log] [blame]
Howard Hinnant3e519522010-05-11 19:42:16 +00001// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
Howard Hinnant5b08a8a2010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnant3e519522010-05-11 19:42:16 +00005//
Howard Hinnant412dbeb2010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnant3e519522010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP___STD_STREAM
12#define _LIBCPP___STD_STREAM
13
14#include <__config>
15#include <ostream>
16#include <istream>
17#include <__locale>
18#include <cstdio>
19
Howard Hinnant073458b2011-10-17 20:05:10 +000020#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnant3e519522010-05-11 19:42:16 +000021#pragma GCC system_header
Howard Hinnant073458b2011-10-17 20:05:10 +000022#endif
Howard Hinnant3e519522010-05-11 19:42:16 +000023
Eric Fiseliera016efb2017-05-31 22:07:49 +000024_LIBCPP_PUSH_MACROS
25#include <__undef_macros>
26
27
Howard Hinnant3e519522010-05-11 19:42:16 +000028_LIBCPP_BEGIN_NAMESPACE_STD
29
Howard Hinnantc2063662011-12-01 20:21:04 +000030static const int __limit = 8;
Howard Hinnant3e519522010-05-11 19:42:16 +000031
32// __stdinbuf
33
34template <class _CharT>
35class _LIBCPP_HIDDEN __stdinbuf
36 : public basic_streambuf<_CharT, char_traits<_CharT> >
37{
38public:
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;
45
Howard Hinnant591ebe32013-03-19 21:34:48 +000046 __stdinbuf(FILE* __fp, state_type* __st);
Howard Hinnant3e519522010-05-11 19:42:16 +000047
48protected:
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);
53
54private:
55
56 FILE* __file_;
57 const codecvt<char_type, char, state_type>* __cv_;
Howard Hinnant591ebe32013-03-19 21:34:48 +000058 state_type* __st_;
Howard Hinnant3e519522010-05-11 19:42:16 +000059 int __encoding_;
Howard Hinnant266abef2013-05-08 21:18:42 +000060 int_type __last_consumed_;
61 bool __last_consumed_is_next_;
Howard Hinnant3e519522010-05-11 19:42:16 +000062 bool __always_noconv_;
63
64 __stdinbuf(const __stdinbuf&);
65 __stdinbuf& operator=(const __stdinbuf&);
66
67 int_type __getchar(bool __consume);
68};
69
70template <class _CharT>
Howard Hinnant591ebe32013-03-19 21:34:48 +000071__stdinbuf<_CharT>::__stdinbuf(FILE* __fp, state_type* __st)
Howard Hinnant3e519522010-05-11 19:42:16 +000072 : __file_(__fp),
Howard Hinnant266abef2013-05-08 21:18:42 +000073 __st_(__st),
74 __last_consumed_(traits_type::eof()),
75 __last_consumed_is_next_(false)
Howard Hinnant3e519522010-05-11 19:42:16 +000076{
77 imbue(this->getloc());
78}
79
80template <class _CharT>
81void
82__stdinbuf<_CharT>::imbue(const locale& __loc)
83{
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");
89}
90
91template <class _CharT>
92typename __stdinbuf<_CharT>::int_type
93__stdinbuf<_CharT>::underflow()
94{
95 return __getchar(false);
96}
97
98template <class _CharT>
99typename __stdinbuf<_CharT>::int_type
100__stdinbuf<_CharT>::uflow()
101{
102 return __getchar(true);
103}
104
105template <class _CharT>
106typename __stdinbuf<_CharT>::int_type
107__stdinbuf<_CharT>::__getchar(bool __consume)
108{
Howard Hinnant266abef2013-05-08 21:18:42 +0000109 if (__last_consumed_is_next_)
110 {
111 int_type __result = __last_consumed_;
112 if (__consume)
113 {
114 __last_consumed_ = traits_type::eof();
115 __last_consumed_is_next_ = false;
116 }
117 return __result;
118 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000119 char __extbuf[__limit];
Howard Hinnantce48a112011-06-30 21:18:19 +0000120 int __nread = _VSTD::max(1, __encoding_);
Howard Hinnant3e519522010-05-11 19:42:16 +0000121 for (int __i = 0; __i < __nread; ++__i)
122 {
Howard Hinnantc2063662011-12-01 20:21:04 +0000123 int __c = getc(__file_);
Howard Hinnant3e519522010-05-11 19:42:16 +0000124 if (__c == EOF)
125 return traits_type::eof();
126 __extbuf[__i] = static_cast<char>(__c);
127 }
128 char_type __1buf;
129 if (__always_noconv_)
130 __1buf = static_cast<char_type>(__extbuf[0]);
131 else
132 {
133 const char* __enxt;
134 char_type* __inxt;
135 codecvt_base::result __r;
136 do
137 {
Howard Hinnant591ebe32013-03-19 21:34:48 +0000138 state_type __sv_st = *__st_;
139 __r = __cv_->in(*__st_, __extbuf, __extbuf + __nread, __enxt,
Howard Hinnant3e519522010-05-11 19:42:16 +0000140 &__1buf, &__1buf + 1, __inxt);
141 switch (__r)
142 {
Howard Hinnantce48a112011-06-30 21:18:19 +0000143 case _VSTD::codecvt_base::ok:
Howard Hinnant3e519522010-05-11 19:42:16 +0000144 break;
145 case codecvt_base::partial:
Howard Hinnant591ebe32013-03-19 21:34:48 +0000146 *__st_ = __sv_st;
Howard Hinnant3e519522010-05-11 19:42:16 +0000147 if (__nread == sizeof(__extbuf))
148 return traits_type::eof();
149 {
Howard Hinnantc2063662011-12-01 20:21:04 +0000150 int __c = getc(__file_);
Howard Hinnant3e519522010-05-11 19:42:16 +0000151 if (__c == EOF)
152 return traits_type::eof();
153 __extbuf[__nread] = static_cast<char>(__c);
154 }
155 ++__nread;
156 break;
157 case codecvt_base::error:
158 return traits_type::eof();
Howard Hinnantce48a112011-06-30 21:18:19 +0000159 case _VSTD::codecvt_base::noconv:
Howard Hinnant3e519522010-05-11 19:42:16 +0000160 __1buf = static_cast<char_type>(__extbuf[0]);
161 break;
162 }
Howard Hinnantce48a112011-06-30 21:18:19 +0000163 } while (__r == _VSTD::codecvt_base::partial);
Howard Hinnant3e519522010-05-11 19:42:16 +0000164 }
165 if (!__consume)
166 {
167 for (int __i = __nread; __i > 0;)
168 {
Howard Hinnant41801f12013-03-11 19:53:48 +0000169 if (ungetc(traits_type::to_int_type(__extbuf[--__i]), __file_) == EOF)
Howard Hinnant3e519522010-05-11 19:42:16 +0000170 return traits_type::eof();
171 }
172 }
Howard Hinnant266abef2013-05-08 21:18:42 +0000173 else
174 __last_consumed_ = traits_type::to_int_type(__1buf);
Howard Hinnant3e519522010-05-11 19:42:16 +0000175 return traits_type::to_int_type(__1buf);
176}
177
178template <class _CharT>
179typename __stdinbuf<_CharT>::int_type
180__stdinbuf<_CharT>::pbackfail(int_type __c)
181{
182 if (traits_type::eq_int_type(__c, traits_type::eof()))
Howard Hinnant3e519522010-05-11 19:42:16 +0000183 {
Howard Hinnant266abef2013-05-08 21:18:42 +0000184 if (!__last_consumed_is_next_)
185 {
186 __c = __last_consumed_;
187 __last_consumed_is_next_ = !traits_type::eq_int_type(__last_consumed_,
188 traits_type::eof());
189 }
190 return __c;
Howard Hinnant3e519522010-05-11 19:42:16 +0000191 }
Howard Hinnant266abef2013-05-08 21:18:42 +0000192 if (__last_consumed_is_next_)
193 {
194 char __extbuf[__limit];
195 char* __enxt;
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))
200 {
201 case _VSTD::codecvt_base::ok:
202 break;
203 case _VSTD::codecvt_base::noconv:
204 __extbuf[0] = static_cast<char>(__last_consumed_);
205 __enxt = __extbuf + 1;
206 break;
207 case codecvt_base::partial:
208 case codecvt_base::error:
Howard Hinnant3e519522010-05-11 19:42:16 +0000209 return traits_type::eof();
Howard Hinnant266abef2013-05-08 21:18:42 +0000210 }
211 while (__enxt > __extbuf)
212 if (ungetc(*--__enxt, __file_) == EOF)
213 return traits_type::eof();
214 }
215 __last_consumed_ = __c;
216 __last_consumed_is_next_ = true;
217 return __c;
Howard Hinnant3e519522010-05-11 19:42:16 +0000218}
219
220// __stdoutbuf
221
222template <class _CharT>
223class _LIBCPP_HIDDEN __stdoutbuf
224 : public basic_streambuf<_CharT, char_traits<_CharT> >
225{
226public:
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;
233
Howard Hinnant591ebe32013-03-19 21:34:48 +0000234 __stdoutbuf(FILE* __fp, state_type* __st);
Howard Hinnant3e519522010-05-11 19:42:16 +0000235
236protected:
237 virtual int_type overflow (int_type __c = traits_type::eof());
Howard Hinnantd7cda062013-08-09 16:25:43 +0000238 virtual streamsize xsputn(const char_type* __s, streamsize __n);
Howard Hinnant3e519522010-05-11 19:42:16 +0000239 virtual int sync();
240 virtual void imbue(const locale& __loc);
241
242private:
243 FILE* __file_;
244 const codecvt<char_type, char, state_type>* __cv_;
Howard Hinnant591ebe32013-03-19 21:34:48 +0000245 state_type* __st_;
Howard Hinnant3e519522010-05-11 19:42:16 +0000246 bool __always_noconv_;
247
248 __stdoutbuf(const __stdoutbuf&);
249 __stdoutbuf& operator=(const __stdoutbuf&);
250};
251
252template <class _CharT>
Howard Hinnant591ebe32013-03-19 21:34:48 +0000253__stdoutbuf<_CharT>::__stdoutbuf(FILE* __fp, state_type* __st)
Howard Hinnant3e519522010-05-11 19:42:16 +0000254 : __file_(__fp),
255 __cv_(&use_facet<codecvt<char_type, char, state_type> >(this->getloc())),
Howard Hinnant591ebe32013-03-19 21:34:48 +0000256 __st_(__st),
Howard Hinnant3e519522010-05-11 19:42:16 +0000257 __always_noconv_(__cv_->always_noconv())
258{
259}
260
261template <class _CharT>
262typename __stdoutbuf<_CharT>::int_type
263__stdoutbuf<_CharT>::overflow(int_type __c)
264{
265 char __extbuf[__limit];
266 char_type __1buf;
267 if (!traits_type::eq_int_type(__c, traits_type::eof()))
268 {
Howard Hinnante9672d02013-06-28 21:40:28 +0000269 __1buf = traits_type::to_char_type(__c);
Howard Hinnant3e519522010-05-11 19:42:16 +0000270 if (__always_noconv_)
271 {
Howard Hinnante9672d02013-06-28 21:40:28 +0000272 if (fwrite(&__1buf, sizeof(char_type), 1, __file_) != 1)
Howard Hinnant3e519522010-05-11 19:42:16 +0000273 return traits_type::eof();
274 }
275 else
276 {
277 char* __extbe = __extbuf;
278 codecvt_base::result __r;
Howard Hinnante9672d02013-06-28 21:40:28 +0000279 char_type* pbase = &__1buf;
280 char_type* pptr = pbase + 1;
Howard Hinnant3e519522010-05-11 19:42:16 +0000281 do
282 {
283 const char_type* __e;
Howard Hinnante9672d02013-06-28 21:40:28 +0000284 __r = __cv_->out(*__st_, pbase, pptr, __e,
Howard Hinnant3e519522010-05-11 19:42:16 +0000285 __extbuf,
286 __extbuf + sizeof(__extbuf),
287 __extbe);
Howard Hinnante9672d02013-06-28 21:40:28 +0000288 if (__e == pbase)
Howard Hinnant3e519522010-05-11 19:42:16 +0000289 return traits_type::eof();
290 if (__r == codecvt_base::noconv)
291 {
Howard Hinnante9672d02013-06-28 21:40:28 +0000292 if (fwrite(pbase, 1, 1, __file_) != 1)
Howard Hinnant3e519522010-05-11 19:42:16 +0000293 return traits_type::eof();
294 }
295 else if (__r == codecvt_base::ok || __r == codecvt_base::partial)
296 {
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)
301 {
Saleem Abdulrasool2177f3c2016-12-31 18:13:34 +0000302 pbase = const_cast<char_type*>(__e);
Howard Hinnant3e519522010-05-11 19:42:16 +0000303 }
304 }
305 else
306 return traits_type::eof();
307 } while (__r == codecvt_base::partial);
308 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000309 }
310 return traits_type::not_eof(__c);
311}
312
313template <class _CharT>
Howard Hinnantd7cda062013-08-09 16:25:43 +0000314streamsize
315__stdoutbuf<_CharT>::xsputn(const char_type* __s, streamsize __n)
316{
317 if (__always_noconv_)
318 return fwrite(__s, sizeof(char_type), __n, __file_);
319 streamsize __i = 0;
320 for (; __i < __n; ++__i, ++__s)
321 if (overflow(traits_type::to_int_type(*__s)) == traits_type::eof())
322 break;
323 return __i;
324}
325
326template <class _CharT>
Howard Hinnant3e519522010-05-11 19:42:16 +0000327int
328__stdoutbuf<_CharT>::sync()
329{
330 char __extbuf[__limit];
331 codecvt_base::result __r;
332 do
333 {
334 char* __extbe;
Howard Hinnant591ebe32013-03-19 21:34:48 +0000335 __r = __cv_->unshift(*__st_, __extbuf,
Howard Hinnant3e519522010-05-11 19:42:16 +0000336 __extbuf + sizeof(__extbuf),
337 __extbe);
338 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf);
339 if (fwrite(__extbuf, 1, __nmemb, __file_) != __nmemb)
340 return -1;
341 } while (__r == codecvt_base::partial);
342 if (__r == codecvt_base::error)
343 return -1;
344 if (fflush(__file_))
345 return -1;
346 return 0;
347}
348
349template <class _CharT>
350void
351__stdoutbuf<_CharT>::imbue(const locale& __loc)
352{
353 sync();
354 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(__loc);
355 __always_noconv_ = __cv_->always_noconv();
356}
357
358_LIBCPP_END_NAMESPACE_STD
359
Eric Fiseliera016efb2017-05-31 22:07:49 +0000360_LIBCPP_POP_MACROS
361
Howard Hinnant3e519522010-05-11 19:42:16 +0000362#endif // _LIBCPP___STD_STREAM