sstream.tcc revision 146897
197403Sobrien// String based streams -*- C++ -*-
297403Sobrien
3132720Skan// Copyright (C) 1997, 1998, 1999, 2001, 2002, 2003
497403Sobrien// Free Software Foundation, Inc.
597403Sobrien//
697403Sobrien// This file is part of the GNU ISO C++ Library.  This library is free
797403Sobrien// software; you can redistribute it and/or modify it under the
897403Sobrien// terms of the GNU General Public License as published by the
997403Sobrien// Free Software Foundation; either version 2, or (at your option)
1097403Sobrien// any later version.
1197403Sobrien
1297403Sobrien// This library is distributed in the hope that it will be useful,
1397403Sobrien// but WITHOUT ANY WARRANTY; without even the implied warranty of
1497403Sobrien// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1597403Sobrien// GNU General Public License for more details.
1697403Sobrien
1797403Sobrien// You should have received a copy of the GNU General Public License along
1897403Sobrien// with this library; see the file COPYING.  If not, write to the Free
1997403Sobrien// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
2097403Sobrien// USA.
2197403Sobrien
2297403Sobrien// As a special exception, you may use this file as part of a free software
2397403Sobrien// library without restriction.  Specifically, if other files instantiate
2497403Sobrien// templates or use macros or inline functions from this file, or you compile
2597403Sobrien// this file and link it with other files to produce an executable, this
2697403Sobrien// file does not by itself cause the resulting executable to be covered by
2797403Sobrien// the GNU General Public License.  This exception does not however
2897403Sobrien// invalidate any other reasons why the executable file might be covered by
2997403Sobrien// the GNU General Public License.
3097403Sobrien
3197403Sobrien//
3297403Sobrien// ISO C++ 14882: 27.7  String-based streams
3397403Sobrien//
3497403Sobrien
35132720Skan#ifndef _SSTREAM_TCC
36132720Skan#define _SSTREAM_TCC 1
3797403Sobrien
3897403Sobrien#pragma GCC system_header
3997403Sobrien
4097403Sobrien#include <sstream>
4197403Sobrien
4297403Sobriennamespace std
4397403Sobrien{
4497403Sobrien  template <class _CharT, class _Traits, class _Alloc>
45132720Skan    typename basic_stringbuf<_CharT, _Traits, _Alloc>::int_type
4697403Sobrien    basic_stringbuf<_CharT, _Traits, _Alloc>::
4797403Sobrien    pbackfail(int_type __c)
4897403Sobrien    {
4997403Sobrien      int_type __ret = traits_type::eof();
50132720Skan      const bool __testeof = traits_type::eq_int_type(__c, __ret);
51132720Skan
52132720Skan      if (this->eback() < this->gptr())
5397403Sobrien	{
54132720Skan	  const bool __testeq = traits_type::eq(traits_type::to_char_type(__c),
55132720Skan						this->gptr()[-1]);
56132720Skan	  this->gbump(-1);
57132720Skan
58132720Skan	  // Try to put back __c into input sequence in one of three ways.
59132720Skan	  // Order these tests done in is unspecified by the standard.
60132720Skan	  if (!__testeof && __testeq)
61132720Skan	    __ret = __c;
62132720Skan	  else if (__testeof)
63132720Skan	    __ret = traits_type::not_eof(__c);
64132720Skan	  else
6597403Sobrien	    {
66132720Skan	      *this->gptr() = traits_type::to_char_type(__c);
6797403Sobrien	      __ret = __c;
6897403Sobrien	    }
6997403Sobrien	}
7097403Sobrien      return __ret;
7197403Sobrien    }
72132720Skan
7397403Sobrien  template <class _CharT, class _Traits, class _Alloc>
74132720Skan    typename basic_stringbuf<_CharT, _Traits, _Alloc>::int_type
7597403Sobrien    basic_stringbuf<_CharT, _Traits, _Alloc>::
7697403Sobrien    overflow(int_type __c)
7797403Sobrien    {
78132720Skan      const bool __testout = this->_M_mode & ios_base::out;
79132720Skan      if (__builtin_expect(!__testout, false))
80132720Skan	return traits_type::eof();
8197403Sobrien
82132720Skan      const bool __testeof = traits_type::eq_int_type(__c, traits_type::eof());
83132720Skan      if (__builtin_expect(__testeof, false))
84132720Skan	return traits_type::not_eof(__c);
85132720Skan
86132720Skan      const __size_type __capacity = _M_string.capacity();
87132720Skan      const __size_type __max_size = _M_string.max_size();
88132720Skan      const bool __testput = this->pptr() < this->epptr();
89132720Skan      if (__builtin_expect(!__testput && __capacity == __max_size, false))
90132720Skan	return traits_type::eof();
91132720Skan
9297403Sobrien      // Try to append __c into output sequence in one of two ways.
9397403Sobrien      // Order these tests done in is unspecified by the standard.
94132720Skan      if (!__testput)
9597403Sobrien	{
96132720Skan	  // NB: Start ostringstream buffers at 512 chars. This is an
97132720Skan	  // experimental value (pronounced "arbitrary" in some of the
98132720Skan	  // hipper english-speaking countries), and can be changed to
99132720Skan	  // suit particular needs.
100132720Skan	  // Then, in virtue of DR 169 (TC) we are allowed to grow more
101132720Skan	  // than one char.
102132720Skan	  const __size_type __opt_len = std::max(__size_type(2 * __capacity),
103132720Skan						 __size_type(512));
104132720Skan	  const __size_type __len = std::min(__opt_len, __max_size);
105132720Skan	  __string_type __tmp;
106132720Skan	  __tmp.reserve(__len);
107132720Skan	  __tmp.assign(_M_string.data(), this->epptr() - this->pbase());
108132720Skan	  _M_string.swap(__tmp);
109132720Skan	  _M_sync(const_cast<char_type*>(_M_string.data()),
110132720Skan		  this->gptr() - this->eback(), this->pptr() - this->pbase());
111132720Skan	}
112132720Skan      return this->sputc(traits_type::to_char_type(__c));
113132720Skan    }
11497403Sobrien
115132720Skan  template <class _CharT, class _Traits, class _Alloc>
116132720Skan    typename basic_stringbuf<_CharT, _Traits, _Alloc>::int_type
117132720Skan    basic_stringbuf<_CharT, _Traits, _Alloc>::
118132720Skan    underflow()
119132720Skan    {
120132720Skan      int_type __ret = traits_type::eof();
121132720Skan      const bool __testin = this->_M_mode & ios_base::in;
122132720Skan      if (__testin)
123132720Skan	{
124132720Skan	  // Update egptr() to match the actual string end.
125132720Skan	  _M_update_egptr();
126146897Skan
127132720Skan	  if (this->gptr() < this->egptr())
128132720Skan	    __ret = traits_type::to_int_type(*this->gptr());
12997403Sobrien	}
13097403Sobrien      return __ret;
13197403Sobrien    }
13297403Sobrien
13397403Sobrien  template <class _CharT, class _Traits, class _Alloc>
13497403Sobrien    typename basic_stringbuf<_CharT, _Traits, _Alloc>::pos_type
13597403Sobrien    basic_stringbuf<_CharT, _Traits, _Alloc>::
13697403Sobrien    seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode __mode)
13797403Sobrien    {
138132720Skan      pos_type __ret =  pos_type(off_type(-1));
139132720Skan      bool __testin = (ios_base::in & this->_M_mode & __mode) != 0;
140132720Skan      bool __testout = (ios_base::out & this->_M_mode & __mode) != 0;
141132720Skan      const bool __testboth = __testin && __testout && __way != ios_base::cur;
14297403Sobrien      __testin &= !(__mode & ios_base::out);
14397403Sobrien      __testout &= !(__mode & ios_base::in);
14497403Sobrien
145146897Skan      // _GLIBCXX_RESOLVE_LIB_DEFECTS
146146897Skan      // 453. basic_stringbuf::seekoff need not always fail for an empty stream.
147146897Skan      const char_type* __beg = __testin ? this->eback() : this->pbase();
148146897Skan      if ((__beg || !__off) && (__testin || __testout || __testboth))
14997403Sobrien	{
150132720Skan	  _M_update_egptr();
15197403Sobrien
15297403Sobrien	  off_type __newoffi = 0;
15397403Sobrien	  off_type __newoffo = 0;
15497403Sobrien	  if (__way == ios_base::cur)
15597403Sobrien	    {
156132720Skan	      __newoffi = this->gptr() - __beg;
157132720Skan	      __newoffo = this->pptr() - __beg;
15897403Sobrien	    }
15997403Sobrien	  else if (__way == ios_base::end)
160132720Skan	    __newoffo = __newoffi = this->egptr() - __beg;
16197403Sobrien
16297403Sobrien	  if ((__testin || __testboth)
163132720Skan	      && __newoffi + __off >= 0
164132720Skan	      && this->egptr() - __beg >= __newoffi + __off)
16597403Sobrien	    {
166132720Skan	      this->gbump((__beg + __newoffi + __off) - this->gptr());
16797403Sobrien	      __ret = pos_type(__newoffi);
16897403Sobrien	    }
16997403Sobrien	  if ((__testout || __testboth)
170132720Skan	      && __newoffo + __off >= 0
171132720Skan	      && this->egptr() - __beg >= __newoffo + __off)
17297403Sobrien	    {
173132720Skan	      this->pbump((__beg + __newoffo + __off) - this->pptr());
17497403Sobrien	      __ret = pos_type(__newoffo);
17597403Sobrien	    }
17697403Sobrien	}
17797403Sobrien      return __ret;
17897403Sobrien    }
17997403Sobrien
18097403Sobrien  template <class _CharT, class _Traits, class _Alloc>
18197403Sobrien    typename basic_stringbuf<_CharT, _Traits, _Alloc>::pos_type
18297403Sobrien    basic_stringbuf<_CharT, _Traits, _Alloc>::
18397403Sobrien    seekpos(pos_type __sp, ios_base::openmode __mode)
18497403Sobrien    {
185132720Skan      pos_type __ret =  pos_type(off_type(-1));
186146897Skan      const bool __testin = (ios_base::in & this->_M_mode & __mode) != 0;
187146897Skan      const bool __testout = (ios_base::out & this->_M_mode & __mode) != 0;
188146897Skan
189146897Skan      const char_type* __beg = __testin ? this->eback() : this->pbase();
190146897Skan      if (__beg)
19197403Sobrien	{
192132720Skan	  _M_update_egptr();
193132720Skan
194146897Skan	  off_type __pos(__sp);
195132720Skan	  const bool __testpos = 0 <= __pos
196132720Skan	                         && __pos <=  this->egptr() - __beg;
197132720Skan	  if ((__testin || __testout) && __testpos)
19897403Sobrien	    {
199132720Skan	      if (__testin)
200132720Skan		this->gbump((__beg + __pos) - this->gptr());
201132720Skan	      if (__testout)
202132720Skan                this->pbump((__beg + __pos) - this->pptr());
203146897Skan	      __ret = __sp;
20497403Sobrien	    }
20597403Sobrien	}
20697403Sobrien      return __ret;
20797403Sobrien    }
20897403Sobrien
20997403Sobrien  // Inhibit implicit instantiations for required instantiations,
210132720Skan  // which are defined via explicit instantiations elsewhere.
21197403Sobrien  // NB:  This syntax is a GNU extension.
212132720Skan#if _GLIBCXX_EXTERN_TEMPLATE
21397403Sobrien  extern template class basic_stringbuf<char>;
214107606Sobrien  extern template class basic_istringstream<char>;
215107606Sobrien  extern template class basic_ostringstream<char>;
216107606Sobrien  extern template class basic_stringstream<char>;
217107606Sobrien
218132720Skan#ifdef _GLIBCXX_USE_WCHAR_T
21997403Sobrien  extern template class basic_stringbuf<wchar_t>;
22097403Sobrien  extern template class basic_istringstream<wchar_t>;
22197403Sobrien  extern template class basic_ostringstream<wchar_t>;
22297403Sobrien  extern template class basic_stringstream<wchar_t>;
223107606Sobrien#endif
224132720Skan#endif
22597403Sobrien} // namespace std
22697403Sobrien
22797403Sobrien#endif
228