__std_stream 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. #ifndef _LIBCPP___STD_STREAM
  10. #define _LIBCPP___STD_STREAM
  11. #include <__config>
  12. #include <__locale>
  13. #include <cstdio>
  14. #include <istream>
  15. #include <ostream>
  16. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  17. # pragma GCC system_header
  18. #endif
  19. _LIBCPP_PUSH_MACROS
  20. #include <__undef_macros>
  21. _LIBCPP_BEGIN_NAMESPACE_STD
  22. static const int __limit = 8;
  23. // __stdinbuf
  24. template <class _CharT>
  25. class _LIBCPP_HIDDEN __stdinbuf
  26. : public basic_streambuf<_CharT, char_traits<_CharT> >
  27. {
  28. public:
  29. typedef _CharT char_type;
  30. typedef char_traits<char_type> traits_type;
  31. typedef typename traits_type::int_type int_type;
  32. typedef typename traits_type::pos_type pos_type;
  33. typedef typename traits_type::off_type off_type;
  34. typedef typename traits_type::state_type state_type;
  35. __stdinbuf(FILE* __fp, state_type* __st);
  36. protected:
  37. virtual int_type underflow();
  38. virtual int_type uflow();
  39. virtual int_type pbackfail(int_type __c = traits_type::eof());
  40. virtual void imbue(const locale& __loc);
  41. private:
  42. FILE* __file_;
  43. const codecvt<char_type, char, state_type>* __cv_;
  44. state_type* __st_;
  45. int __encoding_;
  46. int_type __last_consumed_;
  47. bool __last_consumed_is_next_;
  48. bool __always_noconv_;
  49. __stdinbuf(const __stdinbuf&);
  50. __stdinbuf& operator=(const __stdinbuf&);
  51. int_type __getchar(bool __consume);
  52. };
  53. template <class _CharT>
  54. __stdinbuf<_CharT>::__stdinbuf(FILE* __fp, state_type* __st)
  55. : __file_(__fp),
  56. __st_(__st),
  57. __last_consumed_(traits_type::eof()),
  58. __last_consumed_is_next_(false)
  59. {
  60. imbue(this->getloc());
  61. }
  62. template <class _CharT>
  63. void
  64. __stdinbuf<_CharT>::imbue(const locale& __loc)
  65. {
  66. __cv_ = &use_facet<codecvt<char_type, char, state_type> >(__loc);
  67. __encoding_ = __cv_->encoding();
  68. __always_noconv_ = __cv_->always_noconv();
  69. if (__encoding_ > __limit)
  70. __throw_runtime_error("unsupported locale for standard input");
  71. }
  72. template <class _CharT>
  73. typename __stdinbuf<_CharT>::int_type
  74. __stdinbuf<_CharT>::underflow()
  75. {
  76. return __getchar(false);
  77. }
  78. template <class _CharT>
  79. typename __stdinbuf<_CharT>::int_type
  80. __stdinbuf<_CharT>::uflow()
  81. {
  82. return __getchar(true);
  83. }
  84. template <class _CharT>
  85. typename __stdinbuf<_CharT>::int_type
  86. __stdinbuf<_CharT>::__getchar(bool __consume)
  87. {
  88. if (__last_consumed_is_next_)
  89. {
  90. int_type __result = __last_consumed_;
  91. if (__consume)
  92. {
  93. __last_consumed_ = traits_type::eof();
  94. __last_consumed_is_next_ = false;
  95. }
  96. return __result;
  97. }
  98. char __extbuf[__limit];
  99. int __nread = _VSTD::max(1, __encoding_);
  100. for (int __i = 0; __i < __nread; ++__i)
  101. {
  102. int __c = getc(__file_);
  103. if (__c == EOF)
  104. return traits_type::eof();
  105. __extbuf[__i] = static_cast<char>(__c);
  106. }
  107. char_type __1buf;
  108. if (__always_noconv_)
  109. __1buf = static_cast<char_type>(__extbuf[0]);
  110. else
  111. {
  112. const char* __enxt;
  113. char_type* __inxt;
  114. codecvt_base::result __r;
  115. do
  116. {
  117. state_type __sv_st = *__st_;
  118. __r = __cv_->in(*__st_, __extbuf, __extbuf + __nread, __enxt,
  119. &__1buf, &__1buf + 1, __inxt);
  120. switch (__r)
  121. {
  122. case _VSTD::codecvt_base::ok:
  123. break;
  124. case codecvt_base::partial:
  125. *__st_ = __sv_st;
  126. if (__nread == sizeof(__extbuf))
  127. return traits_type::eof();
  128. {
  129. int __c = getc(__file_);
  130. if (__c == EOF)
  131. return traits_type::eof();
  132. __extbuf[__nread] = static_cast<char>(__c);
  133. }
  134. ++__nread;
  135. break;
  136. case codecvt_base::error:
  137. return traits_type::eof();
  138. case _VSTD::codecvt_base::noconv:
  139. __1buf = static_cast<char_type>(__extbuf[0]);
  140. break;
  141. }
  142. } while (__r == _VSTD::codecvt_base::partial);
  143. }
  144. if (!__consume)
  145. {
  146. for (int __i = __nread; __i > 0;)
  147. {
  148. if (ungetc(traits_type::to_int_type(__extbuf[--__i]), __file_) == EOF)
  149. return traits_type::eof();
  150. }
  151. }
  152. else
  153. __last_consumed_ = traits_type::to_int_type(__1buf);
  154. return traits_type::to_int_type(__1buf);
  155. }
  156. template <class _CharT>
  157. typename __stdinbuf<_CharT>::int_type
  158. __stdinbuf<_CharT>::pbackfail(int_type __c)
  159. {
  160. if (traits_type::eq_int_type(__c, traits_type::eof()))
  161. {
  162. if (!__last_consumed_is_next_)
  163. {
  164. __c = __last_consumed_;
  165. __last_consumed_is_next_ = !traits_type::eq_int_type(__last_consumed_,
  166. traits_type::eof());
  167. }
  168. return __c;
  169. }
  170. if (__last_consumed_is_next_)
  171. {
  172. char __extbuf[__limit];
  173. char* __enxt;
  174. const char_type __ci = traits_type::to_char_type(__last_consumed_);
  175. const char_type* __inxt;
  176. switch (__cv_->out(*__st_, &__ci, &__ci + 1, __inxt,
  177. __extbuf, __extbuf + sizeof(__extbuf), __enxt))
  178. {
  179. case _VSTD::codecvt_base::ok:
  180. break;
  181. case _VSTD::codecvt_base::noconv:
  182. __extbuf[0] = static_cast<char>(__last_consumed_);
  183. __enxt = __extbuf + 1;
  184. break;
  185. case codecvt_base::partial:
  186. case codecvt_base::error:
  187. return traits_type::eof();
  188. }
  189. while (__enxt > __extbuf)
  190. if (ungetc(*--__enxt, __file_) == EOF)
  191. return traits_type::eof();
  192. }
  193. __last_consumed_ = __c;
  194. __last_consumed_is_next_ = true;
  195. return __c;
  196. }
  197. // __stdoutbuf
  198. template <class _CharT>
  199. class _LIBCPP_HIDDEN __stdoutbuf
  200. : public basic_streambuf<_CharT, char_traits<_CharT> >
  201. {
  202. public:
  203. typedef _CharT char_type;
  204. typedef char_traits<char_type> traits_type;
  205. typedef typename traits_type::int_type int_type;
  206. typedef typename traits_type::pos_type pos_type;
  207. typedef typename traits_type::off_type off_type;
  208. typedef typename traits_type::state_type state_type;
  209. __stdoutbuf(FILE* __fp, state_type* __st);
  210. protected:
  211. virtual int_type overflow (int_type __c = traits_type::eof());
  212. virtual streamsize xsputn(const char_type* __s, streamsize __n);
  213. virtual int sync();
  214. virtual void imbue(const locale& __loc);
  215. private:
  216. FILE* __file_;
  217. const codecvt<char_type, char, state_type>* __cv_;
  218. state_type* __st_;
  219. bool __always_noconv_;
  220. __stdoutbuf(const __stdoutbuf&);
  221. __stdoutbuf& operator=(const __stdoutbuf&);
  222. };
  223. template <class _CharT>
  224. __stdoutbuf<_CharT>::__stdoutbuf(FILE* __fp, state_type* __st)
  225. : __file_(__fp),
  226. __cv_(&use_facet<codecvt<char_type, char, state_type> >(this->getloc())),
  227. __st_(__st),
  228. __always_noconv_(__cv_->always_noconv())
  229. {
  230. }
  231. template <class _CharT>
  232. typename __stdoutbuf<_CharT>::int_type
  233. __stdoutbuf<_CharT>::overflow(int_type __c)
  234. {
  235. char __extbuf[__limit];
  236. char_type __1buf;
  237. if (!traits_type::eq_int_type(__c, traits_type::eof()))
  238. {
  239. __1buf = traits_type::to_char_type(__c);
  240. if (__always_noconv_)
  241. {
  242. if (fwrite(&__1buf, 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. char_type* pbase = &__1buf;
  250. char_type* pptr = pbase + 1;
  251. do
  252. {
  253. const char_type* __e;
  254. __r = __cv_->out(*__st_, pbase, pptr, __e,
  255. __extbuf,
  256. __extbuf + sizeof(__extbuf),
  257. __extbe);
  258. if (__e == pbase)
  259. return traits_type::eof();
  260. if (__r == codecvt_base::noconv)
  261. {
  262. if (fwrite(pbase, 1, 1, __file_) != 1)
  263. return traits_type::eof();
  264. }
  265. else if (__r == codecvt_base::ok || __r == codecvt_base::partial)
  266. {
  267. size_t __nmemb = static_cast<size_t>(__extbe - __extbuf);
  268. if (fwrite(__extbuf, 1, __nmemb, __file_) != __nmemb)
  269. return traits_type::eof();
  270. if (__r == codecvt_base::partial)
  271. {
  272. pbase = const_cast<char_type*>(__e);
  273. }
  274. }
  275. else
  276. return traits_type::eof();
  277. } while (__r == codecvt_base::partial);
  278. }
  279. }
  280. return traits_type::not_eof(__c);
  281. }
  282. template <class _CharT>
  283. streamsize
  284. __stdoutbuf<_CharT>::xsputn(const char_type* __s, streamsize __n)
  285. {
  286. if (__always_noconv_)
  287. return fwrite(__s, sizeof(char_type), __n, __file_);
  288. streamsize __i = 0;
  289. for (; __i < __n; ++__i, ++__s)
  290. if (overflow(traits_type::to_int_type(*__s)) == traits_type::eof())
  291. break;
  292. return __i;
  293. }
  294. template <class _CharT>
  295. int
  296. __stdoutbuf<_CharT>::sync()
  297. {
  298. char __extbuf[__limit];
  299. codecvt_base::result __r;
  300. do
  301. {
  302. char* __extbe;
  303. __r = __cv_->unshift(*__st_, __extbuf,
  304. __extbuf + sizeof(__extbuf),
  305. __extbe);
  306. size_t __nmemb = static_cast<size_t>(__extbe - __extbuf);
  307. if (fwrite(__extbuf, 1, __nmemb, __file_) != __nmemb)
  308. return -1;
  309. } while (__r == codecvt_base::partial);
  310. if (__r == codecvt_base::error)
  311. return -1;
  312. if (fflush(__file_))
  313. return -1;
  314. return 0;
  315. }
  316. template <class _CharT>
  317. void
  318. __stdoutbuf<_CharT>::imbue(const locale& __loc)
  319. {
  320. sync();
  321. __cv_ = &use_facet<codecvt<char_type, char, state_type> >(__loc);
  322. __always_noconv_ = __cv_->always_noconv();
  323. }
  324. _LIBCPP_END_NAMESPACE_STD
  325. _LIBCPP_POP_MACROS
  326. #endif // _LIBCPP___STD_STREAM