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