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