1 // strstream definitions -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2005, 2009 Free Software Foundation
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 _GLIBCXX_BEGIN_NAMESPACE(std
)
53 strstreambuf::strstreambuf(streamsize initial_capacity
)
54 : _Base(), _M_alloc_fun(0), _M_free_fun(0), _M_dynamic(true),
55 _M_frozen(false), _M_constant(false)
57 streamsize n
= std::max(initial_capacity
, streamsize(16));
59 char* buf
= _M_alloc(n
);
67 strstreambuf::strstreambuf(void* (*alloc_f
)(size_t), void (*free_f
)(void*))
68 : _Base(), _M_alloc_fun(alloc_f
), _M_free_fun(free_f
), _M_dynamic(true),
69 _M_frozen(false), _M_constant(false)
73 char* buf
= _M_alloc(n
);
81 strstreambuf::strstreambuf(char* get
, streamsize n
, char* put
) throw ()
82 : _Base(), _M_alloc_fun(0), _M_free_fun(0), _M_dynamic(false),
83 _M_frozen(false), _M_constant(false)
84 { _M_setup(get
, put
, n
); }
86 strstreambuf::strstreambuf(signed char* get
, streamsize n
, signed char* put
) throw ()
87 : _Base(), _M_alloc_fun(0), _M_free_fun(0), _M_dynamic(false),
88 _M_frozen(false), _M_constant(false)
89 { _M_setup(reinterpret_cast<char*>(get
), reinterpret_cast<char*>(put
), n
); }
91 strstreambuf::strstreambuf(unsigned char* get
, streamsize n
,
92 unsigned char* put
) throw ()
93 : _Base(), _M_alloc_fun(0), _M_free_fun(0), _M_dynamic(false),
94 _M_frozen(false), _M_constant(false)
95 { _M_setup(reinterpret_cast<char*>(get
), reinterpret_cast<char*>(put
), n
); }
97 strstreambuf::strstreambuf(const char* get
, streamsize n
) throw ()
98 : _Base(), _M_alloc_fun(0), _M_free_fun(0), _M_dynamic(false),
99 _M_frozen(false), _M_constant(true)
100 { _M_setup(const_cast<char*>(get
), 0, n
); }
102 strstreambuf::strstreambuf(const signed char* get
, streamsize n
) throw ()
103 : _Base(), _M_alloc_fun(0), _M_free_fun(0), _M_dynamic(false),
104 _M_frozen(false), _M_constant(true)
105 { _M_setup(reinterpret_cast<char*>(const_cast<signed char*>(get
)), 0, n
); }
107 strstreambuf::strstreambuf(const unsigned char* get
, streamsize n
) throw ()
108 : _Base(), _M_alloc_fun(0), _M_free_fun(0), _M_dynamic(false),
109 _M_frozen(false), _M_constant(true)
110 { _M_setup(reinterpret_cast<char*>(const_cast<unsigned char*>(get
)), 0, n
); }
112 strstreambuf::~strstreambuf()
114 if (_M_dynamic
&& !_M_frozen
)
119 strstreambuf::freeze(bool frozenflag
) throw ()
122 _M_frozen
= frozenflag
;
126 strstreambuf::str() throw ()
133 strstreambuf::pcount() const throw ()
134 { return pptr() ? pptr() - pbase() : 0; }
136 strstreambuf::int_type
137 strstreambuf::overflow(int_type c
)
139 if (c
== traits_type::eof())
140 return traits_type::not_eof(c
);
142 // Try to expand the buffer.
143 if (pptr() == epptr() && _M_dynamic
&& !_M_frozen
&& !_M_constant
)
145 ptrdiff_t old_size
= epptr() - pbase();
146 ptrdiff_t new_size
= std::max(ptrdiff_t(2 * old_size
), ptrdiff_t(1));
148 char* buf
= _M_alloc(new_size
);
151 memcpy(buf
, pbase(), old_size
);
152 char* old_buffer
= pbase();
153 bool reposition_get
= false;
154 ptrdiff_t old_get_offset
;
157 reposition_get
= true;
158 old_get_offset
= gptr() - eback();
161 setp(buf
, buf
+ new_size
);
165 setg(buf
, buf
+ old_get_offset
, buf
+
166 std::max(old_get_offset
, old_size
));
172 if (pptr() != epptr())
179 return traits_type::eof();
182 strstreambuf::int_type
183 strstreambuf::pbackfail(int_type c
)
185 if (gptr() != eback())
187 if (c
== _Traits::eof())
190 return _Traits::not_eof(c
);
192 else if (c
== _Traits::to_int_type(gptr()[-1]))
197 else if (!_M_constant
)
204 return _Traits::eof();
207 strstreambuf::int_type
208 strstreambuf::underflow()
210 if (gptr() == egptr() && pptr() && pptr() > egptr())
211 setg(eback(), gptr(), pptr());
213 if (gptr() != egptr())
214 return (unsigned char) *gptr();
216 return _Traits::eof();
219 basic_streambuf
<char, char_traits
<char> >*
220 strstreambuf::setbuf(char*, streamsize
)
223 strstreambuf::pos_type
224 strstreambuf::seekoff(off_type off
, ios_base::seekdir dir
,
225 ios_base::openmode mode
)
230 if ((mode
& (ios_base::in
| ios_base::out
))
231 == (ios_base::in
| ios_base::out
) &&
232 (dir
== ios_base::beg
|| dir
== ios_base::end
))
233 do_get
= do_put
= true;
234 else if (mode
& ios_base::in
)
236 else if (mode
& ios_base::out
)
239 // !gptr() is here because, according to D.7.1 paragraph 4, the seekable
240 // area is undefined if there is no get area.
241 if ((!do_get
&& !do_put
) || (do_put
&& !pptr()) || !gptr())
242 return pos_type(off_type(-1));
244 char* seeklow
= eback();
245 char* seekhigh
= epptr() ? epptr() : egptr();
254 newoff
= seekhigh
- seeklow
;
257 newoff
= do_put
? pptr() - seeklow
: gptr() - seeklow
;
260 return pos_type(off_type(-1));
264 if (off
< 0 || off
> seekhigh
- seeklow
)
265 return pos_type(off_type(-1));
269 if (seeklow
+ off
< pbase())
271 setp(seeklow
, epptr());
276 setp(pbase(), epptr());
277 pbump(off
- (pbase() - seeklow
));
282 if (off
<= egptr() - seeklow
)
283 setg(seeklow
, seeklow
+ off
, egptr());
284 else if (off
<= pptr() - seeklow
)
285 setg(seeklow
, seeklow
+ off
, pptr());
287 setg(seeklow
, seeklow
+ off
, epptr());
289 return pos_type(newoff
);
292 strstreambuf::pos_type
293 strstreambuf::seekpos(pos_type pos
, ios_base::openmode mode
)
294 { return seekoff(pos
- pos_type(off_type(0)), ios_base::beg
, mode
); }
297 strstreambuf::_M_alloc(size_t n
)
300 return static_cast<char*>(_M_alloc_fun(n
));
306 strstreambuf::_M_free(char* p
)
318 strstreambuf::_M_setup(char* get
, char* put
, streamsize n
) throw ()
322 size_t N
= n
> 0 ? size_t(n
) : n
== 0 ? strlen(get
) : size_t(INT_MAX
);
330 setg(get
, get
, get
+ N
);
334 istrstream::istrstream(char* s
)
335 : basic_ios
<char>(), basic_istream
<char>(0), _M_buf(s
, 0)
336 { basic_ios
<char>::init(&_M_buf
); }
338 istrstream::istrstream(const char* s
)
339 : basic_ios
<char>(), basic_istream
<char>(0), _M_buf(s
, 0)
340 { basic_ios
<char>::init(&_M_buf
); }
342 istrstream::istrstream(char* s
, streamsize n
)
343 : basic_ios
<char>(), basic_istream
<char>(0), _M_buf(s
, n
)
344 { basic_ios
<char>::init(&_M_buf
); }
346 istrstream::istrstream(const char* s
, streamsize n
)
347 : basic_ios
<char>(), basic_istream
<char>(0), _M_buf(s
, n
)
348 { basic_ios
<char>::init(&_M_buf
); }
350 istrstream::~istrstream() { }
353 istrstream::rdbuf() const throw ()
354 { return const_cast<strstreambuf
*>(&_M_buf
); }
357 istrstream::str() throw ()
358 { return _M_buf
.str(); }
360 ostrstream::ostrstream()
361 : basic_ios
<char>(), basic_ostream
<char>(0), _M_buf()
362 { basic_ios
<char>::init(&_M_buf
); }
364 ostrstream::ostrstream(char* s
, int n
, ios_base::openmode mode
)
365 : basic_ios
<char>(), basic_ostream
<char>(0),
366 _M_buf(s
, n
, mode
& ios_base::app
? s
+ strlen(s
) : s
)
367 { basic_ios
<char>::init(&_M_buf
); }
369 ostrstream::~ostrstream() {}
372 ostrstream::rdbuf() const throw ()
373 { return const_cast<strstreambuf
*>(&_M_buf
); }
376 ostrstream::freeze(bool freezeflag
) throw ()
377 { _M_buf
.freeze(freezeflag
); }
380 ostrstream::str() throw ()
381 { return _M_buf
.str(); }
384 ostrstream::pcount() const throw ()
385 { return _M_buf
.pcount(); }
387 strstream::strstream()
388 : basic_ios
<char>(), basic_iostream
<char>(0), _M_buf()
389 { basic_ios
<char>::init(&_M_buf
); }
391 strstream::strstream(char* s
, int n
, ios_base::openmode mode
)
392 : basic_ios
<char>(), basic_iostream
<char>(0),
393 _M_buf(s
, n
, mode
& ios_base::app
? s
+ strlen(s
) : s
)
394 { basic_ios
<char>::init(&_M_buf
); }
396 strstream::~strstream() { }
399 strstream::rdbuf() const throw ()
400 { return const_cast<strstreambuf
*>(&_M_buf
); }
403 strstream::freeze(bool freezeflag
) throw ()
404 { _M_buf
.freeze(freezeflag
); }
407 strstream::pcount() const throw ()
408 { return _M_buf
.pcount(); }
411 strstream::str() throw ()
412 { return _M_buf
.str(); }
414 _GLIBCXX_END_NAMESPACE