# sstream.tcc

Go to the documentation of this file.

    00001 // String based streams -*- C++ -*-
    00002
    00003 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
    00004 // 2006, 2007, 2009
    00005 // Free Software Foundation, Inc.
    00006 //
    00007 // This file is part of the GNU ISO C++ Library.  This library is free
    00008 // software; you can redistribute it and/or modify it under the
    00009 // terms of the GNU General Public License as published by the
    00010 // Free Software Foundation; either version 3, or (at your option)
    00011 // any later version.
    00012
    00013 // This library is distributed in the hope that it will be useful,
    00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00016 // GNU General Public License for more details.
    00017
    00018 // Under Section 7 of GPL version 3, you are granted additional
    00019 // permissions described in the GCC Runtime Library Exception, version
    00020 // 3.1, as published by the Free Software Foundation.
    00021
    00022 // You should have received a copy of the GNU General Public License and
    00023 // a copy of the GCC Runtime Library Exception along with this program;
    00024 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
    00025 // <http://www.gnu.org/licenses/>.
    00026
    00027 /** @file sstream.tcc
    00028  *  This is an internal header file, included by other library headers.
    00029  *  You should not attempt to use it directly.
    00030  */
    00031
    00032 //
    00033 // ISO C++ 14882: 27.7  String-based streams
    00034 //
    00035
    00036 #ifndef _SSTREAM_TCC
    00037 #define _SSTREAM_TCC 1
    00038
    00039 #pragma GCC system_header
    00040
    00041 _GLIBCXX_BEGIN_NAMESPACE(std)
    00042
    00043   template <class _CharT, class _Traits, class _Alloc>
    00044     typename basic_stringbuf<_CharT, _Traits, _Alloc>::int_type
    00045     basic_stringbuf<_CharT, _Traits, _Alloc>::
    00046     pbackfail(int_type __c)
    00047     {
    00048       int_type __ret = traits_type::eof();
    00049       if (this->eback() < this->gptr())
    00050     {
    00051       // Try to put back __c into input sequence in one of three ways.
    00052       // Order these tests done in is unspecified by the standard.
    00053       const bool __testeof = traits_type::eq_int_type(__c, __ret);
    00054       if (!__testeof)
    00055         {
    00056           const bool __testeq = traits_type::eq(traits_type::
    00057                             to_char_type(__c),
    00058                             this->gptr()[-1]);
    00059           const bool __testout = this->_M_mode & ios_base::out;
    00060           if (__testeq || __testout)
    00061         {
    00062           this->gbump(-1);
    00063           if (!__testeq)
    00064             *this->gptr() = traits_type::to_char_type(__c);
    00065           __ret = __c;
    00066         }
    00067         }
    00068       else
    00069         {
    00070           this->gbump(-1);
    00071           __ret = traits_type::not_eof(__c);
    00072         }
    00073     }
    00074       return __ret;
    00075     }
    00076
    00077   template <class _CharT, class _Traits, class _Alloc>
    00078     typename basic_stringbuf<_CharT, _Traits, _Alloc>::int_type
    00079     basic_stringbuf<_CharT, _Traits, _Alloc>::
    00080     overflow(int_type __c)
    00081     {
    00082       const bool __testout = this->_M_mode & ios_base::out;
    00083       if (__builtin_expect(!__testout, false))
    00084     return traits_type::eof();
    00085
    00086       const bool __testeof = traits_type::eq_int_type(__c, traits_type::eof());
    00087       if (__builtin_expect(__testeof, false))
    00088     return traits_type::not_eof(__c);
    00089
    00090       const __size_type __capacity = _M_string.capacity();
    00091       const __size_type __max_size = _M_string.max_size();
    00092       const bool __testput = this->pptr() < this->epptr();
    00093       if (__builtin_expect(!__testput && __capacity == __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       const char_type __conv = traits_type::to_char_type(__c);
    00099       if (!__testput)
    00100     {
    00101       // NB: Start ostringstream buffers at 512 chars.  This is an
    00102       // experimental value (pronounced "arbitrary" in some of the
    00103       // hipper English-speaking countries), and can be changed to
    00104       // suit particular needs.
    00105       //
    00106       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00107       // 169. Bad efficiency of overflow() mandated
    00108       // 432. stringbuf::overflow() makes only one write position
    00109       //      available
    00110       const __size_type __opt_len = std::max(__size_type(2 * __capacity),
    00111                          __size_type(512));
    00112       const __size_type __len = std::min(__opt_len, __max_size);
    00113       __string_type __tmp;
    00114       __tmp.reserve(__len);
    00115       if (this->pbase())
    00116         __tmp.assign(this->pbase(), this->epptr() - this->pbase());
    00117       __tmp.push_back(__conv);
    00118       _M_string.swap(__tmp);
    00119       _M_sync(const_cast<char_type*>(_M_string.data()),
    00120           this->gptr() - this->eback(), this->pptr() - this->pbase());
    00121     }
    00122       else
    00123     *this->pptr() = __conv;
    00124       this->pbump(1);
    00125       return __c;
    00126     }
    00127
    00128   template <class _CharT, class _Traits, class _Alloc>
    00129     typename basic_stringbuf<_CharT, _Traits, _Alloc>::int_type
    00130     basic_stringbuf<_CharT, _Traits, _Alloc>::
    00131     underflow()
    00132     {
    00133       int_type __ret = traits_type::eof();
    00134       const bool __testin = this->_M_mode & ios_base::in;
    00135       if (__testin)
    00136     {
    00137       // Update egptr() to match the actual string end.
    00138       _M_update_egptr();
    00139
    00140       if (this->gptr() < this->egptr())
    00141         __ret = traits_type::to_int_type(*this->gptr());
    00142     }
    00143       return __ret;
    00144     }
    00145
    00146   template <class _CharT, class _Traits, class _Alloc>
    00147     typename basic_stringbuf<_CharT, _Traits, _Alloc>::pos_type
    00148     basic_stringbuf<_CharT, _Traits, _Alloc>::
    00149     seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode __mode)
    00150     {
    00151       pos_type __ret =  pos_type(off_type(-1));
    00152       bool __testin = (ios_base::in & this->_M_mode & __mode) != 0;
    00153       bool __testout = (ios_base::out & this->_M_mode & __mode) != 0;
    00154       const bool __testboth = __testin && __testout && __way != ios_base::cur;
    00155       __testin &= !(__mode & ios_base::out);
    00156       __testout &= !(__mode & ios_base::in);
    00157
    00158       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00159       // 453. basic_stringbuf::seekoff need not always fail for an empty stream.
    00160       const char_type* __beg = __testin ? this->eback() : this->pbase();
    00161       if ((__beg || !__off) && (__testin || __testout || __testboth))
    00162     {
    00163       _M_update_egptr();
    00164
    00165       off_type __newoffi = __off;
    00166       off_type __newoffo = __newoffi;
    00167       if (__way == ios_base::cur)
    00168         {
    00169           __newoffi += this->gptr() - __beg;
    00170           __newoffo += this->pptr() - __beg;
    00171         }
    00172       else if (__way == ios_base::end)
    00173         __newoffo = __newoffi += this->egptr() - __beg;
    00174
    00175       if ((__testin || __testboth)
    00176           && __newoffi >= 0
    00177           && this->egptr() - __beg >= __newoffi)
    00178         {
    00179           this->gbump((__beg + __newoffi) - this->gptr());
    00180           __ret = pos_type(__newoffi);
    00181         }
    00182       if ((__testout || __testboth)
    00183           && __newoffo >= 0
    00184           && this->egptr() - __beg >= __newoffo)
    00185         {
    00186           this->pbump((__beg + __newoffo) - this->pptr());
    00187           __ret = pos_type(__newoffo);
    00188         }
    00189     }
    00190       return __ret;
    00191     }
    00192
    00193   template <class _CharT, class _Traits, class _Alloc>
    00194     typename basic_stringbuf<_CharT, _Traits, _Alloc>::pos_type
    00195     basic_stringbuf<_CharT, _Traits, _Alloc>::
    00196     seekpos(pos_type __sp, ios_base::openmode __mode)
    00197     {
    00198       pos_type __ret =  pos_type(off_type(-1));
    00199       const bool __testin = (ios_base::in & this->_M_mode & __mode) != 0;
    00200       const bool __testout = (ios_base::out & this->_M_mode & __mode) != 0;
    00201
    00202       const char_type* __beg = __testin ? this->eback() : this->pbase();
    00203       if ((__beg || !off_type(__sp)) && (__testin || __testout))
    00204     {
    00205       _M_update_egptr();
    00206
    00207       const off_type __pos(__sp);
    00208       const bool __testpos = (0 <= __pos
    00209                   && __pos <= this->egptr() - __beg);
    00210       if (__testpos)
    00211         {
    00212           if (__testin)
    00213         this->gbump((__beg + __pos) - this->gptr());
    00214           if (__testout)
    00215                 this->pbump((__beg + __pos) - this->pptr());
    00216           __ret = __sp;
    00217         }
    00218     }
    00219       return __ret;
    00220     }
    00221
    00222   template <class _CharT, class _Traits, class _Alloc>
    00223     void
    00224     basic_stringbuf<_CharT, _Traits, _Alloc>::
    00225     _M_sync(char_type* __base, __size_type __i, __size_type __o)
    00226     {
    00227       const bool __testin = _M_mode & ios_base::in;
    00228       const bool __testout = _M_mode & ios_base::out;
    00229       char_type* __endg = __base + _M_string.size();
    00230       char_type* __endp = __base + _M_string.capacity();
    00231
    00232       if (__base != _M_string.data())
    00233     {
    00234       // setbuf: __i == size of buffer area (_M_string.size() == 0).
    00235       __endg += __i;
    00236       __i = 0;
    00237       __endp = __endg;
    00238     }
    00239
    00240       if (__testin)
    00241     this->setg(__base, __base + __i, __endg);
    00242       if (__testout)
    00243     {
    00244       this->setp(__base, __endp);
    00245       this->pbump(__o);
    00246       // egptr() always tracks the string end.  When !__testin,
    00247       // for the correct functioning of the streambuf inlines
    00248       // the other get area pointers are identical.
    00249       if (!__testin)
    00250         this->setg(__endg, __endg, __endg);
    00251     }
    00252     }
    00253
    00254   // Inhibit implicit instantiations for required instantiations,
    00255   // which are defined via explicit instantiations elsewhere.
    00256   // NB:  This syntax is a GNU extension.
    00257 #if _GLIBCXX_EXTERN_TEMPLATE
    00258   extern template class basic_stringbuf<char>;
    00259   extern template class basic_istringstream<char>;
    00260   extern template class basic_ostringstream<char>;
    00261   extern template class basic_stringstream<char>;
    00262
    00263 #ifdef _GLIBCXX_USE_WCHAR_T
    00264   extern template class basic_stringbuf<wchar_t>;
    00265   extern template class basic_istringstream<wchar_t>;
    00266   extern template class basic_ostringstream<wchar_t>;
    00267   extern template class basic_stringstream<wchar_t>;
    00268 #endif
    00269 #endif
    00270
    00271 _GLIBCXX_END_NAMESPACE
    00272
    00273 #endif