1*404b540aSrobert // strstream definitions -*- C++ -*-
2*404b540aSrobert
3*404b540aSrobert // Copyright (C) 2001, 2002, 2003, 2005 Free Software Foundation
4*404b540aSrobert //
5*404b540aSrobert // This file is part of the GNU ISO C++ Library. This library is free
6*404b540aSrobert // software; you can redistribute it and/or modify it under the
7*404b540aSrobert // terms of the GNU General Public License as published by the
8*404b540aSrobert // Free Software Foundation; either version 2, or (at your option)
9*404b540aSrobert // any later version.
10*404b540aSrobert
11*404b540aSrobert // This library is distributed in the hope that it will be useful,
12*404b540aSrobert // but WITHOUT ANY WARRANTY; without even the implied warranty of
13*404b540aSrobert // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14*404b540aSrobert // GNU General Public License for more details.
15*404b540aSrobert
16*404b540aSrobert // You should have received a copy of the GNU General Public License along
17*404b540aSrobert // with this library; see the file COPYING. If not, write to the Free
18*404b540aSrobert // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19*404b540aSrobert // USA.
20*404b540aSrobert
21*404b540aSrobert // As a special exception, you may use this file as part of a free software
22*404b540aSrobert // library without restriction. Specifically, if other files instantiate
23*404b540aSrobert // templates or use macros or inline functions from this file, or you compile
24*404b540aSrobert // this file and link it with other files to produce an executable, this
25*404b540aSrobert // file does not by itself cause the resulting executable to be covered by
26*404b540aSrobert // the GNU General Public License. This exception does not however
27*404b540aSrobert // invalidate any other reasons why the executable file might be covered by
28*404b540aSrobert // the GNU General Public License.
29*404b540aSrobert
30*404b540aSrobert /*
31*404b540aSrobert * Copyright (c) 1998
32*404b540aSrobert * Silicon Graphics Computer Systems, Inc.
33*404b540aSrobert *
34*404b540aSrobert * Permission to use, copy, modify, distribute and sell this software
35*404b540aSrobert * and its documentation for any purpose is hereby granted without fee,
36*404b540aSrobert * provided that the above copyright notice appear in all copies and
37*404b540aSrobert * that both that copyright notice and this permission notice appear
38*404b540aSrobert * in supporting documentation. Silicon Graphics makes no
39*404b540aSrobert * representations about the suitability of this software for any
40*404b540aSrobert * purpose. It is provided "as is" without express or implied warranty.
41*404b540aSrobert */
42*404b540aSrobert
43*404b540aSrobert // Implementation of the classes in header <strstream>.
44*404b540aSrobert // WARNING: The classes defined in <strstream> are DEPRECATED. This
45*404b540aSrobert // header is defined in section D.7.1 of the C++ standard, and it
46*404b540aSrobert // MAY BE REMOVED in a future standard revision. You should use the
47*404b540aSrobert // header <sstream> instead.
48*404b540aSrobert
49*404b540aSrobert #include <strstream>
50*404b540aSrobert #include <algorithm>
51*404b540aSrobert #include <new>
52*404b540aSrobert #include <stdlib.h>
53*404b540aSrobert #include <string.h>
54*404b540aSrobert #include <limits.h>
55*404b540aSrobert
_GLIBCXX_BEGIN_NAMESPACE(std)56*404b540aSrobert _GLIBCXX_BEGIN_NAMESPACE(std)
57*404b540aSrobert
58*404b540aSrobert strstreambuf::strstreambuf(streamsize initial_capacity)
59*404b540aSrobert : _Base(), _M_alloc_fun(0), _M_free_fun(0), _M_dynamic(true),
60*404b540aSrobert _M_frozen(false), _M_constant(false)
61*404b540aSrobert {
62*404b540aSrobert streamsize n = std::max(initial_capacity, streamsize(16));
63*404b540aSrobert
64*404b540aSrobert char* buf = _M_alloc(n);
65*404b540aSrobert if (buf)
66*404b540aSrobert {
67*404b540aSrobert setp(buf, buf + n);
68*404b540aSrobert setg(buf, buf, buf);
69*404b540aSrobert }
70*404b540aSrobert }
71*404b540aSrobert
strstreambuf(void * (* alloc_f)(size_t),void (* free_f)(void *))72*404b540aSrobert strstreambuf::strstreambuf(void* (*alloc_f)(size_t), void (*free_f)(void*))
73*404b540aSrobert : _Base(), _M_alloc_fun(alloc_f), _M_free_fun(free_f), _M_dynamic(true),
74*404b540aSrobert _M_frozen(false), _M_constant(false)
75*404b540aSrobert {
76*404b540aSrobert streamsize n = 16;
77*404b540aSrobert
78*404b540aSrobert char* buf = _M_alloc(n);
79*404b540aSrobert if (buf)
80*404b540aSrobert {
81*404b540aSrobert setp(buf, buf + n);
82*404b540aSrobert setg(buf, buf, buf);
83*404b540aSrobert }
84*404b540aSrobert }
85*404b540aSrobert
strstreambuf(char * get,streamsize n,char * put)86*404b540aSrobert strstreambuf::strstreambuf(char* get, streamsize n, char* put)
87*404b540aSrobert : _Base(), _M_alloc_fun(0), _M_free_fun(0), _M_dynamic(false),
88*404b540aSrobert _M_frozen(false), _M_constant(false)
89*404b540aSrobert { _M_setup(get, put, n); }
90*404b540aSrobert
strstreambuf(signed char * get,streamsize n,signed char * put)91*404b540aSrobert strstreambuf::strstreambuf(signed char* get, streamsize n, signed char* put)
92*404b540aSrobert : _Base(), _M_alloc_fun(0), _M_free_fun(0), _M_dynamic(false),
93*404b540aSrobert _M_frozen(false), _M_constant(false)
94*404b540aSrobert { _M_setup(reinterpret_cast<char*>(get), reinterpret_cast<char*>(put), n); }
95*404b540aSrobert
strstreambuf(unsigned char * get,streamsize n,unsigned char * put)96*404b540aSrobert strstreambuf::strstreambuf(unsigned char* get, streamsize n,
97*404b540aSrobert unsigned char* put)
98*404b540aSrobert : _Base(), _M_alloc_fun(0), _M_free_fun(0), _M_dynamic(false),
99*404b540aSrobert _M_frozen(false), _M_constant(false)
100*404b540aSrobert { _M_setup(reinterpret_cast<char*>(get), reinterpret_cast<char*>(put), n); }
101*404b540aSrobert
strstreambuf(const char * get,streamsize n)102*404b540aSrobert strstreambuf::strstreambuf(const char* get, streamsize n)
103*404b540aSrobert : _Base(), _M_alloc_fun(0), _M_free_fun(0), _M_dynamic(false),
104*404b540aSrobert _M_frozen(false), _M_constant(true)
105*404b540aSrobert { _M_setup(const_cast<char*>(get), 0, n); }
106*404b540aSrobert
strstreambuf(const signed char * get,streamsize n)107*404b540aSrobert strstreambuf::strstreambuf(const signed char* get, streamsize n)
108*404b540aSrobert : _Base(), _M_alloc_fun(0), _M_free_fun(0), _M_dynamic(false),
109*404b540aSrobert _M_frozen(false), _M_constant(true)
110*404b540aSrobert { _M_setup(reinterpret_cast<char*>(const_cast<signed char*>(get)), 0, n); }
111*404b540aSrobert
strstreambuf(const unsigned char * get,streamsize n)112*404b540aSrobert strstreambuf::strstreambuf(const unsigned char* get, streamsize n)
113*404b540aSrobert : _Base(), _M_alloc_fun(0), _M_free_fun(0), _M_dynamic(false),
114*404b540aSrobert _M_frozen(false), _M_constant(true)
115*404b540aSrobert { _M_setup(reinterpret_cast<char*>(const_cast<unsigned char*>(get)), 0, n); }
116*404b540aSrobert
~strstreambuf()117*404b540aSrobert strstreambuf::~strstreambuf()
118*404b540aSrobert {
119*404b540aSrobert if (_M_dynamic && !_M_frozen)
120*404b540aSrobert _M_free(eback());
121*404b540aSrobert }
122*404b540aSrobert
123*404b540aSrobert void
freeze(bool frozenflag)124*404b540aSrobert strstreambuf::freeze(bool frozenflag)
125*404b540aSrobert {
126*404b540aSrobert if (_M_dynamic)
127*404b540aSrobert _M_frozen = frozenflag;
128*404b540aSrobert }
129*404b540aSrobert
130*404b540aSrobert char*
str()131*404b540aSrobert strstreambuf::str()
132*404b540aSrobert {
133*404b540aSrobert freeze(true);
134*404b540aSrobert return eback();
135*404b540aSrobert }
136*404b540aSrobert
137*404b540aSrobert int
pcount() const138*404b540aSrobert strstreambuf::pcount() const
139*404b540aSrobert { return pptr() ? pptr() - pbase() : 0; }
140*404b540aSrobert
141*404b540aSrobert strstreambuf::int_type
overflow(int_type c)142*404b540aSrobert strstreambuf::overflow(int_type c)
143*404b540aSrobert {
144*404b540aSrobert if (c == traits_type::eof())
145*404b540aSrobert return traits_type::not_eof(c);
146*404b540aSrobert
147*404b540aSrobert // Try to expand the buffer.
148*404b540aSrobert if (pptr() == epptr() && _M_dynamic && !_M_frozen && !_M_constant)
149*404b540aSrobert {
150*404b540aSrobert ptrdiff_t old_size = epptr() - pbase();
151*404b540aSrobert ptrdiff_t new_size = std::max(ptrdiff_t(2 * old_size), ptrdiff_t(1));
152*404b540aSrobert
153*404b540aSrobert char* buf = _M_alloc(new_size);
154*404b540aSrobert if (buf)
155*404b540aSrobert {
156*404b540aSrobert memcpy(buf, pbase(), old_size);
157*404b540aSrobert char* old_buffer = pbase();
158*404b540aSrobert bool reposition_get = false;
159*404b540aSrobert ptrdiff_t old_get_offset;
160*404b540aSrobert if (gptr() != 0)
161*404b540aSrobert {
162*404b540aSrobert reposition_get = true;
163*404b540aSrobert old_get_offset = gptr() - eback();
164*404b540aSrobert }
165*404b540aSrobert
166*404b540aSrobert setp(buf, buf + new_size);
167*404b540aSrobert pbump(old_size);
168*404b540aSrobert
169*404b540aSrobert if (reposition_get)
170*404b540aSrobert setg(buf, buf + old_get_offset, buf +
171*404b540aSrobert std::max(old_get_offset, old_size));
172*404b540aSrobert
173*404b540aSrobert _M_free(old_buffer);
174*404b540aSrobert }
175*404b540aSrobert }
176*404b540aSrobert
177*404b540aSrobert if (pptr() != epptr())
178*404b540aSrobert {
179*404b540aSrobert *pptr() = c;
180*404b540aSrobert pbump(1);
181*404b540aSrobert return c;
182*404b540aSrobert }
183*404b540aSrobert else
184*404b540aSrobert return traits_type::eof();
185*404b540aSrobert }
186*404b540aSrobert
187*404b540aSrobert strstreambuf::int_type
pbackfail(int_type c)188*404b540aSrobert strstreambuf::pbackfail(int_type c)
189*404b540aSrobert {
190*404b540aSrobert if (gptr() != eback())
191*404b540aSrobert {
192*404b540aSrobert if (c == _Traits::eof())
193*404b540aSrobert {
194*404b540aSrobert gbump(-1);
195*404b540aSrobert return _Traits::not_eof(c);
196*404b540aSrobert }
197*404b540aSrobert else if (c == _Traits::to_int_type(gptr()[-1]))
198*404b540aSrobert { // KLUDGE
199*404b540aSrobert gbump(-1);
200*404b540aSrobert return c;
201*404b540aSrobert }
202*404b540aSrobert else if (!_M_constant)
203*404b540aSrobert {
204*404b540aSrobert gbump(-1);
205*404b540aSrobert *gptr() = c;
206*404b540aSrobert return c;
207*404b540aSrobert }
208*404b540aSrobert }
209*404b540aSrobert return _Traits::eof();
210*404b540aSrobert }
211*404b540aSrobert
212*404b540aSrobert strstreambuf::int_type
underflow()213*404b540aSrobert strstreambuf::underflow()
214*404b540aSrobert {
215*404b540aSrobert if (gptr() == egptr() && pptr() && pptr() > egptr())
216*404b540aSrobert setg(eback(), gptr(), pptr());
217*404b540aSrobert
218*404b540aSrobert if (gptr() != egptr())
219*404b540aSrobert return (unsigned char) *gptr();
220*404b540aSrobert else
221*404b540aSrobert return _Traits::eof();
222*404b540aSrobert }
223*404b540aSrobert
224*404b540aSrobert basic_streambuf<char, char_traits<char> >*
setbuf(char *,streamsize)225*404b540aSrobert strstreambuf::setbuf(char*, streamsize)
226*404b540aSrobert { return this; }
227*404b540aSrobert
228*404b540aSrobert strstreambuf::pos_type
seekoff(off_type off,ios_base::seekdir dir,ios_base::openmode mode)229*404b540aSrobert strstreambuf::seekoff(off_type off, ios_base::seekdir dir,
230*404b540aSrobert ios_base::openmode mode)
231*404b540aSrobert {
232*404b540aSrobert bool do_get = false;
233*404b540aSrobert bool do_put = false;
234*404b540aSrobert
235*404b540aSrobert if ((mode & (ios_base::in | ios_base::out))
236*404b540aSrobert == (ios_base::in | ios_base::out) &&
237*404b540aSrobert (dir == ios_base::beg || dir == ios_base::end))
238*404b540aSrobert do_get = do_put = true;
239*404b540aSrobert else if (mode & ios_base::in)
240*404b540aSrobert do_get = true;
241*404b540aSrobert else if (mode & ios_base::out)
242*404b540aSrobert do_put = true;
243*404b540aSrobert
244*404b540aSrobert // !gptr() is here because, according to D.7.1 paragraph 4, the seekable
245*404b540aSrobert // area is undefined if there is no get area.
246*404b540aSrobert if ((!do_get && !do_put) || (do_put && !pptr()) || !gptr())
247*404b540aSrobert return pos_type(off_type(-1));
248*404b540aSrobert
249*404b540aSrobert char* seeklow = eback();
250*404b540aSrobert char* seekhigh = epptr() ? epptr() : egptr();
251*404b540aSrobert
252*404b540aSrobert off_type newoff;
253*404b540aSrobert switch (dir)
254*404b540aSrobert {
255*404b540aSrobert case ios_base::beg:
256*404b540aSrobert newoff = 0;
257*404b540aSrobert break;
258*404b540aSrobert case ios_base::end:
259*404b540aSrobert newoff = seekhigh - seeklow;
260*404b540aSrobert break;
261*404b540aSrobert case ios_base::cur:
262*404b540aSrobert newoff = do_put ? pptr() - seeklow : gptr() - seeklow;
263*404b540aSrobert break;
264*404b540aSrobert default:
265*404b540aSrobert return pos_type(off_type(-1));
266*404b540aSrobert }
267*404b540aSrobert
268*404b540aSrobert off += newoff;
269*404b540aSrobert if (off < 0 || off > seekhigh - seeklow)
270*404b540aSrobert return pos_type(off_type(-1));
271*404b540aSrobert
272*404b540aSrobert if (do_put)
273*404b540aSrobert {
274*404b540aSrobert if (seeklow + off < pbase())
275*404b540aSrobert {
276*404b540aSrobert setp(seeklow, epptr());
277*404b540aSrobert pbump(off);
278*404b540aSrobert }
279*404b540aSrobert else
280*404b540aSrobert {
281*404b540aSrobert setp(pbase(), epptr());
282*404b540aSrobert pbump(off - (pbase() - seeklow));
283*404b540aSrobert }
284*404b540aSrobert }
285*404b540aSrobert if (do_get)
286*404b540aSrobert {
287*404b540aSrobert if (off <= egptr() - seeklow)
288*404b540aSrobert setg(seeklow, seeklow + off, egptr());
289*404b540aSrobert else if (off <= pptr() - seeklow)
290*404b540aSrobert setg(seeklow, seeklow + off, pptr());
291*404b540aSrobert else
292*404b540aSrobert setg(seeklow, seeklow + off, epptr());
293*404b540aSrobert }
294*404b540aSrobert return pos_type(newoff);
295*404b540aSrobert }
296*404b540aSrobert
297*404b540aSrobert strstreambuf::pos_type
seekpos(pos_type pos,ios_base::openmode mode)298*404b540aSrobert strstreambuf::seekpos(pos_type pos, ios_base::openmode mode)
299*404b540aSrobert { return seekoff(pos - pos_type(off_type(0)), ios_base::beg, mode); }
300*404b540aSrobert
301*404b540aSrobert char*
_M_alloc(size_t n)302*404b540aSrobert strstreambuf::_M_alloc(size_t n)
303*404b540aSrobert {
304*404b540aSrobert if (_M_alloc_fun)
305*404b540aSrobert return static_cast<char*>(_M_alloc_fun(n));
306*404b540aSrobert else
307*404b540aSrobert return new char[n];
308*404b540aSrobert }
309*404b540aSrobert
310*404b540aSrobert void
_M_free(char * p)311*404b540aSrobert strstreambuf::_M_free(char* p)
312*404b540aSrobert {
313*404b540aSrobert if (p)
314*404b540aSrobert if (_M_free_fun)
315*404b540aSrobert _M_free_fun(p);
316*404b540aSrobert else
317*404b540aSrobert delete[] p;
318*404b540aSrobert }
319*404b540aSrobert
320*404b540aSrobert void
_M_setup(char * get,char * put,streamsize n)321*404b540aSrobert strstreambuf::_M_setup(char* get, char* put, streamsize n)
322*404b540aSrobert {
323*404b540aSrobert if (get)
324*404b540aSrobert {
325*404b540aSrobert size_t N = n > 0 ? size_t(n) : n == 0 ? strlen(get) : size_t(INT_MAX);
326*404b540aSrobert
327*404b540aSrobert if (put)
328*404b540aSrobert {
329*404b540aSrobert setg(get, get, put);
330*404b540aSrobert setp(put, put + N);
331*404b540aSrobert }
332*404b540aSrobert else
333*404b540aSrobert setg(get, get, get + N);
334*404b540aSrobert }
335*404b540aSrobert }
336*404b540aSrobert
istrstream(char * s)337*404b540aSrobert istrstream::istrstream(char* s)
338*404b540aSrobert : basic_ios<char>(), basic_istream<char>(0), _M_buf(s, 0)
339*404b540aSrobert { basic_ios<char>::init(&_M_buf); }
340*404b540aSrobert
istrstream(const char * s)341*404b540aSrobert istrstream::istrstream(const char* s)
342*404b540aSrobert : basic_ios<char>(), basic_istream<char>(0), _M_buf(s, 0)
343*404b540aSrobert { basic_ios<char>::init(&_M_buf); }
344*404b540aSrobert
istrstream(char * s,streamsize n)345*404b540aSrobert istrstream::istrstream(char* s, streamsize n)
346*404b540aSrobert : basic_ios<char>(), basic_istream<char>(0), _M_buf(s, n)
347*404b540aSrobert { basic_ios<char>::init(&_M_buf); }
348*404b540aSrobert
istrstream(const char * s,streamsize n)349*404b540aSrobert istrstream::istrstream(const char* s, streamsize n)
350*404b540aSrobert : basic_ios<char>(), basic_istream<char>(0), _M_buf(s, n)
351*404b540aSrobert { basic_ios<char>::init(&_M_buf); }
352*404b540aSrobert
~istrstream()353*404b540aSrobert istrstream::~istrstream() { }
354*404b540aSrobert
355*404b540aSrobert strstreambuf*
rdbuf() const356*404b540aSrobert istrstream::rdbuf() const
357*404b540aSrobert { return const_cast<strstreambuf*>(&_M_buf); }
358*404b540aSrobert
359*404b540aSrobert char*
str()360*404b540aSrobert istrstream::str()
361*404b540aSrobert { return _M_buf.str(); }
362*404b540aSrobert
ostrstream()363*404b540aSrobert ostrstream::ostrstream()
364*404b540aSrobert : basic_ios<char>(), basic_ostream<char>(0), _M_buf()
365*404b540aSrobert { basic_ios<char>::init(&_M_buf); }
366*404b540aSrobert
ostrstream(char * s,int n,ios_base::openmode mode)367*404b540aSrobert ostrstream::ostrstream(char* s, int n, ios_base::openmode mode)
368*404b540aSrobert : basic_ios<char>(), basic_ostream<char>(0),
369*404b540aSrobert _M_buf(s, n, mode & ios_base::app ? s + strlen(s) : s)
370*404b540aSrobert { basic_ios<char>::init(&_M_buf); }
371*404b540aSrobert
~ostrstream()372*404b540aSrobert ostrstream::~ostrstream() {}
373*404b540aSrobert
374*404b540aSrobert strstreambuf*
rdbuf() const375*404b540aSrobert ostrstream::rdbuf() const
376*404b540aSrobert { return const_cast<strstreambuf*>(&_M_buf); }
377*404b540aSrobert
378*404b540aSrobert void
freeze(bool freezeflag)379*404b540aSrobert ostrstream::freeze(bool freezeflag)
380*404b540aSrobert { _M_buf.freeze(freezeflag); }
381*404b540aSrobert
382*404b540aSrobert char*
str()383*404b540aSrobert ostrstream::str()
384*404b540aSrobert { return _M_buf.str(); }
385*404b540aSrobert
386*404b540aSrobert int
pcount() const387*404b540aSrobert ostrstream::pcount() const
388*404b540aSrobert { return _M_buf.pcount(); }
389*404b540aSrobert
strstream()390*404b540aSrobert strstream::strstream()
391*404b540aSrobert : basic_ios<char>(), basic_iostream<char>(0), _M_buf()
392*404b540aSrobert { basic_ios<char>::init(&_M_buf); }
393*404b540aSrobert
strstream(char * s,int n,ios_base::openmode mode)394*404b540aSrobert strstream::strstream(char* s, int n, ios_base::openmode mode)
395*404b540aSrobert : basic_ios<char>(), basic_iostream<char>(0),
396*404b540aSrobert _M_buf(s, n, mode & ios_base::app ? s + strlen(s) : s)
397*404b540aSrobert { basic_ios<char>::init(&_M_buf); }
398*404b540aSrobert
~strstream()399*404b540aSrobert strstream::~strstream() { }
400*404b540aSrobert
401*404b540aSrobert strstreambuf*
rdbuf() const402*404b540aSrobert strstream::rdbuf() const
403*404b540aSrobert { return const_cast<strstreambuf*>(&_M_buf); }
404*404b540aSrobert
405*404b540aSrobert void
freeze(bool freezeflag)406*404b540aSrobert strstream::freeze(bool freezeflag)
407*404b540aSrobert { _M_buf.freeze(freezeflag); }
408*404b540aSrobert
409*404b540aSrobert int
pcount() const410*404b540aSrobert strstream::pcount() const
411*404b540aSrobert { return _M_buf.pcount(); }
412*404b540aSrobert
413*404b540aSrobert char*
str()414*404b540aSrobert strstream::str()
415*404b540aSrobert { return _M_buf.str(); }
416*404b540aSrobert
417*404b540aSrobert _GLIBCXX_END_NAMESPACE
418