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