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