In gcc/: 2011-04-12 Nicola Pero <nicola.pero@meta-innovation.com>
[official-gcc.git] / libstdc++-v3 / src / strstream.cc
blobc76fc60e0ac57bbfe251feac1464eb3d5f095fa6
1 // strstream definitions -*- C++ -*-
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.
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.
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.
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/>.
27 * Copyright (c) 1998
28 * Silicon Graphics Computer Systems, Inc.
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.
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.
45 #include <strstream>
46 #include <algorithm>
47 #include <new>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <limits.h>
52 namespace std _GLIBCXX_VISIBILITY(default)
54 _GLIBCXX_BEGIN_NAMESPACE_VERSION
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)
60 streamsize n = std::max(initial_capacity, streamsize(16));
62 char* buf = _M_alloc(n);
63 if (buf)
65 setp(buf, buf + n);
66 setg(buf, buf, buf);
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)
74 streamsize n = 16;
76 char* buf = _M_alloc(n);
77 if (buf)
79 setp(buf, buf + n);
80 setg(buf, buf, buf);
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); }
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); }
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); }
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); }
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); }
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); }
115 strstreambuf::~strstreambuf()
117 if (_M_dynamic && !_M_frozen)
118 _M_free(eback());
121 void
122 strstreambuf::freeze(bool frozenflag) throw ()
124 if (_M_dynamic)
125 _M_frozen = frozenflag;
128 char*
129 strstreambuf::str() throw ()
131 freeze(true);
132 return eback();
135 int
136 strstreambuf::pcount() const throw ()
137 { return pptr() ? pptr() - pbase() : 0; }
139 strstreambuf::int_type
140 strstreambuf::overflow(int_type c)
142 if (c == traits_type::eof())
143 return traits_type::not_eof(c);
145 // Try to expand the buffer.
146 if (pptr() == epptr() && _M_dynamic && !_M_frozen && !_M_constant)
148 ptrdiff_t old_size = epptr() - pbase();
149 ptrdiff_t new_size = std::max(ptrdiff_t(2 * old_size), ptrdiff_t(1));
151 char* buf = _M_alloc(new_size);
152 if (buf)
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)
160 reposition_get = true;
161 old_get_offset = gptr() - eback();
164 setp(buf, buf + new_size);
165 __safe_pbump(old_size);
167 if (reposition_get)
168 setg(buf, buf + old_get_offset, buf +
169 std::max(old_get_offset, old_size));
171 _M_free(old_buffer);
175 if (pptr() != epptr())
177 *pptr() = c;
178 pbump(1);
179 return c;
181 else
182 return traits_type::eof();
185 strstreambuf::int_type
186 strstreambuf::pbackfail(int_type c)
188 if (gptr() != eback())
190 if (c == _Traits::eof())
192 gbump(-1);
193 return _Traits::not_eof(c);
195 else if (c == _Traits::to_int_type(gptr()[-1]))
196 { // KLUDGE
197 gbump(-1);
198 return c;
200 else if (!_M_constant)
202 gbump(-1);
203 *gptr() = c;
204 return c;
207 return _Traits::eof();
210 strstreambuf::int_type
211 strstreambuf::underflow()
213 if (gptr() == egptr() && pptr() && pptr() > egptr())
214 setg(eback(), gptr(), pptr());
216 if (gptr() != egptr())
217 return (unsigned char) *gptr();
218 else
219 return _Traits::eof();
222 basic_streambuf<char, char_traits<char> >*
223 strstreambuf::setbuf(char*, streamsize)
224 { return this; }
226 strstreambuf::pos_type
227 strstreambuf::seekoff(off_type off, ios_base::seekdir dir,
228 ios_base::openmode mode)
230 bool do_get = false;
231 bool do_put = false;
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;
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));
247 char* seeklow = eback();
248 char* seekhigh = epptr() ? epptr() : egptr();
250 off_type newoff;
251 switch (dir)
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));
266 off += newoff;
267 if (off < 0 || off > seekhigh - seeklow)
268 return pos_type(off_type(-1));
270 if (do_put)
272 if (seeklow + off < pbase())
274 setp(seeklow, epptr());
275 __safe_pbump(off);
277 else
279 setp(pbase(), epptr());
280 __safe_pbump(off - (pbase() - seeklow));
283 if (do_get)
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());
292 return pos_type(newoff);
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); }
299 char*
300 strstreambuf::_M_alloc(size_t n)
302 if (_M_alloc_fun)
303 return static_cast<char*>(_M_alloc_fun(n));
304 else
305 return new char[n];
308 void
309 strstreambuf::_M_free(char* p)
311 if (p)
313 if (_M_free_fun)
314 _M_free_fun(p);
315 else
316 delete[] p;
320 void
321 strstreambuf::_M_setup(char* get, char* put, streamsize n) throw ()
323 if (get)
325 size_t N = n > 0 ? size_t(n) : n == 0 ? strlen(get) : size_t(INT_MAX);
327 if (put)
329 setg(get, get, put);
330 setp(put, put + N);
332 else
333 setg(get, get, get + N);
337 istrstream::istrstream(char* s)
338 : basic_ios<char>(), basic_istream<char>(0), _M_buf(s, 0)
339 { basic_ios<char>::init(&_M_buf); }
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); }
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); }
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); }
353 istrstream::~istrstream() { }
355 strstreambuf*
356 istrstream::rdbuf() const throw ()
357 { return const_cast<strstreambuf*>(&_M_buf); }
359 char*
360 istrstream::str() throw ()
361 { return _M_buf.str(); }
363 ostrstream::ostrstream()
364 : basic_ios<char>(), basic_ostream<char>(0), _M_buf()
365 { basic_ios<char>::init(&_M_buf); }
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); }
372 ostrstream::~ostrstream() {}
374 strstreambuf*
375 ostrstream::rdbuf() const throw ()
376 { return const_cast<strstreambuf*>(&_M_buf); }
378 void
379 ostrstream::freeze(bool freezeflag) throw ()
380 { _M_buf.freeze(freezeflag); }
382 char*
383 ostrstream::str() throw ()
384 { return _M_buf.str(); }
386 int
387 ostrstream::pcount() const throw ()
388 { return _M_buf.pcount(); }
390 strstream::strstream()
391 : basic_ios<char>(), basic_iostream<char>(0), _M_buf()
392 { basic_ios<char>::init(&_M_buf); }
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); }
399 strstream::~strstream() { }
401 strstreambuf*
402 strstream::rdbuf() const throw ()
403 { return const_cast<strstreambuf*>(&_M_buf); }
405 void
406 strstream::freeze(bool freezeflag) throw ()
407 { _M_buf.freeze(freezeflag); }
409 int
410 strstream::pcount() const throw ()
411 { return _M_buf.pcount(); }
413 char*
414 strstream::str() throw ()
415 { return _M_buf.str(); }
417 _GLIBCXX_END_NAMESPACE_VERSION
418 } // namespace