3 * Silicon Graphics Computer Systems, Inc.
5 * Permission to use, copy, modify, distribute and sell this software
6 * and its documentation for any purpose is hereby granted without fee,
7 * provided that the above copyright notice appear in all copies and
8 * that both that copyright notice and this permission notice appear
9 * in supporting documentation. Silicon Graphics makes no
10 * representations about the suitability of this software for any
11 * purpose. It is provided "as is" without express or implied warranty.
14 // Implementation of the classes in header <strstream>.
15 // WARNING: The classes defined in <strstream> are DEPRECATED. This
16 // header is defined in section D.7.1 of the C++ standard, and it
17 // MAY BE REMOVED in a future standard revision. You should use the
18 // header <sstream> instead.
20 #include <strstream.h>
29 // strstreambuf constructor, destructor.
31 strstreambuf::strstreambuf(streamsize initial_capacity)
33 _M_alloc_fun(0), _M_free_fun(0),
34 _M_dynamic(true), _M_frozen(false), _M_constant(false)
36 streamsize n = max(initial_capacity, streamsize(16));
38 char* buf = _M_alloc(n);
45 strstreambuf::strstreambuf(void* (*alloc_f)(size_t), void (*free_f)(void*))
47 _M_alloc_fun(alloc_f), _M_free_fun(free_f),
48 _M_dynamic(true), _M_frozen(false), _M_constant(false)
52 char* buf = _M_alloc(n);
59 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)
64 _M_setup(get, put, n);
67 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)
72 _M_setup(reinterpret_cast<char*>(get), reinterpret_cast<char*>(put), n);
75 strstreambuf::strstreambuf(unsigned char* get, streamsize n,
78 _M_alloc_fun(0), _M_free_fun(0),
79 _M_dynamic(false), _M_frozen(false), _M_constant(false)
81 _M_setup(reinterpret_cast<char*>(get), reinterpret_cast<char*>(put), n);
84 strstreambuf::strstreambuf(const char* get, streamsize n)
86 _M_alloc_fun(0), _M_free_fun(0),
87 _M_dynamic(false), _M_frozen(false), _M_constant(true)
89 _M_setup(const_cast<char*>(get), 0, n);
92 strstreambuf::strstreambuf(const signed char* get, streamsize n)
94 _M_alloc_fun(0), _M_free_fun(0),
95 _M_dynamic(false), _M_frozen(false), _M_constant(true)
97 _M_setup(reinterpret_cast<char*>(const_cast<signed char*>(get)), 0, n);
100 strstreambuf::strstreambuf(const unsigned char* get, streamsize n)
102 _M_alloc_fun(0), _M_free_fun(0),
103 _M_dynamic(false), _M_frozen(false), _M_constant(true)
105 _M_setup(reinterpret_cast<char*>(const_cast<unsigned char*>(get)), 0, n);
108 strstreambuf::~strstreambuf()
110 if (_M_dynamic && !_M_frozen)
114 void strstreambuf::freeze(bool frozenflag)
117 _M_frozen = frozenflag;
120 char* strstreambuf::str()
126 int strstreambuf::pcount() const
128 return pptr() ? pptr() - pbase() : 0;
131 strstreambuf::int_type strstreambuf::overflow(int_type c) {
132 if (c == traits_type::eof())
133 return traits_type::not_eof(c);
135 // Try to expand the buffer.
136 if (pptr() == epptr() && _M_dynamic && !_M_frozen && !_M_constant) {
137 ptrdiff_t old_size = epptr() - pbase();
138 ptrdiff_t new_size = max(2 * old_size, ptrdiff_t(1));
140 char* buf = _M_alloc(new_size);
142 memcpy(buf, pbase(), old_size);
144 char* old_buffer = pbase();
145 bool reposition_get = false;
146 ptrdiff_t old_get_offset;
148 reposition_get = true;
149 old_get_offset = gptr() - eback();
152 setp(buf, buf + new_size);
156 setg(buf, buf + old_get_offset, buf + max(old_get_offset, old_size));
162 if (pptr() != epptr()) {
168 return traits_type::eof();
171 strstreambuf::int_type strstreambuf::pbackfail(int_type c)
173 if (gptr() != eback()) {
174 if (c == _Traits::eof()) {
176 return _Traits::not_eof(c);
178 else if (c == (unsigned int)(gptr()[-1])) { // (u int) added KLUDGE
182 else if (!_M_constant) {
189 return _Traits::eof();
192 strstreambuf::int_type strstreambuf::underflow()
194 if (gptr() == egptr() && pptr() && pptr() > egptr())
195 setg(eback(), gptr(), pptr());
197 if (gptr() != egptr())
198 return (unsigned char) *gptr();
200 return _Traits::eof();
203 basic_streambuf<char, char_traits<char> >*
204 strstreambuf::setbuf(char*, streamsize)
209 strstreambuf::pos_type
210 strstreambuf::seekoff(off_type off,
211 ios_base::seekdir dir, ios_base::openmode mode)
216 if ((mode & (ios_base::in | ios_base::out)) ==
217 (ios_base::in | ios_base::out) &&
218 (dir == ios_base::beg || dir == ios_base::end))
219 do_get = do_put = true;
220 else if (mode & ios_base::in)
222 else if (mode & ios_base::out)
225 // !gptr() is here because, according to D.7.1 paragraph 4, the seekable
226 // area is undefined if there is no get area.
227 if ((!do_get && !do_put) || (do_put && !pptr()) || !gptr())
228 return pos_type(off_type(-1));
230 char* seeklow = eback();
231 char* seekhigh = epptr() ? epptr() : egptr();
239 newoff = seekhigh - seeklow;
242 newoff = do_put ? pptr() - seeklow : gptr() - seeklow;
245 return pos_type(off_type(-1));
249 if (off < 0 || off > seekhigh - seeklow)
250 return pos_type(off_type(-1));
253 if (seeklow + off < pbase()) {
254 setp(seeklow, epptr());
258 setp(pbase(), epptr());
259 pbump(off - (pbase() - seeklow));
263 if (off <= egptr() - seeklow)
264 setg(seeklow, seeklow + off, egptr());
265 else if (off <= pptr() - seeklow)
266 setg(seeklow, seeklow + off, pptr());
268 setg(seeklow, seeklow + off, epptr());
271 return pos_type(newoff);
274 strstreambuf::pos_type
275 strstreambuf::seekpos(pos_type pos, ios_base::openmode mode)
277 return seekoff(pos - pos_type(off_type(0)), ios_base::beg, mode);
280 char* strstreambuf::_M_alloc(size_t n)
283 return static_cast<char*>(_M_alloc_fun(n));
288 void strstreambuf::_M_free(char* p)
297 void strstreambuf::_M_setup(char* get, char* put, streamsize n)
300 size_t N = n > 0 ? size_t(n) : n == 0 ? strlen(get) : size_t(INT_MAX);
307 setg(get, get, get + N);
312 //----------------------------------------------------------------------
315 istrstream::istrstream(char* s)
316 : basic_ios<char>(), basic_istream<char>(0), _M_buf(s, 0)
318 basic_ios<char>::init(&_M_buf);
321 istrstream::istrstream(const char* s)
322 : basic_ios<char>(), basic_istream<char>(0), _M_buf(s, 0)
324 basic_ios<char>::init(&_M_buf);
327 istrstream::istrstream(char* s, streamsize n)
328 : basic_ios<char>(), basic_istream<char>(0), _M_buf(s, n)
330 basic_ios<char>::init(&_M_buf);
333 istrstream::istrstream(const char* s, streamsize n)
334 : basic_ios<char>(), basic_istream<char>(0), _M_buf(s, n)
336 basic_ios<char>::init(&_M_buf);
339 istrstream::~istrstream() {}
341 strstreambuf* istrstream::rdbuf() const {
342 return const_cast<strstreambuf*>(&_M_buf);
345 char* istrstream::str() { return _M_buf.str(); }
347 //----------------------------------------------------------------------
350 ostrstream::ostrstream()
351 : basic_ios<char>(), basic_ostream<char>(0), _M_buf()
353 basic_ios<char>::init(&_M_buf);
356 ostrstream::ostrstream(char* s, int n, ios_base::openmode mode)
357 : basic_ios<char>(), basic_ostream<char>(0),
358 _M_buf(s, n, mode & ios_base::app ? s + strlen(s) : s)
360 basic_ios<char>::init(&_M_buf);
363 ostrstream::~ostrstream() {}
365 strstreambuf* ostrstream::rdbuf() const
367 return const_cast<strstreambuf*>(&_M_buf);
370 void ostrstream::freeze(bool freezeflag)
372 _M_buf.freeze(freezeflag);
375 char* ostrstream::str()
380 int ostrstream::pcount() const
382 return _M_buf.pcount();
385 //----------------------------------------------------------------------
388 strstream::strstream()
389 : basic_ios<char>(), basic_iostream<char>(0), _M_buf()
391 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)
398 basic_ios<char>::init(&_M_buf);
401 strstream::~strstream() {}
403 strstreambuf* strstream::rdbuf() const
405 return const_cast<strstreambuf*>(&_M_buf);
408 void strstream::freeze(bool freezeflag)
410 _M_buf.freeze(freezeflag);
413 int strstream::pcount() const
415 return _M_buf.pcount();
418 char* strstream::str()