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
10#ifndef _LIBCPP_STREAMBUF
11#define _LIBCPP_STREAMBUF
12
13/*
14 streambuf synopsis
15
16namespace std
17{
18
19template <class charT, class traits = char_traits<charT> >
20class basic_streambuf
21{
22public:
23 // types:
24 typedef charT char_type;
25 typedef traits traits_type;
26 typedef typename traits_type::int_type int_type;
27 typedef typename traits_type::pos_type pos_type;
28 typedef typename traits_type::off_type off_type;
29
30 virtual ~basic_streambuf();
31
32 // 27.6.2.2.1 locales:
33 locale pubimbue(const locale& loc);
34 locale getloc() const;
35
36 // 27.6.2.2.2 buffer and positioning:
37 basic_streambuf* pubsetbuf(char_type* s, streamsize n);
38 pos_type pubseekoff(off_type off, ios_base::seekdir way,
39 ios_base::openmode which = ios_base::in | ios_base::out);
40 pos_type pubseekpos(pos_type sp,
41 ios_base::openmode which = ios_base::in | ios_base::out);
42 int pubsync();
43
44 // Get and put areas:
45 // 27.6.2.2.3 Get area:
46 streamsize in_avail();
47 int_type snextc();
48 int_type sbumpc();
49 int_type sgetc();
50 streamsize sgetn(char_type* s, streamsize n);
51
52 // 27.6.2.2.4 Putback:
53 int_type sputbackc(char_type c);
54 int_type sungetc();
55
56 // 27.6.2.2.5 Put area:
57 int_type sputc(char_type c);
58 streamsize sputn(const char_type* s, streamsize n);
59
60protected:
61 basic_streambuf();
62 basic_streambuf(const basic_streambuf& rhs);
63 basic_streambuf& operator=(const basic_streambuf& rhs);
64 void swap(basic_streambuf& rhs);
65
66 // 27.6.2.3.2 Get area:
67 char_type* eback() const;
68 char_type* gptr() const;
69 char_type* egptr() const;
70 void gbump(int n);
71 void setg(char_type* gbeg, char_type* gnext, char_type* gend);
72
73 // 27.6.2.3.3 Put area:
74 char_type* pbase() const;
75 char_type* pptr() const;
76 char_type* epptr() const;
77 void pbump(int n);
78 void setp(char_type* pbeg, char_type* pend);
79
80 // 27.6.2.4 virtual functions:
81 // 27.6.2.4.1 Locales:
82 virtual void imbue(const locale& loc);
83
84 // 27.6.2.4.2 Buffer management and positioning:
85 virtual basic_streambuf* setbuf(char_type* s, streamsize n);
86 virtual pos_type seekoff(off_type off, ios_base::seekdir way,
87 ios_base::openmode which = ios_base::in | ios_base::out);
88 virtual pos_type seekpos(pos_type sp,
89 ios_base::openmode which = ios_base::in | ios_base::out);
90 virtual int sync();
91
92 // 27.6.2.4.3 Get area:
93 virtual streamsize showmanyc();
94 virtual streamsize xsgetn(char_type* s, streamsize n);
95 virtual int_type underflow();
96 virtual int_type uflow();
97
98 // 27.6.2.4.4 Putback:
99 virtual int_type pbackfail(int_type c = traits_type::eof());
100
101 // 27.6.2.4.5 Put area:
102 virtual streamsize xsputn(const char_type* s, streamsize n);
103 virtual int_type overflow (int_type c = traits_type::eof());
104};
105
106} // std
107
108*/
109
110#if __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
111# include <__cxx03/streambuf>
112#else
113# include <__config>
114
115# if _LIBCPP_HAS_LOCALIZATION
116
117# include <__assert>
118# include <__fwd/streambuf.h>
119# include <__locale>
120# include <__type_traits/is_same.h>
121# include <__utility/is_valid_range.h>
122# include <climits>
123# include <ios>
124# include <iosfwd>
125# include <version>
126
127# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
128# pragma GCC system_header
129# endif
130
131_LIBCPP_PUSH_MACROS
132# include <__undef_macros>
133
134_LIBCPP_BEGIN_NAMESPACE_STD
135
136template <class _CharT, class _Traits>
137class basic_streambuf {
138public:
139 // types:
140 typedef _CharT char_type;
141 typedef _Traits traits_type;
142 typedef typename traits_type::int_type int_type;
143 typedef typename traits_type::pos_type pos_type;
144 typedef typename traits_type::off_type off_type;
145
146 static_assert(is_same<_CharT, typename traits_type::char_type>::value,
147 "traits_type::char_type must be the same type as CharT");
148
149 virtual ~basic_streambuf() {}
150
151 // 27.6.2.2.1 locales:
152 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 locale pubimbue(const locale& __loc) {
153 imbue(__loc);
154 locale __r = __loc_;
155 __loc_ = __loc;
156 return __r;
157 }
158
159 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 locale getloc() const { return __loc_; }
160
161 // 27.6.2.2.2 buffer and positioning:
162 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_streambuf* pubsetbuf(char_type* __s, streamsize __n) {
163 return setbuf(__s, __n);
164 }
165
166 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 pos_type
167 pubseekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode __which = ios_base::in | ios_base::out) {
168 return seekoff(__off, __way, __which);
169 }
170
171 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 pos_type
172 pubseekpos(pos_type __sp, ios_base::openmode __which = ios_base::in | ios_base::out) {
173 return seekpos(__sp, __which);
174 }
175
176 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 int pubsync() { return sync(); }
177
178 // Get and put areas:
179 // 27.6.2.2.3 Get area:
180 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 streamsize in_avail() {
181 if (gptr() < egptr())
182 return static_cast<streamsize>(egptr() - gptr());
183 return showmanyc();
184 }
185
186 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 int_type snextc() {
187 if (sbumpc() == traits_type::eof())
188 return traits_type::eof();
189 return sgetc();
190 }
191
192 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 int_type sbumpc() {
193 if (gptr() == egptr())
194 return uflow();
195 int_type __c = traits_type::to_int_type(*gptr());
196 this->gbump(1);
197 return __c;
198 }
199
200 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 int_type sgetc() {
201 if (gptr() == egptr())
202 return underflow();
203 return traits_type::to_int_type(*gptr());
204 }
205
206 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 streamsize sgetn(char_type* __s, streamsize __n) { return xsgetn(__s, __n); }
207
208 // 27.6.2.2.4 Putback:
209 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 int_type sputbackc(char_type __c) {
210 if (eback() == gptr() || !traits_type::eq(__c, *(gptr() - 1)))
211 return pbackfail(traits_type::to_int_type(__c));
212 this->gbump(-1);
213 return traits_type::to_int_type(*gptr());
214 }
215
216 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 int_type sungetc() {
217 if (eback() == gptr())
218 return pbackfail();
219 this->gbump(-1);
220 return traits_type::to_int_type(*gptr());
221 }
222
223 // 27.6.2.2.5 Put area:
224 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 int_type sputc(char_type __c) {
225 if (pptr() == epptr())
226 return overflow(traits_type::to_int_type(__c));
227 *pptr() = __c;
228 this->pbump(1);
229 return traits_type::to_int_type(__c);
230 }
231
232 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 streamsize sputn(const char_type* __s, streamsize __n) {
233 return xsputn(__s, __n);
234 }
235
236protected:
237 basic_streambuf() {}
238 basic_streambuf(const basic_streambuf& __sb)
239 : __loc_(__sb.__loc_),
240 __binp_(__sb.__binp_),
241 __ninp_(__sb.__ninp_),
242 __einp_(__sb.__einp_),
243 __bout_(__sb.__bout_),
244 __nout_(__sb.__nout_),
245 __eout_(__sb.__eout_) {}
246
247 basic_streambuf& operator=(const basic_streambuf& __sb) {
248 __loc_ = __sb.__loc_;
249 __binp_ = __sb.__binp_;
250 __ninp_ = __sb.__ninp_;
251 __einp_ = __sb.__einp_;
252 __bout_ = __sb.__bout_;
253 __nout_ = __sb.__nout_;
254 __eout_ = __sb.__eout_;
255 return *this;
256 }
257
258 void swap(basic_streambuf& __sb) {
259 std::swap(__loc_, __sb.__loc_);
260 std::swap(__binp_, __sb.__binp_);
261 std::swap(__ninp_, __sb.__ninp_);
262 std::swap(__einp_, __sb.__einp_);
263 std::swap(__bout_, __sb.__bout_);
264 std::swap(__nout_, __sb.__nout_);
265 std::swap(__eout_, __sb.__eout_);
266 }
267
268 // 27.6.2.3.2 Get area:
269 _LIBCPP_HIDE_FROM_ABI char_type* eback() const { return __binp_; }
270 _LIBCPP_HIDE_FROM_ABI char_type* gptr() const { return __ninp_; }
271 _LIBCPP_HIDE_FROM_ABI char_type* egptr() const { return __einp_; }
272
273 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 void gbump(int __n) { __ninp_ += __n; }
274
275 // gbump takes an int, so it might not be able to represent the offset we want to add.
276 _LIBCPP_HIDE_FROM_ABI void __gbump_ptrdiff(ptrdiff_t __n) { __ninp_ += __n; }
277
278 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 void setg(char_type* __gbeg, char_type* __gnext, char_type* __gend) {
279 _LIBCPP_ASSERT_VALID_INPUT_RANGE(std::__is_valid_range(__gbeg, __gnext), "[gbeg, gnext) must be a valid range");
280 _LIBCPP_ASSERT_VALID_INPUT_RANGE(std::__is_valid_range(__gbeg, __gend), "[gbeg, gend) must be a valid range");
281 _LIBCPP_ASSERT_VALID_INPUT_RANGE(std::__is_valid_range(__gnext, __gend), "[gnext, gend) must be a valid range");
282 __binp_ = __gbeg;
283 __ninp_ = __gnext;
284 __einp_ = __gend;
285 }
286
287 // 27.6.2.3.3 Put area:
288 _LIBCPP_HIDE_FROM_ABI char_type* pbase() const { return __bout_; }
289 _LIBCPP_HIDE_FROM_ABI char_type* pptr() const { return __nout_; }
290 _LIBCPP_HIDE_FROM_ABI char_type* epptr() const { return __eout_; }
291
292 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 void pbump(int __n) { __nout_ += __n; }
293
294 _LIBCPP_HIDE_FROM_ABI void __pbump(streamsize __n) { __nout_ += __n; }
295
296 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 void setp(char_type* __pbeg, char_type* __pend) {
297 _LIBCPP_ASSERT_VALID_INPUT_RANGE(std::__is_valid_range(__pbeg, __pend), "[pbeg, pend) must be a valid range");
298 __bout_ = __nout_ = __pbeg;
299 __eout_ = __pend;
300 }
301
302 // 27.6.2.4 virtual functions:
303 // 27.6.2.4.1 Locales:
304 virtual void imbue(const locale&) {}
305
306 // 27.6.2.4.2 Buffer management and positioning:
307 virtual basic_streambuf* setbuf(char_type*, streamsize) { return this; }
308 virtual pos_type seekoff(off_type, ios_base::seekdir, ios_base::openmode = ios_base::in | ios_base::out) {
309 return pos_type(off_type(-1));
310 }
311 virtual pos_type seekpos(pos_type, ios_base::openmode = ios_base::in | ios_base::out) {
312 return pos_type(off_type(-1));
313 }
314 virtual int sync() { return 0; }
315
316 // 27.6.2.4.3 Get area:
317 virtual streamsize showmanyc() { return 0; }
318
319 virtual streamsize xsgetn(char_type* __s, streamsize __n) {
320 int_type __c;
321 streamsize __i = 0;
322 while (__i < __n) {
323 if (gptr() < egptr()) {
324 const streamsize __len = std::min(static_cast<streamsize>(INT_MAX), std::min(egptr() - gptr(), __n - __i));
325 traits_type::copy(__s, gptr(), __len);
326 __s += __len;
327 __i += __len;
328 this->gbump(__len);
329 } else if ((__c = uflow()) != traits_type::eof()) {
330 *__s = traits_type::to_char_type(__c);
331 ++__s;
332 ++__i;
333 } else
334 break;
335 }
336 return __i;
337 }
338
339 virtual int_type underflow() { return traits_type::eof(); }
340 virtual int_type uflow() {
341 if (underflow() == traits_type::eof())
342 return traits_type::eof();
343 int_type __c = traits_type::to_int_type(*gptr());
344 this->gbump(1);
345 return __c;
346 }
347
348 // 27.6.2.4.4 Putback:
349 virtual int_type pbackfail(int_type = traits_type::eof()) { return traits_type::eof(); }
350
351 // 27.6.2.4.5 Put area:
352 virtual streamsize xsputn(const char_type* __s, streamsize __n) {
353 streamsize __i = 0;
354 while (__i < __n) {
355 if (pptr() >= epptr()) {
356 if (overflow(traits_type::to_int_type(*__s)) == traits_type::eof())
357 break;
358 ++__s;
359 ++__i;
360 } else {
361 streamsize __chunk_size = std::min(epptr() - pptr(), __n - __i);
362 traits_type::copy(pptr(), __s, __chunk_size);
363 __pbump(n: __chunk_size);
364 __s += __chunk_size;
365 __i += __chunk_size;
366 }
367 }
368 return __i;
369 }
370
371 virtual int_type overflow(int_type = traits_type::eof()) { return traits_type::eof(); }
372
373private:
374 locale __loc_;
375 char_type* __binp_ = nullptr;
376 char_type* __ninp_ = nullptr;
377 char_type* __einp_ = nullptr;
378 char_type* __bout_ = nullptr;
379 char_type* __nout_ = nullptr;
380 char_type* __eout_ = nullptr;
381
382 template <class _CharT2, class _Traits2, class _Allocator>
383 _LIBCPP_HIDE_FROM_ABI friend basic_istream<_CharT2, _Traits2>&
384 getline(basic_istream<_CharT2, _Traits2>&, basic_string<_CharT2, _Traits2, _Allocator>&, _CharT2);
385};
386
387extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_streambuf<char>;
388
389# if _LIBCPP_HAS_WIDE_CHARACTERS
390extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_streambuf<wchar_t>;
391# endif
392
393_LIBCPP_END_NAMESPACE_STD
394
395_LIBCPP_POP_MACROS
396
397# endif // _LIBCPP_HAS_LOCALIZATION
398
399# if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
400# include <cstdint>
401# include <optional>
402# endif
403#endif // __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
404
405#endif // _LIBCPP_STREAMBUF
406