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 <__memory/valid_range.h>
121# include <__type_traits/is_same.h>
122# include <__utility/scope_guard.h>
123# include <climits>
124# include <ios>
125# include <iosfwd>
126# include <version>
127
128# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
129# pragma GCC system_header
130# endif
131
132_LIBCPP_PUSH_MACROS
133# include <__undef_macros>
134
135_LIBCPP_BEGIN_NAMESPACE_STD
136_LIBCPP_BEGIN_EXPLICIT_ABI_ANNOTATIONS
137
138template <class _CharT, class _Traits>
139class basic_streambuf {
140public:
141 // types:
142 typedef _CharT char_type;
143 typedef _Traits traits_type;
144 typedef typename traits_type::int_type int_type;
145 typedef typename traits_type::pos_type pos_type;
146 typedef typename traits_type::off_type off_type;
147
148 static_assert(is_same<_CharT, typename traits_type::char_type>::value,
149 "traits_type::char_type must be the same type as CharT");
150
151 virtual ~basic_streambuf() {}
152
153 // 27.6.2.2.1 locales:
154 inline _LIBCPP_HIDE_FROM_ABI_SINCE_LLVM8 locale pubimbue(const locale& __loc) {
155 imbue(__loc);
156 locale __r = __loc_;
157 __loc_ = __loc;
158 return __r;
159 }
160
161 [[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI_SINCE_LLVM8 locale getloc() const { return __loc_; }
162
163 // 27.6.2.2.2 buffer and positioning:
164 inline _LIBCPP_HIDE_FROM_ABI_SINCE_LLVM8 basic_streambuf* pubsetbuf(char_type* __s, streamsize __n) {
165 return setbuf(__s, __n);
166 }
167
168 inline _LIBCPP_HIDE_FROM_ABI_SINCE_LLVM8 pos_type
169 pubseekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode __which = ios_base::in | ios_base::out) {
170 return seekoff(__off, __way, __which);
171 }
172
173 inline _LIBCPP_HIDE_FROM_ABI_SINCE_LLVM8 pos_type
174 pubseekpos(pos_type __sp, ios_base::openmode __which = ios_base::in | ios_base::out) {
175 return seekpos(__sp, __which);
176 }
177
178 inline _LIBCPP_HIDE_FROM_ABI_SINCE_LLVM8 int pubsync() { return sync(); }
179
180 // Get and put areas:
181 // 27.6.2.2.3 Get area:
182 [[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI_SINCE_LLVM8 streamsize in_avail() {
183 __check_invariants();
184 auto __guard = std::__make_scope_guard([this] { this->__check_invariants(); });
185
186 if (gptr() < egptr())
187 return static_cast<streamsize>(egptr() - gptr());
188 return showmanyc();
189 }
190
191 [[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI_SINCE_LLVM8 int_type snextc() {
192 __check_invariants();
193 auto __guard = std::__make_scope_guard([this] { this->__check_invariants(); });
194
195 if (sbumpc() == traits_type::eof())
196 return traits_type::eof();
197 return sgetc();
198 }
199
200 inline _LIBCPP_HIDE_FROM_ABI_SINCE_LLVM8 int_type sbumpc() {
201 __check_invariants();
202 auto __guard = std::__make_scope_guard([this] { this->__check_invariants(); });
203
204 if (gptr() == egptr())
205 return uflow();
206 int_type __c = traits_type::to_int_type(*gptr());
207 this->gbump(n: 1);
208 return __c;
209 }
210
211 [[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI_SINCE_LLVM8 int_type sgetc() {
212 __check_invariants();
213 auto __guard = std::__make_scope_guard([this] { this->__check_invariants(); });
214
215 if (gptr() == egptr())
216 return underflow();
217 return traits_type::to_int_type(*gptr());
218 }
219
220 inline _LIBCPP_HIDE_FROM_ABI_SINCE_LLVM8 streamsize sgetn(char_type* __s, streamsize __n) { return xsgetn(__s, __n); }
221
222 // 27.6.2.2.4 Putback:
223 inline _LIBCPP_HIDE_FROM_ABI_SINCE_LLVM8 int_type sputbackc(char_type __c) {
224 __check_invariants();
225 auto __guard = std::__make_scope_guard([this] { this->__check_invariants(); });
226
227 if (eback() == gptr() || !traits_type::eq(__c, *(gptr() - 1)))
228 return pbackfail(traits_type::to_int_type(__c));
229 this->gbump(n: -1);
230 return traits_type::to_int_type(*gptr());
231 }
232
233 inline _LIBCPP_HIDE_FROM_ABI_SINCE_LLVM8 int_type sungetc() {
234 __check_invariants();
235 auto __guard = std::__make_scope_guard([this] { this->__check_invariants(); });
236
237 if (eback() == gptr())
238 return pbackfail();
239 this->gbump(n: -1);
240 return traits_type::to_int_type(*gptr());
241 }
242
243 // 27.6.2.2.5 Put area:
244 inline _LIBCPP_HIDE_FROM_ABI_SINCE_LLVM8 int_type sputc(char_type __c) {
245 __check_invariants();
246 auto __guard = std::__make_scope_guard([this] { this->__check_invariants(); });
247
248 if (pptr() == epptr())
249 return overflow(traits_type::to_int_type(__c));
250 *pptr() = __c;
251 this->pbump(n: 1);
252 return traits_type::to_int_type(__c);
253 }
254
255 inline _LIBCPP_HIDE_FROM_ABI_SINCE_LLVM8 streamsize sputn(const char_type* __s, streamsize __n) {
256 return xsputn(__s, __n);
257 }
258
259protected:
260 basic_streambuf() {}
261 basic_streambuf(const basic_streambuf& __sb)
262 : __loc_(__sb.__loc_),
263 __binp_(__sb.__binp_),
264 __ninp_(__sb.__ninp_),
265 __einp_(__sb.__einp_),
266 __bout_(__sb.__bout_),
267 __nout_(__sb.__nout_),
268 __eout_(__sb.__eout_) {}
269
270 basic_streambuf& operator=(const basic_streambuf& __sb) {
271 __loc_ = __sb.__loc_;
272 __binp_ = __sb.__binp_;
273 __ninp_ = __sb.__ninp_;
274 __einp_ = __sb.__einp_;
275 __bout_ = __sb.__bout_;
276 __nout_ = __sb.__nout_;
277 __eout_ = __sb.__eout_;
278 return *this;
279 }
280
281 void swap(basic_streambuf& __sb) {
282 std::swap(x&: __loc_, y&: __sb.__loc_);
283 std::swap(__binp_, __sb.__binp_);
284 std::swap(__ninp_, __sb.__ninp_);
285 std::swap(__einp_, __sb.__einp_);
286 std::swap(__bout_, __sb.__bout_);
287 std::swap(__nout_, __sb.__nout_);
288 std::swap(__eout_, __sb.__eout_);
289 }
290
291 // 27.6.2.3.2 Get area:
292 _LIBCPP_HIDE_FROM_ABI char_type* eback() const { return __binp_; }
293 _LIBCPP_HIDE_FROM_ABI char_type* gptr() const { return __ninp_; }
294 _LIBCPP_HIDE_FROM_ABI char_type* egptr() const { return __einp_; }
295
296 inline _LIBCPP_HIDE_FROM_ABI_SINCE_LLVM8 void gbump(int __n) { __ninp_ += __n; }
297
298 // gbump takes an int, so it might not be able to represent the offset we want to add.
299 _LIBCPP_HIDE_FROM_ABI void __gbump_ptrdiff(ptrdiff_t __n) { __ninp_ += __n; }
300
301 inline _LIBCPP_HIDE_FROM_ABI_SINCE_LLVM8 void setg(char_type* __gbeg, char_type* __gnext, char_type* __gend) {
302 _LIBCPP_ASSERT_VALID_INPUT_RANGE(std::__is_valid_range(__gbeg, __gnext), "[gbeg, gnext) must be a valid range");
303 _LIBCPP_ASSERT_VALID_INPUT_RANGE(std::__is_valid_range(__gbeg, __gend), "[gbeg, gend) must be a valid range");
304 _LIBCPP_ASSERT_VALID_INPUT_RANGE(std::__is_valid_range(__gnext, __gend), "[gnext, gend) must be a valid range");
305 __binp_ = __gbeg;
306 __ninp_ = __gnext;
307 __einp_ = __gend;
308 }
309
310 // 27.6.2.3.3 Put area:
311 _LIBCPP_HIDE_FROM_ABI char_type* pbase() const { return __bout_; }
312 _LIBCPP_HIDE_FROM_ABI char_type* pptr() const { return __nout_; }
313 _LIBCPP_HIDE_FROM_ABI char_type* epptr() const { return __eout_; }
314
315 inline _LIBCPP_HIDE_FROM_ABI_SINCE_LLVM8 void pbump(int __n) { __nout_ += __n; }
316
317 _LIBCPP_HIDE_FROM_ABI void __pbump(streamsize __n) { __nout_ += __n; }
318
319 inline _LIBCPP_HIDE_FROM_ABI_SINCE_LLVM8 void setp(char_type* __pbeg, char_type* __pend) {
320 _LIBCPP_ASSERT_VALID_INPUT_RANGE(std::__is_valid_range(__pbeg, __pend), "[pbeg, pend) must be a valid range");
321 __bout_ = __nout_ = __pbeg;
322 __eout_ = __pend;
323 }
324
325 // 27.6.2.4 virtual functions:
326 // 27.6.2.4.1 Locales:
327 virtual void imbue(const locale&) {}
328
329 // 27.6.2.4.2 Buffer management and positioning:
330 virtual basic_streambuf* setbuf(char_type*, streamsize) { return this; }
331 virtual pos_type seekoff(off_type, ios_base::seekdir, ios_base::openmode = ios_base::in | ios_base::out) {
332 return pos_type(off_type(-1));
333 }
334 virtual pos_type seekpos(pos_type, ios_base::openmode = ios_base::in | ios_base::out) {
335 return pos_type(off_type(-1));
336 }
337 virtual int sync() { return 0; }
338
339 // 27.6.2.4.3 Get area:
340 virtual streamsize showmanyc() { return 0; }
341
342 virtual streamsize xsgetn(char_type* __s, streamsize __n) {
343 __check_invariants();
344 auto __guard = std::__make_scope_guard([this] { this->__check_invariants(); });
345
346 int_type __c;
347 streamsize __i = 0;
348 while (__i < __n) {
349 if (gptr() < egptr()) {
350 const streamsize __len = std::min(static_cast<streamsize>(INT_MAX), std::min(egptr() - gptr(), __n - __i));
351 traits_type::copy(__s, gptr(), __len);
352 __s += __len;
353 __i += __len;
354 this->gbump(n: __len);
355 } else if ((__c = uflow()) != traits_type::eof()) {
356 *__s = traits_type::to_char_type(__c);
357 ++__s;
358 ++__i;
359 } else
360 break;
361 }
362 return __i;
363 }
364
365 virtual int_type underflow() { return traits_type::eof(); }
366 virtual int_type uflow() {
367 __check_invariants();
368 auto __guard = std::__make_scope_guard([this] { this->__check_invariants(); });
369
370 if (underflow() == traits_type::eof())
371 return traits_type::eof();
372 int_type __c = traits_type::to_int_type(*gptr());
373 this->gbump(n: 1);
374 return __c;
375 }
376
377 // 27.6.2.4.4 Putback:
378 virtual int_type pbackfail(int_type = traits_type::eof()) { return traits_type::eof(); }
379
380 // 27.6.2.4.5 Put area:
381 virtual streamsize xsputn(const char_type* __s, streamsize __n) {
382 __check_invariants();
383 auto __guard = std::__make_scope_guard([this] { this->__check_invariants(); });
384
385 streamsize __i = 0;
386 while (__i < __n) {
387 if (pptr() >= epptr()) {
388 if (overflow(traits_type::to_int_type(*__s)) == traits_type::eof())
389 break;
390 ++__s;
391 ++__i;
392 } else {
393 streamsize __chunk_size = std::min(epptr() - pptr(), __n - __i);
394 traits_type::copy(pptr(), __s, __chunk_size);
395 __pbump(n: __chunk_size);
396 __s += __chunk_size;
397 __i += __chunk_size;
398 }
399 }
400 return __i;
401 }
402
403 virtual int_type overflow(int_type = traits_type::eof()) { return traits_type::eof(); }
404
405 // This function checks some invariants of the class (it isn't exhaustive).
406 _LIBCPP_HIDE_FROM_ABI void __check_invariants() const {
407 _LIBCPP_ASSERT_INTERNAL(pbase() <= pptr(), "this is an invariant of the class");
408 _LIBCPP_ASSERT_INTERNAL(pptr() <= epptr(), "this is an invariant of the class");
409
410 _LIBCPP_ASSERT_INTERNAL(eback() <= gptr(), "this is an invariant of the class");
411 _LIBCPP_ASSERT_INTERNAL(gptr() <= egptr(), "this is an invariant of the class");
412 }
413
414private:
415 locale __loc_;
416 char_type* __binp_ = nullptr;
417 char_type* __ninp_ = nullptr;
418 char_type* __einp_ = nullptr;
419 char_type* __bout_ = nullptr;
420 char_type* __nout_ = nullptr;
421 char_type* __eout_ = nullptr;
422
423 template <class _CharT2, class _Traits2, class _Allocator>
424 _LIBCPP_HIDE_FROM_ABI friend basic_istream<_CharT2, _Traits2>&
425 getline(basic_istream<_CharT2, _Traits2>&, basic_string<_CharT2, _Traits2, _Allocator>&, _CharT2);
426};
427
428extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_streambuf<char>;
429
430# if _LIBCPP_HAS_WIDE_CHARACTERS
431extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_streambuf<wchar_t>;
432# endif
433
434_LIBCPP_END_EXPLICIT_ABI_ANNOTATIONS
435_LIBCPP_END_NAMESPACE_STD
436
437_LIBCPP_POP_MACROS
438
439# endif // _LIBCPP_HAS_LOCALIZATION
440
441# if defined(_LIBCPP_KEEP_TRANSITIVE_INCLUDES_LLVM23) && _LIBCPP_STD_VER <= 20
442# include <cstdint>
443# include <optional>
444# endif
445#endif // __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
446
447#endif // _LIBCPP_STREAMBUF
448