ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

strstream.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 1999
00003  * Silicon Graphics Computer Systems, Inc.
00004  *
00005  * Copyright (c) 1999
00006  * Boris Fomitchev
00007  *
00008  * This material is provided "as is", with absolutely no warranty expressed
00009  * or implied. Any use is at your own risk.
00010  *
00011  * Permission to use or copy this software for any purpose is hereby granted
00012  * without fee, provided the above notices are retained on all copies.
00013  * Permission to modify the code and to distribute modified code is granted,
00014  * provided the above notices are retained, and a notice that the code was
00015  * modified is included with the above copyright notice.
00016  *
00017  */
00018 
00019 // Implementation of the classes in header <strstream>.
00020 // WARNING: The classes defined in <strstream> are DEPRECATED.  This
00021 // header is defined in section D.7.1 of the C++ standard, and it
00022 // MAY BE REMOVED in a future standard revision.  You should use the
00023 // header <sstream> instead.
00024 
00025 #include "stlport_prefix.h"
00026 
00027 #include <strstream>
00028 #include <algorithm>
00029 #include <limits>
00030 
00031 _STLP_BEGIN_NAMESPACE
00032 
00033 // strstreambuf constructor, destructor.
00034 strstreambuf::strstreambuf(streamsize initial_capacity)
00035    : _M_alloc_fun(0), _M_free_fun(0),
00036      _M_dynamic(true), _M_frozen(false), _M_constant(false) {
00037   size_t n = (sizeof(streamsize) > sizeof(size_t)) ? __STATIC_CAST(size_t, (min)(__STATIC_CAST(streamsize, (numeric_limits<size_t>::max)()),
00038                                                                                  (max)(initial_capacity, streamsize(16))))
00039                                                    : __STATIC_CAST(size_t, (max)(initial_capacity, streamsize(16)));
00040 
00041   char* buf = _M_alloc(n);
00042   if (buf) {
00043     setp(buf, buf + n);
00044     setg(buf, buf, buf);
00045   }
00046 }
00047 
00048 strstreambuf::strstreambuf(__alloc_fn alloc_f, __free_fn free_f)
00049   : _M_alloc_fun(alloc_f), _M_free_fun(free_f),
00050     _M_dynamic(true), _M_frozen(false), _M_constant(false) {
00051   size_t n = 16;
00052 
00053   char* buf = _M_alloc(n);
00054   if (buf) {
00055     setp(buf, buf + n);
00056     setg(buf, buf, buf);
00057   }
00058 }
00059 
00060 strstreambuf::strstreambuf(char* get, streamsize n, char* put)
00061   : _M_alloc_fun(0), _M_free_fun(0),
00062     _M_dynamic(false), _M_frozen(false), _M_constant(false) {
00063   _M_setup(get, put, n);
00064 }
00065 
00066 strstreambuf::strstreambuf(signed char* get, streamsize n, signed char* put)
00067   : _M_alloc_fun(0), _M_free_fun(0),
00068     _M_dynamic(false), _M_frozen(false), _M_constant(false) {
00069   _M_setup(__REINTERPRET_CAST(char*,get), __REINTERPRET_CAST(char*,put), n);
00070 }
00071 
00072 strstreambuf::strstreambuf(unsigned char* get, streamsize n,
00073                            unsigned char* put)
00074   : _M_alloc_fun(0), _M_free_fun(0),
00075     _M_dynamic(false), _M_frozen(false), _M_constant(false) {
00076   _M_setup(__REINTERPRET_CAST(char*,get), __REINTERPRET_CAST(char*,put), n);
00077 }
00078 
00079 strstreambuf::strstreambuf(const char* get, streamsize n)
00080   : _M_alloc_fun(0), _M_free_fun(0),
00081     _M_dynamic(false), _M_frozen(false), _M_constant(true) {
00082   _M_setup(__CONST_CAST(char*,get), 0, n);
00083 }
00084 
00085 strstreambuf::strstreambuf(const signed char* get, streamsize n)
00086   : _M_alloc_fun(0), _M_free_fun(0),
00087     _M_dynamic(false), _M_frozen(false), _M_constant(true) {
00088   _M_setup(__REINTERPRET_CAST(char*, __CONST_CAST(signed char*,get)), 0, n);
00089 }
00090 
00091 strstreambuf::strstreambuf(const unsigned char* get, streamsize n)
00092   : _M_alloc_fun(0), _M_free_fun(0),
00093     _M_dynamic(false), _M_frozen(false), _M_constant(true) {
00094   _M_setup(__REINTERPRET_CAST(char*, __CONST_CAST(unsigned char*,get)), 0, n);
00095 }
00096 
00097 strstreambuf::~strstreambuf() {
00098   if (_M_dynamic && !_M_frozen)
00099     _M_free(eback());
00100 }
00101 
00102 void strstreambuf::freeze(bool frozenflag) {
00103   if (_M_dynamic)
00104     _M_frozen = frozenflag;
00105 }
00106 
00107 char* strstreambuf::str() {
00108   freeze(true);
00109   return eback();
00110 }
00111 
00112 int strstreambuf::pcount() const {
00113   return int(pptr() ? pptr() - pbase() : 0);
00114 }
00115 
00116 strstreambuf::int_type strstreambuf::overflow(int_type c) {
00117   if (c == traits_type::eof())
00118     return traits_type::not_eof(c);
00119 
00120   // Try to expand the buffer.
00121   if (pptr() == epptr() && _M_dynamic && !_M_frozen && !_M_constant) {
00122     ptrdiff_t old_size = epptr() - pbase();
00123     ptrdiff_t new_size = (max)(2 * old_size, ptrdiff_t(1));
00124 
00125     char* buf = _M_alloc(new_size);
00126     if (buf) {
00127       memcpy(buf, pbase(), old_size);
00128 
00129       char* old_buffer = pbase();
00130       bool reposition_get = false;
00131       ptrdiff_t old_get_offset;
00132       if (gptr() != 0) {
00133         reposition_get = true;
00134         old_get_offset = gptr() - eback();
00135       }
00136 
00137       setp(buf, buf + new_size);
00138       pbump((int)old_size);
00139 
00140       if (reposition_get)
00141         setg(buf, buf + old_get_offset, buf + (max)(old_get_offset, old_size));
00142 
00143       _M_free(old_buffer);
00144     }
00145   }
00146 
00147   if (pptr() != epptr()) {
00148     *pptr() = traits_type::to_char_type(c);
00149     pbump(1);
00150     return c;
00151   }
00152   else
00153     return traits_type::eof();
00154 }
00155 
00156 strstreambuf::int_type strstreambuf::pbackfail(int_type c) {
00157   if (gptr() != eback()) {
00158     if (c == traits_type::eof()) {
00159       gbump(-1);
00160       return traits_type::not_eof(c);
00161     }
00162     else if (c == gptr()[-1]) {
00163       gbump(-1);
00164       return c;
00165     }
00166     else if (!_M_constant) {
00167       gbump(-1);
00168       *gptr() = traits_type::to_char_type(c);
00169       return c;
00170     }
00171   }
00172 
00173   return traits_type::eof();
00174 }
00175 
00176 strstreambuf::int_type strstreambuf::underflow() {
00177   if (gptr() == egptr() && pptr() && pptr() > egptr())
00178     setg(eback(), gptr(), pptr());
00179 
00180   if (gptr() != egptr())
00181     return (unsigned char) *gptr();
00182   else
00183     return _Traits::eof();
00184 }
00185 
00186 basic_streambuf<char, char_traits<char> >*
00187 strstreambuf::setbuf(char*, streamsize) {
00188   return this;
00189 }
00190 
00191 strstreambuf::pos_type
00192 strstreambuf::seekoff(off_type off,
00193                       ios_base::seekdir dir, ios_base::openmode mode) {
00194   bool do_get = false;
00195   bool do_put = false;
00196 
00197   if ((mode & (ios_base::in | ios_base::out)) ==
00198           (ios_base::in | ios_base::out) &&
00199       (dir == ios_base::beg || dir == ios_base::end))
00200     do_get = do_put = true;
00201   else if (mode & ios_base::in)
00202     do_get = true;
00203   else if (mode & ios_base::out)
00204     do_put = true;
00205 
00206   // !gptr() is here because, according to D.7.1 paragraph 4, the seekable
00207   // area is undefined if there is no get area.
00208   if ((!do_get && !do_put) || (do_put && !pptr()) || !gptr())
00209     return pos_type(off_type(-1));
00210 
00211   char* seeklow  = eback();
00212   char* seekhigh = epptr() ? epptr() : egptr();
00213 
00214   off_type newoff;
00215   switch(dir) {
00216   case ios_base::beg:
00217     newoff = 0;
00218     break;
00219   case ios_base::end:
00220     newoff = seekhigh - seeklow;
00221     break;
00222   case ios_base::cur:
00223     newoff = do_put ? pptr() - seeklow : gptr() - seeklow;
00224     break;
00225   default:
00226     return pos_type(off_type(-1));
00227   }
00228 
00229   off += newoff;
00230   if (off < 0 || off > seekhigh - seeklow)
00231     return pos_type(off_type(-1));
00232 
00233   if (do_put) {
00234     if (seeklow + __STATIC_CAST(ptrdiff_t, off) < pbase()) {
00235       setp(seeklow, epptr());
00236       pbump((int)off);
00237     }
00238     else {
00239       setp(pbase(), epptr());
00240       pbump((int)(off - (pbase() - seeklow)));
00241     }
00242   }
00243   if (do_get) {
00244     if (off <= egptr() - seeklow)
00245       setg(seeklow, seeklow + __STATIC_CAST(ptrdiff_t, off), egptr());
00246     else if (off <= pptr() - seeklow)
00247       setg(seeklow, seeklow + __STATIC_CAST(ptrdiff_t, off), pptr());
00248     else
00249       setg(seeklow, seeklow + __STATIC_CAST(ptrdiff_t, off), epptr());
00250   }
00251 
00252   return pos_type(newoff);
00253 }
00254 
00255 strstreambuf::pos_type
00256 strstreambuf::seekpos(pos_type pos, ios_base::openmode mode) {
00257   return seekoff(pos - pos_type(off_type(0)), ios_base::beg, mode);
00258 }
00259 
00260 
00261 char* strstreambuf::_M_alloc(size_t n) {
00262   if (_M_alloc_fun)
00263     return __STATIC_CAST(char*,_M_alloc_fun(n));
00264   else
00265     return new char[n];
00266 }
00267 
00268 void strstreambuf::_M_free(char* p) {
00269   if (p) {
00270     if (_M_free_fun)
00271       _M_free_fun(p);
00272     else
00273       delete[] p;
00274   }
00275 }
00276 
00277 void strstreambuf::_M_setup(char* get, char* put, streamsize n) {
00278   if (get) {
00279     size_t N = n > 0 ? size_t(n) : n == 0 ? strlen(get) : size_t(INT_MAX);
00280 
00281     if (put) {
00282       setg(get, get, get + N);
00283       setp(put, put + N);
00284     }
00285     else {
00286       setg(get, get, get + N);
00287     }
00288   }
00289 }
00290 
00291 //----------------------------------------------------------------------
00292 // Class istrstream
00293 
00294 istrstream::istrstream(char* s)
00295   : basic_istream<char, char_traits<char> >(0), _M_buf(s, 0) {
00296   this->init(&_M_buf);
00297 }
00298 
00299 istrstream::istrstream(const char* s)
00300   : basic_istream<char, char_traits<char> >(0), _M_buf(s, 0) {
00301   this->init(&_M_buf);
00302 }
00303 
00304 istrstream::istrstream(char* s, streamsize n)
00305   : basic_istream<char, char_traits<char> >(0), _M_buf(s, n) {
00306   this->init(&_M_buf);
00307 }
00308 
00309 istrstream::istrstream(const char* s, streamsize n)
00310   : basic_istream<char, char_traits<char> >(0), _M_buf(s, n) {
00311   this->init(&_M_buf);
00312 }
00313 
00314 istrstream::~istrstream() {}
00315 
00316 strstreambuf* istrstream::rdbuf() const {
00317   return __CONST_CAST(strstreambuf*,&_M_buf);
00318 }
00319 
00320 char* istrstream::str() { return _M_buf.str(); }
00321 
00322 //----------------------------------------------------------------------
00323 // Class ostrstream
00324 
00325 ostrstream::ostrstream()
00326   : basic_ostream<char, char_traits<char> >(0), _M_buf() {
00327   basic_ios<char, char_traits<char> >::init(&_M_buf);
00328 }
00329 
00330 ostrstream::ostrstream(char* s, int n, ios_base::openmode mode)
00331   : basic_ostream<char, char_traits<char> >(0),
00332     _M_buf(s, n, mode & ios_base::app ? s + strlen(s) : s) {
00333   basic_ios<char, char_traits<char> >::init(&_M_buf);
00334 }
00335 
00336 ostrstream::~ostrstream() {}
00337 
00338 strstreambuf* ostrstream::rdbuf() const {
00339   return __CONST_CAST(strstreambuf*,&_M_buf);
00340 }
00341 
00342 void ostrstream::freeze(bool freezeflag) {
00343   _M_buf.freeze(freezeflag);
00344 }
00345 
00346 char* ostrstream::str() {
00347   return _M_buf.str();
00348 }
00349 
00350 int ostrstream::pcount() const {
00351   return _M_buf.pcount();
00352 }
00353 
00354 
00355 //----------------------------------------------------------------------
00356 // Class strstream
00357 
00358 strstream::strstream()
00359   : basic_iostream<char, char_traits<char> >(0), _M_buf() {
00360   basic_ios<char, char_traits<char> >::init(&_M_buf);
00361 }
00362 
00363 strstream::strstream(char* s, int n, ios_base::openmode mode)
00364   : basic_iostream<char, char_traits<char> >(0),
00365     _M_buf(s, n, mode & ios_base::app ? s + strlen(s) : s) {
00366   basic_ios<char, char_traits<char> >::init(&_M_buf);
00367 }
00368 
00369 strstream::~strstream() {}
00370 
00371 strstreambuf* strstream::rdbuf() const {
00372   return __CONST_CAST(strstreambuf*,&_M_buf);
00373 }
00374 
00375 void strstream::freeze(bool freezeflag) {
00376   _M_buf.freeze(freezeflag);
00377 }
00378 
00379 int strstream::pcount() const {
00380   return _M_buf.pcount();
00381 }
00382 
00383 char* strstream::str() {
00384   return _M_buf.str();
00385 }
00386 
00387 _STLP_END_NAMESPACE
00388 
00389 // Local Variables:
00390 // mode:C++
00391 // End:

Generated on Sat May 26 2012 04:34:04 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.