* gnu/regexp/CharIndexedReader.java: Removed.
[official-gcc.git] / libstdc++-v3 / src / strstream.cc
blob832a19c461cac275a14561aac0a5d3c7084bbc2f
1 // strstream definitions -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003 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 2, 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 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // USA.
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
31 * Copyright (c) 1998
32 * Silicon Graphics Computer Systems, Inc.
34 * Permission to use, copy, modify, distribute and sell this software
35 * and its documentation for any purpose is hereby granted without fee,
36 * provided that the above copyright notice appear in all copies and
37 * that both that copyright notice and this permission notice appear
38 * in supporting documentation. Silicon Graphics makes no
39 * representations about the suitability of this software for any
40 * purpose. It is provided "as is" without express or implied warranty.
43 // Implementation of the classes in header <strstream>.
44 // WARNING: The classes defined in <strstream> are DEPRECATED. This
45 // header is defined in section D.7.1 of the C++ standard, and it
46 // MAY BE REMOVED in a future standard revision. You should use the
47 // header <sstream> instead.
49 #include <strstream>
50 #include <algorithm>
51 #include <new>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <limits.h>
56 namespace std
58 strstreambuf::strstreambuf(streamsize initial_capacity)
59 : _Base(), _M_alloc_fun(0), _M_free_fun(0), _M_dynamic(true),
60 _M_frozen(false), _M_constant(false)
62 streamsize n = std::max(initial_capacity, streamsize(16));
64 char* buf = _M_alloc(n);
65 if (buf)
67 setp(buf, buf + n);
68 setg(buf, buf, buf);
72 strstreambuf::strstreambuf(void* (*alloc_f)(size_t), void (*free_f)(void*))
73 : _Base(), _M_alloc_fun(alloc_f), _M_free_fun(free_f), _M_dynamic(true),
74 _M_frozen(false), _M_constant(false)
76 streamsize n = 16;
78 char* buf = _M_alloc(n);
79 if (buf)
81 setp(buf, buf + n);
82 setg(buf, buf, buf);
86 strstreambuf::strstreambuf(char* get, streamsize n, char* put)
87 : _Base(), _M_alloc_fun(0), _M_free_fun(0), _M_dynamic(false),
88 _M_frozen(false), _M_constant(false)
89 { _M_setup(get, put, n); }
91 strstreambuf::strstreambuf(signed char* get, streamsize n, signed char* put)
92 : _Base(), _M_alloc_fun(0), _M_free_fun(0), _M_dynamic(false),
93 _M_frozen(false), _M_constant(false)
94 { _M_setup(reinterpret_cast<char*>(get), reinterpret_cast<char*>(put), n); }
96 strstreambuf::strstreambuf(unsigned char* get, streamsize n,
97 unsigned char* put)
98 : _Base(), _M_alloc_fun(0), _M_free_fun(0), _M_dynamic(false),
99 _M_frozen(false), _M_constant(false)
100 { _M_setup(reinterpret_cast<char*>(get), reinterpret_cast<char*>(put), n); }
102 strstreambuf::strstreambuf(const char* get, streamsize n)
103 : _Base(), _M_alloc_fun(0), _M_free_fun(0), _M_dynamic(false),
104 _M_frozen(false), _M_constant(true)
105 { _M_setup(const_cast<char*>(get), 0, n); }
107 strstreambuf::strstreambuf(const signed char* get, streamsize n)
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<signed char*>(get)), 0, n); }
112 strstreambuf::strstreambuf(const unsigned char* get, streamsize n)
113 : _Base(), _M_alloc_fun(0), _M_free_fun(0), _M_dynamic(false),
114 _M_frozen(false), _M_constant(true)
115 { _M_setup(reinterpret_cast<char*>(const_cast<unsigned char*>(get)), 0, n); }
117 strstreambuf::~strstreambuf()
119 if (_M_dynamic && !_M_frozen)
120 _M_free(eback());
123 void
124 strstreambuf::freeze(bool frozenflag)
126 if (_M_dynamic)
127 _M_frozen = frozenflag;
130 char*
131 strstreambuf::str()
133 freeze(true);
134 return eback();
137 int
138 strstreambuf::pcount() const
139 { return pptr() ? pptr() - pbase() : 0; }
141 strstreambuf::int_type
142 strstreambuf::overflow(int_type c)
144 if (c == traits_type::eof())
145 return traits_type::not_eof(c);
147 // Try to expand the buffer.
148 if (pptr() == epptr() && _M_dynamic && !_M_frozen && !_M_constant)
150 ptrdiff_t old_size = epptr() - pbase();
151 ptrdiff_t new_size = std::max(ptrdiff_t(2 * old_size), ptrdiff_t(1));
153 char* buf = _M_alloc(new_size);
154 if (buf)
156 memcpy(buf, pbase(), old_size);
157 char* old_buffer = pbase();
158 bool reposition_get = false;
159 ptrdiff_t old_get_offset;
160 if (gptr() != 0)
162 reposition_get = true;
163 old_get_offset = gptr() - eback();
166 setp(buf, buf + new_size);
167 pbump(old_size);
169 if (reposition_get)
170 setg(buf, buf + old_get_offset, buf +
171 std::max(old_get_offset, old_size));
173 _M_free(old_buffer);
177 if (pptr() != epptr())
179 *pptr() = c;
180 pbump(1);
181 return c;
183 else
184 return traits_type::eof();
187 strstreambuf::int_type
188 strstreambuf::pbackfail(int_type c)
190 if (gptr() != eback())
192 if (c == _Traits::eof())
194 gbump(-1);
195 return _Traits::not_eof(c);
197 else if (c == _Traits::to_int_type(gptr()[-1]))
198 { // KLUDGE
199 gbump(-1);
200 return c;
202 else if (!_M_constant)
204 gbump(-1);
205 *gptr() = c;
206 return c;
209 return _Traits::eof();
212 strstreambuf::int_type
213 strstreambuf::underflow()
215 if (gptr() == egptr() && pptr() && pptr() > egptr())
216 setg(eback(), gptr(), pptr());
218 if (gptr() != egptr())
219 return (unsigned char) *gptr();
220 else
221 return _Traits::eof();
224 basic_streambuf<char, char_traits<char> >*
225 strstreambuf::setbuf(char*, streamsize)
226 { return this; }
228 strstreambuf::pos_type
229 strstreambuf::seekoff(off_type off, ios_base::seekdir dir,
230 ios_base::openmode mode)
232 bool do_get = false;
233 bool do_put = false;
235 if ((mode & (ios_base::in | ios_base::out))
236 == (ios_base::in | ios_base::out) &&
237 (dir == ios_base::beg || dir == ios_base::end))
238 do_get = do_put = true;
239 else if (mode & ios_base::in)
240 do_get = true;
241 else if (mode & ios_base::out)
242 do_put = true;
244 // !gptr() is here because, according to D.7.1 paragraph 4, the seekable
245 // area is undefined if there is no get area.
246 if ((!do_get && !do_put) || (do_put && !pptr()) || !gptr())
247 return pos_type(off_type(-1));
249 char* seeklow = eback();
250 char* seekhigh = epptr() ? epptr() : egptr();
252 off_type newoff;
253 switch (dir)
255 case ios_base::beg:
256 newoff = 0;
257 break;
258 case ios_base::end:
259 newoff = seekhigh - seeklow;
260 break;
261 case ios_base::cur:
262 newoff = do_put ? pptr() - seeklow : gptr() - seeklow;
263 break;
264 default:
265 return pos_type(off_type(-1));
268 off += newoff;
269 if (off < 0 || off > seekhigh - seeklow)
270 return pos_type(off_type(-1));
272 if (do_put)
274 if (seeklow + off < pbase())
276 setp(seeklow, epptr());
277 pbump(off);
279 else
281 setp(pbase(), epptr());
282 pbump(off - (pbase() - seeklow));
285 if (do_get)
287 if (off <= egptr() - seeklow)
288 setg(seeklow, seeklow + off, egptr());
289 else if (off <= pptr() - seeklow)
290 setg(seeklow, seeklow + off, pptr());
291 else
292 setg(seeklow, seeklow + off, epptr());
294 return pos_type(newoff);
297 strstreambuf::pos_type
298 strstreambuf::seekpos(pos_type pos, ios_base::openmode mode)
299 { return seekoff(pos - pos_type(off_type(0)), ios_base::beg, mode); }
301 char*
302 strstreambuf::_M_alloc(size_t n)
304 if (_M_alloc_fun)
305 return static_cast<char*>(_M_alloc_fun(n));
306 else
307 return new char[n];
310 void
311 strstreambuf::_M_free(char* p)
313 if (p)
314 if (_M_free_fun)
315 _M_free_fun(p);
316 else
317 delete[] p;
320 void
321 strstreambuf::_M_setup(char* get, char* put, streamsize n)
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
357 { return const_cast<strstreambuf*>(&_M_buf); }
359 char*
360 istrstream::str()
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
376 { return const_cast<strstreambuf*>(&_M_buf); }
378 void
379 ostrstream::freeze(bool freezeflag)
380 { _M_buf.freeze(freezeflag); }
382 char*
383 ostrstream::str()
384 { return _M_buf.str(); }
386 int
387 ostrstream::pcount() const
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
403 { return const_cast<strstreambuf*>(&_M_buf); }
405 void
406 strstream::freeze(bool freezeflag)
407 { _M_buf.freeze(freezeflag); }
409 int
410 strstream::pcount() const
411 { return _M_buf.pcount(); }
413 char*
414 strstream::str()
415 { return _M_buf.str(); }
416 } // namespace std