libstdc++
sstream.tcc
Go to the documentation of this file.
00001 // String based streams -*- C++ -*-
00002 
00003 // Copyright (C) 1997-2017 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 3, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 /** @file bits/sstream.tcc
00026  *  This is an internal header file, included by other library headers.
00027  *  Do not attempt to use it directly. @headername{sstream}
00028  */
00029 
00030 //
00031 // ISO C++ 14882: 27.7  String-based streams
00032 //
00033 
00034 #ifndef _SSTREAM_TCC
00035 #define _SSTREAM_TCC 1
00036 
00037 #pragma GCC system_header
00038 
00039 namespace std _GLIBCXX_VISIBILITY(default)
00040 {
00041 _GLIBCXX_BEGIN_NAMESPACE_VERSION
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 
00092 #if _GLIBCXX_USE_CXX11_ABI
00093       if ((this->epptr() - this->pbase()) < __capacity)
00094         {
00095           // There is additional capacity in _M_string that can be used.
00096           char_type* __base = const_cast<char_type*>(_M_string.data());
00097           _M_pbump(__base, __base + __capacity, this->pptr() - this->pbase());
00098           if (_M_mode & ios_base::in)
00099             {
00100               const __size_type __nget = this->gptr() - this->eback();
00101               const __size_type __eget = this->egptr() - this->eback();
00102               this->setg(__base, __base + __nget, __base + __eget + 1);
00103             }
00104           *this->pptr() = traits_type::to_char_type(__c);
00105           this->pbump(1);
00106           return __c;
00107         }
00108 #endif
00109 
00110       const __size_type __max_size = _M_string.max_size();
00111       const bool __testput = this->pptr() < this->epptr();
00112       if (__builtin_expect(!__testput && __capacity == __max_size, false))
00113         return traits_type::eof();
00114 
00115       // Try to append __c into output sequence in one of two ways.
00116       // Order these tests done in is unspecified by the standard.
00117       const char_type __conv = traits_type::to_char_type(__c);
00118       if (!__testput)
00119         {
00120           // NB: Start ostringstream buffers at 512 chars.  This is an
00121           // experimental value (pronounced "arbitrary" in some of the
00122           // hipper English-speaking countries), and can be changed to
00123           // suit particular needs.
00124           //
00125           // _GLIBCXX_RESOLVE_LIB_DEFECTS
00126           // 169. Bad efficiency of overflow() mandated
00127           // 432. stringbuf::overflow() makes only one write position
00128           //      available
00129           const __size_type __opt_len = std::max(__size_type(2 * __capacity),
00130                                                  __size_type(512));
00131           const __size_type __len = std::min(__opt_len, __max_size);
00132           __string_type __tmp;
00133           __tmp.reserve(__len);
00134           if (this->pbase())
00135             __tmp.assign(this->pbase(), this->epptr() - this->pbase());
00136           __tmp.push_back(__conv);
00137           _M_string.swap(__tmp);
00138           _M_sync(const_cast<char_type*>(_M_string.data()),
00139                   this->gptr() - this->eback(), this->pptr() - this->pbase());
00140         }
00141       else
00142         *this->pptr() = __conv;
00143       this->pbump(1);
00144       return __c;
00145     }
00146 
00147   template <class _CharT, class _Traits, class _Alloc>
00148     typename basic_stringbuf<_CharT, _Traits, _Alloc>::int_type
00149     basic_stringbuf<_CharT, _Traits, _Alloc>::
00150     underflow()
00151     {
00152       int_type __ret = traits_type::eof();
00153       const bool __testin = this->_M_mode & ios_base::in;
00154       if (__testin)
00155         {
00156           // Update egptr() to match the actual string end.
00157           _M_update_egptr();
00158 
00159           if (this->gptr() < this->egptr())
00160             __ret = traits_type::to_int_type(*this->gptr());
00161         }
00162       return __ret;
00163     }
00164 
00165   template <class _CharT, class _Traits, class _Alloc>
00166     typename basic_stringbuf<_CharT, _Traits, _Alloc>::pos_type
00167     basic_stringbuf<_CharT, _Traits, _Alloc>::
00168     seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode __mode)
00169     {
00170       pos_type __ret =  pos_type(off_type(-1));
00171       bool __testin = (ios_base::in & this->_M_mode & __mode) != 0;
00172       bool __testout = (ios_base::out & this->_M_mode & __mode) != 0;
00173       const bool __testboth = __testin && __testout && __way != ios_base::cur;
00174       __testin &= !(__mode & ios_base::out);
00175       __testout &= !(__mode & ios_base::in);
00176 
00177       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00178       // 453. basic_stringbuf::seekoff need not always fail for an empty stream.
00179       const char_type* __beg = __testin ? this->eback() : this->pbase();
00180       if ((__beg || !__off) && (__testin || __testout || __testboth))
00181         {
00182           _M_update_egptr();
00183 
00184           off_type __newoffi = __off;
00185           off_type __newoffo = __newoffi;
00186           if (__way == ios_base::cur)
00187             {
00188               __newoffi += this->gptr() - __beg;
00189               __newoffo += this->pptr() - __beg;
00190             }
00191           else if (__way == ios_base::end)
00192             __newoffo = __newoffi += this->egptr() - __beg;
00193 
00194           if ((__testin || __testboth)
00195               && __newoffi >= 0
00196               && this->egptr() - __beg >= __newoffi)
00197             {
00198               this->setg(this->eback(), this->eback() + __newoffi,
00199                          this->egptr());
00200               __ret = pos_type(__newoffi);
00201             }
00202           if ((__testout || __testboth)
00203               && __newoffo >= 0
00204               && this->egptr() - __beg >= __newoffo)
00205             {
00206               _M_pbump(this->pbase(), this->epptr(), __newoffo);
00207               __ret = pos_type(__newoffo);
00208             }
00209         }
00210       return __ret;
00211     }
00212 
00213   template <class _CharT, class _Traits, class _Alloc>
00214     typename basic_stringbuf<_CharT, _Traits, _Alloc>::pos_type
00215     basic_stringbuf<_CharT, _Traits, _Alloc>::
00216     seekpos(pos_type __sp, ios_base::openmode __mode)
00217     {
00218       pos_type __ret =  pos_type(off_type(-1));
00219       const bool __testin = (ios_base::in & this->_M_mode & __mode) != 0;
00220       const bool __testout = (ios_base::out & this->_M_mode & __mode) != 0;
00221 
00222       const char_type* __beg = __testin ? this->eback() : this->pbase();
00223       if ((__beg || !off_type(__sp)) && (__testin || __testout))
00224         {
00225           _M_update_egptr();
00226 
00227           const off_type __pos(__sp);
00228           const bool __testpos = (0 <= __pos
00229                                   && __pos <= this->egptr() - __beg);
00230           if (__testpos)
00231             {
00232               if (__testin)
00233                 this->setg(this->eback(), this->eback() + __pos,
00234                            this->egptr());
00235               if (__testout)
00236                 _M_pbump(this->pbase(), this->epptr(), __pos);
00237               __ret = __sp;
00238             }
00239         }
00240       return __ret;
00241     }
00242 
00243   template <class _CharT, class _Traits, class _Alloc>
00244     void
00245     basic_stringbuf<_CharT, _Traits, _Alloc>::
00246     _M_sync(char_type* __base, __size_type __i, __size_type __o)
00247     {
00248       const bool __testin = _M_mode & ios_base::in;
00249       const bool __testout = _M_mode & ios_base::out;
00250       char_type* __endg = __base + _M_string.size();
00251       char_type* __endp = __base + _M_string.capacity();
00252 
00253       if (__base != _M_string.data())
00254         {
00255           // setbuf: __i == size of buffer area (_M_string.size() == 0).
00256           __endg += __i;
00257           __i = 0;
00258           __endp = __endg;
00259         }
00260 
00261       if (__testin)
00262         this->setg(__base, __base + __i, __endg);
00263       if (__testout)
00264         {
00265           _M_pbump(__base, __endp, __o);
00266           // egptr() always tracks the string end.  When !__testin,
00267           // for the correct functioning of the streambuf inlines
00268           // the other get area pointers are identical.
00269           if (!__testin)
00270             this->setg(__endg, __endg, __endg);
00271         }
00272     }
00273 
00274   template <class _CharT, class _Traits, class _Alloc>
00275     void
00276     basic_stringbuf<_CharT, _Traits, _Alloc>::
00277     _M_pbump(char_type* __pbeg, char_type* __pend, off_type __off)
00278     {
00279       this->setp(__pbeg, __pend);
00280       while (__off > __gnu_cxx::__numeric_traits<int>::__max)
00281         {
00282           this->pbump(__gnu_cxx::__numeric_traits<int>::__max);
00283           __off -= __gnu_cxx::__numeric_traits<int>::__max;
00284         }
00285       this->pbump(__off);
00286     }
00287 
00288   // Inhibit implicit instantiations for required instantiations,
00289   // which are defined via explicit instantiations elsewhere.
00290 #if _GLIBCXX_EXTERN_TEMPLATE
00291   extern template class basic_stringbuf<char>;
00292   extern template class basic_istringstream<char>;
00293   extern template class basic_ostringstream<char>;
00294   extern template class basic_stringstream<char>;
00295 
00296 #ifdef _GLIBCXX_USE_WCHAR_T
00297   extern template class basic_stringbuf<wchar_t>;
00298   extern template class basic_istringstream<wchar_t>;
00299   extern template class basic_ostringstream<wchar_t>;
00300   extern template class basic_stringstream<wchar_t>;
00301 #endif
00302 #endif
00303 
00304 _GLIBCXX_END_NAMESPACE_VERSION
00305 } // namespace std
00306 
00307 #endif