# 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
    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 2, 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 // You should have received a copy of the GNU General Public License along
    00019 // with this library; see the file COPYING.  If not, write to the Free
    00020 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
    00021 // USA.
    00022
    00023 // As a special exception, you may use this file as part of a free software
    00024 // library without restriction.  Specifically, if other files instantiate
    00025 // templates or use macros or inline functions from this file, or you compile
    00026 // this file and link it with other files to produce an executable, this
    00027 // file does not by itself cause the resulting executable to be covered by
    00028 // the GNU General Public License.  This exception does not however
    00029 // invalidate any other reasons why the executable file might be covered by
    00030 // the GNU General Public License.
    00031
    00032 /** @file sstream.tcc
    00033  *  This is an internal header file, included by other library headers.
    00034  *  You should not attempt to use it directly.
    00035  */
    00036
    00037 //
    00038 // ISO C++ 14882: 27.7  String-based streams
    00039 //
    00040
    00041 #ifndef _SSTREAM_TCC
    00042 #define _SSTREAM_TCC 1
    00043
    00044 #pragma GCC system_header
    00045
    00046 _GLIBCXX_BEGIN_NAMESPACE(std)
    00047
    00048   template <class _CharT, class _Traits, class _Alloc>
    00049     typename basic_stringbuf<_CharT, _Traits, _Alloc>::int_type
    00050     basic_stringbuf<_CharT, _Traits, _Alloc>::
    00051     pbackfail(int_type __c)
    00052     {
    00053       int_type __ret = traits_type::eof();
    00054       if (this->eback() < this->gptr())
    00055     {
    00056       // Try to put back __c into input sequence in one of three ways.
    00057       // Order these tests done in is unspecified by the standard.
    00058       const bool __testeof = traits_type::eq_int_type(__c, __ret);
    00059       if (!__testeof)
    00060         {
    00061           const bool __testeq = traits_type::eq(traits_type::
    00062                             to_char_type(__c),
    00063                             this->gptr()[-1]);
    00064           const bool __testout = this->_M_mode & ios_base::out;
    00065           if (__testeq || __testout)
    00066         {
    00067           this->gbump(-1);
    00068           if (!__testeq)
    00069             *this->gptr() = traits_type::to_char_type(__c);
    00070           __ret = __c;
    00071         }
    00072         }
    00073       else
    00074         {
    00075           this->gbump(-1);
    00076           __ret = traits_type::not_eof(__c);
    00077         }
    00078     }
    00079       return __ret;
    00080     }
    00081
    00082   template <class _CharT, class _Traits, class _Alloc>
    00083     typename basic_stringbuf<_CharT, _Traits, _Alloc>::int_type
    00084     basic_stringbuf<_CharT, _Traits, _Alloc>::
    00085     overflow(int_type __c)
    00086     {
    00087       const bool __testout = this->_M_mode & ios_base::out;
    00088       if (__builtin_expect(!__testout, false))
    00089     return traits_type::eof();
    00090
    00091       const bool __testeof = traits_type::eq_int_type(__c, traits_type::eof());
    00092       if (__builtin_expect(__testeof, false))
    00093     return traits_type::not_eof(__c);
    00094
    00095       const __size_type __capacity = _M_string.capacity();
    00096       const __size_type __max_size = _M_string.max_size();
    00097       const bool __testput = this->pptr() < this->epptr();
    00098       if (__builtin_expect(!__testput && __capacity == __max_size, false))
    00099     return traits_type::eof();
    00100
    00101       // Try to append __c into output sequence in one of two ways.
    00102       // Order these tests done in is unspecified by the standard.
    00103       const char_type __conv = traits_type::to_char_type(__c);
    00104       if (!__testput)
    00105     {
    00106       // NB: Start ostringstream buffers at 512 chars.  This is an
    00107       // experimental value (pronounced "arbitrary" in some of the
    00108       // hipper English-speaking countries), and can be changed to
    00109       // suit particular needs.
    00110       //
    00111       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00112       // 169. Bad efficiency of overflow() mandated
    00113       // 432. stringbuf::overflow() makes only one write position
    00114       //      available
    00115       const __size_type __opt_len = std::max(__size_type(2 * __capacity),
    00116                          __size_type(512));
    00117       const __size_type __len = std::min(__opt_len, __max_size);
    00118       __string_type __tmp;
    00119       __tmp.reserve(__len);
    00120       if (this->pbase())
    00121         __tmp.assign(this->pbase(), this->epptr() - this->pbase());
    00122       __tmp.push_back(__conv);
    00123       _M_string.swap(__tmp);
    00124       _M_sync(const_cast<char_type*>(_M_string.data()),
    00125           this->gptr() - this->eback(), this->pptr() - this->pbase());
    00126     }
    00127       else
    00128     *this->pptr() = __conv;
    00129       this->pbump(1);
    00130       return __c;
    00131     }
    00132
    00133   template <class _CharT, class _Traits, class _Alloc>
    00134     typename basic_stringbuf<_CharT, _Traits, _Alloc>::int_type
    00135     basic_stringbuf<_CharT, _Traits, _Alloc>::
    00136     underflow()
    00137     {
    00138       int_type __ret = traits_type::eof();
    00139       const bool __testin = this->_M_mode & ios_base::in;
    00140       if (__testin)
    00141     {
    00142       // Update egptr() to match the actual string end.
    00143       _M_update_egptr();
    00144
    00145       if (this->gptr() < this->egptr())
    00146         __ret = traits_type::to_int_type(*this->gptr());
    00147     }
    00148       return __ret;
    00149     }
    00150
    00151   template <class _CharT, class _Traits, class _Alloc>
    00152     typename basic_stringbuf<_CharT, _Traits, _Alloc>::pos_type
    00153     basic_stringbuf<_CharT, _Traits, _Alloc>::
    00154     seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode __mode)
    00155     {
    00156       pos_type __ret =  pos_type(off_type(-1));
    00157       bool __testin = (ios_base::in & this->_M_mode & __mode) != 0;
    00158       bool __testout = (ios_base::out & this->_M_mode & __mode) != 0;
    00159       const bool __testboth = __testin && __testout && __way != ios_base::cur;
    00160       __testin &= !(__mode & ios_base::out);
    00161       __testout &= !(__mode & ios_base::in);
    00162
    00163       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00164       // 453. basic_stringbuf::seekoff need not always fail for an empty stream.
    00165       const char_type* __beg = __testin ? this->eback() : this->pbase();
    00166       if ((__beg || !__off) && (__testin || __testout || __testboth))
    00167     {
    00168       _M_update_egptr();
    00169
    00170       off_type __newoffi = __off;
    00171       off_type __newoffo = __newoffi;
    00172       if (__way == ios_base::cur)
    00173         {
    00174           __newoffi += this->gptr() - __beg;
    00175           __newoffo += this->pptr() - __beg;
    00176         }
    00177       else if (__way == ios_base::end)
    00178         __newoffo = __newoffi += this->egptr() - __beg;
    00179
    00180       if ((__testin || __testboth)
    00181           && __newoffi >= 0
    00182           && this->egptr() - __beg >= __newoffi)
    00183         {
    00184           this->gbump((__beg + __newoffi) - this->gptr());
    00185           __ret = pos_type(__newoffi);
    00186         }
    00187       if ((__testout || __testboth)
    00188           && __newoffo >= 0
    00189           && this->egptr() - __beg >= __newoffo)
    00190         {
    00191           this->pbump((__beg + __newoffo) - this->pptr());
    00192           __ret = pos_type(__newoffo);
    00193         }
    00194     }
    00195       return __ret;
    00196     }
    00197
    00198   template <class _CharT, class _Traits, class _Alloc>
    00199     typename basic_stringbuf<_CharT, _Traits, _Alloc>::pos_type
    00200     basic_stringbuf<_CharT, _Traits, _Alloc>::
    00201     seekpos(pos_type __sp, ios_base::openmode __mode)
    00202     {
    00203       pos_type __ret =  pos_type(off_type(-1));
    00204       const bool __testin = (ios_base::in & this->_M_mode & __mode) != 0;
    00205       const bool __testout = (ios_base::out & this->_M_mode & __mode) != 0;
    00206
    00207       const char_type* __beg = __testin ? this->eback() : this->pbase();
    00208       if ((__beg || !off_type(__sp)) && (__testin || __testout))
    00209     {
    00210       _M_update_egptr();
    00211
    00212       const off_type __pos(__sp);
    00213       const bool __testpos = (0 <= __pos
    00214                   && __pos <= this->egptr() - __beg);
    00215       if (__testpos)
    00216         {
    00217           if (__testin)
    00218         this->gbump((__beg + __pos) - this->gptr());
    00219           if (__testout)
    00220                 this->pbump((__beg + __pos) - this->pptr());
    00221           __ret = __sp;
    00222         }
    00223     }
    00224       return __ret;
    00225     }
    00226
    00227   template <class _CharT, class _Traits, class _Alloc>
    00228     void
    00229     basic_stringbuf<_CharT, _Traits, _Alloc>::
    00230     _M_sync(char_type* __base, __size_type __i, __size_type __o)
    00231     {
    00232       const bool __testin = _M_mode & ios_base::in;
    00233       const bool __testout = _M_mode & ios_base::out;
    00234       char_type* __endg = __base + _M_string.size();
    00235       char_type* __endp = __base + _M_string.capacity();
    00236
    00237       if (__base != _M_string.data())
    00238     {
    00239       // setbuf: __i == size of buffer area (_M_string.size() == 0).
    00240       __endg += __i;
    00241       __i = 0;
    00242       __endp = __endg;
    00243     }
    00244
    00245       if (__testin)
    00246     this->setg(__base, __base + __i, __endg);
    00247       if (__testout)
    00248     {
    00249       this->setp(__base, __endp);
    00250       this->pbump(__o);
    00251       // egptr() always tracks the string end.  When !__testin,
    00252       // for the correct functioning of the streambuf inlines
    00253       // the other get area pointers are identical.
    00254       if (!__testin)
    00255         this->setg(__endg, __endg, __endg);
    00256     }
    00257     }
    00258
    00259   // Inhibit implicit instantiations for required instantiations,
    00260   // which are defined via explicit instantiations elsewhere.
    00261   // NB:  This syntax is a GNU extension.
    00262 #if _GLIBCXX_EXTERN_TEMPLATE
    00263   extern template class basic_stringbuf<char>;
    00264   extern template class basic_istringstream<char>;
    00265   extern template class basic_ostringstream<char>;
    00266   extern template class basic_stringstream<char>;
    00267
    00268 #ifdef _GLIBCXX_USE_WCHAR_T
    00269   extern template class basic_stringbuf<wchar_t>;
    00270   extern template class basic_istringstream<wchar_t>;
    00271   extern template class basic_ostringstream<wchar_t>;
    00272   extern template class basic_stringstream<wchar_t>;
    00273 #endif
    00274 #endif
    00275
    00276 _GLIBCXX_END_NAMESPACE
    00277
    00278 #endif