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