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