First public contribution.
2 * Portions Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
5 * Silicon Graphics Computer Systems, Inc.
10 * This material is provided "as is", with absolutely no warranty expressed
11 * or implied. Any use is at your own risk.
13 * Permission to use or copy this software for any purpose is hereby granted
14 * without fee, provided the above notices are retained on all copies.
15 * Permission to modify the code and to distribute modified code is granted,
16 * provided the above notices are retained, and a notice that the code was
17 * modified is included with the above copyright notice.
21 // Implementation of the classes in header <strstream>.
22 // WARNING: The classes defined in <strstream> are DEPRECATED. This
23 // header is defined in section D.7.1 of the C++ standard, and it
24 // MAY BE REMOVED in a future standard revision. You should use the
25 // header <sstream> instead.
27 #include "stlport_prefix.h"
35 // strstreambuf constructor, destructor.
36 _STLP_DECLSPEC strstreambuf::strstreambuf(streamsize initial_capacity)
37 : _M_alloc_fun(0), _M_free_fun(0),
38 _M_dynamic(true), _M_frozen(false), _M_constant(false) {
39 size_t n = (sizeof(streamsize) > sizeof(size_t)) ? __STATIC_CAST(size_t, (min)(__STATIC_CAST(streamsize, (numeric_limits<size_t>::max)()),
40 (max)(initial_capacity, streamsize(16))))
41 : __STATIC_CAST(size_t, (max)(initial_capacity, streamsize(16)));
43 char* buf = _M_alloc(n);
50 _STLP_DECLSPEC strstreambuf::strstreambuf(__alloc_fn alloc_f, __free_fn free_f)
51 : _M_alloc_fun(alloc_f), _M_free_fun(free_f),
52 _M_dynamic(true), _M_frozen(false), _M_constant(false) {
55 char* buf = _M_alloc(n);
62 _STLP_DECLSPEC strstreambuf::strstreambuf(char* get, streamsize n, char* put)
63 : _M_alloc_fun(0), _M_free_fun(0),
64 _M_dynamic(false), _M_frozen(false), _M_constant(false) {
65 _M_setup(get, put, n);
68 _STLP_DECLSPEC strstreambuf::strstreambuf(signed char* get, streamsize n, signed char* put)
69 : _M_alloc_fun(0), _M_free_fun(0),
70 _M_dynamic(false), _M_frozen(false), _M_constant(false) {
71 _M_setup(__REINTERPRET_CAST(char*,get), __REINTERPRET_CAST(char*,put), n);
74 _STLP_DECLSPEC strstreambuf::strstreambuf(unsigned char* get, streamsize n,
76 : _M_alloc_fun(0), _M_free_fun(0),
77 _M_dynamic(false), _M_frozen(false), _M_constant(false) {
78 _M_setup(__REINTERPRET_CAST(char*,get), __REINTERPRET_CAST(char*,put), n);
81 _STLP_DECLSPEC strstreambuf::strstreambuf(const char* get, streamsize n)
82 : _M_alloc_fun(0), _M_free_fun(0),
83 _M_dynamic(false), _M_frozen(false), _M_constant(true) {
84 _M_setup(__CONST_CAST(char*,get), 0, n);
87 _STLP_DECLSPEC strstreambuf::strstreambuf(const signed char* get, streamsize n)
88 : _M_alloc_fun(0), _M_free_fun(0),
89 _M_dynamic(false), _M_frozen(false), _M_constant(true) {
90 _M_setup(__REINTERPRET_CAST(char*, __CONST_CAST(signed char*,get)), 0, n);
93 _STLP_DECLSPEC strstreambuf::strstreambuf(const unsigned char* get, streamsize n)
94 : _M_alloc_fun(0), _M_free_fun(0),
95 _M_dynamic(false), _M_frozen(false), _M_constant(true) {
96 _M_setup(__REINTERPRET_CAST(char*, __CONST_CAST(unsigned char*,get)), 0, n);
99 _STLP_DECLSPEC strstreambuf::~strstreambuf() {
100 if (_M_dynamic && !_M_frozen)
104 _STLP_DECLSPEC void strstreambuf::freeze(bool frozenflag) {
106 _M_frozen = frozenflag;
109 _STLP_DECLSPEC char* strstreambuf::str() {
114 _STLP_DECLSPEC int strstreambuf::pcount() const {
115 return int(pptr() ? pptr() - pbase() : 0);
118 _STLP_DECLSPEC strstreambuf::int_type strstreambuf::overflow(int_type c) {
119 if (c == traits_type::eof())
120 return traits_type::not_eof(c);
122 // Try to expand the buffer.
123 if (pptr() == epptr() && _M_dynamic && !_M_frozen && !_M_constant) {
124 ptrdiff_t old_size = epptr() - pbase();
125 ptrdiff_t new_size = (max)(2 * old_size, ptrdiff_t(1));
127 char* buf = _M_alloc(new_size);
129 memcpy(buf, pbase(), old_size);
131 char* old_buffer = pbase();
132 bool reposition_get = false;
133 ptrdiff_t old_get_offset;
135 reposition_get = true;
136 old_get_offset = gptr() - eback();
139 setp(buf, buf + new_size);
140 pbump((int)old_size);
143 setg(buf, buf + old_get_offset, buf + (max)(old_get_offset, old_size));
149 if (pptr() != epptr()) {
150 *pptr() = traits_type::to_char_type(c);
155 return traits_type::eof();
158 _STLP_DECLSPEC strstreambuf::int_type strstreambuf::pbackfail(int_type c) {
159 if (gptr() != eback()) {
160 if (c == traits_type::eof()) {
162 return traits_type::not_eof(c);
164 else if (c == gptr()[-1]) {
168 else if (!_M_constant) {
170 *gptr() = traits_type::to_char_type(c);
175 return traits_type::eof();
178 _STLP_DECLSPEC strstreambuf::int_type strstreambuf::underflow() {
179 if (gptr() == egptr() && pptr() && pptr() > egptr())
180 setg(eback(), gptr(), pptr());
182 if (gptr() != egptr())
183 return (unsigned char) *gptr();
185 return _Traits::eof();
188 _STLP_DECLSPEC basic_streambuf<char, char_traits<char> >*
189 strstreambuf::setbuf(char*, streamsize) {
193 _STLP_DECLSPEC strstreambuf::pos_type
194 strstreambuf::seekoff(off_type off,
195 ios_base::seekdir dir, ios_base::openmode mode) {
199 if ((mode & (ios_base::in | ios_base::out)) ==
200 (ios_base::in | ios_base::out) &&
201 (dir == ios_base::beg || dir == ios_base::end))
202 do_get = do_put = true;
203 else if (mode & ios_base::in)
205 else if (mode & ios_base::out)
208 // !gptr() is here because, according to D.7.1 paragraph 4, the seekable
209 // area is undefined if there is no get area.
210 if ((!do_get && !do_put) || (do_put && !pptr()) || !gptr())
211 return pos_type(off_type(-1));
213 char* seeklow = eback();
214 char* seekhigh = epptr() ? epptr() : egptr();
222 newoff = seekhigh - seeklow;
225 newoff = do_put ? pptr() - seeklow : gptr() - seeklow;
228 return pos_type(off_type(-1));
232 if (off < 0 || off > seekhigh - seeklow)
233 return pos_type(off_type(-1));
236 if (seeklow + __STATIC_CAST(ptrdiff_t, off) < pbase()) {
237 setp(seeklow, epptr());
241 setp(pbase(), epptr());
242 pbump((int)(off - (pbase() - seeklow)));
246 if (off <= egptr() - seeklow)
247 setg(seeklow, seeklow + __STATIC_CAST(ptrdiff_t, off), egptr());
248 else if (off <= pptr() - seeklow)
249 setg(seeklow, seeklow + __STATIC_CAST(ptrdiff_t, off), pptr());
251 setg(seeklow, seeklow + __STATIC_CAST(ptrdiff_t, off), epptr());
254 return pos_type(newoff);
257 _STLP_DECLSPEC strstreambuf::pos_type
258 strstreambuf::seekpos(pos_type pos, ios_base::openmode mode) {
259 return seekoff(pos - pos_type(off_type(0)), ios_base::beg, mode);
263 char* strstreambuf::_M_alloc(size_t n) {
265 return __STATIC_CAST(char*,_M_alloc_fun(n));
270 void strstreambuf::_M_free(char* p) {
278 void strstreambuf::_M_setup(char* get, char* put, streamsize n) {
280 size_t N = n > 0 ? size_t(n) : n == 0 ? strlen(get) : size_t(INT_MAX);
283 setg(get, get, get + N);
287 setg(get, get, get + N);
292 //----------------------------------------------------------------------
295 _STLP_DECLSPEC istrstream::istrstream(char* s)
296 : basic_istream<char, char_traits<char> >(0), _M_buf(s, 0) {
300 _STLP_DECLSPEC istrstream::istrstream(const char* s)
301 : basic_istream<char, char_traits<char> >(0), _M_buf(s, 0) {
305 _STLP_DECLSPEC istrstream::istrstream(char* s, streamsize n)
306 : basic_istream<char, char_traits<char> >(0), _M_buf(s, n) {
310 _STLP_DECLSPEC istrstream::istrstream(const char* s, streamsize n)
311 : basic_istream<char, char_traits<char> >(0), _M_buf(s, n) {
315 _STLP_DECLSPEC istrstream::~istrstream() {}
317 _STLP_DECLSPEC strstreambuf* istrstream::rdbuf() const {
318 return __CONST_CAST(strstreambuf*,&_M_buf);
321 _STLP_DECLSPEC char* istrstream::str() { return _M_buf.str(); }
323 //----------------------------------------------------------------------
326 _STLP_DECLSPEC ostrstream::ostrstream()
327 : basic_ostream<char, char_traits<char> >(0), _M_buf() {
328 basic_ios<char, char_traits<char> >::init(&_M_buf);
331 _STLP_DECLSPEC ostrstream::ostrstream(char* s, int n, ios_base::openmode mode)
332 : basic_ostream<char, char_traits<char> >(0),
333 _M_buf(s, n, mode & ios_base::app ? s + strlen(s) : s) {
334 basic_ios<char, char_traits<char> >::init(&_M_buf);
337 _STLP_DECLSPEC ostrstream::~ostrstream() {}
339 _STLP_DECLSPEC strstreambuf* ostrstream::rdbuf() const {
340 return __CONST_CAST(strstreambuf*,&_M_buf);
343 _STLP_DECLSPEC void ostrstream::freeze(bool freezeflag) {
344 _M_buf.freeze(freezeflag);
347 _STLP_DECLSPEC char* ostrstream::str() {
351 _STLP_DECLSPEC int ostrstream::pcount() const {
352 return _M_buf.pcount();
356 //----------------------------------------------------------------------
359 _STLP_DECLSPEC strstream::strstream()
360 : basic_iostream<char, char_traits<char> >(0), _M_buf() {
361 basic_ios<char, char_traits<char> >::init(&_M_buf);
364 _STLP_DECLSPEC strstream::strstream(char* s, int n, ios_base::openmode mode)
365 : basic_iostream<char, char_traits<char> >(0),
366 _M_buf(s, n, mode & ios_base::app ? s + strlen(s) : s) {
367 basic_ios<char, char_traits<char> >::init(&_M_buf);
370 _STLP_DECLSPEC strstream::~strstream() {}
372 _STLP_DECLSPEC strstreambuf* strstream::rdbuf() const {
373 return __CONST_CAST(strstreambuf*,&_M_buf);
376 _STLP_DECLSPEC void strstream::freeze(bool freezeflag) {
377 _M_buf.freeze(freezeflag);
380 _STLP_DECLSPEC int strstream::pcount() const {
381 return _M_buf.pcount();
384 _STLP_DECLSPEC char* strstream::str() {