1// -*- C++ -*- 2//===------------------------- streambuf ----------------------------------===// 3// 4// The LLVM Compiler Infrastructure 5// 6// This file is dual licensed under the MIT and the University of Illinois Open 7// Source Licenses. See LICENSE.TXT for details. 8// 9//===----------------------------------------------------------------------===// 10 11#ifndef _LIBCPP_STEAMBUF 12#define _LIBCPP_STEAMBUF 13 14/* 15 streambuf synopsis 16 17namespace std 18{ 19 20template <class charT, class traits = char_traits<charT> > 21class basic_streambuf 22{ 23public: 24 // types: 25 typedef charT char_type; 26 typedef traits traits_type; 27 typedef typename traits_type::int_type int_type; 28 typedef typename traits_type::pos_type pos_type; 29 typedef typename traits_type::off_type off_type; 30 31 virtual ~basic_streambuf(); 32 33 // 27.6.2.2.1 locales: 34 locale pubimbue(const locale& loc); 35 locale getloc() const; 36 37 // 27.6.2.2.2 buffer and positioning: 38 basic_streambuf* pubsetbuf(char_type* s, streamsize n); 39 pos_type pubseekoff(off_type off, ios_base::seekdir way, 40 ios_base::openmode which = ios_base::in | ios_base::out); 41 pos_type pubseekpos(pos_type sp, 42 ios_base::openmode which = ios_base::in | ios_base::out); 43 int pubsync(); 44 45 // Get and put areas: 46 // 27.6.2.2.3 Get area: 47 streamsize in_avail(); 48 int_type snextc(); 49 int_type sbumpc(); 50 int_type sgetc(); 51 streamsize sgetn(char_type* s, streamsize n); 52 53 // 27.6.2.2.4 Putback: 54 int_type sputbackc(char_type c); 55 int_type sungetc(); 56 57 // 27.6.2.2.5 Put area: 58 int_type sputc(char_type c); 59 streamsize sputn(const char_type* s, streamsize n); 60 61protected: 62 basic_streambuf(); 63 basic_streambuf(const basic_streambuf& rhs); 64 basic_streambuf& operator=(const basic_streambuf& rhs); 65 void swap(basic_streambuf& rhs); 66 67 // 27.6.2.3.2 Get area: 68 char_type* eback() const; 69 char_type* gptr() const; 70 char_type* egptr() const; 71 void gbump(int n); 72 void setg(char_type* gbeg, char_type* gnext, char_type* gend); 73 74 // 27.6.2.3.3 Put area: 75 char_type* pbase() const; 76 char_type* pptr() const; 77 char_type* epptr() const; 78 void pbump(int n); 79 void setp(char_type* pbeg, char_type* pend); 80 81 // 27.6.2.4 virtual functions: 82 // 27.6.2.4.1 Locales: 83 virtual void imbue(const locale& loc); 84 85 // 27.6.2.4.2 Buffer management and positioning: 86 virtual basic_streambuf* setbuf(char_type* s, streamsize n); 87 virtual pos_type seekoff(off_type off, ios_base::seekdir way, 88 ios_base::openmode which = ios_base::in | ios_base::out); 89 virtual pos_type seekpos(pos_type sp, 90 ios_base::openmode which = ios_base::in | ios_base::out); 91 virtual int sync(); 92 93 // 27.6.2.4.3 Get area: 94 virtual streamsize showmanyc(); 95 virtual streamsize xsgetn(char_type* s, streamsize n); 96 virtual int_type underflow(); 97 virtual int_type uflow(); 98 99 // 27.6.2.4.4 Putback: 100 virtual int_type pbackfail(int_type c = traits_type::eof()); 101 102 // 27.6.2.4.5 Put area: 103 virtual streamsize xsputn(const char_type* s, streamsize n); 104 virtual int_type overflow (int_type c = traits_type::eof()); 105}; 106 107} // std 108 109*/ 110 111#include <__config> 112#include <iosfwd> 113#include <ios> 114 115#pragma GCC system_header 116 117_LIBCPP_BEGIN_NAMESPACE_STD 118 119template <class _CharT, class _Traits> 120class _LIBCPP_VISIBLE basic_streambuf 121{ 122public: 123 // types: 124 typedef _CharT char_type; 125 typedef _Traits traits_type; 126 typedef typename traits_type::int_type int_type; 127 typedef typename traits_type::pos_type pos_type; 128 typedef typename traits_type::off_type off_type; 129 130 virtual ~basic_streambuf(); 131 132 // 27.6.2.2.1 locales: 133 locale pubimbue(const locale& __loc); 134 locale getloc() const; 135 136 // 27.6.2.2.2 buffer and positioning: 137 basic_streambuf* pubsetbuf(char_type* __s, streamsize __n); 138 pos_type pubseekoff(off_type __off, ios_base::seekdir __way, 139 ios_base::openmode __which = ios_base::in | ios_base::out); 140 pos_type pubseekpos(pos_type __sp, 141 ios_base::openmode __which = ios_base::in | ios_base::out); 142 int pubsync(); 143 144 // Get and put areas: 145 // 27.6.2.2.3 Get area: 146 streamsize in_avail(); 147 int_type snextc(); 148 int_type sbumpc(); 149 int_type sgetc(); 150 streamsize sgetn(char_type* __s, streamsize __n); 151 152 // 27.6.2.2.4 Putback: 153 int_type sputbackc(char_type __c); 154 int_type sungetc(); 155 156 // 27.6.2.2.5 Put area: 157 int_type sputc(char_type __c); 158 streamsize sputn(const char_type* __s, streamsize __n); 159 160protected: 161 basic_streambuf(); 162 basic_streambuf(const basic_streambuf& __rhs); 163 basic_streambuf& operator=(const basic_streambuf& __rhs); 164 void swap(basic_streambuf& __rhs); 165 166 // 27.6.2.3.2 Get area: 167 _LIBCPP_ALWAYS_INLINE char_type* eback() const {return __binp_;} 168 _LIBCPP_ALWAYS_INLINE char_type* gptr() const {return __ninp_;} 169 _LIBCPP_ALWAYS_INLINE char_type* egptr() const {return __einp_;} 170 void gbump(int __n); 171 void setg(char_type* __gbeg, char_type* __gnext, char_type* __gend); 172 173 // 27.6.2.3.3 Put area: 174 _LIBCPP_ALWAYS_INLINE char_type* pbase() const {return __bout_;} 175 _LIBCPP_ALWAYS_INLINE char_type* pptr() const {return __nout_;} 176 _LIBCPP_ALWAYS_INLINE char_type* epptr() const {return __eout_;} 177 void pbump(int __n); 178 void setp(char_type* __pbeg, char_type* __pend); 179 180 // 27.6.2.4 virtual functions: 181 // 27.6.2.4.1 Locales: 182 virtual void imbue(const locale& __loc); 183 184 // 27.6.2.4.2 Buffer management and positioning: 185 virtual basic_streambuf* setbuf(char_type* __s, streamsize __n); 186 virtual pos_type seekoff(off_type __off, ios_base::seekdir __way, 187 ios_base::openmode __which = ios_base::in | ios_base::out); 188 virtual pos_type seekpos(pos_type __sp, 189 ios_base::openmode __which = ios_base::in | ios_base::out); 190 virtual int sync(); 191 192 // 27.6.2.4.3 Get area: 193 virtual streamsize showmanyc(); 194 virtual streamsize xsgetn(char_type* __s, streamsize __n); 195 virtual int_type underflow(); 196 virtual int_type uflow(); 197 198 // 27.6.2.4.4 Putback: 199 virtual int_type pbackfail(int_type __c = traits_type::eof()); 200 201 // 27.6.2.4.5 Put area: 202 virtual streamsize xsputn(const char_type* __s, streamsize __n); 203 virtual int_type overflow(int_type __c = traits_type::eof()); 204 205private: 206 locale __loc_; 207 char_type* __binp_; 208 char_type* __ninp_; 209 char_type* __einp_; 210 char_type* __bout_; 211 char_type* __nout_; 212 char_type* __eout_; 213}; 214 215template <class _CharT, class _Traits> 216basic_streambuf<_CharT, _Traits>::~basic_streambuf() 217{ 218} 219 220template <class _CharT, class _Traits> 221inline _LIBCPP_INLINE_VISIBILITY 222locale 223basic_streambuf<_CharT, _Traits>::pubimbue(const locale& __loc) 224{ 225 imbue(__loc); 226 locale __r = __loc_; 227 __loc_ = __loc; 228 return __r; 229} 230 231template <class _CharT, class _Traits> 232inline _LIBCPP_INLINE_VISIBILITY 233locale 234basic_streambuf<_CharT, _Traits>::getloc() const 235{ 236 return __loc_; 237} 238 239template <class _CharT, class _Traits> 240inline _LIBCPP_INLINE_VISIBILITY 241basic_streambuf<_CharT, _Traits>* 242basic_streambuf<_CharT, _Traits>::pubsetbuf(char_type* __s, streamsize __n) 243{ 244 return setbuf(__s, __n); 245} 246 247template <class _CharT, class _Traits> 248inline _LIBCPP_INLINE_VISIBILITY 249typename basic_streambuf<_CharT, _Traits>::pos_type 250basic_streambuf<_CharT, _Traits>::pubseekoff(off_type __off, 251 ios_base::seekdir __way, 252 ios_base::openmode __which) 253{ 254 return seekoff(__off, __way, __which); 255} 256 257template <class _CharT, class _Traits> 258inline _LIBCPP_INLINE_VISIBILITY 259typename basic_streambuf<_CharT, _Traits>::pos_type 260basic_streambuf<_CharT, _Traits>::pubseekpos(pos_type __sp, 261 ios_base::openmode __which) 262{ 263 return seekpos(__sp, __which); 264} 265 266template <class _CharT, class _Traits> 267inline _LIBCPP_INLINE_VISIBILITY 268int 269basic_streambuf<_CharT, _Traits>::pubsync() 270{ 271 return sync(); 272} 273 274template <class _CharT, class _Traits> 275inline _LIBCPP_INLINE_VISIBILITY 276streamsize 277basic_streambuf<_CharT, _Traits>::in_avail() 278{ 279 if (__ninp_ < __einp_) 280 return static_cast<streamsize>(__einp_ - __ninp_); 281 return showmanyc(); 282} 283 284template <class _CharT, class _Traits> 285inline _LIBCPP_INLINE_VISIBILITY 286typename basic_streambuf<_CharT, _Traits>::int_type 287basic_streambuf<_CharT, _Traits>::snextc() 288{ 289 if (sbumpc() == traits_type::eof()) 290 return traits_type::eof(); 291 return sgetc(); 292} 293 294template <class _CharT, class _Traits> 295inline _LIBCPP_INLINE_VISIBILITY 296typename basic_streambuf<_CharT, _Traits>::int_type 297basic_streambuf<_CharT, _Traits>::sbumpc() 298{ 299 if (__ninp_ == __einp_) 300 return uflow(); 301 return traits_type::to_int_type(*__ninp_++); 302} 303 304template <class _CharT, class _Traits> 305inline _LIBCPP_INLINE_VISIBILITY 306typename basic_streambuf<_CharT, _Traits>::int_type 307basic_streambuf<_CharT, _Traits>::sgetc() 308{ 309 if (__ninp_ == __einp_) 310 return underflow(); 311 return traits_type::to_int_type(*__ninp_); 312} 313 314template <class _CharT, class _Traits> 315inline _LIBCPP_INLINE_VISIBILITY 316streamsize 317basic_streambuf<_CharT, _Traits>::sgetn(char_type* __s, streamsize __n) 318{ 319 return xsgetn(__s, __n); 320} 321 322template <class _CharT, class _Traits> 323inline _LIBCPP_INLINE_VISIBILITY 324typename basic_streambuf<_CharT, _Traits>::int_type 325basic_streambuf<_CharT, _Traits>::sputbackc(char_type __c) 326{ 327 if (__binp_ == __ninp_ || !traits_type::eq(__c, __ninp_[-1])) 328 return pbackfail(traits_type::to_int_type(__c)); 329 return traits_type::to_int_type(*--__ninp_); 330} 331 332template <class _CharT, class _Traits> 333inline _LIBCPP_INLINE_VISIBILITY 334typename basic_streambuf<_CharT, _Traits>::int_type 335basic_streambuf<_CharT, _Traits>::sungetc() 336{ 337 if (__binp_ == __ninp_) 338 return pbackfail(); 339 return traits_type::to_int_type(*--__ninp_); 340} 341 342template <class _CharT, class _Traits> 343inline _LIBCPP_INLINE_VISIBILITY 344typename basic_streambuf<_CharT, _Traits>::int_type 345basic_streambuf<_CharT, _Traits>::sputc(char_type __c) 346{ 347 if (__nout_ == __eout_) 348 return overflow(traits_type::to_int_type(__c)); 349 *__nout_++ = __c; 350 return traits_type::to_int_type(__c); 351} 352 353template <class _CharT, class _Traits> 354inline _LIBCPP_INLINE_VISIBILITY 355streamsize 356basic_streambuf<_CharT, _Traits>::sputn(const char_type* __s, streamsize __n) 357{ 358 return xsputn(__s, __n); 359} 360 361template <class _CharT, class _Traits> 362basic_streambuf<_CharT, _Traits>::basic_streambuf() 363 : __binp_(0), 364 __ninp_(0), 365 __einp_(0), 366 __bout_(0), 367 __nout_(0), 368 __eout_(0) 369{ 370} 371 372template <class _CharT, class _Traits> 373basic_streambuf<_CharT, _Traits>::basic_streambuf(const basic_streambuf& __sb) 374 : __loc_(__sb.__loc_), 375 __binp_(__sb.__binp_), 376 __ninp_(__sb.__ninp_), 377 __einp_(__sb.__einp_), 378 __bout_(__sb.__bout_), 379 __nout_(__sb.__nout_), 380 __eout_(__sb.__eout_) 381{ 382} 383 384template <class _CharT, class _Traits> 385basic_streambuf<_CharT, _Traits>& 386basic_streambuf<_CharT, _Traits>::operator=(const basic_streambuf& __sb) 387{ 388 __loc_ = __sb.__loc_; 389 __binp_ = __sb.__binp_; 390 __ninp_ = __sb.__ninp_; 391 __einp_ = __sb.__einp_; 392 __bout_ = __sb.__bout_; 393 __nout_ = __sb.__nout_; 394 __eout_ = __sb.__eout_; 395 return *this; 396} 397 398template <class _CharT, class _Traits> 399void 400basic_streambuf<_CharT, _Traits>::swap(basic_streambuf& __sb) 401{ 402 _VSTD::swap(__loc_, __sb.__loc_); 403 _VSTD::swap(__binp_, __sb.__binp_); 404 _VSTD::swap(__ninp_, __sb.__ninp_); 405 _VSTD::swap(__einp_, __sb.__einp_); 406 _VSTD::swap(__bout_, __sb.__bout_); 407 _VSTD::swap(__nout_, __sb.__nout_); 408 _VSTD::swap(__eout_, __sb.__eout_); 409} 410 411template <class _CharT, class _Traits> 412inline _LIBCPP_INLINE_VISIBILITY 413void 414basic_streambuf<_CharT, _Traits>::gbump(int __n) 415{ 416 __ninp_ += __n; 417} 418 419template <class _CharT, class _Traits> 420inline _LIBCPP_INLINE_VISIBILITY 421void 422basic_streambuf<_CharT, _Traits>::setg(char_type* __gbeg, char_type* __gnext, 423 char_type* __gend) 424{ 425 __binp_ = __gbeg; 426 __ninp_ = __gnext; 427 __einp_ = __gend; 428} 429 430template <class _CharT, class _Traits> 431inline _LIBCPP_INLINE_VISIBILITY 432void 433basic_streambuf<_CharT, _Traits>::pbump(int __n) 434{ 435 __nout_ += __n; 436} 437 438template <class _CharT, class _Traits> 439inline _LIBCPP_INLINE_VISIBILITY 440void 441basic_streambuf<_CharT, _Traits>::setp(char_type* __pbeg, char_type* __pend) 442{ 443 __bout_ = __nout_ = __pbeg; 444 __eout_ = __pend; 445} 446 447template <class _CharT, class _Traits> 448void 449basic_streambuf<_CharT, _Traits>::imbue(const locale&) 450{ 451} 452 453template <class _CharT, class _Traits> 454basic_streambuf<_CharT, _Traits>* 455basic_streambuf<_CharT, _Traits>::setbuf(char_type*, streamsize) 456{ 457 return this; 458} 459 460template <class _CharT, class _Traits> 461typename basic_streambuf<_CharT, _Traits>::pos_type 462basic_streambuf<_CharT, _Traits>::seekoff(off_type __off, ios_base::seekdir __way, 463 ios_base::openmode __which) 464{ 465 return pos_type(off_type(-1)); 466} 467 468template <class _CharT, class _Traits> 469typename basic_streambuf<_CharT, _Traits>::pos_type 470basic_streambuf<_CharT, _Traits>::seekpos(pos_type __sp, ios_base::openmode __which) 471{ 472 return pos_type(off_type(-1)); 473} 474 475template <class _CharT, class _Traits> 476int 477basic_streambuf<_CharT, _Traits>::sync() 478{ 479 return 0; 480} 481 482template <class _CharT, class _Traits> 483streamsize 484basic_streambuf<_CharT, _Traits>::showmanyc() 485{ 486 return 0; 487} 488 489template <class _CharT, class _Traits> 490streamsize 491basic_streambuf<_CharT, _Traits>::xsgetn(char_type* __s, streamsize __n) 492{ 493 const int_type __eof = traits_type::eof(); 494 int_type __c; 495 streamsize __i = 0; 496 for (;__i < __n; ++__i, ++__s) 497 { 498 if (__ninp_ < __einp_) 499 *__s = *__ninp_++; 500 else if ((__c = uflow()) != __eof) 501 *__s = traits_type::to_char_type(__c); 502 else 503 break; 504 } 505 return __i; 506} 507 508template <class _CharT, class _Traits> 509typename basic_streambuf<_CharT, _Traits>::int_type 510basic_streambuf<_CharT, _Traits>::underflow() 511{ 512 return traits_type::eof(); 513} 514 515template <class _CharT, class _Traits> 516typename basic_streambuf<_CharT, _Traits>::int_type 517basic_streambuf<_CharT, _Traits>::uflow() 518{ 519 if (underflow() == traits_type::eof()) 520 return traits_type::eof(); 521 return traits_type::to_int_type(*__ninp_++); 522} 523 524template <class _CharT, class _Traits> 525typename basic_streambuf<_CharT, _Traits>::int_type 526basic_streambuf<_CharT, _Traits>::pbackfail(int_type) 527{ 528 return traits_type::eof(); 529} 530 531template <class _CharT, class _Traits> 532streamsize 533basic_streambuf<_CharT, _Traits>::xsputn(const char_type* __s, streamsize __n) 534{ 535 streamsize __i = 0; 536 int_type __eof = traits_type::eof(); 537 for (; __i < __n; ++__s, ++__i) 538 { 539 if (__nout_ < __eout_) 540 *__nout_++ = *__s; 541 else if (overflow(*__s) == __eof) 542 break; 543 } 544 return __i; 545} 546 547template <class _CharT, class _Traits> 548typename basic_streambuf<_CharT, _Traits>::int_type 549basic_streambuf<_CharT, _Traits>::overflow(int_type __c) 550{ 551 return traits_type::eof(); 552} 553 554extern template class basic_streambuf<char>; 555extern template class basic_streambuf<wchar_t>; 556 557extern template class basic_ios<char>; 558extern template class basic_ios<wchar_t>; 559 560_LIBCPP_END_NAMESPACE_STD 561 562#endif // _LIBCPP_STEAMBUF 563