1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/genericopenlibs/cppstdlib/stl/src/strstream.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,392 @@
1.4 +/*
1.5 + * Portions Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
1.6 + *
1.7 + * Copyright (c) 1999
1.8 + * Silicon Graphics Computer Systems, Inc.
1.9 + *
1.10 + * Copyright (c) 1999
1.11 + * Boris Fomitchev
1.12 + *
1.13 + * This material is provided "as is", with absolutely no warranty expressed
1.14 + * or implied. Any use is at your own risk.
1.15 + *
1.16 + * Permission to use or copy this software for any purpose is hereby granted
1.17 + * without fee, provided the above notices are retained on all copies.
1.18 + * Permission to modify the code and to distribute modified code is granted,
1.19 + * provided the above notices are retained, and a notice that the code was
1.20 + * modified is included with the above copyright notice.
1.21 + *
1.22 + */
1.23 +
1.24 +// Implementation of the classes in header <strstream>.
1.25 +// WARNING: The classes defined in <strstream> are DEPRECATED. This
1.26 +// header is defined in section D.7.1 of the C++ standard, and it
1.27 +// MAY BE REMOVED in a future standard revision. You should use the
1.28 +// header <sstream> instead.
1.29 +
1.30 +#include "stlport_prefix.h"
1.31 +
1.32 +#include <strstream>
1.33 +#include <algorithm>
1.34 +#include <limits>
1.35 +
1.36 +_STLP_BEGIN_NAMESPACE
1.37 +
1.38 +// strstreambuf constructor, destructor.
1.39 +_STLP_DECLSPEC strstreambuf::strstreambuf(streamsize initial_capacity)
1.40 + : _M_alloc_fun(0), _M_free_fun(0),
1.41 + _M_dynamic(true), _M_frozen(false), _M_constant(false) {
1.42 + size_t n = (sizeof(streamsize) > sizeof(size_t)) ? __STATIC_CAST(size_t, (min)(__STATIC_CAST(streamsize, (numeric_limits<size_t>::max)()),
1.43 + (max)(initial_capacity, streamsize(16))))
1.44 + : __STATIC_CAST(size_t, (max)(initial_capacity, streamsize(16)));
1.45 +
1.46 + char* buf = _M_alloc(n);
1.47 + if (buf) {
1.48 + setp(buf, buf + n);
1.49 + setg(buf, buf, buf);
1.50 + }
1.51 +}
1.52 +
1.53 +_STLP_DECLSPEC strstreambuf::strstreambuf(__alloc_fn alloc_f, __free_fn free_f)
1.54 + : _M_alloc_fun(alloc_f), _M_free_fun(free_f),
1.55 + _M_dynamic(true), _M_frozen(false), _M_constant(false) {
1.56 + size_t n = 16;
1.57 +
1.58 + char* buf = _M_alloc(n);
1.59 + if (buf) {
1.60 + setp(buf, buf + n);
1.61 + setg(buf, buf, buf);
1.62 + }
1.63 +}
1.64 +
1.65 +_STLP_DECLSPEC strstreambuf::strstreambuf(char* get, streamsize n, char* put)
1.66 + : _M_alloc_fun(0), _M_free_fun(0),
1.67 + _M_dynamic(false), _M_frozen(false), _M_constant(false) {
1.68 + _M_setup(get, put, n);
1.69 +}
1.70 +
1.71 +_STLP_DECLSPEC strstreambuf::strstreambuf(signed char* get, streamsize n, signed char* put)
1.72 + : _M_alloc_fun(0), _M_free_fun(0),
1.73 + _M_dynamic(false), _M_frozen(false), _M_constant(false) {
1.74 + _M_setup(__REINTERPRET_CAST(char*,get), __REINTERPRET_CAST(char*,put), n);
1.75 +}
1.76 +
1.77 +_STLP_DECLSPEC strstreambuf::strstreambuf(unsigned char* get, streamsize n,
1.78 + unsigned char* put)
1.79 + : _M_alloc_fun(0), _M_free_fun(0),
1.80 + _M_dynamic(false), _M_frozen(false), _M_constant(false) {
1.81 + _M_setup(__REINTERPRET_CAST(char*,get), __REINTERPRET_CAST(char*,put), n);
1.82 +}
1.83 +
1.84 +_STLP_DECLSPEC strstreambuf::strstreambuf(const char* get, streamsize n)
1.85 + : _M_alloc_fun(0), _M_free_fun(0),
1.86 + _M_dynamic(false), _M_frozen(false), _M_constant(true) {
1.87 + _M_setup(__CONST_CAST(char*,get), 0, n);
1.88 +}
1.89 +
1.90 +_STLP_DECLSPEC strstreambuf::strstreambuf(const signed char* get, streamsize n)
1.91 + : _M_alloc_fun(0), _M_free_fun(0),
1.92 + _M_dynamic(false), _M_frozen(false), _M_constant(true) {
1.93 + _M_setup(__REINTERPRET_CAST(char*, __CONST_CAST(signed char*,get)), 0, n);
1.94 +}
1.95 +
1.96 +_STLP_DECLSPEC strstreambuf::strstreambuf(const unsigned char* get, streamsize n)
1.97 + : _M_alloc_fun(0), _M_free_fun(0),
1.98 + _M_dynamic(false), _M_frozen(false), _M_constant(true) {
1.99 + _M_setup(__REINTERPRET_CAST(char*, __CONST_CAST(unsigned char*,get)), 0, n);
1.100 +}
1.101 +
1.102 +_STLP_DECLSPEC strstreambuf::~strstreambuf() {
1.103 + if (_M_dynamic && !_M_frozen)
1.104 + _M_free(eback());
1.105 +}
1.106 +
1.107 +_STLP_DECLSPEC void strstreambuf::freeze(bool frozenflag) {
1.108 + if (_M_dynamic)
1.109 + _M_frozen = frozenflag;
1.110 +}
1.111 +
1.112 +_STLP_DECLSPEC char* strstreambuf::str() {
1.113 + freeze(true);
1.114 + return eback();
1.115 +}
1.116 +
1.117 +_STLP_DECLSPEC int strstreambuf::pcount() const {
1.118 + return int(pptr() ? pptr() - pbase() : 0);
1.119 +}
1.120 +
1.121 +_STLP_DECLSPEC strstreambuf::int_type strstreambuf::overflow(int_type c) {
1.122 + if (c == traits_type::eof())
1.123 + return traits_type::not_eof(c);
1.124 +
1.125 + // Try to expand the buffer.
1.126 + if (pptr() == epptr() && _M_dynamic && !_M_frozen && !_M_constant) {
1.127 + ptrdiff_t old_size = epptr() - pbase();
1.128 + ptrdiff_t new_size = (max)(2 * old_size, ptrdiff_t(1));
1.129 +
1.130 + char* buf = _M_alloc(new_size);
1.131 + if (buf) {
1.132 + memcpy(buf, pbase(), old_size);
1.133 +
1.134 + char* old_buffer = pbase();
1.135 + bool reposition_get = false;
1.136 + ptrdiff_t old_get_offset;
1.137 + if (gptr() != 0) {
1.138 + reposition_get = true;
1.139 + old_get_offset = gptr() - eback();
1.140 + }
1.141 +
1.142 + setp(buf, buf + new_size);
1.143 + pbump((int)old_size);
1.144 +
1.145 + if (reposition_get)
1.146 + setg(buf, buf + old_get_offset, buf + (max)(old_get_offset, old_size));
1.147 +
1.148 + _M_free(old_buffer);
1.149 + }
1.150 + }
1.151 +
1.152 + if (pptr() != epptr()) {
1.153 + *pptr() = traits_type::to_char_type(c);
1.154 + pbump(1);
1.155 + return c;
1.156 + }
1.157 + else
1.158 + return traits_type::eof();
1.159 +}
1.160 +
1.161 +_STLP_DECLSPEC strstreambuf::int_type strstreambuf::pbackfail(int_type c) {
1.162 + if (gptr() != eback()) {
1.163 + if (c == traits_type::eof()) {
1.164 + gbump(-1);
1.165 + return traits_type::not_eof(c);
1.166 + }
1.167 + else if (c == gptr()[-1]) {
1.168 + gbump(-1);
1.169 + return c;
1.170 + }
1.171 + else if (!_M_constant) {
1.172 + gbump(-1);
1.173 + *gptr() = traits_type::to_char_type(c);
1.174 + return c;
1.175 + }
1.176 + }
1.177 +
1.178 + return traits_type::eof();
1.179 +}
1.180 +
1.181 +_STLP_DECLSPEC strstreambuf::int_type strstreambuf::underflow() {
1.182 + if (gptr() == egptr() && pptr() && pptr() > egptr())
1.183 + setg(eback(), gptr(), pptr());
1.184 +
1.185 + if (gptr() != egptr())
1.186 + return (unsigned char) *gptr();
1.187 + else
1.188 + return _Traits::eof();
1.189 +}
1.190 +
1.191 +_STLP_DECLSPEC basic_streambuf<char, char_traits<char> >*
1.192 +strstreambuf::setbuf(char*, streamsize) {
1.193 + return this;
1.194 +}
1.195 +
1.196 +_STLP_DECLSPEC strstreambuf::pos_type
1.197 +strstreambuf::seekoff(off_type off,
1.198 + ios_base::seekdir dir, ios_base::openmode mode) {
1.199 + bool do_get = false;
1.200 + bool do_put = false;
1.201 +
1.202 + if ((mode & (ios_base::in | ios_base::out)) ==
1.203 + (ios_base::in | ios_base::out) &&
1.204 + (dir == ios_base::beg || dir == ios_base::end))
1.205 + do_get = do_put = true;
1.206 + else if (mode & ios_base::in)
1.207 + do_get = true;
1.208 + else if (mode & ios_base::out)
1.209 + do_put = true;
1.210 +
1.211 + // !gptr() is here because, according to D.7.1 paragraph 4, the seekable
1.212 + // area is undefined if there is no get area.
1.213 + if ((!do_get && !do_put) || (do_put && !pptr()) || !gptr())
1.214 + return pos_type(off_type(-1));
1.215 +
1.216 + char* seeklow = eback();
1.217 + char* seekhigh = epptr() ? epptr() : egptr();
1.218 +
1.219 + off_type newoff;
1.220 + switch(dir) {
1.221 + case ios_base::beg:
1.222 + newoff = 0;
1.223 + break;
1.224 + case ios_base::end:
1.225 + newoff = seekhigh - seeklow;
1.226 + break;
1.227 + case ios_base::cur:
1.228 + newoff = do_put ? pptr() - seeklow : gptr() - seeklow;
1.229 + break;
1.230 + default:
1.231 + return pos_type(off_type(-1));
1.232 + }
1.233 +
1.234 + off += newoff;
1.235 + if (off < 0 || off > seekhigh - seeklow)
1.236 + return pos_type(off_type(-1));
1.237 +
1.238 + if (do_put) {
1.239 + if (seeklow + __STATIC_CAST(ptrdiff_t, off) < pbase()) {
1.240 + setp(seeklow, epptr());
1.241 + pbump((int)off);
1.242 + }
1.243 + else {
1.244 + setp(pbase(), epptr());
1.245 + pbump((int)(off - (pbase() - seeklow)));
1.246 + }
1.247 + }
1.248 + if (do_get) {
1.249 + if (off <= egptr() - seeklow)
1.250 + setg(seeklow, seeklow + __STATIC_CAST(ptrdiff_t, off), egptr());
1.251 + else if (off <= pptr() - seeklow)
1.252 + setg(seeklow, seeklow + __STATIC_CAST(ptrdiff_t, off), pptr());
1.253 + else
1.254 + setg(seeklow, seeklow + __STATIC_CAST(ptrdiff_t, off), epptr());
1.255 + }
1.256 +
1.257 + return pos_type(newoff);
1.258 +}
1.259 +
1.260 +_STLP_DECLSPEC strstreambuf::pos_type
1.261 +strstreambuf::seekpos(pos_type pos, ios_base::openmode mode) {
1.262 + return seekoff(pos - pos_type(off_type(0)), ios_base::beg, mode);
1.263 +}
1.264 +
1.265 +
1.266 +char* strstreambuf::_M_alloc(size_t n) {
1.267 + if (_M_alloc_fun)
1.268 + return __STATIC_CAST(char*,_M_alloc_fun(n));
1.269 + else
1.270 + return new char[n];
1.271 +}
1.272 +
1.273 +void strstreambuf::_M_free(char* p) {
1.274 + if (p)
1.275 + if (_M_free_fun)
1.276 + _M_free_fun(p);
1.277 + else
1.278 + delete[] p;
1.279 +}
1.280 +
1.281 +void strstreambuf::_M_setup(char* get, char* put, streamsize n) {
1.282 + if (get) {
1.283 + size_t N = n > 0 ? size_t(n) : n == 0 ? strlen(get) : size_t(INT_MAX);
1.284 +
1.285 + if (put) {
1.286 + setg(get, get, get + N);
1.287 + setp(put, put + N);
1.288 + }
1.289 + else {
1.290 + setg(get, get, get + N);
1.291 + }
1.292 + }
1.293 +}
1.294 +
1.295 +//----------------------------------------------------------------------
1.296 +// Class istrstream
1.297 +
1.298 +_STLP_DECLSPEC istrstream::istrstream(char* s)
1.299 + : basic_istream<char, char_traits<char> >(0), _M_buf(s, 0) {
1.300 + this->init(&_M_buf);
1.301 +}
1.302 +
1.303 +_STLP_DECLSPEC istrstream::istrstream(const char* s)
1.304 + : basic_istream<char, char_traits<char> >(0), _M_buf(s, 0) {
1.305 + this->init(&_M_buf);
1.306 +}
1.307 +
1.308 +_STLP_DECLSPEC istrstream::istrstream(char* s, streamsize n)
1.309 + : basic_istream<char, char_traits<char> >(0), _M_buf(s, n) {
1.310 + this->init(&_M_buf);
1.311 +}
1.312 +
1.313 +_STLP_DECLSPEC istrstream::istrstream(const char* s, streamsize n)
1.314 + : basic_istream<char, char_traits<char> >(0), _M_buf(s, n) {
1.315 + this->init(&_M_buf);
1.316 +}
1.317 +
1.318 +_STLP_DECLSPEC istrstream::~istrstream() {}
1.319 +
1.320 +_STLP_DECLSPEC strstreambuf* istrstream::rdbuf() const {
1.321 + return __CONST_CAST(strstreambuf*,&_M_buf);
1.322 +}
1.323 +
1.324 +_STLP_DECLSPEC char* istrstream::str() { return _M_buf.str(); }
1.325 +
1.326 +//----------------------------------------------------------------------
1.327 +// Class ostrstream
1.328 +
1.329 +_STLP_DECLSPEC ostrstream::ostrstream()
1.330 + : basic_ostream<char, char_traits<char> >(0), _M_buf() {
1.331 + basic_ios<char, char_traits<char> >::init(&_M_buf);
1.332 +}
1.333 +
1.334 +_STLP_DECLSPEC ostrstream::ostrstream(char* s, int n, ios_base::openmode mode)
1.335 + : basic_ostream<char, char_traits<char> >(0),
1.336 + _M_buf(s, n, mode & ios_base::app ? s + strlen(s) : s) {
1.337 + basic_ios<char, char_traits<char> >::init(&_M_buf);
1.338 +}
1.339 +
1.340 +_STLP_DECLSPEC ostrstream::~ostrstream() {}
1.341 +
1.342 +_STLP_DECLSPEC strstreambuf* ostrstream::rdbuf() const {
1.343 + return __CONST_CAST(strstreambuf*,&_M_buf);
1.344 +}
1.345 +
1.346 +_STLP_DECLSPEC void ostrstream::freeze(bool freezeflag) {
1.347 + _M_buf.freeze(freezeflag);
1.348 +}
1.349 +
1.350 +_STLP_DECLSPEC char* ostrstream::str() {
1.351 + return _M_buf.str();
1.352 +}
1.353 +
1.354 +_STLP_DECLSPEC int ostrstream::pcount() const {
1.355 + return _M_buf.pcount();
1.356 +}
1.357 +
1.358 +
1.359 +//----------------------------------------------------------------------
1.360 +// Class strstream
1.361 +
1.362 +_STLP_DECLSPEC strstream::strstream()
1.363 + : basic_iostream<char, char_traits<char> >(0), _M_buf() {
1.364 + basic_ios<char, char_traits<char> >::init(&_M_buf);
1.365 +}
1.366 +
1.367 +_STLP_DECLSPEC strstream::strstream(char* s, int n, ios_base::openmode mode)
1.368 + : basic_iostream<char, char_traits<char> >(0),
1.369 + _M_buf(s, n, mode & ios_base::app ? s + strlen(s) : s) {
1.370 + basic_ios<char, char_traits<char> >::init(&_M_buf);
1.371 +}
1.372 +
1.373 +_STLP_DECLSPEC strstream::~strstream() {}
1.374 +
1.375 +_STLP_DECLSPEC strstreambuf* strstream::rdbuf() const {
1.376 + return __CONST_CAST(strstreambuf*,&_M_buf);
1.377 +}
1.378 +
1.379 +_STLP_DECLSPEC void strstream::freeze(bool freezeflag) {
1.380 + _M_buf.freeze(freezeflag);
1.381 +}
1.382 +
1.383 +_STLP_DECLSPEC int strstream::pcount() const {
1.384 + return _M_buf.pcount();
1.385 +}
1.386 +
1.387 +_STLP_DECLSPEC char* strstream::str() {
1.388 + return _M_buf.str();
1.389 +}
1.390 +
1.391 +_STLP_END_NAMESPACE
1.392 +
1.393 +// Local Variables:
1.394 +// mode:C++
1.395 +// End: