]> git.buserror.net Git - polintos/scott/priv.git/blob - lib/c++/stlport/strstream.cpp
Add STLport 5.1.4
[polintos/scott/priv.git] / lib / c++ / stlport / strstream.cpp
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
31 _STLP_BEGIN_NAMESPACE
32
33 // strstreambuf constructor, destructor.
34 strstreambuf::strstreambuf(streamsize initial_capacity)
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
60 strstreambuf::strstreambuf(char* get, streamsize n, char* put)
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) {
69   _M_setup(__REINTERPRET_CAST(char*,get), __REINTERPRET_CAST(char*,put), n);
70 }
71
72 strstreambuf::strstreambuf(unsigned char* get, streamsize n,
73                            unsigned char* put)
74   : _M_alloc_fun(0), _M_free_fun(0),
75     _M_dynamic(false), _M_frozen(false), _M_constant(false) {
76   _M_setup(__REINTERPRET_CAST(char*,get), __REINTERPRET_CAST(char*,put), n);
77 }
78
79 strstreambuf::strstreambuf(const char* get, streamsize n)
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
85 strstreambuf::strstreambuf(const signed char* get, streamsize n)
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
91 strstreambuf::strstreambuf(const unsigned char* get, streamsize n)
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
97 strstreambuf::~strstreambuf() {
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
107 char* strstreambuf::str() {
108   freeze(true);
109   return eback();
110 }
111
112 int strstreambuf::pcount() const {
113   return int(pptr() ? pptr() - pbase() : 0);
114 }
115
116 strstreambuf::int_type strstreambuf::overflow(int_type c) {
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()) {
148     *pptr() = traits_type::to_char_type(c);
149     pbump(1);
150     return c;
151   }
152   else
153     return traits_type::eof();
154 }
155
156 strstreambuf::int_type strstreambuf::pbackfail(int_type c) {
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);
168       *gptr() = traits_type::to_char_type(c);
169       return c;
170     }
171   }
172
173   return traits_type::eof();
174 }
175
176 strstreambuf::int_type strstreambuf::underflow() {
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
186 basic_streambuf<char, char_traits<char> >*
187 strstreambuf::setbuf(char*, streamsize) {
188   return this;
189 }
190
191 strstreambuf::pos_type
192 strstreambuf::seekoff(off_type off,
193                       ios_base::seekdir dir, ios_base::openmode mode) {
194   bool do_get = false;
195   bool do_put = false;
196
197   if ((mode & (ios_base::in | ios_base::out)) ==
198           (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
255 strstreambuf::pos_type
256 strstreambuf::seekpos(pos_type pos, ios_base::openmode mode) {
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
268 void strstreambuf::_M_free(char* p) {
269   if (p)
270     if (_M_free_fun)
271       _M_free_fun(p);
272     else
273       delete[] p;
274 }
275
276 void strstreambuf::_M_setup(char* get, char* put, streamsize n) {
277   if (get) {
278     size_t N = n > 0 ? size_t(n) : n == 0 ? strlen(get) : size_t(INT_MAX);
279
280     if (put) {
281       setg(get, get, get + N);
282       setp(put, put + N);
283     }
284     else {
285       setg(get, get, get + N);
286     }
287   }
288 }
289
290 //----------------------------------------------------------------------
291 // Class istrstream
292
293 istrstream::istrstream(char* s)
294   : basic_istream<char, char_traits<char> >(0), _M_buf(s, 0) {
295   this->init(&_M_buf);
296 }
297
298 istrstream::istrstream(const char* s)
299   : basic_istream<char, char_traits<char> >(0), _M_buf(s, 0) {
300   this->init(&_M_buf);
301 }
302
303 istrstream::istrstream(char* s, streamsize n)
304   : basic_istream<char, char_traits<char> >(0), _M_buf(s, n) {
305   this->init(&_M_buf);
306 }
307
308 istrstream::istrstream(const char* s, streamsize n)
309   : basic_istream<char, char_traits<char> >(0), _M_buf(s, n) {
310   this->init(&_M_buf);
311 }
312
313 istrstream::~istrstream() {}
314
315 strstreambuf* istrstream::rdbuf() const {
316   return __CONST_CAST(strstreambuf*,&_M_buf);
317 }
318
319 char* istrstream::str() { return _M_buf.str(); }
320
321 //----------------------------------------------------------------------
322 // Class ostrstream
323
324 ostrstream::ostrstream()
325   : basic_ostream<char, char_traits<char> >(0), _M_buf() {
326   basic_ios<char, char_traits<char> >::init(&_M_buf);
327 }
328
329 ostrstream::ostrstream(char* s, int n, ios_base::openmode mode)
330   : basic_ostream<char, char_traits<char> >(0),
331     _M_buf(s, n, mode & ios_base::app ? s + strlen(s) : s) {
332   basic_ios<char, char_traits<char> >::init(&_M_buf);
333 }
334
335 ostrstream::~ostrstream() {}
336
337 strstreambuf* ostrstream::rdbuf() const {
338   return __CONST_CAST(strstreambuf*,&_M_buf);
339 }
340
341 void ostrstream::freeze(bool freezeflag) {
342   _M_buf.freeze(freezeflag);
343 }
344
345 char* ostrstream::str() {
346   return _M_buf.str();
347 }
348
349 int ostrstream::pcount() const {
350   return _M_buf.pcount();
351 }
352
353
354 //----------------------------------------------------------------------
355 // Class strstream
356
357 strstream::strstream()
358   : basic_iostream<char, char_traits<char> >(0), _M_buf() {
359   basic_ios<char, char_traits<char> >::init(&_M_buf);
360 }
361
362 strstream::strstream(char* s, int n, ios_base::openmode mode)
363   : basic_iostream<char, char_traits<char> >(0),
364     _M_buf(s, n, mode & ios_base::app ? s + strlen(s) : s) {
365   basic_ios<char, char_traits<char> >::init(&_M_buf);
366 }
367
368 strstream::~strstream() {}
369
370 strstreambuf* strstream::rdbuf() const {
371   return __CONST_CAST(strstreambuf*,&_M_buf);
372 }
373
374 void strstream::freeze(bool freezeflag) {
375   _M_buf.freeze(freezeflag);
376 }
377
378 int strstream::pcount() const {
379   return _M_buf.pcount();
380 }
381
382 char* strstream::str() {
383   return _M_buf.str();
384 }
385
386 _STLP_END_NAMESPACE
387
388 // Local Variables:
389 // mode:C++
390 // End: