Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | // -*- C++ -*- |
| 2 | //===----------------------------------------------------------------------===// |
| 3 | // |
Howard Hinnant | f5256e1 | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 4 | // The LLVM Compiler Infrastructure |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5 | // |
Howard Hinnant | b64f8b0 | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 6 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 7 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 8 | // |
| 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 Hinnant | 66c6f97 | 2011-11-29 16:45:27 +0000 | [diff] [blame] | 20 | #include <__undef_min_max> |
| 21 | |
Howard Hinnant | 08e1747 | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 22 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 23 | #pragma GCC system_header |
Howard Hinnant | 08e1747 | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 24 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 25 | |
| 26 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 27 | |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 28 | static const int __limit = 8; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 29 | |
| 30 | // __stdinbuf |
| 31 | |
| 32 | template <class _CharT> |
| 33 | class _LIBCPP_HIDDEN __stdinbuf |
| 34 | : public basic_streambuf<_CharT, char_traits<_CharT> > |
| 35 | { |
| 36 | public: |
| 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 | |
Howard Hinnant | 903439f | 2013-03-19 21:34:48 +0000 | [diff] [blame] | 44 | __stdinbuf(FILE* __fp, state_type* __st); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 45 | |
| 46 | protected: |
| 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 | |
| 52 | private: |
| 53 | |
| 54 | FILE* __file_; |
| 55 | const codecvt<char_type, char, state_type>* __cv_; |
Howard Hinnant | 903439f | 2013-03-19 21:34:48 +0000 | [diff] [blame] | 56 | state_type* __st_; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 57 | int __encoding_; |
Howard Hinnant | a7f5c1b | 2013-05-08 21:18:42 +0000 | [diff] [blame] | 58 | int_type __last_consumed_; |
| 59 | bool __last_consumed_is_next_; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 60 | bool __always_noconv_; |
| 61 | |
| 62 | __stdinbuf(const __stdinbuf&); |
| 63 | __stdinbuf& operator=(const __stdinbuf&); |
| 64 | |
| 65 | int_type __getchar(bool __consume); |
| 66 | }; |
| 67 | |
| 68 | template <class _CharT> |
Howard Hinnant | 903439f | 2013-03-19 21:34:48 +0000 | [diff] [blame] | 69 | __stdinbuf<_CharT>::__stdinbuf(FILE* __fp, state_type* __st) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 70 | : __file_(__fp), |
Howard Hinnant | a7f5c1b | 2013-05-08 21:18:42 +0000 | [diff] [blame] | 71 | __st_(__st), |
| 72 | __last_consumed_(traits_type::eof()), |
| 73 | __last_consumed_is_next_(false) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 74 | { |
| 75 | imbue(this->getloc()); |
| 76 | } |
| 77 | |
| 78 | template <class _CharT> |
| 79 | void |
| 80 | __stdinbuf<_CharT>::imbue(const locale& __loc) |
| 81 | { |
| 82 | __cv_ = &use_facet<codecvt<char_type, char, state_type> >(__loc); |
| 83 | __encoding_ = __cv_->encoding(); |
| 84 | __always_noconv_ = __cv_->always_noconv(); |
| 85 | if (__encoding_ > __limit) |
| 86 | __throw_runtime_error("unsupported locale for standard input"); |
| 87 | } |
| 88 | |
| 89 | template <class _CharT> |
| 90 | typename __stdinbuf<_CharT>::int_type |
| 91 | __stdinbuf<_CharT>::underflow() |
| 92 | { |
| 93 | return __getchar(false); |
| 94 | } |
| 95 | |
| 96 | template <class _CharT> |
| 97 | typename __stdinbuf<_CharT>::int_type |
| 98 | __stdinbuf<_CharT>::uflow() |
| 99 | { |
| 100 | return __getchar(true); |
| 101 | } |
| 102 | |
| 103 | template <class _CharT> |
| 104 | typename __stdinbuf<_CharT>::int_type |
| 105 | __stdinbuf<_CharT>::__getchar(bool __consume) |
| 106 | { |
Howard Hinnant | a7f5c1b | 2013-05-08 21:18:42 +0000 | [diff] [blame] | 107 | if (__last_consumed_is_next_) |
| 108 | { |
| 109 | int_type __result = __last_consumed_; |
| 110 | if (__consume) |
| 111 | { |
| 112 | __last_consumed_ = traits_type::eof(); |
| 113 | __last_consumed_is_next_ = false; |
| 114 | } |
| 115 | return __result; |
| 116 | } |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 117 | char __extbuf[__limit]; |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 118 | int __nread = _VSTD::max(1, __encoding_); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 119 | for (int __i = 0; __i < __nread; ++__i) |
| 120 | { |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 121 | int __c = getc(__file_); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 122 | if (__c == EOF) |
| 123 | return traits_type::eof(); |
| 124 | __extbuf[__i] = static_cast<char>(__c); |
| 125 | } |
| 126 | char_type __1buf; |
| 127 | if (__always_noconv_) |
| 128 | __1buf = static_cast<char_type>(__extbuf[0]); |
| 129 | else |
| 130 | { |
| 131 | const char* __enxt; |
| 132 | char_type* __inxt; |
| 133 | codecvt_base::result __r; |
| 134 | do |
| 135 | { |
Howard Hinnant | 903439f | 2013-03-19 21:34:48 +0000 | [diff] [blame] | 136 | state_type __sv_st = *__st_; |
| 137 | __r = __cv_->in(*__st_, __extbuf, __extbuf + __nread, __enxt, |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 138 | &__1buf, &__1buf + 1, __inxt); |
| 139 | switch (__r) |
| 140 | { |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 141 | case _VSTD::codecvt_base::ok: |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 142 | break; |
| 143 | case codecvt_base::partial: |
Howard Hinnant | 903439f | 2013-03-19 21:34:48 +0000 | [diff] [blame] | 144 | *__st_ = __sv_st; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 145 | if (__nread == sizeof(__extbuf)) |
| 146 | return traits_type::eof(); |
| 147 | { |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 148 | int __c = getc(__file_); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 149 | if (__c == EOF) |
| 150 | return traits_type::eof(); |
| 151 | __extbuf[__nread] = static_cast<char>(__c); |
| 152 | } |
| 153 | ++__nread; |
| 154 | break; |
| 155 | case codecvt_base::error: |
| 156 | return traits_type::eof(); |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 157 | case _VSTD::codecvt_base::noconv: |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 158 | __1buf = static_cast<char_type>(__extbuf[0]); |
| 159 | break; |
| 160 | } |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 161 | } while (__r == _VSTD::codecvt_base::partial); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 162 | } |
| 163 | if (!__consume) |
| 164 | { |
| 165 | for (int __i = __nread; __i > 0;) |
| 166 | { |
Howard Hinnant | c789025 | 2013-03-11 19:53:48 +0000 | [diff] [blame] | 167 | if (ungetc(traits_type::to_int_type(__extbuf[--__i]), __file_) == EOF) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 168 | return traits_type::eof(); |
| 169 | } |
| 170 | } |
Howard Hinnant | a7f5c1b | 2013-05-08 21:18:42 +0000 | [diff] [blame] | 171 | else |
| 172 | __last_consumed_ = traits_type::to_int_type(__1buf); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 173 | return traits_type::to_int_type(__1buf); |
| 174 | } |
| 175 | |
| 176 | template <class _CharT> |
| 177 | typename __stdinbuf<_CharT>::int_type |
| 178 | __stdinbuf<_CharT>::pbackfail(int_type __c) |
| 179 | { |
| 180 | if (traits_type::eq_int_type(__c, traits_type::eof())) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 181 | { |
Howard Hinnant | a7f5c1b | 2013-05-08 21:18:42 +0000 | [diff] [blame] | 182 | if (!__last_consumed_is_next_) |
| 183 | { |
| 184 | __c = __last_consumed_; |
| 185 | __last_consumed_is_next_ = !traits_type::eq_int_type(__last_consumed_, |
| 186 | traits_type::eof()); |
| 187 | } |
| 188 | return __c; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 189 | } |
Howard Hinnant | a7f5c1b | 2013-05-08 21:18:42 +0000 | [diff] [blame] | 190 | if (__last_consumed_is_next_) |
| 191 | { |
| 192 | char __extbuf[__limit]; |
| 193 | char* __enxt; |
| 194 | const char_type __ci = traits_type::to_char_type(__last_consumed_); |
| 195 | const char_type* __inxt; |
| 196 | switch (__cv_->out(*__st_, &__ci, &__ci + 1, __inxt, |
| 197 | __extbuf, __extbuf + sizeof(__extbuf), __enxt)) |
| 198 | { |
| 199 | case _VSTD::codecvt_base::ok: |
| 200 | break; |
| 201 | case _VSTD::codecvt_base::noconv: |
| 202 | __extbuf[0] = static_cast<char>(__last_consumed_); |
| 203 | __enxt = __extbuf + 1; |
| 204 | break; |
| 205 | case codecvt_base::partial: |
| 206 | case codecvt_base::error: |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 207 | return traits_type::eof(); |
Howard Hinnant | a7f5c1b | 2013-05-08 21:18:42 +0000 | [diff] [blame] | 208 | } |
| 209 | while (__enxt > __extbuf) |
| 210 | if (ungetc(*--__enxt, __file_) == EOF) |
| 211 | return traits_type::eof(); |
| 212 | } |
| 213 | __last_consumed_ = __c; |
| 214 | __last_consumed_is_next_ = true; |
| 215 | return __c; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | // __stdoutbuf |
| 219 | |
| 220 | template <class _CharT> |
| 221 | class _LIBCPP_HIDDEN __stdoutbuf |
| 222 | : public basic_streambuf<_CharT, char_traits<_CharT> > |
| 223 | { |
| 224 | public: |
| 225 | typedef _CharT char_type; |
| 226 | typedef char_traits<char_type> traits_type; |
| 227 | typedef typename traits_type::int_type int_type; |
| 228 | typedef typename traits_type::pos_type pos_type; |
| 229 | typedef typename traits_type::off_type off_type; |
| 230 | typedef typename traits_type::state_type state_type; |
| 231 | |
Howard Hinnant | 903439f | 2013-03-19 21:34:48 +0000 | [diff] [blame] | 232 | __stdoutbuf(FILE* __fp, state_type* __st); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 233 | |
| 234 | protected: |
| 235 | virtual int_type overflow (int_type __c = traits_type::eof()); |
| 236 | virtual int sync(); |
| 237 | virtual void imbue(const locale& __loc); |
| 238 | |
| 239 | private: |
| 240 | FILE* __file_; |
| 241 | const codecvt<char_type, char, state_type>* __cv_; |
Howard Hinnant | 903439f | 2013-03-19 21:34:48 +0000 | [diff] [blame] | 242 | state_type* __st_; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 243 | bool __always_noconv_; |
| 244 | |
| 245 | __stdoutbuf(const __stdoutbuf&); |
| 246 | __stdoutbuf& operator=(const __stdoutbuf&); |
| 247 | }; |
| 248 | |
| 249 | template <class _CharT> |
Howard Hinnant | 903439f | 2013-03-19 21:34:48 +0000 | [diff] [blame] | 250 | __stdoutbuf<_CharT>::__stdoutbuf(FILE* __fp, state_type* __st) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 251 | : __file_(__fp), |
| 252 | __cv_(&use_facet<codecvt<char_type, char, state_type> >(this->getloc())), |
Howard Hinnant | 903439f | 2013-03-19 21:34:48 +0000 | [diff] [blame] | 253 | __st_(__st), |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 254 | __always_noconv_(__cv_->always_noconv()) |
| 255 | { |
| 256 | } |
| 257 | |
| 258 | template <class _CharT> |
| 259 | typename __stdoutbuf<_CharT>::int_type |
| 260 | __stdoutbuf<_CharT>::overflow(int_type __c) |
| 261 | { |
| 262 | char __extbuf[__limit]; |
| 263 | char_type __1buf; |
| 264 | if (!traits_type::eq_int_type(__c, traits_type::eof())) |
| 265 | { |
Howard Hinnant | ab135d7 | 2013-06-28 21:40:28 +0000 | [diff] [blame] | 266 | __1buf = traits_type::to_char_type(__c); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 267 | if (__always_noconv_) |
| 268 | { |
Howard Hinnant | ab135d7 | 2013-06-28 21:40:28 +0000 | [diff] [blame] | 269 | if (fwrite(&__1buf, sizeof(char_type), 1, __file_) != 1) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 270 | return traits_type::eof(); |
| 271 | } |
| 272 | else |
| 273 | { |
| 274 | char* __extbe = __extbuf; |
| 275 | codecvt_base::result __r; |
Howard Hinnant | ab135d7 | 2013-06-28 21:40:28 +0000 | [diff] [blame] | 276 | char_type* pbase = &__1buf; |
| 277 | char_type* pptr = pbase + 1; |
| 278 | char_type* epptr = pptr; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 279 | do |
| 280 | { |
| 281 | const char_type* __e; |
Howard Hinnant | ab135d7 | 2013-06-28 21:40:28 +0000 | [diff] [blame] | 282 | __r = __cv_->out(*__st_, pbase, pptr, __e, |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 283 | __extbuf, |
| 284 | __extbuf + sizeof(__extbuf), |
| 285 | __extbe); |
Howard Hinnant | ab135d7 | 2013-06-28 21:40:28 +0000 | [diff] [blame] | 286 | if (__e == pbase) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 287 | return traits_type::eof(); |
| 288 | if (__r == codecvt_base::noconv) |
| 289 | { |
Howard Hinnant | ab135d7 | 2013-06-28 21:40:28 +0000 | [diff] [blame] | 290 | if (fwrite(pbase, 1, 1, __file_) != 1) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 291 | return traits_type::eof(); |
| 292 | } |
| 293 | else if (__r == codecvt_base::ok || __r == codecvt_base::partial) |
| 294 | { |
| 295 | size_t __nmemb = static_cast<size_t>(__extbe - __extbuf); |
| 296 | if (fwrite(__extbuf, 1, __nmemb, __file_) != __nmemb) |
| 297 | return traits_type::eof(); |
| 298 | if (__r == codecvt_base::partial) |
| 299 | { |
Howard Hinnant | ab135d7 | 2013-06-28 21:40:28 +0000 | [diff] [blame] | 300 | pbase = (char_type*)__e; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 301 | } |
| 302 | } |
| 303 | else |
| 304 | return traits_type::eof(); |
| 305 | } while (__r == codecvt_base::partial); |
| 306 | } |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 307 | } |
| 308 | return traits_type::not_eof(__c); |
| 309 | } |
| 310 | |
| 311 | template <class _CharT> |
| 312 | int |
| 313 | __stdoutbuf<_CharT>::sync() |
| 314 | { |
| 315 | char __extbuf[__limit]; |
| 316 | codecvt_base::result __r; |
| 317 | do |
| 318 | { |
| 319 | char* __extbe; |
Howard Hinnant | 903439f | 2013-03-19 21:34:48 +0000 | [diff] [blame] | 320 | __r = __cv_->unshift(*__st_, __extbuf, |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 321 | __extbuf + sizeof(__extbuf), |
| 322 | __extbe); |
| 323 | size_t __nmemb = static_cast<size_t>(__extbe - __extbuf); |
| 324 | if (fwrite(__extbuf, 1, __nmemb, __file_) != __nmemb) |
| 325 | return -1; |
| 326 | } while (__r == codecvt_base::partial); |
| 327 | if (__r == codecvt_base::error) |
| 328 | return -1; |
| 329 | if (fflush(__file_)) |
| 330 | return -1; |
| 331 | return 0; |
| 332 | } |
| 333 | |
| 334 | template <class _CharT> |
| 335 | void |
| 336 | __stdoutbuf<_CharT>::imbue(const locale& __loc) |
| 337 | { |
| 338 | sync(); |
| 339 | __cv_ = &use_facet<codecvt<char_type, char, state_type> >(__loc); |
| 340 | __always_noconv_ = __cv_->always_noconv(); |
| 341 | } |
| 342 | |
| 343 | _LIBCPP_END_NAMESPACE_STD |
| 344 | |
| 345 | #endif // _LIBCPP___STD_STREAM |