1 // strstream definitions -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2005, 2009, 2010, 2011
4 // Free Software Foundation
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 3, or (at your option)
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.
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
28 * Silicon Graphics Computer Systems, Inc.
30 * Permission to use, copy, modify, distribute and sell this software
31 * and its documentation for any purpose is hereby granted without fee,
32 * provided that the above copyright notice appear in all copies and
33 * that both that copyright notice and this permission notice appear
34 * in supporting documentation. Silicon Graphics makes no
35 * representations about the suitability of this software for any
36 * purpose. It is provided "as is" without express or implied warranty.
39 // Implementation of the classes in header <strstream>.
40 // WARNING: The classes defined in <strstream> are DEPRECATED. This
41 // header is defined in section D.7.1 of the C++ standard, and it
42 // MAY BE REMOVED in a future standard revision. You should use the
43 // header <sstream> instead.
52 namespace std
_GLIBCXX_VISIBILITY(default)
54 _GLIBCXX_BEGIN_NAMESPACE_VERSION
56 strstreambuf::strstreambuf(streamsize initial_capacity
)
57 : _Base(), _M_alloc_fun(0), _M_free_fun(0), _M_dynamic(true),
58 _M_frozen(false), _M_constant(false)
60 streamsize n
= std::max(initial_capacity
, streamsize(16));
62 char* buf
= _M_alloc(n
);
70 strstreambuf::strstreambuf(void* (*alloc_f
)(size_t), void (*free_f
)(void*))
71 : _Base(), _M_alloc_fun(alloc_f
), _M_free_fun(free_f
), _M_dynamic(true),
72 _M_frozen(false), _M_constant(false)
76 char* buf
= _M_alloc(n
);
84 strstreambuf::strstreambuf(char* get
, streamsize n
, char* put
) throw ()
85 : _Base(), _M_alloc_fun(0), _M_free_fun(0), _M_dynamic(false),
86 _M_frozen(false), _M_constant(false)
87 { _M_setup(get
, put
, n
); }
89 strstreambuf::strstreambuf(signed char* get
, streamsize n
, signed char* put
) throw ()
90 : _Base(), _M_alloc_fun(0), _M_free_fun(0), _M_dynamic(false),
91 _M_frozen(false), _M_constant(false)
92 { _M_setup(reinterpret_cast<char*>(get
), reinterpret_cast<char*>(put
), n
); }
94 strstreambuf::strstreambuf(unsigned char* get
, streamsize n
,
95 unsigned char* put
) throw ()
96 : _Base(), _M_alloc_fun(0), _M_free_fun(0), _M_dynamic(false),
97 _M_frozen(false), _M_constant(false)
98 { _M_setup(reinterpret_cast<char*>(get
), reinterpret_cast<char*>(put
), n
); }
100 strstreambuf::strstreambuf(const char* get
, streamsize n
) throw ()
101 : _Base(), _M_alloc_fun(0), _M_free_fun(0), _M_dynamic(false),
102 _M_frozen(false), _M_constant(true)
103 { _M_setup(const_cast<char*>(get
), 0, n
); }
105 strstreambuf::strstreambuf(const signed char* get
, streamsize n
) throw ()
106 : _Base(), _M_alloc_fun(0), _M_free_fun(0), _M_dynamic(false),
107 _M_frozen(false), _M_constant(true)
108 { _M_setup(reinterpret_cast<char*>(const_cast<signed char*>(get
)), 0, n
); }
110 strstreambuf::strstreambuf(const unsigned char* get
, streamsize n
) throw ()
111 : _Base(), _M_alloc_fun(0), _M_free_fun(0), _M_dynamic(false),
112 _M_frozen(false), _M_constant(true)
113 { _M_setup(reinterpret_cast<char*>(const_cast<unsigned char*>(get
)), 0, n
); }
115 strstreambuf::~strstreambuf()
117 if (_M_dynamic
&& !_M_frozen
)
122 strstreambuf::freeze(bool frozenflag
) throw ()
125 _M_frozen
= frozenflag
;
129 strstreambuf::str() throw ()
136 strstreambuf::pcount() const throw ()
137 { return pptr() ? pptr() - pbase() : 0; }
139 strstreambuf::int_type
140 strstreambuf::overflow(int_type c
)
142 if (c
== traits_type::eof())
143 return traits_type::not_eof(c
);
145 // Try to expand the buffer.
146 if (pptr() == epptr() && _M_dynamic
&& !_M_frozen
&& !_M_constant
)
148 ptrdiff_t old_size
= epptr() - pbase();
149 ptrdiff_t new_size
= std::max(ptrdiff_t(2 * old_size
), ptrdiff_t(1));
151 char* buf
= _M_alloc(new_size
);
154 memcpy(buf
, pbase(), old_size
);
155 char* old_buffer
= pbase();
156 bool reposition_get
= false;
157 ptrdiff_t old_get_offset
;
160 reposition_get
= true;
161 old_get_offset
= gptr() - eback();
164 setp(buf
, buf
+ new_size
);
165 __safe_pbump(old_size
);
168 setg(buf
, buf
+ old_get_offset
, buf
+
169 std::max(old_get_offset
, old_size
));
175 if (pptr() != epptr())
182 return traits_type::eof();
185 strstreambuf::int_type
186 strstreambuf::pbackfail(int_type c
)
188 if (gptr() != eback())
190 if (c
== _Traits::eof())
193 return _Traits::not_eof(c
);
195 else if (c
== _Traits::to_int_type(gptr()[-1]))
200 else if (!_M_constant
)
207 return _Traits::eof();
210 strstreambuf::int_type
211 strstreambuf::underflow()
213 if (gptr() == egptr() && pptr() && pptr() > egptr())
214 setg(eback(), gptr(), pptr());
216 if (gptr() != egptr())
217 return (unsigned char) *gptr();
219 return _Traits::eof();
222 basic_streambuf
<char, char_traits
<char> >*
223 strstreambuf::setbuf(char*, streamsize
)
226 strstreambuf::pos_type
227 strstreambuf::seekoff(off_type off
, ios_base::seekdir dir
,
228 ios_base::openmode mode
)
233 if ((mode
& (ios_base::in
| ios_base::out
))
234 == (ios_base::in
| ios_base::out
) &&
235 (dir
== ios_base::beg
|| dir
== ios_base::end
))
236 do_get
= do_put
= true;
237 else if (mode
& ios_base::in
)
239 else if (mode
& ios_base::out
)
242 // !gptr() is here because, according to D.7.1 paragraph 4, the seekable
243 // area is undefined if there is no get area.
244 if ((!do_get
&& !do_put
) || (do_put
&& !pptr()) || !gptr())
245 return pos_type(off_type(-1));
247 char* seeklow
= eback();
248 char* seekhigh
= epptr() ? epptr() : egptr();
257 newoff
= seekhigh
- seeklow
;
260 newoff
= do_put
? pptr() - seeklow
: gptr() - seeklow
;
263 return pos_type(off_type(-1));
267 if (off
< 0 || off
> seekhigh
- seeklow
)
268 return pos_type(off_type(-1));
272 if (seeklow
+ off
< pbase())
274 setp(seeklow
, epptr());
279 setp(pbase(), epptr());
280 __safe_pbump(off
- (pbase() - seeklow
));
285 if (off
<= egptr() - seeklow
)
286 setg(seeklow
, seeklow
+ off
, egptr());
287 else if (off
<= pptr() - seeklow
)
288 setg(seeklow
, seeklow
+ off
, pptr());
290 setg(seeklow
, seeklow
+ off
, epptr());
292 return pos_type(newoff
);
295 strstreambuf::pos_type
296 strstreambuf::seekpos(pos_type pos
, ios_base::openmode mode
)
297 { return seekoff(pos
- pos_type(off_type(0)), ios_base::beg
, mode
); }
300 strstreambuf::_M_alloc(size_t n
)
303 return static_cast<char*>(_M_alloc_fun(n
));
309 strstreambuf::_M_free(char* p
)
321 strstreambuf::_M_setup(char* get
, char* put
, streamsize n
) throw ()
325 size_t N
= n
> 0 ? size_t(n
) : n
== 0 ? strlen(get
) : size_t(INT_MAX
);
333 setg(get
, get
, get
+ N
);
337 istrstream::istrstream(char* s
)
338 : basic_ios
<char>(), basic_istream
<char>(0), _M_buf(s
, 0)
339 { basic_ios
<char>::init(&_M_buf
); }
341 istrstream::istrstream(const char* s
)
342 : basic_ios
<char>(), basic_istream
<char>(0), _M_buf(s
, 0)
343 { basic_ios
<char>::init(&_M_buf
); }
345 istrstream::istrstream(char* s
, streamsize n
)
346 : basic_ios
<char>(), basic_istream
<char>(0), _M_buf(s
, n
)
347 { basic_ios
<char>::init(&_M_buf
); }
349 istrstream::istrstream(const char* s
, streamsize n
)
350 : basic_ios
<char>(), basic_istream
<char>(0), _M_buf(s
, n
)
351 { basic_ios
<char>::init(&_M_buf
); }
353 istrstream::~istrstream() { }
356 istrstream::rdbuf() const throw ()
357 { return const_cast<strstreambuf
*>(&_M_buf
); }
360 istrstream::str() throw ()
361 { return _M_buf
.str(); }
363 ostrstream::ostrstream()
364 : basic_ios
<char>(), basic_ostream
<char>(0), _M_buf()
365 { basic_ios
<char>::init(&_M_buf
); }
367 ostrstream::ostrstream(char* s
, int n
, ios_base::openmode mode
)
368 : basic_ios
<char>(), basic_ostream
<char>(0),
369 _M_buf(s
, n
, mode
& ios_base::app
? s
+ strlen(s
) : s
)
370 { basic_ios
<char>::init(&_M_buf
); }
372 ostrstream::~ostrstream() {}
375 ostrstream::rdbuf() const throw ()
376 { return const_cast<strstreambuf
*>(&_M_buf
); }
379 ostrstream::freeze(bool freezeflag
) throw ()
380 { _M_buf
.freeze(freezeflag
); }
383 ostrstream::str() throw ()
384 { return _M_buf
.str(); }
387 ostrstream::pcount() const throw ()
388 { return _M_buf
.pcount(); }
390 strstream::strstream()
391 : basic_ios
<char>(), basic_iostream
<char>(0), _M_buf()
392 { basic_ios
<char>::init(&_M_buf
); }
394 strstream::strstream(char* s
, int n
, ios_base::openmode mode
)
395 : basic_ios
<char>(), basic_iostream
<char>(0),
396 _M_buf(s
, n
, mode
& ios_base::app
? s
+ strlen(s
) : s
)
397 { basic_ios
<char>::init(&_M_buf
); }
399 strstream::~strstream() { }
402 strstream::rdbuf() const throw ()
403 { return const_cast<strstreambuf
*>(&_M_buf
); }
406 strstream::freeze(bool freezeflag
) throw ()
407 { _M_buf
.freeze(freezeflag
); }
410 strstream::pcount() const throw ()
411 { return _M_buf
.pcount(); }
414 strstream::str() throw ()
415 { return _M_buf
.str(); }
417 _GLIBCXX_END_NAMESPACE_VERSION