vfs: check userland buffers before reading them.
[haiku.git] / headers / cpp / strstream.h
blob967e5b0b76ac6623bb1f466b99efcd4c6bb5ac90
1 /* This is part of libio/iostream, providing -*- C++ -*- input/output.
2 Copyright (C) 1993, 1999 Free Software Foundation
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.
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.
15 You should have received a copy of the GNU General Public License
16 along with this library; see the file COPYING. If not, write to the Free
17 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
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. */
25 /* Written by Per Bothner (bothner@cygnus.com). */
27 #ifndef __STRSTREAM_H
28 #define __STRSTREAM_H
29 #ifdef __GNUG__
30 #pragma interface
31 #endif
32 #include <iostream.h>
33 #include <strfile.h>
35 extern "C++" {
36 class strstreambuf : public streambuf
38 struct _IO_str_fields _s;
39 friend class istrstream;
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);
77 class 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);
87 class istrstream : public strstreambase, public istream {
88 public:
89 istrstream(const char*, int=0);
92 class 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(); }
102 class 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(); }
111 } // extern "C++"
113 #endif /*!__STRSTREAM_H*/