streambuf.tcc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // Stream buffer classes -*- C++ -*-
  2. // Copyright (C) 1997-2022 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file bits/streambuf.tcc
  21. * This is an internal header file, included by other library headers.
  22. * Do not attempt to use it directly. @headername{streambuf}
  23. */
  24. //
  25. // ISO C++ 14882: 27.5 Stream buffers
  26. //
  27. #ifndef _STREAMBUF_TCC
  28. #define _STREAMBUF_TCC 1
  29. #pragma GCC system_header
  30. namespace std _GLIBCXX_VISIBILITY(default)
  31. {
  32. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  33. template<typename _CharT, typename _Traits>
  34. streamsize
  35. basic_streambuf<_CharT, _Traits>::
  36. xsgetn(char_type* __s, streamsize __n)
  37. {
  38. streamsize __ret = 0;
  39. while (__ret < __n)
  40. {
  41. const streamsize __buf_len = this->egptr() - this->gptr();
  42. if (__buf_len)
  43. {
  44. const streamsize __remaining = __n - __ret;
  45. const streamsize __len = std::min(__buf_len, __remaining);
  46. traits_type::copy(__s, this->gptr(), __len);
  47. __ret += __len;
  48. __s += __len;
  49. this->__safe_gbump(__len);
  50. }
  51. if (__ret < __n)
  52. {
  53. const int_type __c = this->uflow();
  54. if (!traits_type::eq_int_type(__c, traits_type::eof()))
  55. {
  56. traits_type::assign(*__s++, traits_type::to_char_type(__c));
  57. ++__ret;
  58. }
  59. else
  60. break;
  61. }
  62. }
  63. return __ret;
  64. }
  65. template<typename _CharT, typename _Traits>
  66. streamsize
  67. basic_streambuf<_CharT, _Traits>::
  68. xsputn(const char_type* __s, streamsize __n)
  69. {
  70. streamsize __ret = 0;
  71. while (__ret < __n)
  72. {
  73. const streamsize __buf_len = this->epptr() - this->pptr();
  74. if (__buf_len)
  75. {
  76. const streamsize __remaining = __n - __ret;
  77. const streamsize __len = std::min(__buf_len, __remaining);
  78. traits_type::copy(this->pptr(), __s, __len);
  79. __ret += __len;
  80. __s += __len;
  81. this->__safe_pbump(__len);
  82. }
  83. if (__ret < __n)
  84. {
  85. int_type __c = this->overflow(traits_type::to_int_type(*__s));
  86. if (!traits_type::eq_int_type(__c, traits_type::eof()))
  87. {
  88. ++__ret;
  89. ++__s;
  90. }
  91. else
  92. break;
  93. }
  94. }
  95. return __ret;
  96. }
  97. // Conceivably, this could be used to implement buffer-to-buffer
  98. // copies, if this was ever desired in an un-ambiguous way by the
  99. // standard.
  100. template<typename _CharT, typename _Traits>
  101. streamsize
  102. __copy_streambufs_eof(basic_streambuf<_CharT, _Traits>* __sbin,
  103. basic_streambuf<_CharT, _Traits>* __sbout,
  104. bool& __ineof)
  105. {
  106. streamsize __ret = 0;
  107. __ineof = true;
  108. typename _Traits::int_type __c = __sbin->sgetc();
  109. while (!_Traits::eq_int_type(__c, _Traits::eof()))
  110. {
  111. __c = __sbout->sputc(_Traits::to_char_type(__c));
  112. if (_Traits::eq_int_type(__c, _Traits::eof()))
  113. {
  114. __ineof = false;
  115. break;
  116. }
  117. ++__ret;
  118. __c = __sbin->snextc();
  119. }
  120. return __ret;
  121. }
  122. template<typename _CharT, typename _Traits>
  123. inline streamsize
  124. __copy_streambufs(basic_streambuf<_CharT, _Traits>* __sbin,
  125. basic_streambuf<_CharT, _Traits>* __sbout)
  126. {
  127. bool __ineof;
  128. return __copy_streambufs_eof(__sbin, __sbout, __ineof);
  129. }
  130. // Inhibit implicit instantiations for required instantiations,
  131. // which are defined via explicit instantiations elsewhere.
  132. #if _GLIBCXX_EXTERN_TEMPLATE
  133. extern template class basic_streambuf<char>;
  134. extern template
  135. streamsize
  136. __copy_streambufs(basic_streambuf<char>*,
  137. basic_streambuf<char>*);
  138. #ifdef _GLIBCXX_USE_WCHAR_T
  139. extern template class basic_streambuf<wchar_t>;
  140. extern template
  141. streamsize
  142. __copy_streambufs(basic_streambuf<wchar_t>*,
  143. basic_streambuf<wchar_t>*);
  144. #endif
  145. #endif
  146. _GLIBCXX_END_NAMESPACE_VERSION
  147. } // namespace std
  148. #endif