libstdc++
sstream.tcc
Go to the documentation of this file.
00001 // String based streams -*- C++ -*-
00002 
00003 // Copyright (C) 1997-2013 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       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->setg(this->eback(), this->eback() + __newoffi,
00180              this->egptr());
00181           __ret = pos_type(__newoffi);
00182         }
00183       if ((__testout || __testboth)
00184           && __newoffo >= 0
00185           && this->egptr() - __beg >= __newoffo)
00186         {
00187           _M_pbump(this->pbase(), this->epptr(), __newoffo);
00188           __ret = pos_type(__newoffo);
00189         }
00190     }
00191       return __ret;
00192     }
00193 
00194   template <class _CharT, class _Traits, class _Alloc>
00195     typename basic_stringbuf<_CharT, _Traits, _Alloc>::pos_type
00196     basic_stringbuf<_CharT, _Traits, _Alloc>::
00197     seekpos(pos_type __sp, ios_base::openmode __mode)
00198     {
00199       pos_type __ret =  pos_type(off_type(-1));
00200       const bool __testin = (ios_base::in & this->_M_mode & __mode) != 0;
00201       const bool __testout = (ios_base::out & this->_M_mode & __mode) != 0;
00202 
00203       const char_type* __beg = __testin ? this->eback() : this->pbase();
00204       if ((__beg || !off_type(__sp)) && (__testin || __testout))
00205     {
00206       _M_update_egptr();
00207 
00208       const off_type __pos(__sp);
00209       const bool __testpos = (0 <= __pos
00210                   && __pos <= this->egptr() - __beg);
00211       if (__testpos)
00212         {
00213           if (__testin)
00214         this->setg(this->eback(), this->eback() + __pos,
00215                this->egptr());
00216           if (__testout)
00217         _M_pbump(this->pbase(), this->epptr(), __pos);
00218           __ret = __sp;
00219         }
00220     }
00221       return __ret;
00222     }
00223 
00224   template <class _CharT, class _Traits, class _Alloc>
00225     void
00226     basic_stringbuf<_CharT, _Traits, _Alloc>::
00227     _M_sync(char_type* __base, __size_type __i, __size_type __o)
00228     {
00229       const bool __testin = _M_mode & ios_base::in;
00230       const bool __testout = _M_mode & ios_base::out;
00231       char_type* __endg = __base + _M_string.size();
00232       char_type* __endp = __base + _M_string.capacity();
00233 
00234       if (__base != _M_string.data())
00235     {
00236       // setbuf: __i == size of buffer area (_M_string.size() == 0).
00237       __endg += __i;
00238       __i = 0;
00239       __endp = __endg;
00240     }
00241 
00242       if (__testin)
00243     this->setg(__base, __base + __i, __endg);
00244       if (__testout)
00245     {
00246       _M_pbump(__base, __endp, __o);
00247       // egptr() always tracks the string end.  When !__testin,
00248       // for the correct functioning of the streambuf inlines
00249       // the other get area pointers are identical.
00250       if (!__testin)
00251         this->setg(__endg, __endg, __endg);
00252     }
00253     }
00254 
00255   template <class _CharT, class _Traits, class _Alloc>
00256     void
00257     basic_stringbuf<_CharT, _Traits, _Alloc>::
00258     _M_pbump(char_type* __pbeg, char_type* __pend, off_type __off)
00259     {
00260       this->setp(__pbeg, __pend);
00261       while (__off > __gnu_cxx::__numeric_traits<int>::__max)
00262     {
00263       this->pbump(__gnu_cxx::__numeric_traits<int>::__max);
00264       __off -= __gnu_cxx::__numeric_traits<int>::__max;
00265     }
00266       this->pbump(__off);
00267     }
00268 
00269   // Inhibit implicit instantiations for required instantiations,
00270   // which are defined via explicit instantiations elsewhere.
00271 #if _GLIBCXX_EXTERN_TEMPLATE
00272   extern template class basic_stringbuf<char>;
00273   extern template class basic_istringstream<char>;
00274   extern template class basic_ostringstream<char>;
00275   extern template class basic_stringstream<char>;
00276 
00277 #ifdef _GLIBCXX_USE_WCHAR_T
00278   extern template class basic_stringbuf<wchar_t>;
00279   extern template class basic_istringstream<wchar_t>;
00280   extern template class basic_ostringstream<wchar_t>;
00281   extern template class basic_stringstream<wchar_t>;
00282 #endif
00283 #endif
00284 
00285 _GLIBCXX_END_NAMESPACE_VERSION
00286 } // namespace std
00287 
00288 #endif