1 // strstream definitions -*- C++ -*-
3 // Copyright (C) 2001-2019 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
27 * Silicon Graphics Computer Systems, Inc.
29 * Permission to use, copy, modify, distribute and sell this software
30 * and its documentation for any purpose is hereby granted without fee,
31 * provided that the above copyright notice appear in all copies and
32 * that both that copyright notice and this permission notice appear
33 * in supporting documentation. Silicon Graphics makes no
34 * representations about the suitability of this software for any
35 * purpose. It is provided "as is" without express or implied warranty.
38 // Implementation of the classes in header <strstream>.
39 // WARNING: The classes defined in <strstream> are DEPRECATED. This
40 // header is defined in section D.7.1 of the C++ standard, and it
41 // MAY BE REMOVED in a future standard revision. You should use the
42 // header <sstream> instead.
51 namespace std
_GLIBCXX_VISIBILITY(default)
53 _GLIBCXX_BEGIN_NAMESPACE_VERSION
55 strstreambuf::strstreambuf(streamsize initial_capacity
)
56 : _Base(), _M_alloc_fun(0), _M_free_fun(0), _M_dynamic(true),
57 _M_frozen(false), _M_constant(false)
59 streamsize n
= std::max(initial_capacity
, streamsize(16));
61 char* buf
= _M_alloc(n
);
69 strstreambuf::strstreambuf(void* (*alloc_f
)(size_t), void (*free_f
)(void*))
70 : _Base(), _M_alloc_fun(alloc_f
), _M_free_fun(free_f
), _M_dynamic(true),
71 _M_frozen(false), _M_constant(false)
75 char* buf
= _M_alloc(n
);
83 strstreambuf::strstreambuf(char* get
, streamsize n
, char* put
) throw ()
84 : _Base(), _M_alloc_fun(0), _M_free_fun(0), _M_dynamic(false),
85 _M_frozen(false), _M_constant(false)
86 { _M_setup(get
, put
, n
); }
88 strstreambuf::strstreambuf(signed char* get
, streamsize n
, signed char* put
) throw ()
89 : _Base(), _M_alloc_fun(0), _M_free_fun(0), _M_dynamic(false),
90 _M_frozen(false), _M_constant(false)
91 { _M_setup(reinterpret_cast<char*>(get
), reinterpret_cast<char*>(put
), n
); }
93 strstreambuf::strstreambuf(unsigned char* get
, streamsize n
,
94 unsigned char* put
) throw ()
95 : _Base(), _M_alloc_fun(0), _M_free_fun(0), _M_dynamic(false),
96 _M_frozen(false), _M_constant(false)
97 { _M_setup(reinterpret_cast<char*>(get
), reinterpret_cast<char*>(put
), n
); }
99 strstreambuf::strstreambuf(const char* get
, streamsize n
) throw ()
100 : _Base(), _M_alloc_fun(0), _M_free_fun(0), _M_dynamic(false),
101 _M_frozen(false), _M_constant(true)
102 { _M_setup(const_cast<char*>(get
), 0, n
); }
104 strstreambuf::strstreambuf(const signed char* get
, streamsize n
) throw ()
105 : _Base(), _M_alloc_fun(0), _M_free_fun(0), _M_dynamic(false),
106 _M_frozen(false), _M_constant(true)
107 { _M_setup(reinterpret_cast<char*>(const_cast<signed char*>(get
)), 0, n
); }
109 strstreambuf::strstreambuf(const unsigned char* get
, streamsize n
) throw ()
110 : _Base(), _M_alloc_fun(0), _M_free_fun(0), _M_dynamic(false),
111 _M_frozen(false), _M_constant(true)
112 { _M_setup(reinterpret_cast<char*>(const_cast<unsigned char*>(get
)), 0, n
); }
114 strstreambuf::~strstreambuf()
116 if (_M_dynamic
&& !_M_frozen
)
121 strstreambuf::freeze(bool frozenflag
) throw ()
124 _M_frozen
= frozenflag
;
128 strstreambuf::str() throw ()
135 strstreambuf::pcount() const throw ()
136 { return pptr() ? pptr() - pbase() : 0; }
138 strstreambuf::int_type
139 strstreambuf::overflow(int_type c
)
141 if (c
== traits_type::eof())
142 return traits_type::not_eof(c
);
144 // Try to expand the buffer.
145 if (pptr() == epptr() && _M_dynamic
&& !_M_frozen
&& !_M_constant
)
147 ptrdiff_t old_size
= epptr() - pbase();
148 ptrdiff_t new_size
= std::max(ptrdiff_t(2 * old_size
), ptrdiff_t(1));
150 char* buf
= _M_alloc(new_size
);
153 memcpy(buf
, pbase(), old_size
);
154 char* old_buffer
= pbase();
155 bool reposition_get
= false;
156 ptrdiff_t old_get_offset
;
159 reposition_get
= true;
160 old_get_offset
= gptr() - eback();
163 setp(buf
, buf
+ new_size
);
164 __safe_pbump(old_size
);
167 setg(buf
, buf
+ old_get_offset
, buf
+
168 std::max(old_get_offset
, old_size
));
174 if (pptr() != epptr())
181 return traits_type::eof();
184 strstreambuf::int_type
185 strstreambuf::pbackfail(int_type c
)
187 if (gptr() != eback())
189 if (c
== _Traits::eof())
192 return _Traits::not_eof(c
);
194 else if (c
== _Traits::to_int_type(gptr()[-1]))
199 else if (!_M_constant
)
206 return _Traits::eof();
209 strstreambuf::int_type
210 strstreambuf::underflow()
212 if (gptr() == egptr() && pptr() && pptr() > egptr())
213 setg(eback(), gptr(), pptr());
215 if (gptr() != egptr())
216 return (unsigned char) *gptr();
218 return _Traits::eof();
221 basic_streambuf
<char, char_traits
<char> >*
222 strstreambuf::setbuf(char*, streamsize
)
225 strstreambuf::pos_type
226 strstreambuf::seekoff(off_type off
, ios_base::seekdir dir
,
227 ios_base::openmode mode
)
232 if ((mode
& (ios_base::in
| ios_base::out
))
233 == (ios_base::in
| ios_base::out
) &&
234 (dir
== ios_base::beg
|| dir
== ios_base::end
))
235 do_get
= do_put
= true;
236 else if (mode
& ios_base::in
)
238 else if (mode
& ios_base::out
)
241 // !gptr() is here because, according to D.7.1 paragraph 4, the seekable
242 // area is undefined if there is no get area.
243 if ((!do_get
&& !do_put
) || (do_put
&& !pptr()) || !gptr())
244 return pos_type(off_type(-1));
246 char* seeklow
= eback();
247 char* seekhigh
= epptr() ? epptr() : egptr();
256 newoff
= seekhigh
- seeklow
;
259 newoff
= do_put
? pptr() - seeklow
: gptr() - seeklow
;
262 return pos_type(off_type(-1));
266 if (off
< 0 || off
> seekhigh
- seeklow
)
267 return pos_type(off_type(-1));
271 if (seeklow
+ off
< pbase())
273 setp(seeklow
, epptr());
278 setp(pbase(), epptr());
279 __safe_pbump(off
- (pbase() - seeklow
));
284 if (off
<= egptr() - seeklow
)
285 setg(seeklow
, seeklow
+ off
, egptr());
286 else if (off
<= pptr() - seeklow
)
287 setg(seeklow
, seeklow
+ off
, pptr());
289 setg(seeklow
, seeklow
+ off
, epptr());
291 return pos_type(newoff
);
294 strstreambuf::pos_type
295 strstreambuf::seekpos(pos_type pos
, ios_base::openmode mode
)
296 { return seekoff(pos
- pos_type(off_type(0)), ios_base::beg
, mode
); }
299 strstreambuf::_M_alloc(size_t n
)
302 return static_cast<char*>(_M_alloc_fun(n
));
308 strstreambuf::_M_free(char* p
)
320 strstreambuf::_M_setup(char* get
, char* put
, streamsize n
) throw ()
324 size_t N
= n
> 0 ? size_t(n
) : n
== 0 ? strlen(get
) : size_t(INT_MAX
);
332 setg(get
, get
, get
+ N
);
336 istrstream::istrstream(char* s
)
337 : basic_ios
<char>(), basic_istream
<char>(0), _M_buf(s
, 0)
338 { basic_ios
<char>::init(&_M_buf
); }
340 istrstream::istrstream(const char* s
)
341 : basic_ios
<char>(), basic_istream
<char>(0), _M_buf(s
, 0)
342 { basic_ios
<char>::init(&_M_buf
); }
344 istrstream::istrstream(char* s
, streamsize n
)
345 : basic_ios
<char>(), basic_istream
<char>(0), _M_buf(s
, n
)
346 { basic_ios
<char>::init(&_M_buf
); }
348 istrstream::istrstream(const char* s
, streamsize n
)
349 : basic_ios
<char>(), basic_istream
<char>(0), _M_buf(s
, n
)
350 { basic_ios
<char>::init(&_M_buf
); }
352 istrstream::~istrstream() { }
355 istrstream::rdbuf() const throw ()
356 { return const_cast<strstreambuf
*>(&_M_buf
); }
359 istrstream::str() throw ()
360 { return _M_buf
.str(); }
362 ostrstream::ostrstream()
363 : basic_ios
<char>(), basic_ostream
<char>(0), _M_buf()
364 { basic_ios
<char>::init(&_M_buf
); }
366 ostrstream::ostrstream(char* s
, int n
, ios_base::openmode mode
)
367 : basic_ios
<char>(), basic_ostream
<char>(0),
368 _M_buf(s
, n
, mode
& ios_base::app
? s
+ strlen(s
) : s
)
369 { basic_ios
<char>::init(&_M_buf
); }
371 ostrstream::~ostrstream() {}
374 ostrstream::rdbuf() const throw ()
375 { return const_cast<strstreambuf
*>(&_M_buf
); }
378 ostrstream::freeze(bool freezeflag
) throw ()
379 { _M_buf
.freeze(freezeflag
); }
382 ostrstream::str() throw ()
383 { return _M_buf
.str(); }
386 ostrstream::pcount() const throw ()
387 { return _M_buf
.pcount(); }
389 strstream::strstream()
390 : basic_ios
<char>(), basic_iostream
<char>(0), _M_buf()
391 { basic_ios
<char>::init(&_M_buf
); }
393 strstream::strstream(char* s
, int n
, ios_base::openmode mode
)
394 : basic_ios
<char>(), basic_iostream
<char>(0),
395 _M_buf(s
, n
, mode
& ios_base::app
? s
+ strlen(s
) : s
)
396 { basic_ios
<char>::init(&_M_buf
); }
398 strstream::~strstream() { }
401 strstream::rdbuf() const throw ()
402 { return const_cast<strstreambuf
*>(&_M_buf
); }
405 strstream::freeze(bool freezeflag
) throw ()
406 { _M_buf
.freeze(freezeflag
); }
409 strstream::pcount() const throw ()
410 { return _M_buf
.pcount(); }
413 strstream::str() throw ()
414 { return _M_buf
.str(); }
416 _GLIBCXX_END_NAMESPACE_VERSION