# sstream.tcc

    00001 // String based streams -*- C++ -*-
    00002
    00003 // Copyright (C) 1997, 1998, 1999, 2001, 2002, 2003
    00004 // Free Software Foundation, Inc.
    00005 //
    00006 // This file is part of the GNU ISO C++ Library.  This library is free
    00007 // software; you can redistribute it and/or modify it under the
    00008 // terms of the GNU General Public License as published by the
    00009 // Free Software Foundation; either version 2, or (at your option)
    00010 // any later version.
    00011
    00012 // This library is distributed in the hope that it will be useful,
    00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00015 // GNU General Public License for more details.
    00016
    00017 // You should have received a copy of the GNU General Public License along
    00018 // with this library; see the file COPYING.  If not, write to the Free
    00019 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
    00020 // USA.
    00021
    00022 // As a special exception, you may use this file as part of a free software
    00023 // library without restriction.  Specifically, if other files instantiate
    00024 // templates or use macros or inline functions from this file, or you compile
    00025 // this file and link it with other files to produce an executable, this
    00026 // file does not by itself cause the resulting executable to be covered by
    00027 // the GNU General Public License.  This exception does not however
    00028 // invalidate any other reasons why the executable file might be covered by
    00029 // the GNU General Public License.
    00030
    00031 //
    00032 // ISO C++ 14882: 27.7  String-based streams
    00033 //
    00034
    00035 #ifndef _SSTREAM_TCC
    00036 #define _SSTREAM_TCC 1
    00037
    00038 #pragma GCC system_header
    00039
    00040 #include <sstream>
    00041
    00042 namespace std
    00043 {
    00044   template <class _CharT, class _Traits, class _Alloc>
    00045     typename basic_stringbuf<_CharT, _Traits, _Alloc>::int_type
    00046     basic_stringbuf<_CharT, _Traits, _Alloc>::
    00047     pbackfail(int_type __c)
    00048     {
    00049       int_type __ret = traits_type::eof();
    00050       const bool __testeof = traits_type::eq_int_type(__c, __ret);
    00051
    00052       if (this->eback() < this->gptr())
    00053     {
    00054       const bool __testeq = traits_type::eq(traits_type::to_char_type(__c),
    00055                         this->gptr()[-1]);
    00056       this->gbump(-1);
    00057
    00058       // Try to put back __c into input sequence in one of three ways.
    00059       // Order these tests done in is unspecified by the standard.
    00060       if (!__testeof && __testeq)
    00061         __ret = __c;
    00062       else if (__testeof)
    00063         __ret = traits_type::not_eof(__c);
    00064       else
    00065         {
    00066           *this->gptr() = traits_type::to_char_type(__c);
    00067           __ret = __c;
    00068         }
    00069     }
    00070       return __ret;
    00071     }
    00072
    00073   template <class _CharT, class _Traits, class _Alloc>
    00074     typename basic_stringbuf<_CharT, _Traits, _Alloc>::int_type
    00075     basic_stringbuf<_CharT, _Traits, _Alloc>::
    00076     overflow(int_type __c)
    00077     {
    00078       const bool __testout = this->_M_mode & ios_base::out;
    00079       if (__builtin_expect(!__testout, false))
    00080     return traits_type::eof();
    00081
    00082       const bool __testeof = traits_type::eq_int_type(__c, traits_type::eof());
    00083       if (__builtin_expect(__testeof, false))
    00084     return traits_type::not_eof(__c);
    00085
    00086       // NB: Start ostringstream buffers at 512 chars. This is an
    00087       // experimental value (pronounced "arbitrary" in some of the
    00088       // hipper english-speaking countries), and can be changed to
    00089       // suit particular needs.
    00090       const __size_type __len = std::max(__size_type(_M_string.capacity() + 1),
    00091                      __size_type(512));
    00092       const bool __testput = this->pptr() < this->epptr();
    00093       if (__builtin_expect(!__testput && __len > _M_string.max_size(), false))
    00094     return traits_type::eof();
    00095
    00096       // Try to append __c into output sequence in one of two ways.
    00097       // Order these tests done in is unspecified by the standard.
    00098       if (!__testput)
    00099     {
    00100       // In virtue of DR 169 (TC) we are allowed to grow more than
    00101       // one char. That's easy to implement thanks to the exponential
    00102       // growth policy builtin into basic_string.
    00103       __string_type __tmp;
    00104       __tmp.reserve(__len);
    00105       __tmp.assign(_M_string.data(), this->epptr() - this->pbase());
    00106       _M_string.swap(__tmp);
    00107       _M_sync(const_cast<char_type*>(_M_string.data()),
    00108           this->gptr() - this->eback(), this->pptr() - this->pbase());
    00109     }
    00110       return this->sputc(traits_type::to_char_type(__c));
    00111     }
    00112
    00113   template <class _CharT, class _Traits, class _Alloc>
    00114     typename basic_stringbuf<_CharT, _Traits, _Alloc>::int_type
    00115     basic_stringbuf<_CharT, _Traits, _Alloc>::
    00116     underflow()
    00117     {
    00118       int_type __ret = traits_type::eof();
    00119       const bool __testin = this->_M_mode & ios_base::in;
    00120       if (__testin)
    00121     {
    00122       // Update egptr() to match the actual string end.
    00123       _M_update_egptr();
    00124       if (this->gptr() < this->egptr())
    00125         __ret = traits_type::to_int_type(*this->gptr());
    00126     }
    00127       return __ret;
    00128     }
    00129
    00130   template <class _CharT, class _Traits, class _Alloc>
    00131     typename basic_stringbuf<_CharT, _Traits, _Alloc>::pos_type
    00132     basic_stringbuf<_CharT, _Traits, _Alloc>::
    00133     seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode __mode)
    00134     {
    00135       pos_type __ret =  pos_type(off_type(-1));
    00136       bool __testin = (ios_base::in & this->_M_mode & __mode) != 0;
    00137       bool __testout = (ios_base::out & this->_M_mode & __mode) != 0;
    00138       const bool __testboth = __testin && __testout && __way != ios_base::cur;
    00139       __testin &= !(__mode & ios_base::out);
    00140       __testout &= !(__mode & ios_base::in);
    00141
    00142       if (_M_string.capacity() && (__testin || __testout || __testboth))
    00143     {
    00144       char_type* __beg = __testin ? this->eback() : this->pbase();
    00145
    00146       _M_update_egptr();
    00147
    00148       off_type __newoffi = 0;
    00149       off_type __newoffo = 0;
    00150       if (__way == ios_base::cur)
    00151         {
    00152           __newoffi = this->gptr() - __beg;
    00153           __newoffo = this->pptr() - __beg;
    00154         }
    00155       else if (__way == ios_base::end)
    00156         __newoffo = __newoffi = this->egptr() - __beg;
    00157
    00158       if ((__testin || __testboth)
    00159           && __newoffi + __off >= 0
    00160           && this->egptr() - __beg >= __newoffi + __off)
    00161         {
    00162           this->gbump((__beg + __newoffi + __off) - this->gptr());
    00163           __ret = pos_type(__newoffi);
    00164         }
    00165       if ((__testout || __testboth)
    00166           && __newoffo + __off >= 0
    00167           && this->egptr() - __beg >= __newoffo + __off)
    00168         {
    00169           this->pbump((__beg + __newoffo + __off) - this->pptr());
    00170           __ret = pos_type(__newoffo);
    00171         }
    00172     }
    00173       return __ret;
    00174     }
    00175
    00176   template <class _CharT, class _Traits, class _Alloc>
    00177     typename basic_stringbuf<_CharT, _Traits, _Alloc>::pos_type
    00178     basic_stringbuf<_CharT, _Traits, _Alloc>::
    00179     seekpos(pos_type __sp, ios_base::openmode __mode)
    00180     {
    00181       pos_type __ret =  pos_type(off_type(-1));
    00182       if (_M_string.capacity())
    00183     {
    00184       off_type __pos (__sp);
    00185       const bool __testin = (ios_base::in & this->_M_mode & __mode) != 0;
    00186       const bool __testout = (ios_base::out & this->_M_mode & __mode) != 0;
    00187       char_type* __beg = __testin ? this->eback() : this->pbase();
    00188
    00189       _M_update_egptr();
    00190
    00191       const bool __testpos = 0 <= __pos
    00192                              && __pos <=  this->egptr() - __beg;
    00193       if ((__testin || __testout) && __testpos)
    00194         {
    00195           if (__testin)
    00196         this->gbump((__beg + __pos) - this->gptr());
    00197           if (__testout)
    00198                 this->pbump((__beg + __pos) - this->pptr());
    00199           __ret = pos_type(off_type(__pos));
    00200         }
    00201     }
    00202       return __ret;
    00203     }
    00204
    00205   // Inhibit implicit instantiations for required instantiations,
    00206   // which are defined via explicit instantiations elsewhere.
    00207   // NB:  This syntax is a GNU extension.
    00208 #if _GLIBCXX_EXTERN_TEMPLATE
    00209   extern template class basic_stringbuf<char>;
    00210   extern template class basic_istringstream<char>;
    00211   extern template class basic_ostringstream<char>;
    00212   extern template class basic_stringstream<char>;
    00213
    00214 #ifdef _GLIBCXX_USE_WCHAR_T
    00215   extern template class basic_stringbuf<wchar_t>;
    00216   extern template class basic_istringstream<wchar_t>;
    00217   extern template class basic_ostringstream<wchar_t>;
    00218   extern template class basic_stringstream<wchar_t>;
    00219 #endif
    00220 #endif
    00221 } // namespace std
    00222
    00223 #endif