1*e4b17023SJohn Marino // String based streams -*- C++ -*- 2*e4b17023SJohn Marino 3*e4b17023SJohn Marino // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 4*e4b17023SJohn Marino // 2006, 2007, 2008, 2009, 2010, 2011 5*e4b17023SJohn Marino // Free Software Foundation, Inc. 6*e4b17023SJohn Marino // 7*e4b17023SJohn Marino // This file is part of the GNU ISO C++ Library. This library is free 8*e4b17023SJohn Marino // software; you can redistribute it and/or modify it under the 9*e4b17023SJohn Marino // terms of the GNU General Public License as published by the 10*e4b17023SJohn Marino // Free Software Foundation; either version 3, or (at your option) 11*e4b17023SJohn Marino // any later version. 12*e4b17023SJohn Marino 13*e4b17023SJohn Marino // This library is distributed in the hope that it will be useful, 14*e4b17023SJohn Marino // but WITHOUT ANY WARRANTY; without even the implied warranty of 15*e4b17023SJohn Marino // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16*e4b17023SJohn Marino // GNU General Public License for more details. 17*e4b17023SJohn Marino 18*e4b17023SJohn Marino // Under Section 7 of GPL version 3, you are granted additional 19*e4b17023SJohn Marino // permissions described in the GCC Runtime Library Exception, version 20*e4b17023SJohn Marino // 3.1, as published by the Free Software Foundation. 21*e4b17023SJohn Marino 22*e4b17023SJohn Marino // You should have received a copy of the GNU General Public License and 23*e4b17023SJohn Marino // a copy of the GCC Runtime Library Exception along with this program; 24*e4b17023SJohn Marino // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 25*e4b17023SJohn Marino // <http://www.gnu.org/licenses/>. 26*e4b17023SJohn Marino 27*e4b17023SJohn Marino /** @file bits/sstream.tcc 28*e4b17023SJohn Marino * This is an internal header file, included by other library headers. 29*e4b17023SJohn Marino * Do not attempt to use it directly. @headername{sstream} 30*e4b17023SJohn Marino */ 31*e4b17023SJohn Marino 32*e4b17023SJohn Marino // 33*e4b17023SJohn Marino // ISO C++ 14882: 27.7 String-based streams 34*e4b17023SJohn Marino // 35*e4b17023SJohn Marino 36*e4b17023SJohn Marino #ifndef _SSTREAM_TCC 37*e4b17023SJohn Marino #define _SSTREAM_TCC 1 38*e4b17023SJohn Marino 39*e4b17023SJohn Marino #pragma GCC system_header 40*e4b17023SJohn Marino 41*e4b17023SJohn Marino namespace std _GLIBCXX_VISIBILITY(default) 42*e4b17023SJohn Marino { 43*e4b17023SJohn Marino _GLIBCXX_BEGIN_NAMESPACE_VERSION 44*e4b17023SJohn Marino 45*e4b17023SJohn Marino template <class _CharT, class _Traits, class _Alloc> 46*e4b17023SJohn Marino typename basic_stringbuf<_CharT, _Traits, _Alloc>::int_type 47*e4b17023SJohn Marino basic_stringbuf<_CharT, _Traits, _Alloc>:: pbackfail(int_type __c)48*e4b17023SJohn Marino pbackfail(int_type __c) 49*e4b17023SJohn Marino { 50*e4b17023SJohn Marino int_type __ret = traits_type::eof(); 51*e4b17023SJohn Marino if (this->eback() < this->gptr()) 52*e4b17023SJohn Marino { 53*e4b17023SJohn Marino // Try to put back __c into input sequence in one of three ways. 54*e4b17023SJohn Marino // Order these tests done in is unspecified by the standard. 55*e4b17023SJohn Marino const bool __testeof = traits_type::eq_int_type(__c, __ret); 56*e4b17023SJohn Marino if (!__testeof) 57*e4b17023SJohn Marino { 58*e4b17023SJohn Marino const bool __testeq = traits_type::eq(traits_type:: 59*e4b17023SJohn Marino to_char_type(__c), 60*e4b17023SJohn Marino this->gptr()[-1]); 61*e4b17023SJohn Marino const bool __testout = this->_M_mode & ios_base::out; 62*e4b17023SJohn Marino if (__testeq || __testout) 63*e4b17023SJohn Marino { 64*e4b17023SJohn Marino this->gbump(-1); 65*e4b17023SJohn Marino if (!__testeq) 66*e4b17023SJohn Marino *this->gptr() = traits_type::to_char_type(__c); 67*e4b17023SJohn Marino __ret = __c; 68*e4b17023SJohn Marino } 69*e4b17023SJohn Marino } 70*e4b17023SJohn Marino else 71*e4b17023SJohn Marino { 72*e4b17023SJohn Marino this->gbump(-1); 73*e4b17023SJohn Marino __ret = traits_type::not_eof(__c); 74*e4b17023SJohn Marino } 75*e4b17023SJohn Marino } 76*e4b17023SJohn Marino return __ret; 77*e4b17023SJohn Marino } 78*e4b17023SJohn Marino 79*e4b17023SJohn Marino template <class _CharT, class _Traits, class _Alloc> 80*e4b17023SJohn Marino typename basic_stringbuf<_CharT, _Traits, _Alloc>::int_type 81*e4b17023SJohn Marino basic_stringbuf<_CharT, _Traits, _Alloc>:: overflow(int_type __c)82*e4b17023SJohn Marino overflow(int_type __c) 83*e4b17023SJohn Marino { 84*e4b17023SJohn Marino const bool __testout = this->_M_mode & ios_base::out; 85*e4b17023SJohn Marino if (__builtin_expect(!__testout, false)) 86*e4b17023SJohn Marino return traits_type::eof(); 87*e4b17023SJohn Marino 88*e4b17023SJohn Marino const bool __testeof = traits_type::eq_int_type(__c, traits_type::eof()); 89*e4b17023SJohn Marino if (__builtin_expect(__testeof, false)) 90*e4b17023SJohn Marino return traits_type::not_eof(__c); 91*e4b17023SJohn Marino 92*e4b17023SJohn Marino const __size_type __capacity = _M_string.capacity(); 93*e4b17023SJohn Marino const __size_type __max_size = _M_string.max_size(); 94*e4b17023SJohn Marino const bool __testput = this->pptr() < this->epptr(); 95*e4b17023SJohn Marino if (__builtin_expect(!__testput && __capacity == __max_size, false)) 96*e4b17023SJohn Marino return traits_type::eof(); 97*e4b17023SJohn Marino 98*e4b17023SJohn Marino // Try to append __c into output sequence in one of two ways. 99*e4b17023SJohn Marino // Order these tests done in is unspecified by the standard. 100*e4b17023SJohn Marino const char_type __conv = traits_type::to_char_type(__c); 101*e4b17023SJohn Marino if (!__testput) 102*e4b17023SJohn Marino { 103*e4b17023SJohn Marino // NB: Start ostringstream buffers at 512 chars. This is an 104*e4b17023SJohn Marino // experimental value (pronounced "arbitrary" in some of the 105*e4b17023SJohn Marino // hipper English-speaking countries), and can be changed to 106*e4b17023SJohn Marino // suit particular needs. 107*e4b17023SJohn Marino // 108*e4b17023SJohn Marino // _GLIBCXX_RESOLVE_LIB_DEFECTS 109*e4b17023SJohn Marino // 169. Bad efficiency of overflow() mandated 110*e4b17023SJohn Marino // 432. stringbuf::overflow() makes only one write position 111*e4b17023SJohn Marino // available 112*e4b17023SJohn Marino const __size_type __opt_len = std::max(__size_type(2 * __capacity), 113*e4b17023SJohn Marino __size_type(512)); 114*e4b17023SJohn Marino const __size_type __len = std::min(__opt_len, __max_size); 115*e4b17023SJohn Marino __string_type __tmp; 116*e4b17023SJohn Marino __tmp.reserve(__len); 117*e4b17023SJohn Marino if (this->pbase()) 118*e4b17023SJohn Marino __tmp.assign(this->pbase(), this->epptr() - this->pbase()); 119*e4b17023SJohn Marino __tmp.push_back(__conv); 120*e4b17023SJohn Marino _M_string.swap(__tmp); 121*e4b17023SJohn Marino _M_sync(const_cast<char_type*>(_M_string.data()), 122*e4b17023SJohn Marino this->gptr() - this->eback(), this->pptr() - this->pbase()); 123*e4b17023SJohn Marino } 124*e4b17023SJohn Marino else 125*e4b17023SJohn Marino *this->pptr() = __conv; 126*e4b17023SJohn Marino this->pbump(1); 127*e4b17023SJohn Marino return __c; 128*e4b17023SJohn Marino } 129*e4b17023SJohn Marino 130*e4b17023SJohn Marino template <class _CharT, class _Traits, class _Alloc> 131*e4b17023SJohn Marino typename basic_stringbuf<_CharT, _Traits, _Alloc>::int_type 132*e4b17023SJohn Marino basic_stringbuf<_CharT, _Traits, _Alloc>:: underflow()133*e4b17023SJohn Marino underflow() 134*e4b17023SJohn Marino { 135*e4b17023SJohn Marino int_type __ret = traits_type::eof(); 136*e4b17023SJohn Marino const bool __testin = this->_M_mode & ios_base::in; 137*e4b17023SJohn Marino if (__testin) 138*e4b17023SJohn Marino { 139*e4b17023SJohn Marino // Update egptr() to match the actual string end. 140*e4b17023SJohn Marino _M_update_egptr(); 141*e4b17023SJohn Marino 142*e4b17023SJohn Marino if (this->gptr() < this->egptr()) 143*e4b17023SJohn Marino __ret = traits_type::to_int_type(*this->gptr()); 144*e4b17023SJohn Marino } 145*e4b17023SJohn Marino return __ret; 146*e4b17023SJohn Marino } 147*e4b17023SJohn Marino 148*e4b17023SJohn Marino template <class _CharT, class _Traits, class _Alloc> 149*e4b17023SJohn Marino typename basic_stringbuf<_CharT, _Traits, _Alloc>::pos_type 150*e4b17023SJohn Marino basic_stringbuf<_CharT, _Traits, _Alloc>:: seekoff(off_type __off,ios_base::seekdir __way,ios_base::openmode __mode)151*e4b17023SJohn Marino seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode __mode) 152*e4b17023SJohn Marino { 153*e4b17023SJohn Marino pos_type __ret = pos_type(off_type(-1)); 154*e4b17023SJohn Marino bool __testin = (ios_base::in & this->_M_mode & __mode) != 0; 155*e4b17023SJohn Marino bool __testout = (ios_base::out & this->_M_mode & __mode) != 0; 156*e4b17023SJohn Marino const bool __testboth = __testin && __testout && __way != ios_base::cur; 157*e4b17023SJohn Marino __testin &= !(__mode & ios_base::out); 158*e4b17023SJohn Marino __testout &= !(__mode & ios_base::in); 159*e4b17023SJohn Marino 160*e4b17023SJohn Marino // _GLIBCXX_RESOLVE_LIB_DEFECTS 161*e4b17023SJohn Marino // 453. basic_stringbuf::seekoff need not always fail for an empty stream. 162*e4b17023SJohn Marino const char_type* __beg = __testin ? this->eback() : this->pbase(); 163*e4b17023SJohn Marino if ((__beg || !__off) && (__testin || __testout || __testboth)) 164*e4b17023SJohn Marino { 165*e4b17023SJohn Marino _M_update_egptr(); 166*e4b17023SJohn Marino 167*e4b17023SJohn Marino off_type __newoffi = __off; 168*e4b17023SJohn Marino off_type __newoffo = __newoffi; 169*e4b17023SJohn Marino if (__way == ios_base::cur) 170*e4b17023SJohn Marino { 171*e4b17023SJohn Marino __newoffi += this->gptr() - __beg; 172*e4b17023SJohn Marino __newoffo += this->pptr() - __beg; 173*e4b17023SJohn Marino } 174*e4b17023SJohn Marino else if (__way == ios_base::end) 175*e4b17023SJohn Marino __newoffo = __newoffi += this->egptr() - __beg; 176*e4b17023SJohn Marino 177*e4b17023SJohn Marino if ((__testin || __testboth) 178*e4b17023SJohn Marino && __newoffi >= 0 179*e4b17023SJohn Marino && this->egptr() - __beg >= __newoffi) 180*e4b17023SJohn Marino { 181*e4b17023SJohn Marino this->setg(this->eback(), this->eback() + __newoffi, 182*e4b17023SJohn Marino this->egptr()); 183*e4b17023SJohn Marino __ret = pos_type(__newoffi); 184*e4b17023SJohn Marino } 185*e4b17023SJohn Marino if ((__testout || __testboth) 186*e4b17023SJohn Marino && __newoffo >= 0 187*e4b17023SJohn Marino && this->egptr() - __beg >= __newoffo) 188*e4b17023SJohn Marino { 189*e4b17023SJohn Marino _M_pbump(this->pbase(), this->epptr(), __newoffo); 190*e4b17023SJohn Marino __ret = pos_type(__newoffo); 191*e4b17023SJohn Marino } 192*e4b17023SJohn Marino } 193*e4b17023SJohn Marino return __ret; 194*e4b17023SJohn Marino } 195*e4b17023SJohn Marino 196*e4b17023SJohn Marino template <class _CharT, class _Traits, class _Alloc> 197*e4b17023SJohn Marino typename basic_stringbuf<_CharT, _Traits, _Alloc>::pos_type 198*e4b17023SJohn Marino basic_stringbuf<_CharT, _Traits, _Alloc>:: seekpos(pos_type __sp,ios_base::openmode __mode)199*e4b17023SJohn Marino seekpos(pos_type __sp, ios_base::openmode __mode) 200*e4b17023SJohn Marino { 201*e4b17023SJohn Marino pos_type __ret = pos_type(off_type(-1)); 202*e4b17023SJohn Marino const bool __testin = (ios_base::in & this->_M_mode & __mode) != 0; 203*e4b17023SJohn Marino const bool __testout = (ios_base::out & this->_M_mode & __mode) != 0; 204*e4b17023SJohn Marino 205*e4b17023SJohn Marino const char_type* __beg = __testin ? this->eback() : this->pbase(); 206*e4b17023SJohn Marino if ((__beg || !off_type(__sp)) && (__testin || __testout)) 207*e4b17023SJohn Marino { 208*e4b17023SJohn Marino _M_update_egptr(); 209*e4b17023SJohn Marino 210*e4b17023SJohn Marino const off_type __pos(__sp); 211*e4b17023SJohn Marino const bool __testpos = (0 <= __pos 212*e4b17023SJohn Marino && __pos <= this->egptr() - __beg); 213*e4b17023SJohn Marino if (__testpos) 214*e4b17023SJohn Marino { 215*e4b17023SJohn Marino if (__testin) 216*e4b17023SJohn Marino this->setg(this->eback(), this->eback() + __pos, 217*e4b17023SJohn Marino this->egptr()); 218*e4b17023SJohn Marino if (__testout) 219*e4b17023SJohn Marino _M_pbump(this->pbase(), this->epptr(), __pos); 220*e4b17023SJohn Marino __ret = __sp; 221*e4b17023SJohn Marino } 222*e4b17023SJohn Marino } 223*e4b17023SJohn Marino return __ret; 224*e4b17023SJohn Marino } 225*e4b17023SJohn Marino 226*e4b17023SJohn Marino template <class _CharT, class _Traits, class _Alloc> 227*e4b17023SJohn Marino void 228*e4b17023SJohn Marino basic_stringbuf<_CharT, _Traits, _Alloc>:: _M_sync(char_type * __base,__size_type __i,__size_type __o)229*e4b17023SJohn Marino _M_sync(char_type* __base, __size_type __i, __size_type __o) 230*e4b17023SJohn Marino { 231*e4b17023SJohn Marino const bool __testin = _M_mode & ios_base::in; 232*e4b17023SJohn Marino const bool __testout = _M_mode & ios_base::out; 233*e4b17023SJohn Marino char_type* __endg = __base + _M_string.size(); 234*e4b17023SJohn Marino char_type* __endp = __base + _M_string.capacity(); 235*e4b17023SJohn Marino 236*e4b17023SJohn Marino if (__base != _M_string.data()) 237*e4b17023SJohn Marino { 238*e4b17023SJohn Marino // setbuf: __i == size of buffer area (_M_string.size() == 0). 239*e4b17023SJohn Marino __endg += __i; 240*e4b17023SJohn Marino __i = 0; 241*e4b17023SJohn Marino __endp = __endg; 242*e4b17023SJohn Marino } 243*e4b17023SJohn Marino 244*e4b17023SJohn Marino if (__testin) 245*e4b17023SJohn Marino this->setg(__base, __base + __i, __endg); 246*e4b17023SJohn Marino if (__testout) 247*e4b17023SJohn Marino { 248*e4b17023SJohn Marino _M_pbump(__base, __endp, __o); 249*e4b17023SJohn Marino // egptr() always tracks the string end. When !__testin, 250*e4b17023SJohn Marino // for the correct functioning of the streambuf inlines 251*e4b17023SJohn Marino // the other get area pointers are identical. 252*e4b17023SJohn Marino if (!__testin) 253*e4b17023SJohn Marino this->setg(__endg, __endg, __endg); 254*e4b17023SJohn Marino } 255*e4b17023SJohn Marino } 256*e4b17023SJohn Marino 257*e4b17023SJohn Marino template <class _CharT, class _Traits, class _Alloc> 258*e4b17023SJohn Marino void 259*e4b17023SJohn Marino basic_stringbuf<_CharT, _Traits, _Alloc>:: _M_pbump(char_type * __pbeg,char_type * __pend,off_type __off)260*e4b17023SJohn Marino _M_pbump(char_type* __pbeg, char_type* __pend, off_type __off) 261*e4b17023SJohn Marino { 262*e4b17023SJohn Marino this->setp(__pbeg, __pend); 263*e4b17023SJohn Marino while (__off > __gnu_cxx::__numeric_traits<int>::__max) 264*e4b17023SJohn Marino { 265*e4b17023SJohn Marino this->pbump(__gnu_cxx::__numeric_traits<int>::__max); 266*e4b17023SJohn Marino __off -= __gnu_cxx::__numeric_traits<int>::__max; 267*e4b17023SJohn Marino } 268*e4b17023SJohn Marino this->pbump(__off); 269*e4b17023SJohn Marino } 270*e4b17023SJohn Marino 271*e4b17023SJohn Marino // Inhibit implicit instantiations for required instantiations, 272*e4b17023SJohn Marino // which are defined via explicit instantiations elsewhere. 273*e4b17023SJohn Marino #if _GLIBCXX_EXTERN_TEMPLATE 274*e4b17023SJohn Marino extern template class basic_stringbuf<char>; 275*e4b17023SJohn Marino extern template class basic_istringstream<char>; 276*e4b17023SJohn Marino extern template class basic_ostringstream<char>; 277*e4b17023SJohn Marino extern template class basic_stringstream<char>; 278*e4b17023SJohn Marino 279*e4b17023SJohn Marino #ifdef _GLIBCXX_USE_WCHAR_T 280*e4b17023SJohn Marino extern template class basic_stringbuf<wchar_t>; 281*e4b17023SJohn Marino extern template class basic_istringstream<wchar_t>; 282*e4b17023SJohn Marino extern template class basic_ostringstream<wchar_t>; 283*e4b17023SJohn Marino extern template class basic_stringstream<wchar_t>; 284*e4b17023SJohn Marino #endif 285*e4b17023SJohn Marino #endif 286*e4b17023SJohn Marino 287*e4b17023SJohn Marino _GLIBCXX_END_NAMESPACE_VERSION 288*e4b17023SJohn Marino } // namespace std 289*e4b17023SJohn Marino 290*e4b17023SJohn Marino #endif 291