1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: "wxWindows stream" base classes
4 // Author: Guilhem Lavaux
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_WXSTREAM_H__
13 #define _WX_WXSTREAM_H__
24 #include "wx/object.h"
25 #include "wx/string.h"
26 #include "wx/filefn.h" // for off_t, wxInvalidOffset and wxSeekMode
28 class WXDLLEXPORT wxStreamBase
;
29 class WXDLLEXPORT wxInputStream
;
30 class WXDLLEXPORT wxOutputStream
;
32 typedef wxInputStream
& (*__wxInputManip
)(wxInputStream
&);
33 typedef wxOutputStream
& (*__wxOutputManip
)(wxOutputStream
&);
35 WXDLLEXPORT wxOutputStream
& wxEndL(wxOutputStream
& o_stream
);
37 // ---------------------------------------------------------------------------
38 // wxStream: base classes
39 // ---------------------------------------------------------------------------
41 #define wxStream_NOERROR wxSTR_NOERROR
42 #define wxStream_EOF wxSTR_EOF
43 #define wxStream_WRITE_ERR wxSTR_WRITE_ERROR
44 #define wxStream_READ_ERR wxSTR_READ_ERROR
53 class WXDLLEXPORT wxStreamBase
{
56 virtual ~wxStreamBase();
58 wxStreamError
LastError() const { return m_lasterror
; }
59 virtual size_t StreamSize() const { return ~((size_t)0); }
63 virtual size_t OnSysRead(void *buffer
, size_t bufsize
);
64 virtual size_t OnSysWrite(const void *buffer
, size_t bufsize
);
65 virtual off_t
OnSysSeek(off_t seek
, wxSeekMode mode
);
66 virtual off_t
OnSysTell() const;
69 friend class wxStreamBuffer
;
72 wxStreamError m_lasterror
;
75 class WXDLLEXPORT wxInputStream
: public wxStreamBase
{
78 virtual ~wxInputStream();
83 virtual wxInputStream
& Read(void *buffer
, size_t size
);
84 wxInputStream
& Read(wxOutputStream
& stream_out
);
87 virtual off_t
SeekI(off_t pos
, wxSeekMode mode
= wxFromStart
);
88 virtual off_t
TellI() const;
91 virtual size_t LastRead() { return wxStreamBase::m_lastcount
; }
94 size_t Ungetch(void *buffer
, size_t size
);
98 wxInputStream
& operator>>(wxOutputStream
& out
) { return Read(out
); }
100 wxInputStream
& operator>>(wxObject
*& obj
);
102 wxInputStream
& operator>>( __wxInputManip func
) { return func(*this); }
110 char *AllocSpaceWBack(size_t needed_size
);
111 size_t GetWBack(char *buf
, size_t bsize
);
115 class WXDLLEXPORT wxOutputStream
: public wxStreamBase
{
118 virtual ~wxOutputStream();
120 virtual wxOutputStream
& Write(const void *buffer
, size_t size
);
121 wxOutputStream
& Write(wxInputStream
& stream_in
);
123 virtual off_t
SeekO(off_t pos
, wxSeekMode mode
= wxFromStart
);
124 virtual off_t
TellO() const;
126 virtual size_t LastWrite() const { return wxStreamBase::m_lastcount
; }
130 wxOutputStream
& operator<<(wxInputStream
& out
) { return Write(out
); }
132 wxOutputStream
& operator<<(wxObject
& obj
);
134 wxOutputStream
& operator<<( __wxOutputManip func
) { return func(*this); }
137 // ---------------------------------------------------------------------------
139 // ---------------------------------------------------------------------------
141 class WXDLLEXPORT wxFilterInputStream
: public wxInputStream
{
143 wxFilterInputStream();
144 wxFilterInputStream(wxInputStream
& stream
);
145 ~wxFilterInputStream();
147 char Peek() { return m_parent_i_stream
->Peek(); }
149 wxStreamError
LastError() const { return m_parent_i_stream
->LastError(); }
150 size_t StreamSize() const { return m_parent_i_stream
->StreamSize(); }
153 wxInputStream
*m_parent_i_stream
;
156 class WXDLLEXPORT wxFilterOutputStream
: public wxOutputStream
{
158 wxFilterOutputStream();
159 wxFilterOutputStream(wxOutputStream
& stream
);
160 ~wxFilterOutputStream();
162 wxStreamError
LastError() const { return m_parent_o_stream
->LastError(); }
163 size_t StreamSize() const { return m_parent_o_stream
->StreamSize(); }
166 wxOutputStream
*m_parent_o_stream
;
169 // ---------------------------------------------------------------------------
171 // ---------------------------------------------------------------------------
173 class WXDLLEXPORT wxStreamBuffer
{
176 read
= 0, write
, read_write
182 wxStreamBuffer(wxStreamBase
& stream
, BufMode mode
);
183 wxStreamBuffer(BufMode mode
);
184 wxStreamBuffer(const wxStreamBuffer
& buf
);
190 size_t Read(void *buffer
, size_t size
);
191 size_t Read(wxStreamBuffer
*buf
);
192 size_t Write(const void *buffer
, size_t size
);
193 size_t Write(wxStreamBuffer
*buf
);
196 void PutChar(char c
);
198 off_t
Seek(off_t pos
, wxSeekMode mode
);
204 void SetBufferIO(char *buffer_start
, char *buffer_end
);
205 void SetBufferIO(size_t bufsize
);
206 char *GetBufferStart() const { return m_buffer_start
; }
207 char *GetBufferEnd() const { return m_buffer_end
; }
208 char *GetBufferPos() const { return m_buffer_pos
; }
209 off_t
GetIntPosition() const { return m_buffer_pos
-m_buffer_start
; }
210 void SetIntPosition(off_t pos
) { m_buffer_pos
= m_buffer_start
+pos
; }
211 size_t GetLastAccess() const { return m_buffer_end
-m_buffer_start
; }
213 void Fixed(bool fixed
) { m_fixed
= fixed
; }
214 void Flushable(bool f
) { m_flushable
= f
; }
218 size_t GetDataLeft();
223 wxStreamBase
*Stream() { return m_stream
; }
226 void GetFromBuffer(void *buffer
, size_t size
);
227 void PutToBuffer(const void *buffer
, size_t size
);
230 char *m_buffer_start
, *m_buffer_end
, *m_buffer_pos
;
231 size_t m_buffer_size
;
234 size_t m_wbacksize
, m_wbackcur
;
236 bool m_fixed
, m_flushable
;
238 wxStreamBase
*m_stream
;
240 bool m_destroybuf
, m_destroystream
;
243 // ---------------------------------------------------------------------------
245 // ---------------------------------------------------------------------------
247 class wxBufferedInputStream
: public wxFilterInputStream
{
249 wxBufferedInputStream(wxInputStream
& stream
);
250 ~wxBufferedInputStream();
252 wxInputStream
& Read(void *buffer
, size_t size
);
254 // Position functions
255 off_t
SeekI(off_t pos
, wxSeekMode mode
= wxFromStart
);
258 wxStreamBuffer
*InputStreamBuffer() const { return m_i_streambuf
; }
261 size_t OnSysRead(void *buffer
, size_t bufsize
);
262 off_t
OnSysSeek(off_t seek
, wxSeekMode mode
);
263 off_t
OnSysTell() const;
266 wxStreamBuffer
*m_i_streambuf
;
269 class wxBufferedOutputStream
: public wxFilterOutputStream
{
271 wxBufferedOutputStream(wxOutputStream
& stream
);
272 ~wxBufferedOutputStream();
274 wxOutputStream
& Write(const void *buffer
, size_t size
);
276 // Position functions
277 off_t
SeekO(off_t pos
, wxSeekMode mode
= wxFromStart
);
282 wxStreamBuffer
*OutputStreamBuffer() const { return m_o_streambuf
; }
285 size_t OnSysWrite(const void *buffer
, size_t bufsize
);
286 off_t
OnSysSeek(off_t seek
, wxSeekMode mode
);
287 off_t
OnSysTell() const;
290 wxStreamBuffer
*m_o_streambuf
;