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