Daily bump.
[official-gcc.git] / libstdc++-v3 / src / strstream.cc
blobfbc5f518297dde456e5261cb8c8f164727c9aedd
1 // strstream definitions -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2005, 2009 Free Software Foundation
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
26 * Copyright (c) 1998
27 * Silicon Graphics Computer Systems, Inc.
29 * Permission to use, copy, modify, distribute and sell this software
30 * and its documentation for any purpose is hereby granted without fee,
31 * provided that the above copyright notice appear in all copies and
32 * that both that copyright notice and this permission notice appear
33 * in supporting documentation. Silicon Graphics makes no
34 * representations about the suitability of this software for any
35 * purpose. It is provided "as is" without express or implied warranty.
38 // Implementation of the classes in header <strstream>.
39 // WARNING: The classes defined in <strstream> are DEPRECATED. This
40 // header is defined in section D.7.1 of the C++ standard, and it
41 // MAY BE REMOVED in a future standard revision. You should use the
42 // header <sstream> instead.
44 #include <strstream>
45 #include <algorithm>
46 #include <new>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <limits.h>
51 _GLIBCXX_BEGIN_NAMESPACE(std)
53 strstreambuf::strstreambuf(streamsize initial_capacity)
54 : _Base(), _M_alloc_fun(0), _M_free_fun(0), _M_dynamic(true),
55 _M_frozen(false), _M_constant(false)
57 streamsize n = std::max(initial_capacity, streamsize(16));
59 char* buf = _M_alloc(n);
60 if (buf)
62 setp(buf, buf + n);
63 setg(buf, buf, buf);
67 strstreambuf::strstreambuf(void* (*alloc_f)(size_t), void (*free_f)(void*))
68 : _Base(), _M_alloc_fun(alloc_f), _M_free_fun(free_f), _M_dynamic(true),
69 _M_frozen(false), _M_constant(false)
71 streamsize n = 16;
73 char* buf = _M_alloc(n);
74 if (buf)
76 setp(buf, buf + n);
77 setg(buf, buf, buf);
81 strstreambuf::strstreambuf(char* get, streamsize n, char* put) throw ()
82 : _Base(), _M_alloc_fun(0), _M_free_fun(0), _M_dynamic(false),
83 _M_frozen(false), _M_constant(false)
84 { _M_setup(get, put, n); }
86 strstreambuf::strstreambuf(signed char* get, streamsize n, signed char* put) throw ()
87 : _Base(), _M_alloc_fun(0), _M_free_fun(0), _M_dynamic(false),
88 _M_frozen(false), _M_constant(false)
89 { _M_setup(reinterpret_cast<char*>(get), reinterpret_cast<char*>(put), n); }
91 strstreambuf::strstreambuf(unsigned char* get, streamsize n,
92 unsigned char* put) throw ()
93 : _Base(), _M_alloc_fun(0), _M_free_fun(0), _M_dynamic(false),
94 _M_frozen(false), _M_constant(false)
95 { _M_setup(reinterpret_cast<char*>(get), reinterpret_cast<char*>(put), n); }
97 strstreambuf::strstreambuf(const char* get, streamsize n) throw ()
98 : _Base(), _M_alloc_fun(0), _M_free_fun(0), _M_dynamic(false),
99 _M_frozen(false), _M_constant(true)
100 { _M_setup(const_cast<char*>(get), 0, n); }
102 strstreambuf::strstreambuf(const signed char* get, streamsize n) throw ()
103 : _Base(), _M_alloc_fun(0), _M_free_fun(0), _M_dynamic(false),
104 _M_frozen(false), _M_constant(true)
105 { _M_setup(reinterpret_cast<char*>(const_cast<signed char*>(get)), 0, n); }
107 strstreambuf::strstreambuf(const unsigned char* get, streamsize n) throw ()
108 : _Base(), _M_alloc_fun(0), _M_free_fun(0), _M_dynamic(false),
109 _M_frozen(false), _M_constant(true)
110 { _M_setup(reinterpret_cast<char*>(const_cast<unsigned char*>(get)), 0, n); }
112 strstreambuf::~strstreambuf()
114 if (_M_dynamic && !_M_frozen)
115 _M_free(eback());
118 void
119 strstreambuf::freeze(bool frozenflag) throw ()
121 if (_M_dynamic)
122 _M_frozen = frozenflag;
125 char*
126 strstreambuf::str() throw ()
128 freeze(true);
129 return eback();
132 int
133 strstreambuf::pcount() const throw ()
134 { return pptr() ? pptr() - pbase() : 0; }
136 strstreambuf::int_type
137 strstreambuf::overflow(int_type c)
139 if (c == traits_type::eof())
140 return traits_type::not_eof(c);
142 // Try to expand the buffer.
143 if (pptr() == epptr() && _M_dynamic && !_M_frozen && !_M_constant)
145 ptrdiff_t old_size = epptr() - pbase();
146 ptrdiff_t new_size = std::max(ptrdiff_t(2 * old_size), ptrdiff_t(1));
148 char* buf = _M_alloc(new_size);
149 if (buf)
151 memcpy(buf, pbase(), old_size);
152 char* old_buffer = pbase();
153 bool reposition_get = false;
154 ptrdiff_t old_get_offset;
155 if (gptr() != 0)
157 reposition_get = true;
158 old_get_offset = gptr() - eback();
161 setp(buf, buf + new_size);
162 pbump(old_size);
164 if (reposition_get)
165 setg(buf, buf + old_get_offset, buf +
166 std::max(old_get_offset, old_size));
168 _M_free(old_buffer);
172 if (pptr() != epptr())
174 *pptr() = c;
175 pbump(1);
176 return c;
178 else
179 return traits_type::eof();
182 strstreambuf::int_type
183 strstreambuf::pbackfail(int_type c)
185 if (gptr() != eback())
187 if (c == _Traits::eof())
189 gbump(-1);
190 return _Traits::not_eof(c);
192 else if (c == _Traits::to_int_type(gptr()[-1]))
193 { // KLUDGE
194 gbump(-1);
195 return c;
197 else if (!_M_constant)
199 gbump(-1);
200 *gptr() = c;
201 return c;
204 return _Traits::eof();
207 strstreambuf::int_type
208 strstreambuf::underflow()
210 if (gptr() == egptr() && pptr() && pptr() > egptr())
211 setg(eback(), gptr(), pptr());
213 if (gptr() != egptr())
214 return (unsigned char) *gptr();
215 else
216 return _Traits::eof();
219 basic_streambuf<char, char_traits<char> >*
220 strstreambuf::setbuf(char*, streamsize)
221 { return this; }
223 strstreambuf::pos_type
224 strstreambuf::seekoff(off_type off, ios_base::seekdir dir,
225 ios_base::openmode mode)
227 bool do_get = false;
228 bool do_put = false;
230 if ((mode & (ios_base::in | ios_base::out))
231 == (ios_base::in | ios_base::out) &&
232 (dir == ios_base::beg || dir == ios_base::end))
233 do_get = do_put = true;
234 else if (mode & ios_base::in)
235 do_get = true;
236 else if (mode & ios_base::out)
237 do_put = true;
239 // !gptr() is here because, according to D.7.1 paragraph 4, the seekable
240 // area is undefined if there is no get area.
241 if ((!do_get && !do_put) || (do_put && !pptr()) || !gptr())
242 return pos_type(off_type(-1));
244 char* seeklow = eback();
245 char* seekhigh = epptr() ? epptr() : egptr();
247 off_type newoff;
248 switch (dir)
250 case ios_base::beg:
251 newoff = 0;
252 break;
253 case ios_base::end:
254 newoff = seekhigh - seeklow;
255 break;
256 case ios_base::cur:
257 newoff = do_put ? pptr() - seeklow : gptr() - seeklow;
258 break;
259 default:
260 return pos_type(off_type(-1));
263 off += newoff;
264 if (off < 0 || off > seekhigh - seeklow)
265 return pos_type(off_type(-1));
267 if (do_put)
269 if (seeklow + off < pbase())
271 setp(seeklow, epptr());
272 pbump(off);
274 else
276 setp(pbase(), epptr());
277 pbump(off - (pbase() - seeklow));
280 if (do_get)
282 if (off <= egptr() - seeklow)
283 setg(seeklow, seeklow + off, egptr());
284 else if (off <= pptr() - seeklow)
285 setg(seeklow, seeklow + off, pptr());
286 else
287 setg(seeklow, seeklow + off, epptr());
289 return pos_type(newoff);
292 strstreambuf::pos_type
293 strstreambuf::seekpos(pos_type pos, ios_base::openmode mode)
294 { return seekoff(pos - pos_type(off_type(0)), ios_base::beg, mode); }
296 char*
297 strstreambuf::_M_alloc(size_t n)
299 if (_M_alloc_fun)
300 return static_cast<char*>(_M_alloc_fun(n));
301 else
302 return new char[n];
305 void
306 strstreambuf::_M_free(char* p)
308 if (p)
310 if (_M_free_fun)
311 _M_free_fun(p);
312 else
313 delete[] p;
317 void
318 strstreambuf::_M_setup(char* get, char* put, streamsize n) throw ()
320 if (get)
322 size_t N = n > 0 ? size_t(n) : n == 0 ? strlen(get) : size_t(INT_MAX);
324 if (put)
326 setg(get, get, put);
327 setp(put, put + N);
329 else
330 setg(get, get, get + N);
334 istrstream::istrstream(char* s)
335 : basic_ios<char>(), basic_istream<char>(0), _M_buf(s, 0)
336 { basic_ios<char>::init(&_M_buf); }
338 istrstream::istrstream(const char* s)
339 : basic_ios<char>(), basic_istream<char>(0), _M_buf(s, 0)
340 { basic_ios<char>::init(&_M_buf); }
342 istrstream::istrstream(char* s, streamsize n)
343 : basic_ios<char>(), basic_istream<char>(0), _M_buf(s, n)
344 { basic_ios<char>::init(&_M_buf); }
346 istrstream::istrstream(const char* s, streamsize n)
347 : basic_ios<char>(), basic_istream<char>(0), _M_buf(s, n)
348 { basic_ios<char>::init(&_M_buf); }
350 istrstream::~istrstream() { }
352 strstreambuf*
353 istrstream::rdbuf() const throw ()
354 { return const_cast<strstreambuf*>(&_M_buf); }
356 char*
357 istrstream::str() throw ()
358 { return _M_buf.str(); }
360 ostrstream::ostrstream()
361 : basic_ios<char>(), basic_ostream<char>(0), _M_buf()
362 { basic_ios<char>::init(&_M_buf); }
364 ostrstream::ostrstream(char* s, int n, ios_base::openmode mode)
365 : basic_ios<char>(), basic_ostream<char>(0),
366 _M_buf(s, n, mode & ios_base::app ? s + strlen(s) : s)
367 { basic_ios<char>::init(&_M_buf); }
369 ostrstream::~ostrstream() {}
371 strstreambuf*
372 ostrstream::rdbuf() const throw ()
373 { return const_cast<strstreambuf*>(&_M_buf); }
375 void
376 ostrstream::freeze(bool freezeflag) throw ()
377 { _M_buf.freeze(freezeflag); }
379 char*
380 ostrstream::str() throw ()
381 { return _M_buf.str(); }
383 int
384 ostrstream::pcount() const throw ()
385 { return _M_buf.pcount(); }
387 strstream::strstream()
388 : basic_ios<char>(), basic_iostream<char>(0), _M_buf()
389 { basic_ios<char>::init(&_M_buf); }
391 strstream::strstream(char* s, int n, ios_base::openmode mode)
392 : basic_ios<char>(), basic_iostream<char>(0),
393 _M_buf(s, n, mode & ios_base::app ? s + strlen(s) : s)
394 { basic_ios<char>::init(&_M_buf); }
396 strstream::~strstream() { }
398 strstreambuf*
399 strstream::rdbuf() const throw ()
400 { return const_cast<strstreambuf*>(&_M_buf); }
402 void
403 strstream::freeze(bool freezeflag) throw ()
404 { _M_buf.freeze(freezeflag); }
406 int
407 strstream::pcount() const throw ()
408 { return _M_buf.pcount(); }
410 char*
411 strstream::str() throw ()
412 { return _M_buf.str(); }
414 _GLIBCXX_END_NAMESPACE