// This is part of the iostream library, providing input/output for C++.
// Copyright (C) 1991 Per Bothner.
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Library General Public License for more details.
// You should have received a copy of the GNU Library General Public
// License along with this library; if not, write to the Free
// Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
static void* default_alloc(_G_size_t size)
return (void*)new char[size];
static void default_free(void* ptr)
istrstream::istrstream(const char *cp, int n)
init(new strstreambuf(cp, n));
init(new strstreambuf());
strstreambase::strstreambase(char *cp, int n, int mode)
if (mode == ios::app || mode == ios::ate)
pstart = cp + strlen(cp);
init(new strstreambuf(cp, n, pstart));
char *strstreambuf::str()
_G_size_t strstreambuf::pcount()
_G_size_t put_len = pptr() - pbase();
if (put_len < _len) put_len = _len;
int strstreambuf::overflow(int c /* = EOF */)
const int flush_only = c == EOF;
if (_flags & _S_NO_WRITES)
return flush_only ? 0 : EOF;
size_t pos = pptr() - pbase();
size_t get_pos = gptr() - pbase();
if (pos > _len) _len = pos;
if (pos >= blen() + flush_only) {
size_t new_size = 2 * blen();
if (frozen()) /* not allowed to enlarge */
new_buf = (char*)(*_allocate_buffer)(new_size);
memcpy(new_buf, base(), blen());
if (lenp == &_len) /* use '\0'-filling */
memset(new_buf + pos, 0, blen() - pos);
_base = NULL; // So setb() won't try to delete _base.
setb(new_buf, new_buf + new_size, 1);
setg(base(), base() + get_pos, base() + _len);
*pptr() = (unsigned char) c;
int strstreambuf::underflow()
size_t ppos = pptr() - pbase();
if (ppos > _len) _len = ppos;
setg(base(), gptr(), base() + _len);
void strstreambuf::init_dynamic(_alloc_type alloc, _free_type free,
_allocate_buffer = alloc ? alloc : default_alloc;
_free_buffer = free ? free : default_free;
char * buf = (char*)(*_allocate_buffer)(initial_size);
setb(buf, buf + initial_size, 1);
setp(buf, buf + initial_size);
void strstreambuf::init_static(char *ptr, int size, char *pstart)
// If size is negative 'the characters are assumed to
// continue indefinitely.' This is kind of messy ...
// Try increasing powers of 2, as long as we don't wrap around.
// This can lose in pathological cases (ptr near the end
// of the address space). A better solution might be to
// adjust the size on underflow/overflow. FIXME.
for (int s; s = 2*size, s > 0 && ptr + s > ptr && s < 0x4000000L; )
// The following semi-portable kludge assumes that
// sizeof(unsigned long) == sizeof(char*). Hence,
// (unsigned long)(-1) should be the largest possible address.
unsigned long highest = (unsigned long)(-1);
// Pointers are signed on some brain-damaged systems, in
// which case we divide by two to get the maximum signed address.
if ((char*)highest < ptr)
size = (char*)highest - ptr;
void strstreambuf::init_static (const char *ptr, int size)
init_static((char*)ptr, size, NULL);
strstreambuf::~strstreambuf()
if (_base && !(_flags & _S_USER_BUF))
streampos strstreambuf::seekoff(streamoff off, _seek_dir dir,
int mode /*=ios::in|ios::out*/)
size_t cur_size = pcount();
// Move the get pointer, if requested.
default: /*case ios::beg: */
if (off < 0 || (size_t)off > cur_size)
setg(base(), base() + off, base() + cur_size);
// Move the put pointer, if requested.
default: /*case ios::beg: */
if (off < 0 || (size_t)off > cur_size)
pbump(base() + off - pptr());
int strstreambuf::pbackfail(int c)
if ((_flags & _S_NO_WRITES) && c != EOF)
return backupbuf::pbackfail(c);