/home/ntakagi/work/STLport-5.1.5/src/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 void strstreambuf::_M_setup(char* get, char* put, streamsize n) {
00277   if (get) {
00278     size_t N = n > 0 ? size_t(n) : n == 0 ? strlen(get) : size_t(INT_MAX);
00279 
00280     if (put) {
00281       setg(get, get, get + N);
00282       setp(put, put + N);
00283     }
00284     else {
00285       setg(get, get, get + N);
00286     }
00287   }
00288 }
00289 
00290 //----------------------------------------------------------------------
00291 // Class istrstream
00292 
00293 istrstream::istrstream(char* s)
00294   : basic_istream<char, char_traits<char> >(0), _M_buf(s, 0) {
00295   this->init(&_M_buf);
00296 }
00297 
00298 istrstream::istrstream(const char* s)
00299   : basic_istream<char, char_traits<char> >(0), _M_buf(s, 0) {
00300   this->init(&_M_buf);
00301 }
00302 
00303 istrstream::istrstream(char* s, streamsize n)
00304   : basic_istream<char, char_traits<char> >(0), _M_buf(s, n) {
00305   this->init(&_M_buf);
00306 }
00307 
00308 istrstream::istrstream(const char* s, streamsize n)
00309   : basic_istream<char, char_traits<char> >(0), _M_buf(s, n) {
00310   this->init(&_M_buf);
00311 }
00312 
00313 istrstream::~istrstream() {}
00314 
00315 strstreambuf* istrstream::rdbuf() const {
00316   return __CONST_CAST(strstreambuf*,&_M_buf);
00317 }
00318 
00319 char* istrstream::str() { return _M_buf.str(); }
00320 
00321 //----------------------------------------------------------------------
00322 // Class ostrstream
00323 
00324 ostrstream::ostrstream()
00325   : basic_ostream<char, char_traits<char> >(0), _M_buf() {
00326   basic_ios<char, char_traits<char> >::init(&_M_buf);
00327 }
00328 
00329 ostrstream::ostrstream(char* s, int n, ios_base::openmode mode)
00330   : basic_ostream<char, char_traits<char> >(0),
00331     _M_buf(s, n, mode & ios_base::app ? s + strlen(s) : s) {
00332   basic_ios<char, char_traits<char> >::init(&_M_buf);
00333 }
00334 
00335 ostrstream::~ostrstream() {}
00336 
00337 strstreambuf* ostrstream::rdbuf() const {
00338   return __CONST_CAST(strstreambuf*,&_M_buf);
00339 }
00340 
00341 void ostrstream::freeze(bool freezeflag) {
00342   _M_buf.freeze(freezeflag);
00343 }
00344 
00345 char* ostrstream::str() {
00346   return _M_buf.str();
00347 }
00348 
00349 int ostrstream::pcount() const {
00350   return _M_buf.pcount();
00351 }
00352 
00353 
00354 //----------------------------------------------------------------------
00355 // Class strstream
00356 
00357 strstream::strstream()
00358   : basic_iostream<char, char_traits<char> >(0), _M_buf() {
00359   basic_ios<char, char_traits<char> >::init(&_M_buf);
00360 }
00361 
00362 strstream::strstream(char* s, int n, ios_base::openmode mode)
00363   : basic_iostream<char, char_traits<char> >(0),
00364     _M_buf(s, n, mode & ios_base::app ? s + strlen(s) : s) {
00365   basic_ios<char, char_traits<char> >::init(&_M_buf);
00366 }
00367 
00368 strstream::~strstream() {}
00369 
00370 strstreambuf* strstream::rdbuf() const {
00371   return __CONST_CAST(strstreambuf*,&_M_buf);
00372 }
00373 
00374 void strstream::freeze(bool freezeflag) {
00375   _M_buf.freeze(freezeflag);
00376 }
00377 
00378 int strstream::pcount() const {
00379   return _M_buf.pcount();
00380 }
00381 
00382 char* strstream::str() {
00383   return _M_buf.str();
00384 }
00385 
00386 _STLP_END_NAMESPACE
00387 
00388 // Local Variables:
00389 // mode:C++
00390 // End:



Generated on Mon Mar 10 15:32:17 2008 by  doxygen 1.5.1