Home | History | Annotate | Download | only in include
      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 
     17 namespace std
     18 {
     19 
     20 template <class charT, class traits = char_traits<charT> >
     21 class basic_streambuf
     22 {
     23 public:
     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 
     61 protected:
     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 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
    116 #pragma GCC system_header
    117 #endif
    118 
    119 _LIBCPP_BEGIN_NAMESPACE_STD
    120 
    121 template <class _CharT, class _Traits>
    122 class _LIBCPP_VISIBLE basic_streambuf
    123 {
    124 public:
    125     // types:
    126     typedef _CharT                         char_type;
    127     typedef _Traits                        traits_type;
    128     typedef typename traits_type::int_type int_type;
    129     typedef typename traits_type::pos_type pos_type;
    130     typedef typename traits_type::off_type off_type;
    131 
    132     virtual ~basic_streambuf();
    133 
    134     // 27.6.2.2.1 locales:
    135     locale pubimbue(const locale& __loc);
    136     locale getloc() const;
    137 
    138     // 27.6.2.2.2 buffer and positioning:
    139     basic_streambuf* pubsetbuf(char_type* __s, streamsize __n);
    140     pos_type pubseekoff(off_type __off, ios_base::seekdir __way,
    141                         ios_base::openmode __which = ios_base::in | ios_base::out);
    142     pos_type pubseekpos(pos_type __sp,
    143                         ios_base::openmode __which = ios_base::in | ios_base::out);
    144     int pubsync();
    145 
    146     // Get and put areas:
    147     // 27.6.2.2.3 Get area:
    148     streamsize in_avail();
    149     int_type snextc();
    150     int_type sbumpc();
    151     int_type sgetc();
    152     streamsize sgetn(char_type* __s, streamsize __n);
    153 
    154     // 27.6.2.2.4 Putback:
    155     int_type sputbackc(char_type __c);
    156     int_type sungetc();
    157 
    158     // 27.6.2.2.5 Put area:
    159     int_type sputc(char_type __c);
    160     streamsize sputn(const char_type* __s, streamsize __n);
    161 
    162 protected:
    163     basic_streambuf();
    164     basic_streambuf(const basic_streambuf& __rhs);
    165     basic_streambuf& operator=(const basic_streambuf& __rhs);
    166     void swap(basic_streambuf& __rhs);
    167 
    168     // 27.6.2.3.2 Get area:
    169     _LIBCPP_ALWAYS_INLINE char_type* eback() const {return __binp_;}
    170     _LIBCPP_ALWAYS_INLINE char_type* gptr()  const {return __ninp_;}
    171     _LIBCPP_ALWAYS_INLINE char_type* egptr() const {return __einp_;}
    172     void gbump(int __n);
    173     void setg(char_type* __gbeg, char_type* __gnext, char_type* __gend);
    174 
    175     // 27.6.2.3.3 Put area:
    176     _LIBCPP_ALWAYS_INLINE char_type* pbase() const {return __bout_;}
    177     _LIBCPP_ALWAYS_INLINE char_type* pptr()  const {return __nout_;}
    178     _LIBCPP_ALWAYS_INLINE char_type* epptr() const {return __eout_;}
    179     void pbump(int __n);
    180     void setp(char_type* __pbeg, char_type* __pend);
    181 
    182     // 27.6.2.4 virtual functions:
    183     // 27.6.2.4.1 Locales:
    184     virtual void imbue(const locale& __loc);
    185 
    186     // 27.6.2.4.2 Buffer management and positioning:
    187     virtual basic_streambuf* setbuf(char_type* __s, streamsize __n);
    188     virtual pos_type seekoff(off_type __off, ios_base::seekdir __way,
    189                              ios_base::openmode __which = ios_base::in | ios_base::out);
    190     virtual pos_type seekpos(pos_type __sp,
    191                              ios_base::openmode __which = ios_base::in | ios_base::out);
    192     virtual int sync();
    193 
    194     // 27.6.2.4.3 Get area:
    195     virtual streamsize showmanyc();
    196     virtual streamsize xsgetn(char_type* __s, streamsize __n);
    197     virtual int_type underflow();
    198     virtual int_type uflow();
    199 
    200     // 27.6.2.4.4 Putback:
    201     virtual int_type pbackfail(int_type __c = traits_type::eof());
    202 
    203     // 27.6.2.4.5 Put area:
    204     virtual streamsize xsputn(const char_type* __s, streamsize __n);
    205     virtual int_type overflow(int_type __c = traits_type::eof());
    206 
    207 private:
    208     locale __loc_;
    209     char_type* __binp_;
    210     char_type* __ninp_;
    211     char_type* __einp_;
    212     char_type* __bout_;
    213     char_type* __nout_;
    214     char_type* __eout_;
    215 };
    216 
    217 template <class _CharT, class _Traits>
    218 basic_streambuf<_CharT, _Traits>::~basic_streambuf()
    219 {
    220 }
    221 
    222 template <class _CharT, class _Traits>
    223 inline _LIBCPP_INLINE_VISIBILITY
    224 locale
    225 basic_streambuf<_CharT, _Traits>::pubimbue(const locale& __loc)
    226 {
    227     imbue(__loc);
    228     locale __r = __loc_;
    229     __loc_ = __loc;
    230     return __r;
    231 }
    232 
    233 template <class _CharT, class _Traits>
    234 inline _LIBCPP_INLINE_VISIBILITY
    235 locale
    236 basic_streambuf<_CharT, _Traits>::getloc() const
    237 {
    238     return __loc_;
    239 }
    240 
    241 template <class _CharT, class _Traits>
    242 inline _LIBCPP_INLINE_VISIBILITY
    243 basic_streambuf<_CharT, _Traits>*
    244 basic_streambuf<_CharT, _Traits>::pubsetbuf(char_type* __s, streamsize __n)
    245 {
    246     return setbuf(__s, __n);
    247 }
    248 
    249 template <class _CharT, class _Traits>
    250 inline _LIBCPP_INLINE_VISIBILITY
    251 typename basic_streambuf<_CharT, _Traits>::pos_type
    252 basic_streambuf<_CharT, _Traits>::pubseekoff(off_type __off,
    253                                              ios_base::seekdir __way,
    254                                              ios_base::openmode __which)
    255 {
    256     return seekoff(__off, __way, __which);
    257 }
    258 
    259 template <class _CharT, class _Traits>
    260 inline _LIBCPP_INLINE_VISIBILITY
    261 typename basic_streambuf<_CharT, _Traits>::pos_type
    262 basic_streambuf<_CharT, _Traits>::pubseekpos(pos_type __sp,
    263                                              ios_base::openmode __which)
    264 {
    265     return seekpos(__sp, __which);
    266 }
    267 
    268 template <class _CharT, class _Traits>
    269 inline _LIBCPP_INLINE_VISIBILITY
    270 int
    271 basic_streambuf<_CharT, _Traits>::pubsync()
    272 {
    273     return sync();
    274 }
    275 
    276 template <class _CharT, class _Traits>
    277 inline _LIBCPP_INLINE_VISIBILITY
    278 streamsize
    279 basic_streambuf<_CharT, _Traits>::in_avail()
    280 {
    281     if (__ninp_ < __einp_)
    282         return static_cast<streamsize>(__einp_ - __ninp_);
    283     return showmanyc();
    284 }
    285 
    286 template <class _CharT, class _Traits>
    287 inline _LIBCPP_INLINE_VISIBILITY
    288 typename basic_streambuf<_CharT, _Traits>::int_type
    289 basic_streambuf<_CharT, _Traits>::snextc()
    290 {
    291     if (sbumpc() == traits_type::eof())
    292         return traits_type::eof();
    293     return sgetc();
    294 }
    295 
    296 template <class _CharT, class _Traits>
    297 inline _LIBCPP_INLINE_VISIBILITY
    298 typename basic_streambuf<_CharT, _Traits>::int_type
    299 basic_streambuf<_CharT, _Traits>::sbumpc()
    300 {
    301     if (__ninp_ == __einp_)
    302         return uflow();
    303     return traits_type::to_int_type(*__ninp_++);
    304 }
    305 
    306 template <class _CharT, class _Traits>
    307 inline _LIBCPP_INLINE_VISIBILITY
    308 typename basic_streambuf<_CharT, _Traits>::int_type
    309 basic_streambuf<_CharT, _Traits>::sgetc()
    310 {
    311     if (__ninp_ == __einp_)
    312         return underflow();
    313     return traits_type::to_int_type(*__ninp_);
    314 }
    315 
    316 template <class _CharT, class _Traits>
    317 inline _LIBCPP_INLINE_VISIBILITY
    318 streamsize
    319 basic_streambuf<_CharT, _Traits>::sgetn(char_type* __s, streamsize __n)
    320 {
    321     return xsgetn(__s, __n);
    322 }
    323 
    324 template <class _CharT, class _Traits>
    325 inline _LIBCPP_INLINE_VISIBILITY
    326 typename basic_streambuf<_CharT, _Traits>::int_type
    327 basic_streambuf<_CharT, _Traits>::sputbackc(char_type __c)
    328 {
    329     if (__binp_ == __ninp_ || !traits_type::eq(__c, __ninp_[-1]))
    330         return pbackfail(traits_type::to_int_type(__c));
    331     return traits_type::to_int_type(*--__ninp_);
    332 }
    333 
    334 template <class _CharT, class _Traits>
    335 inline _LIBCPP_INLINE_VISIBILITY
    336 typename basic_streambuf<_CharT, _Traits>::int_type
    337 basic_streambuf<_CharT, _Traits>::sungetc()
    338 {
    339     if (__binp_ == __ninp_)
    340         return pbackfail();
    341     return traits_type::to_int_type(*--__ninp_);
    342 }
    343 
    344 template <class _CharT, class _Traits>
    345 inline _LIBCPP_INLINE_VISIBILITY
    346 typename basic_streambuf<_CharT, _Traits>::int_type
    347 basic_streambuf<_CharT, _Traits>::sputc(char_type __c)
    348 {
    349     if (__nout_ == __eout_)
    350         return overflow(traits_type::to_int_type(__c));
    351     *__nout_++ = __c;
    352     return traits_type::to_int_type(__c);
    353 }
    354 
    355 template <class _CharT, class _Traits>
    356 inline _LIBCPP_INLINE_VISIBILITY
    357 streamsize
    358 basic_streambuf<_CharT, _Traits>::sputn(const char_type* __s, streamsize __n)
    359 {
    360     return xsputn(__s, __n);
    361 }
    362 
    363 template <class _CharT, class _Traits>
    364 basic_streambuf<_CharT, _Traits>::basic_streambuf()
    365     : __binp_(0),
    366       __ninp_(0),
    367       __einp_(0),
    368       __bout_(0),
    369       __nout_(0),
    370       __eout_(0)
    371 {
    372 }
    373 
    374 template <class _CharT, class _Traits>
    375 basic_streambuf<_CharT, _Traits>::basic_streambuf(const basic_streambuf& __sb)
    376     : __loc_(__sb.__loc_),
    377       __binp_(__sb.__binp_),
    378       __ninp_(__sb.__ninp_),
    379       __einp_(__sb.__einp_),
    380       __bout_(__sb.__bout_),
    381       __nout_(__sb.__nout_),
    382       __eout_(__sb.__eout_)
    383 {
    384 }
    385 
    386 template <class _CharT, class _Traits>
    387 basic_streambuf<_CharT, _Traits>&
    388 basic_streambuf<_CharT, _Traits>::operator=(const basic_streambuf& __sb)
    389 {
    390     __loc_ = __sb.__loc_;
    391     __binp_ = __sb.__binp_;
    392     __ninp_ = __sb.__ninp_;
    393     __einp_ = __sb.__einp_;
    394     __bout_ = __sb.__bout_;
    395     __nout_ = __sb.__nout_;
    396     __eout_ = __sb.__eout_;
    397     return *this;
    398 }
    399 
    400 template <class _CharT, class _Traits>
    401 void
    402 basic_streambuf<_CharT, _Traits>::swap(basic_streambuf& __sb)
    403 {
    404     _VSTD::swap(__loc_, __sb.__loc_);
    405     _VSTD::swap(__binp_, __sb.__binp_);
    406     _VSTD::swap(__ninp_, __sb.__ninp_);
    407     _VSTD::swap(__einp_, __sb.__einp_);
    408     _VSTD::swap(__bout_, __sb.__bout_);
    409     _VSTD::swap(__nout_, __sb.__nout_);
    410     _VSTD::swap(__eout_, __sb.__eout_);
    411 }
    412 
    413 template <class _CharT, class _Traits>
    414 inline _LIBCPP_INLINE_VISIBILITY
    415 void
    416 basic_streambuf<_CharT, _Traits>::gbump(int __n)
    417 {
    418     __ninp_ += __n;
    419 }
    420 
    421 template <class _CharT, class _Traits>
    422 inline _LIBCPP_INLINE_VISIBILITY
    423 void
    424 basic_streambuf<_CharT, _Traits>::setg(char_type* __gbeg, char_type* __gnext,
    425                                                           char_type* __gend)
    426 {
    427     __binp_ = __gbeg;
    428     __ninp_ = __gnext;
    429     __einp_ = __gend;
    430 }
    431 
    432 template <class _CharT, class _Traits>
    433 inline _LIBCPP_INLINE_VISIBILITY
    434 void
    435 basic_streambuf<_CharT, _Traits>::pbump(int __n)
    436 {
    437     __nout_ += __n;
    438 }
    439 
    440 template <class _CharT, class _Traits>
    441 inline _LIBCPP_INLINE_VISIBILITY
    442 void
    443 basic_streambuf<_CharT, _Traits>::setp(char_type* __pbeg, char_type* __pend)
    444 {
    445     __bout_ = __nout_ = __pbeg;
    446     __eout_ = __pend;
    447 }
    448 
    449 template <class _CharT, class _Traits>
    450 void
    451 basic_streambuf<_CharT, _Traits>::imbue(const locale&)
    452 {
    453 }
    454 
    455 template <class _CharT, class _Traits>
    456 basic_streambuf<_CharT, _Traits>*
    457 basic_streambuf<_CharT, _Traits>::setbuf(char_type*, streamsize)
    458 {
    459     return this;
    460 }
    461 
    462 template <class _CharT, class _Traits>
    463 typename basic_streambuf<_CharT, _Traits>::pos_type
    464 basic_streambuf<_CharT, _Traits>::seekoff(off_type, ios_base::seekdir,
    465                                           ios_base::openmode)
    466 {
    467     return pos_type(off_type(-1));
    468 }
    469 
    470 template <class _CharT, class _Traits>
    471 typename basic_streambuf<_CharT, _Traits>::pos_type
    472 basic_streambuf<_CharT, _Traits>::seekpos(pos_type, ios_base::openmode)
    473 {
    474     return pos_type(off_type(-1));
    475 }
    476 
    477 template <class _CharT, class _Traits>
    478 int
    479 basic_streambuf<_CharT, _Traits>::sync()
    480 {
    481     return 0;
    482 }
    483 
    484 template <class _CharT, class _Traits>
    485 streamsize
    486 basic_streambuf<_CharT, _Traits>::showmanyc()
    487 {
    488     return 0;
    489 }
    490 
    491 template <class _CharT, class _Traits>
    492 streamsize
    493 basic_streambuf<_CharT, _Traits>::xsgetn(char_type* __s, streamsize __n)
    494 {
    495     const int_type __eof = traits_type::eof();
    496     int_type __c;
    497     streamsize __i = 0;
    498     for (;__i < __n; ++__i, ++__s)
    499     {
    500         if (__ninp_ < __einp_)
    501             *__s = *__ninp_++;
    502         else if ((__c = uflow()) != __eof)
    503             *__s = traits_type::to_char_type(__c);
    504         else
    505             break;
    506     }
    507     return __i;
    508 }
    509 
    510 template <class _CharT, class _Traits>
    511 typename basic_streambuf<_CharT, _Traits>::int_type
    512 basic_streambuf<_CharT, _Traits>::underflow()
    513 {
    514     return traits_type::eof();
    515 }
    516 
    517 template <class _CharT, class _Traits>
    518 typename basic_streambuf<_CharT, _Traits>::int_type
    519 basic_streambuf<_CharT, _Traits>::uflow()
    520 {
    521     if (underflow() == traits_type::eof())
    522         return traits_type::eof();
    523     return traits_type::to_int_type(*__ninp_++);
    524 }
    525 
    526 template <class _CharT, class _Traits>
    527 typename basic_streambuf<_CharT, _Traits>::int_type
    528 basic_streambuf<_CharT, _Traits>::pbackfail(int_type)
    529 {
    530     return traits_type::eof();
    531 }
    532 
    533 template <class _CharT, class _Traits>
    534 streamsize
    535 basic_streambuf<_CharT, _Traits>::xsputn(const char_type* __s, streamsize __n)
    536 {
    537     streamsize __i = 0;
    538     int_type __eof = traits_type::eof();
    539     for (; __i < __n; ++__s, ++__i)
    540     {
    541         if (__nout_ < __eout_)
    542             *__nout_++ = *__s;
    543         else if (overflow(traits_type::to_int_type(*__s)) == __eof)
    544             break;
    545     }
    546     return __i;
    547 }
    548 
    549 template <class _CharT, class _Traits>
    550 typename basic_streambuf<_CharT, _Traits>::int_type
    551 basic_streambuf<_CharT, _Traits>::overflow(int_type)
    552 {
    553     return traits_type::eof();
    554 }
    555 
    556 _LIBCPP_EXTERN_TEMPLATE(class basic_streambuf<char>)
    557 _LIBCPP_EXTERN_TEMPLATE(class basic_streambuf<wchar_t>)
    558 
    559 _LIBCPP_EXTERN_TEMPLATE(class basic_ios<char>)
    560 _LIBCPP_EXTERN_TEMPLATE(class basic_ios<wchar_t>)
    561 
    562 _LIBCPP_END_NAMESPACE_STD
    563 
    564 #endif  // _LIBCPP_STEAMBUF
    565