ReactOS  0.4.15-dev-1039-gb9754fa
strstream.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 1999
3  * Silicon Graphics Computer Systems, Inc.
4  *
5  * Copyright (c) 1999
6  * Boris Fomitchev
7  *
8  * This material is provided "as is", with absolutely no warranty expressed
9  * or implied. Any use is at your own risk.
10  *
11  * Permission to use or copy this software for any purpose is hereby granted
12  * without fee, provided the above notices are retained on all copies.
13  * Permission to modify the code and to distribute modified code is granted,
14  * provided the above notices are retained, and a notice that the code was
15  * modified is included with the above copyright notice.
16  *
17  */
18 
19 // Implementation of the classes in header <strstream>.
20 // WARNING: The classes defined in <strstream> are DEPRECATED. This
21 // header is defined in section D.7.1 of the C++ standard, and it
22 // MAY BE REMOVED in a future standard revision. You should use the
23 // header <sstream> instead.
24 
25 #include "stlport_prefix.h"
26 
27 #include <strstream>
28 #include <algorithm>
29 #include <limits>
30 
32 
33 // strstreambuf constructor, destructor.
35  : _M_alloc_fun(0), _M_free_fun(0),
36  _M_dynamic(true), _M_frozen(false), _M_constant(false) {
37  size_t n = (sizeof(streamsize) > sizeof(size_t)) ? __STATIC_CAST(size_t, (min)(__STATIC_CAST(streamsize, (numeric_limits<size_t>::max)()),
38  (max)(initial_capacity, streamsize(16))))
39  : __STATIC_CAST(size_t, (max)(initial_capacity, streamsize(16)));
40 
41  char* buf = _M_alloc(n);
42  if (buf) {
43  setp(buf, buf + n);
44  setg(buf, buf, buf);
45  }
46 }
47 
48 strstreambuf::strstreambuf(__alloc_fn alloc_f, __free_fn free_f)
49  : _M_alloc_fun(alloc_f), _M_free_fun(free_f),
50  _M_dynamic(true), _M_frozen(false), _M_constant(false) {
51  size_t n = 16;
52 
53  char* buf = _M_alloc(n);
54  if (buf) {
55  setp(buf, buf + n);
56  setg(buf, buf, buf);
57  }
58 }
59 
61  : _M_alloc_fun(0), _M_free_fun(0),
62  _M_dynamic(false), _M_frozen(false), _M_constant(false) {
63  _M_setup(get, put, n);
64 }
65 
66 strstreambuf::strstreambuf(signed char* get, streamsize n, signed char* put)
67  : _M_alloc_fun(0), _M_free_fun(0),
68  _M_dynamic(false), _M_frozen(false), _M_constant(false) {
70 }
71 
73  unsigned char* put)
74  : _M_alloc_fun(0), _M_free_fun(0),
75  _M_dynamic(false), _M_frozen(false), _M_constant(false) {
77 }
78 
80  : _M_alloc_fun(0), _M_free_fun(0),
81  _M_dynamic(false), _M_frozen(false), _M_constant(true) {
82  _M_setup(__CONST_CAST(char*,get), 0, n);
83 }
84 
86  : _M_alloc_fun(0), _M_free_fun(0),
87  _M_dynamic(false), _M_frozen(false), _M_constant(true) {
88  _M_setup(__REINTERPRET_CAST(char*, __CONST_CAST(signed char*,get)), 0, n);
89 }
90 
92  : _M_alloc_fun(0), _M_free_fun(0),
93  _M_dynamic(false), _M_frozen(false), _M_constant(true) {
94  _M_setup(__REINTERPRET_CAST(char*, __CONST_CAST(unsigned char*,get)), 0, n);
95 }
96 
98  if (_M_dynamic && !_M_frozen)
99  _M_free(eback());
100 }
101 
102 void strstreambuf::freeze(bool frozenflag) {
103  if (_M_dynamic)
104  _M_frozen = frozenflag;
105 }
106 
108  freeze(true);
109  return eback();
110 }
111 
112 int strstreambuf::pcount() const {
113  return int(pptr() ? pptr() - pbase() : 0);
114 }
115 
117  if (c == traits_type::eof())
118  return traits_type::not_eof(c);
119 
120  // Try to expand the buffer.
121  if (pptr() == epptr() && _M_dynamic && !_M_frozen && !_M_constant) {
122  ptrdiff_t old_size = epptr() - pbase();
123  ptrdiff_t new_size = (max)(2 * old_size, ptrdiff_t(1));
124 
125  char* buf = _M_alloc(new_size);
126  if (buf) {
127  memcpy(buf, pbase(), old_size);
128 
129  char* old_buffer = pbase();
130  bool reposition_get = false;
131  ptrdiff_t old_get_offset;
132  if (gptr() != 0) {
133  reposition_get = true;
134  old_get_offset = gptr() - eback();
135  }
136 
137  setp(buf, buf + new_size);
138  pbump((int)old_size);
139 
140  if (reposition_get)
141  setg(buf, buf + old_get_offset, buf + (max)(old_get_offset, old_size));
142 
143  _M_free(old_buffer);
144  }
145  }
146 
147  if (pptr() != epptr()) {
149  pbump(1);
150  return c;
151  }
152  else
153  return traits_type::eof();
154 }
155 
157  if (gptr() != eback()) {
158  if (c == traits_type::eof()) {
159  gbump(-1);
160  return traits_type::not_eof(c);
161  }
162  else if (c == gptr()[-1]) {
163  gbump(-1);
164  return c;
165  }
166  else if (!_M_constant) {
167  gbump(-1);
169  return c;
170  }
171  }
172 
173  return traits_type::eof();
174 }
175 
177  if (gptr() == egptr() && pptr() && pptr() > egptr())
178  setg(eback(), gptr(), pptr());
179 
180  if (gptr() != egptr())
181  return (unsigned char) *gptr();
182  else
183  return _Traits::eof();
184 }
185 
188  return this;
189 }
190 
194  bool do_get = false;
195  bool do_put = false;
196 
197  if ((mode & (ios_base::in | ios_base::out)) ==
199  (dir == ios_base::beg || dir == ios_base::end))
200  do_get = do_put = true;
201  else if (mode & ios_base::in)
202  do_get = true;
203  else if (mode & ios_base::out)
204  do_put = true;
205 
206  // !gptr() is here because, according to D.7.1 paragraph 4, the seekable
207  // area is undefined if there is no get area.
208  if ((!do_get && !do_put) || (do_put && !pptr()) || !gptr())
209  return pos_type(off_type(-1));
210 
211  char* seeklow = eback();
212  char* seekhigh = epptr() ? epptr() : egptr();
213 
214  off_type newoff;
215  switch(dir) {
216  case ios_base::beg:
217  newoff = 0;
218  break;
219  case ios_base::end:
220  newoff = seekhigh - seeklow;
221  break;
222  case ios_base::cur:
223  newoff = do_put ? pptr() - seeklow : gptr() - seeklow;
224  break;
225  default:
226  return pos_type(off_type(-1));
227  }
228 
229  off += newoff;
230  if (off < 0 || off > seekhigh - seeklow)
231  return pos_type(off_type(-1));
232 
233  if (do_put) {
234  if (seeklow + __STATIC_CAST(ptrdiff_t, off) < pbase()) {
235  setp(seeklow, epptr());
236  pbump((int)off);
237  }
238  else {
239  setp(pbase(), epptr());
240  pbump((int)(off - (pbase() - seeklow)));
241  }
242  }
243  if (do_get) {
244  if (off <= egptr() - seeklow)
245  setg(seeklow, seeklow + __STATIC_CAST(ptrdiff_t, off), egptr());
246  else if (off <= pptr() - seeklow)
247  setg(seeklow, seeklow + __STATIC_CAST(ptrdiff_t, off), pptr());
248  else
249  setg(seeklow, seeklow + __STATIC_CAST(ptrdiff_t, off), epptr());
250  }
251 
252  return pos_type(newoff);
253 }
254 
257  return seekoff(pos - pos_type(off_type(0)), ios_base::beg, mode);
258 }
259 
260 
261 char* strstreambuf::_M_alloc(size_t n) {
262  if (_M_alloc_fun)
263  return __STATIC_CAST(char*,_M_alloc_fun(n));
264  else
265  return new char[n];
266 }
267 
269  if (p) {
270  if (_M_free_fun)
271  _M_free_fun(p);
272  else
273  delete[] p;
274  }
275 }
276 
278  if (get) {
279  size_t N = n > 0 ? size_t(n) : n == 0 ? strlen(get) : size_t(INT_MAX);
280 
281  if (put) {
282  setg(get, get, get + N);
283  setp(put, put + N);
284  }
285  else {
286  setg(get, get, get + N);
287  }
288  }
289 }
290 
291 //----------------------------------------------------------------------
292 // Class istrstream
293 
295  : basic_istream<char, char_traits<char> >(0), _M_buf(s, 0) {
296  this->init(&_M_buf);
297 }
298 
300  : basic_istream<char, char_traits<char> >(0), _M_buf(s, 0) {
301  this->init(&_M_buf);
302 }
303 
305  : basic_istream<char, char_traits<char> >(0), _M_buf(s, n) {
306  this->init(&_M_buf);
307 }
308 
310  : basic_istream<char, char_traits<char> >(0), _M_buf(s, n) {
311  this->init(&_M_buf);
312 }
313 
315 
317  return __CONST_CAST(strstreambuf*,&_M_buf);
318 }
319 
320 char* istrstream::str() { return _M_buf.str(); }
321 
322 //----------------------------------------------------------------------
323 // Class ostrstream
324 
326  : basic_ostream<char, char_traits<char> >(0), _M_buf() {
328 }
329 
332  _M_buf(s, n, mode & ios_base::app ? s + strlen(s) : s) {
334 }
335 
337 
339  return __CONST_CAST(strstreambuf*,&_M_buf);
340 }
341 
342 void ostrstream::freeze(bool freezeflag) {
343  _M_buf.freeze(freezeflag);
344 }
345 
347  return _M_buf.str();
348 }
349 
350 int ostrstream::pcount() const {
351  return _M_buf.pcount();
352 }
353 
354 
355 //----------------------------------------------------------------------
356 // Class strstream
357 
359  : basic_iostream<char, char_traits<char> >(0), _M_buf() {
361 }
362 
365  _M_buf(s, n, mode & ios_base::app ? s + strlen(s) : s) {
367 }
368 
370 
372  return __CONST_CAST(strstreambuf*,&_M_buf);
373 }
374 
375 void strstream::freeze(bool freezeflag) {
376  _M_buf.freeze(freezeflag);
377 }
378 
379 int strstream::pcount() const {
380  return _M_buf.pcount();
381 }
382 
383 char* strstream::str() {
384  return _M_buf.str();
385 }
386 
388 
389 // Local Variables:
390 // mode:C++
391 // End:
static int_type _STLP_CALL eof()
Definition: char_traits.h:193
char_traits< char > ::int_type int_type
Definition: _streambuf.h:54
static int_type _STLP_CALL not_eof(const int_type &__c)
Definition: char_traits.h:181
#define max(a, b)
Definition: svc.c:63
char * str()
Definition: strstream.cpp:107
virtual ~strstream()
Definition: strstream.cpp:369
#define INT_MAX
Definition: limits.h:40
strstreambuf _M_buf
Definition: _strstream.h:133
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
#define __STATIC_CAST(__x, __y)
Definition: features.h:585
char * str()
Definition: strstream.cpp:346
GLdouble n
Definition: glext.h:7729
GLuint GLuint end
Definition: gl.h:1545
virtual ~istrstream()
Definition: strstream.cpp:314
void freeze(bool=true)
Definition: strstream.cpp:102
int seekdir
Definition: _ios_base.h:60
void setp(char_type *__pbegin, char_type *__pend)
Definition: _streambuf.h:116
strstreambuf * rdbuf() const
Definition: strstream.cpp:316
void setg(char_type *__gbegin, char_type *__gnext, char_type *__gend)
Definition: _streambuf.h:92
#define put(ret, state, sp, n)
Definition: match.c:105
bool _M_constant
Definition: _strstream.h:96
char_traits< char > ::pos_type pos_type
Definition: _streambuf.h:55
char * str()
Definition: strstream.cpp:320
#define true
Definition: osdep.h:36
void _M_free(char *)
Definition: strstream.cpp:268
int pcount() const
Definition: strstream.cpp:112
strstreambuf * rdbuf() const
Definition: strstream.cpp:338
unsigned char
Definition: typeof.h:29
__free_fn _M_free_fun
Definition: _strstream.h:93
#define false
Definition: osdep.h:35
unsigned int dir
Definition: maze.c:112
char * _M_alloc(size_t)
Definition: strstream.cpp:261
bool _M_frozen
Definition: _strstream.h:95
Definition: _ios.h:48
strstreambuf _M_buf
Definition: _strstream.h:156
__kernel_size_t size_t
Definition: linux.h:237
__alloc_fn _M_alloc_fun
Definition: _strstream.h:92
void get(int argc, const char *argv[])
Definition: cmds.c:480
int openmode
Definition: _ios_base.h:59
const GLubyte * c
Definition: glext.h:8905
void freeze(bool=true)
Definition: strstream.cpp:342
virtual ~ostrstream()
Definition: strstream.cpp:336
static FILE * out
Definition: regtests2xml.c:44
strstreambuf(streamsize _Initial_capacity=0)
Definition: strstream.cpp:34
void init(basic_streambuf< char, char_traits< char > > *__streambuf)
Definition: _ios.c:97
virtual _Base * setbuf(char *__buf, streamsize __n)
Definition: strstream.cpp:187
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
GLdouble s
Definition: gl.h:2039
strstreambuf _M_buf
Definition: _strstream.h:114
GLenum mode
Definition: glext.h:6217
#define __REINTERPRET_CAST(__x, __y)
Definition: features.h:586
istrstream(char *)
Definition: strstream.cpp:294
void freeze(bool=true)
Definition: strstream.cpp:375
virtual int_type overflow(int_type __c=_Traits::eof())
Definition: strstream.cpp:116
virtual pos_type seekpos(pos_type __pos, ios_base::openmode __mode=ios_base::in|ios_base::out)
Definition: strstream.cpp:256
virtual int_type pbackfail(int_type __c=_Traits::eof())
Definition: strstream.cpp:156
virtual int_type underflow()
Definition: strstream.cpp:176
int pcount() const
Definition: strstream.cpp:350
void _M_setup(char *__get, char *__put, streamsize __n)
Definition: strstream.cpp:277
virtual ~strstreambuf()
Definition: strstream.cpp:97
GLuint in
Definition: glext.h:9616
#define __CONST_CAST(__x, __y)
Definition: features.h:584
#define _STLP_END_NAMESPACE
Definition: features.h:503
#define min(a, b)
Definition: monoChain.cc:55
__kernel_ptrdiff_t ptrdiff_t
Definition: linux.h:247
char * str()
Definition: strstream.cpp:383
ptrdiff_t streamsize
Definition: char_traits.h:81
#define c
Definition: ke_i.h:80
bool _M_dynamic
Definition: _strstream.h:94
#define _STLP_BEGIN_NAMESPACE
Definition: features.h:501
GLfloat GLfloat p
Definition: glext.h:8902
int pcount() const
Definition: strstream.cpp:379
char_traits< char > ::off_type off_type
Definition: _streambuf.h:56
strstreambuf * rdbuf() const
Definition: strstream.cpp:371
virtual pos_type seekoff(off_type __off, ios_base::seekdir __dir, ios_base::openmode __mode=ios_base::in|ios_base::out)
Definition: strstream.cpp:192
static char _STLP_CALL to_char_type(const int &__c)
Definition: char_traits.h:219
unsigned int(__cdecl typeof(jpeg_read_scanlines))(struct jpeg_decompress_struct *
Definition: typeof.h:31
off
Definition: i386-dis.c:3909