|
|
1.1 root 1: /* This is part of libio/iostream, providing -*- C++ -*- input/output.
2: Copyright (C) 1993 Free Software Foundation
3:
4: This file is part of the GNU IO Library. This library is free
5: software; you can redistribute it and/or modify it under the
6: terms of the GNU General Public License as published by the
7: Free Software Foundation; either version 2, or (at your option)
8: any later version.
9:
10: This library is distributed in the hope that it will be useful,
11: but WITHOUT ANY WARRANTY; without even the implied warranty of
12: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13: GNU General Public License for more details.
14:
15: You should have received a copy of the GNU General Public License
16: along with GNU CC; see the file COPYING. If not, write to
17: the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
18:
19: As a special exception, if you link this library with files
20: compiled with a GNU compiler to produce an executable, this does not cause
21: the resulting executable to be covered by the GNU General Public License.
22: This exception does not however invalidate any other reasons why
23: the executable file might be covered by the GNU General Public License. */
24:
25: /* Written by Per Bothner ([email protected]). */
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:
35: class strstreambuf : public streambuf
36: {
37: struct _IO_str_fields _s;
38:
39: void init_dynamic(_IO_alloc_type alloc, _IO_free_type free,
40: int initial_size = 128);
41: void init_static(char *ptr, int size, char *pstart);
42: void init_readonly(const char *ptr, int size);
43: protected:
44: int is_static() const { return _s._allocate_buffer == (_IO_alloc_type)0; }
45: virtual int overflow(int = EOF);
46: virtual int underflow();
47: virtual int pbackfail(int c);
48: public:
49: virtual ~strstreambuf();
50: strstreambuf() { init_dynamic(0, 0); }
51: strstreambuf(int initial_size) { init_dynamic(0, 0, initial_size); }
52: strstreambuf(void *(*alloc)(_IO_size_t), void (*free)(void*))
53: { init_dynamic(alloc, free); }
54: strstreambuf(char *ptr, int size, char *pstart = NULL)
55: { init_static(ptr, size, pstart); }
56: strstreambuf(unsigned char *ptr, int size, unsigned char *pstart = NULL)
57: { init_static((char*)ptr, size, (char*)pstart); }
58: strstreambuf(const char *ptr, int size)
59: { init_readonly(ptr, size); }
60: strstreambuf(const unsigned char *ptr, int size)
61: { init_readonly((const char*)ptr, size); }
62: strstreambuf(signed char *ptr, int size, signed char *pstart = NULL)
63: { init_static((char*)ptr, size, (char*)pstart); }
64: strstreambuf(const signed char *ptr, int size)
65: { init_readonly((const char*)ptr, size); }
66: // Note: frozen() is always true if is_static().
67: int frozen() { return _flags & _IO_USER_BUF ? 1 : 0; }
68: void freeze(int n=1)
69: { if (!is_static())
70: { if (n) _flags |= _IO_USER_BUF; else _flags &= ~_IO_USER_BUF; } }
71: _IO_ssize_t pcount();
72: char *str();
73: virtual streampos seekoff(streamoff, _seek_dir, int mode=ios::in|ios::out);
74: };
75:
76: class strstreambase : virtual public ios {
77: public:
78: strstreambuf* rdbuf() { return (strstreambuf*)ios::rdbuf(); }
79: protected:
80: strstreambase() { }
81: strstreambase(char *cp, int n, int mode=ios::out);
82: };
83:
84: class istrstream : public strstreambase, public istream {
85: public:
86: istrstream(const char*, int=0);
87: };
88:
89: class ostrstream : public strstreambase, public ostream {
90: public:
91: ostrstream();
92: ostrstream(char *cp, int n, int mode=ios::out) :strstreambase(cp,n,mode){}
93: _IO_ssize_t pcount() { return ((strstreambuf*)_strbuf)->pcount(); }
94: char *str() { return ((strstreambuf*)_strbuf)->str(); }
95: void freeze(int n = 1) { ((strstreambuf*)_strbuf)->freeze(n); }
96: int frozen() { return ((strstreambuf*)_strbuf)->frozen(); }
97: };
98:
99: class strstream : public strstreambase, public iostream {
100: public:
101: strstream() : strstreambase() { init(new strstreambuf()); }
102: strstream(char *cp, int n, int mode=ios::out) :strstreambase(cp,n,mode){}
103: _IO_ssize_t pcount() { return ((strstreambuf*)_strbuf)->pcount(); }
104: char *str() { return ((strstreambuf*)_strbuf)->str(); }
105: void freeze(int n = 1) { ((strstreambuf*)_strbuf)->freeze(n); }
106: int frozen() { return ((strstreambuf*)_strbuf)->frozen(); }
107: };
108:
109: #endif /*!__STRSTREAM_H*/
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.