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