1/* This is part of libio/iostream, providing -*- C++ -*- input/output.
2Copyright (C) 1993, 1999 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#ifndef __STRSTREAM_H
28#define __STRSTREAM_H
29#ifdef __GNUG__
30#pragma interface
31#endif
32#include <iostream.h>
33#include <strfile.h>
34
35extern "C++" {
36class strstreambuf : public streambuf
37{
38  struct _IO_str_fields _s;
39  friend class istrstream;
40
41    void init_dynamic(_IO_alloc_type alloc, _IO_free_type free,
42		      int initial_size = 0);
43    void init_static(char *ptr, int size, char *pstart);
44    void init_readonly(const char *ptr, int size);
45  protected:
46    virtual int overflow(int = EOF);
47    virtual int underflow();
48    virtual int pbackfail(int c);
49  public:
50    virtual ~strstreambuf();
51    strstreambuf() { init_dynamic(0, 0); }
52    strstreambuf(int initial_size) { init_dynamic(0, 0, initial_size); }
53    strstreambuf(void *(*__alloc)(_IO_size_t), void (*__free)(void*))
54	{ init_dynamic(__alloc, __free); }
55    strstreambuf(char *ptr, int size, char *pstart = NULL)
56	{ init_static(ptr, size, pstart); }
57    strstreambuf(unsigned char *ptr, int size, unsigned char *pstart = NULL)
58	{ init_static((char*)ptr, size, (char*)pstart); }
59    strstreambuf(const char *ptr, int size)
60	{ init_readonly(ptr, size); }
61    strstreambuf(const unsigned char *ptr, int size)
62	{ init_readonly((const char*)ptr, size); }
63    strstreambuf(signed char *ptr, int size, signed char *pstart = NULL)
64	{ init_static((char*)ptr, size, (char*)pstart); }
65    strstreambuf(const signed char *ptr, int size)
66	{ init_readonly((const char*)ptr, size); }
67    // Note: frozen() is always true if !_IO_STR_DYNAMIC(this).
68    int frozen() { return _flags & _IO_USER_BUF ? 1 : 0; }
69    void freeze(int n=1)
70	{ if (_IO_STR_DYNAMIC(this))
71	    { if (n) _flags |= _IO_USER_BUF; else _flags &= ~_IO_USER_BUF; } }
72    _IO_ssize_t pcount();
73    char *str();
74    virtual streampos seekoff(streamoff, _seek_dir, int mode=ios::in|ios::out);
75};
76
77class strstreambase : virtual public ios {
78  protected:
79    strstreambuf __my_sb;
80  public:
81    strstreambuf* rdbuf() { return &__my_sb; }
82  protected:
83    strstreambase() { init (&__my_sb); }
84    strstreambase(char *cp, int n, int mode=ios::out);
85};
86
87class istrstream : public strstreambase, public istream {
88  public:
89    istrstream(const char*, int=0);
90};
91
92class ostrstream : public strstreambase, public ostream {
93  public:
94    ostrstream() { }
95    ostrstream(char *cp, int n, int mode=ios::out) :strstreambase(cp,n,mode){}
96    _IO_ssize_t pcount() { return ((strstreambuf*)_strbuf)->pcount(); }
97    char *str() { return ((strstreambuf*)_strbuf)->str(); }
98    void freeze(int n = 1) { ((strstreambuf*)_strbuf)->freeze(n); }
99    int frozen() { return ((strstreambuf*)_strbuf)->frozen(); }
100};
101
102class strstream : public strstreambase, public iostream {
103  public:
104  strstream() { }
105    strstream(char *cp, int n, int mode=ios::out) :strstreambase(cp,n,mode){}
106    _IO_ssize_t pcount() { return ((strstreambuf*)_strbuf)->pcount(); }
107    char *str() { return ((strstreambuf*)_strbuf)->str(); }
108    void freeze(int n = 1) { ((strstreambuf*)_strbuf)->freeze(n); }
109    int frozen() { return ((strstreambuf*)_strbuf)->frozen(); }
110};
111} // extern "C++"
112
113#endif /*!__STRSTREAM_H*/
114