libstdc++
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, 2008, 2009, 2010, 2011
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 bits/sstream.tcc
00028  *  This is an internal header file, included by other library headers.
00029  *  Do not attempt to use it directly. @headername{sstream}
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 namespace std _GLIBCXX_VISIBILITY(default)
00042 {
00043 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00044 
00045   template <class _CharT, class _Traits, class _Alloc>
00046     typename basic_stringbuf<_CharT, _Traits, _Alloc>::int_type
00047     basic_stringbuf<_CharT, _Traits, _Alloc>::
00048     pbackfail(int_type __c)
00049     {
00050       int_type __ret = traits_type::eof();
00051       if (this->eback() < this->gptr())
00052     {
00053       // Try to put back __c into input sequence in one of three ways.
00054       // Order these tests done in is unspecified by the standard.
00055       const bool __testeof = traits_type::eq_int_type(__c, __ret);
00056       if (!__testeof)
00057         {
00058           const bool __testeq = traits_type::eq(traits_type::
00059                             to_char_type(__c),
00060                             this->gptr()[-1]);    
00061           const bool __testout = this->_M_mode & ios_base::out;
00062           if (__testeq || __testout)
00063         {
00064           this->gbump(-1);
00065           if (!__testeq)
00066             *this->gptr() = traits_type::to_char_type(__c);
00067           __ret = __c;
00068         }
00069         }
00070       else
00071         {
00072           this->gbump(-1);
00073           __ret = traits_type::not_eof(__c);
00074         }
00075     }
00076       return __ret;
00077     }
00078 
00079   template <class _CharT, class _Traits, class _Alloc>
00080     typename basic_stringbuf<_CharT, _Traits, _Alloc>::int_type
00081     basic_stringbuf<_CharT, _Traits, _Alloc>::
00082     overflow(int_type __c)
00083     {
00084       const bool __testout = this->_M_mode & ios_base::out;
00085       if (__builtin_expect(!__testout, false))
00086     return traits_type::eof();
00087 
00088       const bool __testeof = traits_type::eq_int_type(__c, traits_type::eof());
00089       if (__builtin_expect(__testeof, false))
00090     return traits_type::not_eof(__c);
00091 
00092       const __size_type __capacity = _M_string.capacity();
00093       const __size_type __max_size = _M_string.max_size();
00094       const bool __testput = this->pptr() < this->epptr();
00095       if (__builtin_expect(!__testput && __capacity == __max_size, false))
00096     return traits_type::eof();
00097 
00098       // Try to append __c into output sequence in one of two ways.
00099       // Order these tests done in is unspecified by the standard.
00100       const char_type __conv = traits_type::to_char_type(__c);
00101       if (!__testput)
00102     {
00103       // NB: Start ostringstream buffers at 512 chars.  This is an
00104       // experimental value (pronounced "arbitrary" in some of the
00105       // hipper English-speaking countries), and can be changed to
00106       // suit particular needs.
00107       //
00108       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00109       // 169. Bad efficiency of overflow() mandated
00110       // 432. stringbuf::overflow() makes only one write position
00111       //      available
00112       const __size_type __opt_len = std::max(__size_type(2 * __capacity),
00113                          __size_type(512));
00114       const __size_type __len = std::min(__opt_len, __max_size);
00115       __string_type __tmp;
00116       __tmp.reserve(__len);
00117       if (this->pbase())
00118         __tmp.assign(this->pbase(), this->epptr() - this->pbase());
00119       __tmp.push_back(__conv);
00120       _M_string.swap(__tmp);
00121       _M_sync(const_cast<char_type*>(_M_string.data()),
00122           this->gptr() - this->eback(), this->pptr() - this->pbase());
00123     }
00124       else
00125     *this->pptr() = __conv;
00126       this->pbump(1);
00127       return __c;
00128     }
00129 
00130   template <class _CharT, class _Traits, class _Alloc>
00131     typename basic_stringbuf<_CharT, _Traits, _Alloc>::int_type
00132     basic_stringbuf<_CharT, _Traits, _Alloc>::
00133     underflow()
00134     {
00135       int_type __ret = traits_type::eof();
00136       const bool __testin = this->_M_mode & ios_base::in;
00137       if (__testin)
00138     {
00139       // Update egptr() to match the actual string end.
00140       _M_update_egptr();
00141 
00142       if (this->gptr() < this->egptr())
00143         __ret = traits_type::to_int_type(*this->gptr());
00144     }
00145       return __ret;
00146     }
00147 
00148   template <class _CharT, class _Traits, class _Alloc>
00149     typename basic_stringbuf<_CharT, _Traits, _Alloc>::pos_type
00150     basic_stringbuf<_CharT, _Traits, _Alloc>::
00151     seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode __mode)
00152     {
00153       pos_type __ret =  pos_type(off_type(-1));
00154       bool __testin = (ios_base::in & this->_M_mode & __mode) != 0;
00155       bool __testout = (ios_base::out & this->_M_mode & __mode) != 0;
00156       const bool __testboth = __testin && __testout && __way != ios_base::cur;
00157       __testin &= !(__mode & ios_base::out);
00158       __testout &= !(__mode & ios_base::in);
00159 
00160       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00161       // 453. basic_stringbuf::seekoff need not always fail for an empty stream.
00162       const char_type* __beg = __testin ? this->eback() : this->pbase();
00163       if ((__beg || !__off) && (__testin || __testout || __testboth))
00164     {
00165       _M_update_egptr();
00166 
00167       off_type __newoffi = __off;
00168       off_type __newoffo = __newoffi;
00169       if (__way == ios_base::cur)
00170         {
00171           __newoffi += this->gptr() - __beg;
00172           __newoffo += this->pptr() - __beg;
00173         }
00174       else if (__way == ios_base::end)
00175         __newoffo = __newoffi += this->egptr() - __beg;
00176 
00177       if ((__testin || __testboth)
00178           && __newoffi >= 0
00179           && this->egptr() - __beg >= __newoffi)
00180         {
00181           this->setg(this->eback(), this->eback() + __newoffi,
00182              this->egptr());
00183           __ret = pos_type(__newoffi);
00184         }
00185       if ((__testout || __testboth)
00186           && __newoffo >= 0
00187           && this->egptr() - __beg >= __newoffo)
00188         {
00189           _M_pbump(this->pbase(), this->epptr(), __newoffo);
00190           __ret = pos_type(__newoffo);
00191         }
00192     }
00193       return __ret;
00194     }
00195 
00196   template <class _CharT, class _Traits, class _Alloc>
00197     typename basic_stringbuf<_CharT, _Traits, _Alloc>::pos_type
00198     basic_stringbuf<_CharT, _Traits, _Alloc>::
00199     seekpos(pos_type __sp, ios_base::openmode __mode)
00200     {
00201       pos_type __ret =  pos_type(off_type(-1));
00202       const bool __testin = (ios_base::in & this->_M_mode & __mode) != 0;
00203       const bool __testout = (ios_base::out & this->_M_mode & __mode) != 0;
00204 
00205       const char_type* __beg = __testin ? this->eback() : this->pbase();
00206       if ((__beg || !off_type(__sp)) && (__testin || __testout))
00207     {
00208       _M_update_egptr();
00209 
00210       const off_type __pos(__sp);
00211       const bool __testpos = (0 <= __pos
00212                   && __pos <= this->egptr() - __beg);
00213       if (__testpos)
00214         {
00215           if (__testin)
00216         this->setg(this->eback(), this->eback() + __pos,
00217                this->egptr());
00218           if (__testout)
00219         _M_pbump(this->pbase(), this->epptr(), __pos);
00220           __ret = __sp;
00221         }
00222     }
00223       return __ret;
00224     }
00225 
00226   template <class _CharT, class _Traits, class _Alloc>
00227     void
00228     basic_stringbuf<_CharT, _Traits, _Alloc>::
00229     _M_sync(char_type* __base, __size_type __i, __size_type __o)
00230     {
00231       const bool __testin = _M_mode & ios_base::in;
00232       const bool __testout = _M_mode & ios_base::out;
00233       char_type* __endg = __base + _M_string.size();
00234       char_type* __endp = __base + _M_string.capacity();
00235 
00236       if (__base != _M_string.data())
00237     {
00238       // setbuf: __i == size of buffer area (_M_string.size() == 0).
00239       __endg += __i;
00240       __i = 0;
00241       __endp = __endg;
00242     }
00243 
00244       if (__testin)
00245     this->setg(__base, __base + __i, __endg);
00246       if (__testout)
00247     {
00248       _M_pbump(__base, __endp, __o);
00249       // egptr() always tracks the string end.  When !__testin,
00250       // for the correct functioning of the streambuf inlines
00251       // the other get area pointers are identical.
00252       if (!__testin)
00253         this->setg(__endg, __endg, __endg);
00254     }
00255     }
00256 
00257   template <class _CharT, class _Traits, class _Alloc>
00258     void
00259     basic_stringbuf<_CharT, _Traits, _Alloc>::
00260     _M_pbump(char_type* __pbeg, char_type* __pend, off_type __off)
00261     {
00262       this->setp(__pbeg, __pend);
00263       while (__off > __gnu_cxx::__numeric_traits<int>::__max)
00264     {
00265       this->pbump(__gnu_cxx::__numeric_traits<int>::__max);
00266       __off -= __gnu_cxx::__numeric_traits<int>::__max;
00267     }
00268       this->pbump(__off);
00269     }
00270 
00271   // Inhibit implicit instantiations for required instantiations,
00272   // which are defined via explicit instantiations elsewhere.
00273 #if _GLIBCXX_EXTERN_TEMPLATE
00274   extern template class basic_stringbuf<char>;
00275   extern template class basic_istringstream<char>;
00276   extern template class basic_ostringstream<char>;
00277   extern template class basic_stringstream<char>;
00278 
00279 #ifdef _GLIBCXX_USE_WCHAR_T
00280   extern template class basic_stringbuf<wchar_t>;
00281   extern template class basic_istringstream<wchar_t>;
00282   extern template class basic_ostringstream<wchar_t>;
00283   extern template class basic_stringstream<wchar_t>;
00284 #endif
00285 #endif
00286 
00287 _GLIBCXX_END_NAMESPACE_VERSION
00288 } // namespace std
00289 
00290 #endif