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