michael@0: /* michael@0: * Copyright (c) 1999 michael@0: * Silicon Graphics Computer Systems, Inc. michael@0: * michael@0: * Copyright (c) 1999 michael@0: * Boris Fomitchev michael@0: * michael@0: * This material is provided "as is", with absolutely no warranty expressed michael@0: * or implied. Any use is at your own risk. michael@0: * michael@0: * Permission to use or copy this software for any purpose is hereby granted michael@0: * without fee, provided the above notices are retained on all copies. michael@0: * Permission to modify the code and to distribute modified code is granted, michael@0: * provided the above notices are retained, and a notice that the code was michael@0: * modified is included with the above copyright notice. michael@0: * michael@0: */ michael@0: michael@0: // Implementation of the classes in header . michael@0: // WARNING: The classes defined in are DEPRECATED. This michael@0: // header is defined in section D.7.1 of the C++ standard, and it michael@0: // MAY BE REMOVED in a future standard revision. You should use the michael@0: // header instead. michael@0: michael@0: #include "stlport_prefix.h" michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: _STLP_BEGIN_NAMESPACE michael@0: michael@0: // strstreambuf constructor, destructor. michael@0: strstreambuf::strstreambuf(streamsize initial_capacity) michael@0: : _M_alloc_fun(0), _M_free_fun(0), michael@0: _M_dynamic(true), _M_frozen(false), _M_constant(false) { michael@0: size_t n = (sizeof(streamsize) > sizeof(size_t)) ? __STATIC_CAST(size_t, (min)(__STATIC_CAST(streamsize, (numeric_limits::max)()), michael@0: (max)(initial_capacity, streamsize(16)))) michael@0: : __STATIC_CAST(size_t, (max)(initial_capacity, streamsize(16))); michael@0: michael@0: char* buf = _M_alloc(n); michael@0: if (buf) { michael@0: setp(buf, buf + n); michael@0: setg(buf, buf, buf); michael@0: } michael@0: } michael@0: michael@0: strstreambuf::strstreambuf(__alloc_fn alloc_f, __free_fn free_f) michael@0: : _M_alloc_fun(alloc_f), _M_free_fun(free_f), michael@0: _M_dynamic(true), _M_frozen(false), _M_constant(false) { michael@0: size_t n = 16; michael@0: michael@0: char* buf = _M_alloc(n); michael@0: if (buf) { michael@0: setp(buf, buf + n); michael@0: setg(buf, buf, buf); michael@0: } michael@0: } michael@0: michael@0: strstreambuf::strstreambuf(char* get, streamsize n, char* put) michael@0: : _M_alloc_fun(0), _M_free_fun(0), michael@0: _M_dynamic(false), _M_frozen(false), _M_constant(false) { michael@0: _M_setup(get, put, n); michael@0: } michael@0: michael@0: strstreambuf::strstreambuf(signed char* get, streamsize n, signed char* put) michael@0: : _M_alloc_fun(0), _M_free_fun(0), michael@0: _M_dynamic(false), _M_frozen(false), _M_constant(false) { michael@0: _M_setup(__REINTERPRET_CAST(char*,get), __REINTERPRET_CAST(char*,put), n); michael@0: } michael@0: michael@0: strstreambuf::strstreambuf(unsigned char* get, streamsize n, michael@0: unsigned char* put) michael@0: : _M_alloc_fun(0), _M_free_fun(0), michael@0: _M_dynamic(false), _M_frozen(false), _M_constant(false) { michael@0: _M_setup(__REINTERPRET_CAST(char*,get), __REINTERPRET_CAST(char*,put), n); michael@0: } michael@0: michael@0: strstreambuf::strstreambuf(const char* get, streamsize n) michael@0: : _M_alloc_fun(0), _M_free_fun(0), michael@0: _M_dynamic(false), _M_frozen(false), _M_constant(true) { michael@0: _M_setup(__CONST_CAST(char*,get), 0, n); michael@0: } michael@0: michael@0: strstreambuf::strstreambuf(const signed char* get, streamsize n) michael@0: : _M_alloc_fun(0), _M_free_fun(0), michael@0: _M_dynamic(false), _M_frozen(false), _M_constant(true) { michael@0: _M_setup(__REINTERPRET_CAST(char*, __CONST_CAST(signed char*,get)), 0, n); michael@0: } michael@0: michael@0: strstreambuf::strstreambuf(const unsigned char* get, streamsize n) michael@0: : _M_alloc_fun(0), _M_free_fun(0), michael@0: _M_dynamic(false), _M_frozen(false), _M_constant(true) { michael@0: _M_setup(__REINTERPRET_CAST(char*, __CONST_CAST(unsigned char*,get)), 0, n); michael@0: } michael@0: michael@0: strstreambuf::~strstreambuf() { michael@0: if (_M_dynamic && !_M_frozen) michael@0: _M_free(eback()); michael@0: } michael@0: michael@0: void strstreambuf::freeze(bool frozenflag) { michael@0: if (_M_dynamic) michael@0: _M_frozen = frozenflag; michael@0: } michael@0: michael@0: char* strstreambuf::str() { michael@0: freeze(true); michael@0: return eback(); michael@0: } michael@0: michael@0: int strstreambuf::pcount() const { michael@0: return int(pptr() ? pptr() - pbase() : 0); michael@0: } michael@0: michael@0: strstreambuf::int_type strstreambuf::overflow(int_type c) { michael@0: if (c == traits_type::eof()) michael@0: return traits_type::not_eof(c); michael@0: michael@0: // Try to expand the buffer. michael@0: if (pptr() == epptr() && _M_dynamic && !_M_frozen && !_M_constant) { michael@0: ptrdiff_t old_size = epptr() - pbase(); michael@0: ptrdiff_t new_size = (max)(2 * old_size, ptrdiff_t(1)); michael@0: michael@0: char* buf = _M_alloc(new_size); michael@0: if (buf) { michael@0: memcpy(buf, pbase(), old_size); michael@0: michael@0: char* old_buffer = pbase(); michael@0: bool reposition_get = false; michael@0: ptrdiff_t old_get_offset; michael@0: if (gptr() != 0) { michael@0: reposition_get = true; michael@0: old_get_offset = gptr() - eback(); michael@0: } michael@0: michael@0: setp(buf, buf + new_size); michael@0: pbump((int)old_size); michael@0: michael@0: if (reposition_get) michael@0: setg(buf, buf + old_get_offset, buf + (max)(old_get_offset, old_size)); michael@0: michael@0: _M_free(old_buffer); michael@0: } michael@0: } michael@0: michael@0: if (pptr() != epptr()) { michael@0: *pptr() = traits_type::to_char_type(c); michael@0: pbump(1); michael@0: return c; michael@0: } michael@0: else michael@0: return traits_type::eof(); michael@0: } michael@0: michael@0: strstreambuf::int_type strstreambuf::pbackfail(int_type c) { michael@0: if (gptr() != eback()) { michael@0: if (c == traits_type::eof()) { michael@0: gbump(-1); michael@0: return traits_type::not_eof(c); michael@0: } michael@0: else if (c == gptr()[-1]) { michael@0: gbump(-1); michael@0: return c; michael@0: } michael@0: else if (!_M_constant) { michael@0: gbump(-1); michael@0: *gptr() = traits_type::to_char_type(c); michael@0: return c; michael@0: } michael@0: } michael@0: michael@0: return traits_type::eof(); michael@0: } michael@0: michael@0: strstreambuf::int_type strstreambuf::underflow() { michael@0: if (gptr() == egptr() && pptr() && pptr() > egptr()) michael@0: setg(eback(), gptr(), pptr()); michael@0: michael@0: if (gptr() != egptr()) michael@0: return (unsigned char) *gptr(); michael@0: else michael@0: return _Traits::eof(); michael@0: } michael@0: michael@0: basic_streambuf >* michael@0: strstreambuf::setbuf(char*, streamsize) { michael@0: return this; michael@0: } michael@0: michael@0: strstreambuf::pos_type michael@0: strstreambuf::seekoff(off_type off, michael@0: ios_base::seekdir dir, ios_base::openmode mode) { michael@0: bool do_get = false; michael@0: bool do_put = false; michael@0: michael@0: if ((mode & (ios_base::in | ios_base::out)) == michael@0: (ios_base::in | ios_base::out) && michael@0: (dir == ios_base::beg || dir == ios_base::end)) michael@0: do_get = do_put = true; michael@0: else if (mode & ios_base::in) michael@0: do_get = true; michael@0: else if (mode & ios_base::out) michael@0: do_put = true; michael@0: michael@0: // !gptr() is here because, according to D.7.1 paragraph 4, the seekable michael@0: // area is undefined if there is no get area. michael@0: if ((!do_get && !do_put) || (do_put && !pptr()) || !gptr()) michael@0: return pos_type(off_type(-1)); michael@0: michael@0: char* seeklow = eback(); michael@0: char* seekhigh = epptr() ? epptr() : egptr(); michael@0: michael@0: off_type newoff; michael@0: switch(dir) { michael@0: case ios_base::beg: michael@0: newoff = 0; michael@0: break; michael@0: case ios_base::end: michael@0: newoff = seekhigh - seeklow; michael@0: break; michael@0: case ios_base::cur: michael@0: newoff = do_put ? pptr() - seeklow : gptr() - seeklow; michael@0: break; michael@0: default: michael@0: return pos_type(off_type(-1)); michael@0: } michael@0: michael@0: off += newoff; michael@0: if (off < 0 || off > seekhigh - seeklow) michael@0: return pos_type(off_type(-1)); michael@0: michael@0: if (do_put) { michael@0: if (seeklow + __STATIC_CAST(ptrdiff_t, off) < pbase()) { michael@0: setp(seeklow, epptr()); michael@0: pbump((int)off); michael@0: } michael@0: else { michael@0: setp(pbase(), epptr()); michael@0: pbump((int)(off - (pbase() - seeklow))); michael@0: } michael@0: } michael@0: if (do_get) { michael@0: if (off <= egptr() - seeklow) michael@0: setg(seeklow, seeklow + __STATIC_CAST(ptrdiff_t, off), egptr()); michael@0: else if (off <= pptr() - seeklow) michael@0: setg(seeklow, seeklow + __STATIC_CAST(ptrdiff_t, off), pptr()); michael@0: else michael@0: setg(seeklow, seeklow + __STATIC_CAST(ptrdiff_t, off), epptr()); michael@0: } michael@0: michael@0: return pos_type(newoff); michael@0: } michael@0: michael@0: strstreambuf::pos_type michael@0: strstreambuf::seekpos(pos_type pos, ios_base::openmode mode) { michael@0: return seekoff(pos - pos_type(off_type(0)), ios_base::beg, mode); michael@0: } michael@0: michael@0: michael@0: char* strstreambuf::_M_alloc(size_t n) { michael@0: if (_M_alloc_fun) michael@0: return __STATIC_CAST(char*,_M_alloc_fun(n)); michael@0: else michael@0: return new char[n]; michael@0: } michael@0: michael@0: void strstreambuf::_M_free(char* p) { michael@0: if (p) { michael@0: if (_M_free_fun) michael@0: _M_free_fun(p); michael@0: else michael@0: delete[] p; michael@0: } michael@0: } michael@0: michael@0: void strstreambuf::_M_setup(char* get, char* put, streamsize n) { michael@0: if (get) { michael@0: size_t N = n > 0 ? size_t(n) : n == 0 ? strlen(get) : size_t(INT_MAX); michael@0: michael@0: if (put) { michael@0: setg(get, get, get + N); michael@0: setp(put, put + N); michael@0: } michael@0: else { michael@0: setg(get, get, get + N); michael@0: } michael@0: } michael@0: } michael@0: michael@0: //---------------------------------------------------------------------- michael@0: // Class istrstream michael@0: michael@0: istrstream::istrstream(char* s) michael@0: : basic_istream >(0), _M_buf(s, 0) { michael@0: this->init(&_M_buf); michael@0: } michael@0: michael@0: istrstream::istrstream(const char* s) michael@0: : basic_istream >(0), _M_buf(s, 0) { michael@0: this->init(&_M_buf); michael@0: } michael@0: michael@0: istrstream::istrstream(char* s, streamsize n) michael@0: : basic_istream >(0), _M_buf(s, n) { michael@0: this->init(&_M_buf); michael@0: } michael@0: michael@0: istrstream::istrstream(const char* s, streamsize n) michael@0: : basic_istream >(0), _M_buf(s, n) { michael@0: this->init(&_M_buf); michael@0: } michael@0: michael@0: istrstream::~istrstream() {} michael@0: michael@0: strstreambuf* istrstream::rdbuf() const { michael@0: return __CONST_CAST(strstreambuf*,&_M_buf); michael@0: } michael@0: michael@0: char* istrstream::str() { return _M_buf.str(); } michael@0: michael@0: //---------------------------------------------------------------------- michael@0: // Class ostrstream michael@0: michael@0: ostrstream::ostrstream() michael@0: : basic_ostream >(0), _M_buf() { michael@0: basic_ios >::init(&_M_buf); michael@0: } michael@0: michael@0: ostrstream::ostrstream(char* s, int n, ios_base::openmode mode) michael@0: : basic_ostream >(0), michael@0: _M_buf(s, n, mode & ios_base::app ? s + strlen(s) : s) { michael@0: basic_ios >::init(&_M_buf); michael@0: } michael@0: michael@0: ostrstream::~ostrstream() {} michael@0: michael@0: strstreambuf* ostrstream::rdbuf() const { michael@0: return __CONST_CAST(strstreambuf*,&_M_buf); michael@0: } michael@0: michael@0: void ostrstream::freeze(bool freezeflag) { michael@0: _M_buf.freeze(freezeflag); michael@0: } michael@0: michael@0: char* ostrstream::str() { michael@0: return _M_buf.str(); michael@0: } michael@0: michael@0: int ostrstream::pcount() const { michael@0: return _M_buf.pcount(); michael@0: } michael@0: michael@0: michael@0: //---------------------------------------------------------------------- michael@0: // Class strstream michael@0: michael@0: strstream::strstream() michael@0: : basic_iostream >(0), _M_buf() { michael@0: basic_ios >::init(&_M_buf); michael@0: } michael@0: michael@0: strstream::strstream(char* s, int n, ios_base::openmode mode) michael@0: : basic_iostream >(0), michael@0: _M_buf(s, n, mode & ios_base::app ? s + strlen(s) : s) { michael@0: basic_ios >::init(&_M_buf); michael@0: } michael@0: michael@0: strstream::~strstream() {} michael@0: michael@0: strstreambuf* strstream::rdbuf() const { michael@0: return __CONST_CAST(strstreambuf*,&_M_buf); michael@0: } michael@0: michael@0: void strstream::freeze(bool freezeflag) { michael@0: _M_buf.freeze(freezeflag); michael@0: } michael@0: michael@0: int strstream::pcount() const { michael@0: return _M_buf.pcount(); michael@0: } michael@0: michael@0: char* strstream::str() { michael@0: return _M_buf.str(); michael@0: } michael@0: michael@0: _STLP_END_NAMESPACE michael@0: michael@0: // Local Variables: michael@0: // mode:C++ michael@0: // End: