3 * Silicon Graphics Computer Systems, Inc.
8 * This material is provided "as is", with absolutely no warranty expressed
9 * or implied. Any use is at your own risk.
11 * Permission to use or copy this software for any purpose is hereby granted
12 * without fee, provided the above notices are retained on all copies.
13 * Permission to modify the code and to distribute modified code is granted,
14 * provided the above notices are retained, and a notice that the code was
15 * modified is included with the above copyright notice.
19 // Implementation of the classes in header <strstream>.
20 // WARNING: The classes defined in <strstream> are DEPRECATED. This
21 // header is defined in section D.7.1 of the C++ standard, and it
22 // MAY BE REMOVED in a future standard revision. You should use the
23 // header <sstream> instead.
25 #include "stlport_prefix.h"
33 // strstreambuf constructor, destructor.
34 strstreambuf::strstreambuf(streamsize initial_capacity)
35 : _M_alloc_fun(0), _M_free_fun(0),
36 _M_dynamic(true), _M_frozen(false), _M_constant(false) {
37 size_t n = (sizeof(streamsize) > sizeof(size_t)) ? __STATIC_CAST(size_t, (min)(__STATIC_CAST(streamsize, (numeric_limits<size_t>::max)()),
38 (max)(initial_capacity, streamsize(16))))
39 : __STATIC_CAST(size_t, (max)(initial_capacity, streamsize(16)));
41 char* buf = _M_alloc(n);
48 strstreambuf::strstreambuf(__alloc_fn alloc_f, __free_fn free_f)
49 : _M_alloc_fun(alloc_f), _M_free_fun(free_f),
50 _M_dynamic(true), _M_frozen(false), _M_constant(false) {
53 char* buf = _M_alloc(n);
60 strstreambuf::strstreambuf(char* get, streamsize n, char* put)
61 : _M_alloc_fun(0), _M_free_fun(0),
62 _M_dynamic(false), _M_frozen(false), _M_constant(false) {
63 _M_setup(get, put, n);
66 strstreambuf::strstreambuf(signed char* get, streamsize n, signed char* put)
67 : _M_alloc_fun(0), _M_free_fun(0),
68 _M_dynamic(false), _M_frozen(false), _M_constant(false) {
69 _M_setup(__REINTERPRET_CAST(char*,get), __REINTERPRET_CAST(char*,put), n);
72 strstreambuf::strstreambuf(unsigned char* get, streamsize n,
74 : _M_alloc_fun(0), _M_free_fun(0),
75 _M_dynamic(false), _M_frozen(false), _M_constant(false) {
76 _M_setup(__REINTERPRET_CAST(char*,get), __REINTERPRET_CAST(char*,put), n);
79 strstreambuf::strstreambuf(const char* get, streamsize n)
80 : _M_alloc_fun(0), _M_free_fun(0),
81 _M_dynamic(false), _M_frozen(false), _M_constant(true) {
82 _M_setup(__CONST_CAST(char*,get), 0, n);
85 strstreambuf::strstreambuf(const signed char* get, streamsize n)
86 : _M_alloc_fun(0), _M_free_fun(0),
87 _M_dynamic(false), _M_frozen(false), _M_constant(true) {
88 _M_setup(__REINTERPRET_CAST(char*, __CONST_CAST(signed char*,get)), 0, n);
91 strstreambuf::strstreambuf(const unsigned char* get, streamsize n)
92 : _M_alloc_fun(0), _M_free_fun(0),
93 _M_dynamic(false), _M_frozen(false), _M_constant(true) {
94 _M_setup(__REINTERPRET_CAST(char*, __CONST_CAST(unsigned char*,get)), 0, n);
97 strstreambuf::~strstreambuf() {
98 if (_M_dynamic && !_M_frozen)
102 void strstreambuf::freeze(bool frozenflag) {
104 _M_frozen = frozenflag;
107 char* strstreambuf::str() {
112 int strstreambuf::pcount() const {
113 return int(pptr() ? pptr() - pbase() : 0);
116 strstreambuf::int_type strstreambuf::overflow(int_type c) {
117 if (c == traits_type::eof())
118 return traits_type::not_eof(c);
120 // Try to expand the buffer.
121 if (pptr() == epptr() && _M_dynamic && !_M_frozen && !_M_constant) {
122 ptrdiff_t old_size = epptr() - pbase();
123 ptrdiff_t new_size = (max)(2 * old_size, ptrdiff_t(1));
125 char* buf = _M_alloc(new_size);
127 memcpy(buf, pbase(), old_size);
129 char* old_buffer = pbase();
130 bool reposition_get = false;
131 ptrdiff_t old_get_offset;
133 reposition_get = true;
134 old_get_offset = gptr() - eback();
137 setp(buf, buf + new_size);
138 pbump((int)old_size);
141 setg(buf, buf + old_get_offset, buf + (max)(old_get_offset, old_size));
147 if (pptr() != epptr()) {
148 *pptr() = traits_type::to_char_type(c);
153 return traits_type::eof();
156 strstreambuf::int_type strstreambuf::pbackfail(int_type c) {
157 if (gptr() != eback()) {
158 if (c == traits_type::eof()) {
160 return traits_type::not_eof(c);
162 else if (c == gptr()[-1]) {
166 else if (!_M_constant) {
168 *gptr() = traits_type::to_char_type(c);
173 return traits_type::eof();
176 strstreambuf::int_type strstreambuf::underflow() {
177 if (gptr() == egptr() && pptr() && pptr() > egptr())
178 setg(eback(), gptr(), pptr());
180 if (gptr() != egptr())
181 return (unsigned char) *gptr();
183 return _Traits::eof();
186 basic_streambuf<char, char_traits<char> >*
187 strstreambuf::setbuf(char*, streamsize) {
191 strstreambuf::pos_type
192 strstreambuf::seekoff(off_type off,
193 ios_base::seekdir dir, ios_base::openmode mode) {
197 if ((mode & (ios_base::in | ios_base::out)) ==
198 (ios_base::in | ios_base::out) &&
199 (dir == ios_base::beg || dir == ios_base::end))
200 do_get = do_put = true;
201 else if (mode & ios_base::in)
203 else if (mode & ios_base::out)
206 // !gptr() is here because, according to D.7.1 paragraph 4, the seekable
207 // area is undefined if there is no get area.
208 if ((!do_get && !do_put) || (do_put && !pptr()) || !gptr())
209 return pos_type(off_type(-1));
211 char* seeklow = eback();
212 char* seekhigh = epptr() ? epptr() : egptr();
220 newoff = seekhigh - seeklow;
223 newoff = do_put ? pptr() - seeklow : gptr() - seeklow;
226 return pos_type(off_type(-1));
230 if (off < 0 || off > seekhigh - seeklow)
231 return pos_type(off_type(-1));
234 if (seeklow + __STATIC_CAST(ptrdiff_t, off) < pbase()) {
235 setp(seeklow, epptr());
239 setp(pbase(), epptr());
240 pbump((int)(off - (pbase() - seeklow)));
244 if (off <= egptr() - seeklow)
245 setg(seeklow, seeklow + __STATIC_CAST(ptrdiff_t, off), egptr());
246 else if (off <= pptr() - seeklow)
247 setg(seeklow, seeklow + __STATIC_CAST(ptrdiff_t, off), pptr());
249 setg(seeklow, seeklow + __STATIC_CAST(ptrdiff_t, off), epptr());
252 return pos_type(newoff);
255 strstreambuf::pos_type
256 strstreambuf::seekpos(pos_type pos, ios_base::openmode mode) {
257 return seekoff(pos - pos_type(off_type(0)), ios_base::beg, mode);
261 char* strstreambuf::_M_alloc(size_t n) {
263 return __STATIC_CAST(char*,_M_alloc_fun(n));
268 void strstreambuf::_M_free(char* p) {
276 void strstreambuf::_M_setup(char* get, char* put, streamsize n) {
278 size_t N = n > 0 ? size_t(n) : n == 0 ? strlen(get) : size_t(INT_MAX);
281 setg(get, get, get + N);
285 setg(get, get, get + N);
290 //----------------------------------------------------------------------
293 istrstream::istrstream(char* s)
294 : basic_istream<char, char_traits<char> >(0), _M_buf(s, 0) {
298 istrstream::istrstream(const char* s)
299 : basic_istream<char, char_traits<char> >(0), _M_buf(s, 0) {
303 istrstream::istrstream(char* s, streamsize n)
304 : basic_istream<char, char_traits<char> >(0), _M_buf(s, n) {
308 istrstream::istrstream(const char* s, streamsize n)
309 : basic_istream<char, char_traits<char> >(0), _M_buf(s, n) {
313 istrstream::~istrstream() {}
315 strstreambuf* istrstream::rdbuf() const {
316 return __CONST_CAST(strstreambuf*,&_M_buf);
319 char* istrstream::str() { return _M_buf.str(); }
321 //----------------------------------------------------------------------
324 ostrstream::ostrstream()
325 : basic_ostream<char, char_traits<char> >(0), _M_buf() {
326 basic_ios<char, char_traits<char> >::init(&_M_buf);
329 ostrstream::ostrstream(char* s, int n, ios_base::openmode mode)
330 : basic_ostream<char, char_traits<char> >(0),
331 _M_buf(s, n, mode & ios_base::app ? s + strlen(s) : s) {
332 basic_ios<char, char_traits<char> >::init(&_M_buf);
335 ostrstream::~ostrstream() {}
337 strstreambuf* ostrstream::rdbuf() const {
338 return __CONST_CAST(strstreambuf*,&_M_buf);
341 void ostrstream::freeze(bool freezeflag) {
342 _M_buf.freeze(freezeflag);
345 char* ostrstream::str() {
349 int ostrstream::pcount() const {
350 return _M_buf.pcount();
354 //----------------------------------------------------------------------
357 strstream::strstream()
358 : basic_iostream<char, char_traits<char> >(0), _M_buf() {
359 basic_ios<char, char_traits<char> >::init(&_M_buf);
362 strstream::strstream(char* s, int n, ios_base::openmode mode)
363 : basic_iostream<char, char_traits<char> >(0),
364 _M_buf(s, n, mode & ios_base::app ? s + strlen(s) : s) {
365 basic_ios<char, char_traits<char> >::init(&_M_buf);
368 strstream::~strstream() {}
370 strstreambuf* strstream::rdbuf() const {
371 return __CONST_CAST(strstreambuf*,&_M_buf);
374 void strstream::freeze(bool freezeflag) {
375 _M_buf.freeze(freezeflag);
378 int strstream::pcount() const {
379 return _M_buf.pcount();
382 char* strstream::str() {