1 // String based streams -*- C++ -*-
2 
3 // Copyright (C) 1997, 1998, 1999, 2001, 2002, 2003
4 // Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library.  This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
10 // any later version.
11 
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 // GNU General Public License for more details.
16 
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING.  If not, write to the Free
19 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 // USA.
21 
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction.  Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License.  This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
30 
31 //
32 // ISO C++ 14882: 27.7  String-based streams
33 //
34 
35 #ifndef _SSTREAM_TCC
36 #define _SSTREAM_TCC 1
37 
38 #pragma GCC system_header
39 
40 #include <sstream>
41 
42 namespace std
43 {
44   template <class _CharT, class _Traits, class _Alloc>
45     typename basic_stringbuf<_CharT, _Traits, _Alloc>::int_type
46     basic_stringbuf<_CharT, _Traits, _Alloc>::
pbackfail(int_type __c)47     pbackfail(int_type __c)
48     {
49       int_type __ret = traits_type::eof();
50       const bool __testeof = traits_type::eq_int_type(__c, __ret);
51 
52       if (this->eback() < this->gptr())
53 	{
54 	  const bool __testeq = traits_type::eq(traits_type::to_char_type(__c),
55 						this->gptr()[-1]);
56 	  this->gbump(-1);
57 
58 	  // Try to put back __c into input sequence in one of three ways.
59 	  // Order these tests done in is unspecified by the standard.
60 	  if (!__testeof && __testeq)
61 	    __ret = __c;
62 	  else if (__testeof)
63 	    __ret = traits_type::not_eof(__c);
64 	  else
65 	    {
66 	      *this->gptr() = traits_type::to_char_type(__c);
67 	      __ret = __c;
68 	    }
69 	}
70       return __ret;
71     }
72 
73   template <class _CharT, class _Traits, class _Alloc>
74     typename basic_stringbuf<_CharT, _Traits, _Alloc>::int_type
75     basic_stringbuf<_CharT, _Traits, _Alloc>::
overflow(int_type __c)76     overflow(int_type __c)
77     {
78       const bool __testout = this->_M_mode & ios_base::out;
79       if (__builtin_expect(!__testout, false))
80 	return traits_type::eof();
81 
82       const bool __testeof = traits_type::eq_int_type(__c, traits_type::eof());
83       if (__builtin_expect(__testeof, false))
84 	return traits_type::not_eof(__c);
85 
86       const __size_type __capacity = _M_string.capacity();
87       const __size_type __max_size = _M_string.max_size();
88       const bool __testput = this->pptr() < this->epptr();
89       if (__builtin_expect(!__testput && __capacity == __max_size, false))
90 	return traits_type::eof();
91 
92       // Try to append __c into output sequence in one of two ways.
93       // Order these tests done in is unspecified by the standard.
94       if (!__testput)
95 	{
96 	  // NB: Start ostringstream buffers at 512 chars. This is an
97 	  // experimental value (pronounced "arbitrary" in some of the
98 	  // hipper english-speaking countries), and can be changed to
99 	  // suit particular needs.
100 	  // Then, in virtue of DR 169 (TC) we are allowed to grow more
101 	  // than one char.
102 	  const __size_type __opt_len = std::max(__size_type(2 * __capacity),
103 						 __size_type(512));
104 	  const __size_type __len = std::min(__opt_len, __max_size);
105 	  __string_type __tmp;
106 	  __tmp.reserve(__len);
107 	  __tmp.assign(_M_string.data(), this->epptr() - this->pbase());
108 	  _M_string.swap(__tmp);
109 	  _M_sync(const_cast<char_type*>(_M_string.data()),
110 		  this->gptr() - this->eback(), this->pptr() - this->pbase());
111 	}
112       return this->sputc(traits_type::to_char_type(__c));
113     }
114 
115   template <class _CharT, class _Traits, class _Alloc>
116     typename basic_stringbuf<_CharT, _Traits, _Alloc>::int_type
117     basic_stringbuf<_CharT, _Traits, _Alloc>::
underflow()118     underflow()
119     {
120       int_type __ret = traits_type::eof();
121       const bool __testin = this->_M_mode & ios_base::in;
122       if (__testin)
123 	{
124 	  // Update egptr() to match the actual string end.
125 	  _M_update_egptr();
126 	  if (this->gptr() < this->egptr())
127 	    __ret = traits_type::to_int_type(*this->gptr());
128 	}
129       return __ret;
130     }
131 
132   template <class _CharT, class _Traits, class _Alloc>
133     typename basic_stringbuf<_CharT, _Traits, _Alloc>::pos_type
134     basic_stringbuf<_CharT, _Traits, _Alloc>::
seekoff(off_type __off,ios_base::seekdir __way,ios_base::openmode __mode)135     seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode __mode)
136     {
137       pos_type __ret =  pos_type(off_type(-1));
138       bool __testin = (ios_base::in & this->_M_mode & __mode) != 0;
139       bool __testout = (ios_base::out & this->_M_mode & __mode) != 0;
140       const bool __testboth = __testin && __testout && __way != ios_base::cur;
141       __testin &= !(__mode & ios_base::out);
142       __testout &= !(__mode & ios_base::in);
143 
144       if (_M_string.capacity() && (__testin || __testout || __testboth))
145 	{
146 	  char_type* __beg = __testin ? this->eback() : this->pbase();
147 
148 	  _M_update_egptr();
149 
150 	  off_type __newoffi = 0;
151 	  off_type __newoffo = 0;
152 	  if (__way == ios_base::cur)
153 	    {
154 	      __newoffi = this->gptr() - __beg;
155 	      __newoffo = this->pptr() - __beg;
156 	    }
157 	  else if (__way == ios_base::end)
158 	    __newoffo = __newoffi = this->egptr() - __beg;
159 
160 	  if ((__testin || __testboth)
161 	      && __newoffi + __off >= 0
162 	      && this->egptr() - __beg >= __newoffi + __off)
163 	    {
164 	      this->gbump((__beg + __newoffi + __off) - this->gptr());
165 	      __ret = pos_type(__newoffi);
166 	    }
167 	  if ((__testout || __testboth)
168 	      && __newoffo + __off >= 0
169 	      && this->egptr() - __beg >= __newoffo + __off)
170 	    {
171 	      this->pbump((__beg + __newoffo + __off) - this->pptr());
172 	      __ret = pos_type(__newoffo);
173 	    }
174 	}
175       return __ret;
176     }
177 
178   template <class _CharT, class _Traits, class _Alloc>
179     typename basic_stringbuf<_CharT, _Traits, _Alloc>::pos_type
180     basic_stringbuf<_CharT, _Traits, _Alloc>::
seekpos(pos_type __sp,ios_base::openmode __mode)181     seekpos(pos_type __sp, ios_base::openmode __mode)
182     {
183       pos_type __ret =  pos_type(off_type(-1));
184       if (_M_string.capacity())
185 	{
186 	  off_type __pos (__sp);
187 	  const bool __testin = (ios_base::in & this->_M_mode & __mode) != 0;
188 	  const bool __testout = (ios_base::out & this->_M_mode & __mode) != 0;
189 	  char_type* __beg = __testin ? this->eback() : this->pbase();
190 
191 	  _M_update_egptr();
192 
193 	  const bool __testpos = 0 <= __pos
194 	                         && __pos <=  this->egptr() - __beg;
195 	  if ((__testin || __testout) && __testpos)
196 	    {
197 	      if (__testin)
198 		this->gbump((__beg + __pos) - this->gptr());
199 	      if (__testout)
200                 this->pbump((__beg + __pos) - this->pptr());
201 	      __ret = pos_type(off_type(__pos));
202 	    }
203 	}
204       return __ret;
205     }
206 
207   // Inhibit implicit instantiations for required instantiations,
208   // which are defined via explicit instantiations elsewhere.
209   // NB:  This syntax is a GNU extension.
210 #if _GLIBCXX_EXTERN_TEMPLATE
211   extern template class basic_stringbuf<char>;
212   extern template class basic_istringstream<char>;
213   extern template class basic_ostringstream<char>;
214   extern template class basic_stringstream<char>;
215 
216 #ifdef _GLIBCXX_USE_WCHAR_T
217   extern template class basic_stringbuf<wchar_t>;
218   extern template class basic_istringstream<wchar_t>;
219   extern template class basic_ostringstream<wchar_t>;
220   extern template class basic_stringstream<wchar_t>;
221 #endif
222 #endif
223 } // namespace std
224 
225 #endif
226