streambuf.tcc 4.8 KB

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