blob: fa194ea8ce2e542e5246d4a36376af57e2c5b360 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-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 Hinnantbc8d3f92010-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 Hinnant66c6f972011-11-29 16:45:27 +000020#include <__undef_min_max>
21
Howard Hinnant08e17472011-10-17 20:05:10 +000022#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000023#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +000024#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000025
26_LIBCPP_BEGIN_NAMESPACE_STD
27
Howard Hinnantec3773c2011-12-01 20:21:04 +000028static const int __limit = 8;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000029
30// __stdinbuf
31
32template <class _CharT>
33class _LIBCPP_HIDDEN __stdinbuf
34 : public basic_streambuf<_CharT, char_traits<_CharT> >
35{
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 explicit __stdinbuf(FILE* __fp);
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
54 FILE* __file_;
55 const codecvt<char_type, char, state_type>* __cv_;
56 state_type __st_;
57 int __encoding_;
58 bool __always_noconv_;
59
60 __stdinbuf(const __stdinbuf&);
61 __stdinbuf& operator=(const __stdinbuf&);
62
63 int_type __getchar(bool __consume);
64};
65
66template <class _CharT>
67__stdinbuf<_CharT>::__stdinbuf(FILE* __fp)
68 : __file_(__fp),
69 __st_()
70{
71 imbue(this->getloc());
72}
73
74template <class _CharT>
75void
76__stdinbuf<_CharT>::imbue(const locale& __loc)
77{
78 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(__loc);
79 __encoding_ = __cv_->encoding();
80 __always_noconv_ = __cv_->always_noconv();
81 if (__encoding_ > __limit)
82 __throw_runtime_error("unsupported locale for standard input");
83}
84
85template <class _CharT>
86typename __stdinbuf<_CharT>::int_type
87__stdinbuf<_CharT>::underflow()
88{
89 return __getchar(false);
90}
91
92template <class _CharT>
93typename __stdinbuf<_CharT>::int_type
94__stdinbuf<_CharT>::uflow()
95{
96 return __getchar(true);
97}
98
99template <class _CharT>
100typename __stdinbuf<_CharT>::int_type
101__stdinbuf<_CharT>::__getchar(bool __consume)
102{
103 char __extbuf[__limit];
Howard Hinnant0949eed2011-06-30 21:18:19 +0000104 int __nread = _VSTD::max(1, __encoding_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000105 for (int __i = 0; __i < __nread; ++__i)
106 {
Howard Hinnantec3773c2011-12-01 20:21:04 +0000107 int __c = getc(__file_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000108 if (__c == EOF)
109 return traits_type::eof();
110 __extbuf[__i] = static_cast<char>(__c);
111 }
112 char_type __1buf;
113 if (__always_noconv_)
114 __1buf = static_cast<char_type>(__extbuf[0]);
115 else
116 {
117 const char* __enxt;
118 char_type* __inxt;
119 codecvt_base::result __r;
120 do
121 {
122 state_type __sv_st = __st_;
123 __r = __cv_->in(__st_, __extbuf, __extbuf + __nread, __enxt,
124 &__1buf, &__1buf + 1, __inxt);
125 switch (__r)
126 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000127 case _VSTD::codecvt_base::ok:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000128 break;
129 case codecvt_base::partial:
130 __st_ = __sv_st;
131 if (__nread == sizeof(__extbuf))
132 return traits_type::eof();
133 {
Howard Hinnantec3773c2011-12-01 20:21:04 +0000134 int __c = getc(__file_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000135 if (__c == EOF)
136 return traits_type::eof();
137 __extbuf[__nread] = static_cast<char>(__c);
138 }
139 ++__nread;
140 break;
141 case codecvt_base::error:
142 return traits_type::eof();
Howard Hinnant0949eed2011-06-30 21:18:19 +0000143 case _VSTD::codecvt_base::noconv:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000144 __1buf = static_cast<char_type>(__extbuf[0]);
145 break;
146 }
Howard Hinnant0949eed2011-06-30 21:18:19 +0000147 } while (__r == _VSTD::codecvt_base::partial);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000148 }
149 if (!__consume)
150 {
151 for (int __i = __nread; __i > 0;)
152 {
Howard Hinnantc7890252013-03-11 19:53:48 +0000153 if (ungetc(traits_type::to_int_type(__extbuf[--__i]), __file_) == EOF)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000154 return traits_type::eof();
155 }
156 }
157 return traits_type::to_int_type(__1buf);
158}
159
160template <class _CharT>
161typename __stdinbuf<_CharT>::int_type
162__stdinbuf<_CharT>::pbackfail(int_type __c)
163{
164 if (traits_type::eq_int_type(__c, traits_type::eof()))
165 return __c;
166 char __extbuf[__limit];
167 char* __enxt;
168 const char_type __ci = traits_type::to_char_type(__c);
169 const char_type* __inxt;
170 switch (__cv_->out(__st_, &__ci, &__ci + 1, __inxt,
171 __extbuf, __extbuf + sizeof(__extbuf), __enxt))
172 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000173 case _VSTD::codecvt_base::ok:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000174 break;
Howard Hinnant0949eed2011-06-30 21:18:19 +0000175 case _VSTD::codecvt_base::noconv:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000176 __extbuf[0] = static_cast<char>(__c);
177 __enxt = __extbuf + 1;
178 break;
179 case codecvt_base::partial:
180 case codecvt_base::error:
181 return traits_type::eof();
182 }
183 while (__enxt > __extbuf)
184 if (ungetc(*--__enxt, __file_) == EOF)
185 return traits_type::eof();
Howard Hinnantf5256e12010-05-11 21:36:01 +0000186 return traits_type::not_eof(__c);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000187}
188
189// __stdoutbuf
190
191template <class _CharT>
192class _LIBCPP_HIDDEN __stdoutbuf
193 : public basic_streambuf<_CharT, char_traits<_CharT> >
194{
195public:
196 typedef _CharT char_type;
197 typedef char_traits<char_type> traits_type;
198 typedef typename traits_type::int_type int_type;
199 typedef typename traits_type::pos_type pos_type;
200 typedef typename traits_type::off_type off_type;
201 typedef typename traits_type::state_type state_type;
202
203 explicit __stdoutbuf(FILE* __fp);
204
205protected:
206 virtual int_type overflow (int_type __c = traits_type::eof());
207 virtual int sync();
208 virtual void imbue(const locale& __loc);
209
210private:
211 FILE* __file_;
212 const codecvt<char_type, char, state_type>* __cv_;
213 state_type __st_;
214 bool __always_noconv_;
215
216 __stdoutbuf(const __stdoutbuf&);
217 __stdoutbuf& operator=(const __stdoutbuf&);
218};
219
220template <class _CharT>
221__stdoutbuf<_CharT>::__stdoutbuf(FILE* __fp)
222 : __file_(__fp),
223 __cv_(&use_facet<codecvt<char_type, char, state_type> >(this->getloc())),
224 __st_(),
225 __always_noconv_(__cv_->always_noconv())
226{
227}
228
229template <class _CharT>
230typename __stdoutbuf<_CharT>::int_type
231__stdoutbuf<_CharT>::overflow(int_type __c)
232{
233 char __extbuf[__limit];
234 char_type __1buf;
235 if (!traits_type::eq_int_type(__c, traits_type::eof()))
236 {
237 this->setp(&__1buf, &__1buf+1);
238 *this->pptr() = traits_type::to_char_type(__c);
239 this->pbump(1);
240 if (__always_noconv_)
241 {
242 if (fwrite(this->pbase(), sizeof(char_type), 1, __file_) != 1)
243 return traits_type::eof();
244 }
245 else
246 {
247 char* __extbe = __extbuf;
248 codecvt_base::result __r;
249 do
250 {
251 const char_type* __e;
252 __r = __cv_->out(__st_, this->pbase(), this->pptr(), __e,
253 __extbuf,
254 __extbuf + sizeof(__extbuf),
255 __extbe);
256 if (__e == this->pbase())
257 return traits_type::eof();
258 if (__r == codecvt_base::noconv)
259 {
260 if (fwrite(this->pbase(), 1, 1, __file_) != 1)
261 return traits_type::eof();
262 }
263 else if (__r == codecvt_base::ok || __r == codecvt_base::partial)
264 {
265 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf);
266 if (fwrite(__extbuf, 1, __nmemb, __file_) != __nmemb)
267 return traits_type::eof();
268 if (__r == codecvt_base::partial)
269 {
270 this->setp((char_type*)__e, this->pptr());
Howard Hinnantec3773c2011-12-01 20:21:04 +0000271 this->pbump(static_cast<int>(this->epptr() - this->pbase()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000272 }
273 }
274 else
275 return traits_type::eof();
276 } while (__r == codecvt_base::partial);
277 }
278 this->setp(0, 0);
279 }
280 return traits_type::not_eof(__c);
281}
282
283template <class _CharT>
284int
285__stdoutbuf<_CharT>::sync()
286{
287 char __extbuf[__limit];
288 codecvt_base::result __r;
289 do
290 {
291 char* __extbe;
292 __r = __cv_->unshift(__st_, __extbuf,
293 __extbuf + sizeof(__extbuf),
294 __extbe);
295 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf);
296 if (fwrite(__extbuf, 1, __nmemb, __file_) != __nmemb)
297 return -1;
298 } while (__r == codecvt_base::partial);
299 if (__r == codecvt_base::error)
300 return -1;
301 if (fflush(__file_))
302 return -1;
303 return 0;
304}
305
306template <class _CharT>
307void
308__stdoutbuf<_CharT>::imbue(const locale& __loc)
309{
310 sync();
311 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(__loc);
312 __always_noconv_ = __cv_->always_noconv();
313}
314
315_LIBCPP_END_NAMESPACE_STD
316
317#endif // _LIBCPP___STD_STREAM