1/* This is part of libio/iostream, providing -*- C++ -*- input/output.
2Copyright (C) 1993 Free Software Foundation
3
4This file is part of the GNU IO Library.  This library is free
5software; you can redistribute it and/or modify it under the
6terms of the GNU General Public License as published by the
7Free Software Foundation; either version 2, or (at your option)
8any later version.
9
10This library is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this library; see the file COPYING.  If not, write to the Free
17Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19As a special exception, if you link this library with files
20compiled with a GNU compiler to produce an executable, this does not cause
21the resulting executable to be covered by the GNU General Public License.
22This exception does not however invalidate any other reasons why
23the executable file might be covered by the GNU General Public License. */
24
25/* Written by Per Bothner (bothner@cygnus.com). */
26
27#ifdef __GNUG__
28#pragma implementation
29#endif
30#include "iostreamP.h"
31#include "strstream.h"
32#include <string.h>
33
34static void* default_alloc(_IO_size_t size)
35{
36    return (void*)new char[size];
37}
38
39static void default_free(void* ptr)
40{
41    delete [] (char*)ptr;
42}
43
44istrstream::istrstream(const char *cp, int n)
45{
46  __my_sb.init_readonly (cp, n);
47}
48
49strstreambase::strstreambase(char *cp, int n, int mode)
50: __my_sb (cp, n,
51	   (mode == ios::app || mode == ios::ate) ? cp + strlen(cp) : cp)
52{
53  init (&__my_sb);
54}
55
56char *strstreambuf::str()
57{
58    freeze(1);
59    return base();
60}
61
62_IO_ssize_t strstreambuf::pcount () { return _IO_write_ptr - _IO_write_base; }
63
64int strstreambuf::overflow(int c /* = EOF */)
65{
66  return _IO_str_overflow (this, c);
67}
68
69int strstreambuf::underflow()
70{
71  return _IO_str_underflow(this);
72}
73
74
75void strstreambuf::init_dynamic(_IO_alloc_type alloc, _IO_free_type free,
76				int initial_size)
77
78{
79    _s._allocate_buffer = alloc ? alloc : default_alloc;
80    _s._free_buffer = free ? free : default_free;
81    if (initial_size > 0)
82      {
83	char * buf = (char*)(*_s._allocate_buffer)(initial_size);
84	setb(buf, buf + initial_size, 1);
85	setp(buf, buf + initial_size);
86	setg(buf, buf, buf);
87      }
88}
89
90void strstreambuf::init_static(char *ptr, int size, char *pstart)
91{
92  _IO_str_init_static (reinterpret_cast<_IO_strfile_*>(this), ptr, size, pstart);
93}
94
95void strstreambuf::init_readonly (const char *ptr, int size)
96{
97  _IO_str_init_readonly (reinterpret_cast<_IO_strfile_*>(this), ptr, size);
98}
99
100strstreambuf::~strstreambuf()
101{
102    if (_IO_buf_base && !(_flags & _IO_USER_BUF))
103        (_s._free_buffer)(_IO_buf_base);
104    _IO_buf_base = NULL;
105}
106
107streampos strstreambuf::seekoff(streamoff off, _seek_dir dir,
108					int mode /*=ios::in|ios::out*/)
109{
110  return _IO_str_seekoff (this, off, dir, mode);
111}
112
113int strstreambuf::pbackfail(int c)
114{
115  return _IO_str_pbackfail (this, c);
116}
117