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 wxSTREAM_NOERROR
42 #define wxStream_EOF wxSTREAM_EOF
43 #define wxStream_WRITE_ERR wxSTREAM_WRITE_ERROR
44 #define wxStream_READ_ERR wxSTREAM_READ_ERROR
53 class WXDLLEXPORT wxStreamBase
{
56 virtual ~wxStreamBase();
58 bool operator!() const { return (LastError() != wxSTREAM_NOERROR
); }
59 wxStreamError
LastError() const { return m_lasterror
; }
60 virtual size_t GetSize() const { return ~((size_t)0); }
61 size_t StreamSize() const { return GetSize(); }
65 virtual size_t OnSysRead(void *buffer
, size_t bufsize
);
66 virtual size_t OnSysWrite(const void *buffer
, size_t bufsize
);
67 virtual off_t
OnSysSeek(off_t seek
, wxSeekMode mode
);
68 virtual off_t
OnSysTell() const;
71 friend class wxStreamBuffer
;
74 wxStreamError m_lasterror
;
77 class WXDLLEXPORT wxInputStream
: public wxStreamBase
{
80 virtual ~wxInputStream();
85 virtual wxInputStream
& Read(void *buffer
, size_t size
);
86 wxInputStream
& Read(wxOutputStream
& stream_out
);
89 virtual off_t
SeekI(off_t pos
, wxSeekMode mode
= wxFromStart
);
90 virtual off_t
TellI() const;
93 virtual size_t LastRead() { return wxStreamBase::m_lastcount
; }
96 size_t Ungetch(const void *buffer
, size_t size
);
100 wxInputStream
& operator>>(wxOutputStream
& out
) { return Read(out
); }
102 wxInputStream
& operator>>(wxObject
*& obj
);
104 wxInputStream
& operator>>( __wxInputManip func
) { return func(*this); }
112 char *AllocSpaceWBack(size_t needed_size
);
113 size_t GetWBack(char *buf
, size_t bsize
);
117 class WXDLLEXPORT wxOutputStream
: public wxStreamBase
{
120 virtual ~wxOutputStream();
122 virtual wxOutputStream
& Write(const void *buffer
, size_t size
);
123 wxOutputStream
& Write(wxInputStream
& stream_in
);
125 virtual off_t
SeekO(off_t pos
, wxSeekMode mode
= wxFromStart
);
126 virtual off_t
TellO() const;
128 virtual size_t LastWrite() const { return wxStreamBase::m_lastcount
; }
132 wxOutputStream
& operator<<(wxInputStream
& out
) { return Write(out
); }
134 wxOutputStream
& operator<<(wxObject
& obj
);
136 wxOutputStream
& operator<<( __wxOutputManip func
) { return func(*this); }
139 // ---------------------------------------------------------------------------
140 // A stream for measuring streamed output
141 // ---------------------------------------------------------------------------
143 class wxCountingOutputStream
: public wxOutputStream
{
145 wxCountingOutputStream();
147 size_t GetSize() const;
148 bool Ok() const { return TRUE
; }
152 size_t OnSysWrite(const void *buffer
, size_t size
);
153 off_t
OnSysSeek(off_t pos
, wxSeekMode mode
);
154 off_t
OnSysTell() const;
161 // ---------------------------------------------------------------------------
163 // ---------------------------------------------------------------------------
165 class WXDLLEXPORT wxFilterInputStream
: public wxInputStream
{
167 wxFilterInputStream();
168 wxFilterInputStream(wxInputStream
& stream
);
169 ~wxFilterInputStream();
171 char Peek() { return m_parent_i_stream
->Peek(); }
173 wxStreamError
LastError() const { return m_parent_i_stream
->LastError(); }
174 size_t GetSize() const { return m_parent_i_stream
->GetSize(); }
177 wxInputStream
*m_parent_i_stream
;
180 class WXDLLEXPORT wxFilterOutputStream
: public wxOutputStream
{
182 wxFilterOutputStream();
183 wxFilterOutputStream(wxOutputStream
& stream
);
184 ~wxFilterOutputStream();
186 wxStreamError
LastError() const { return m_parent_o_stream
->LastError(); }
187 size_t GetSize() const { return m_parent_o_stream
->GetSize(); }
190 wxOutputStream
*m_parent_o_stream
;
193 // ---------------------------------------------------------------------------
195 // ---------------------------------------------------------------------------
197 class WXDLLEXPORT wxStreamBuffer
{
200 read
= 0, write
, read_write
206 wxStreamBuffer(wxStreamBase
& stream
, BufMode mode
);
207 wxStreamBuffer(BufMode mode
);
208 wxStreamBuffer(const wxStreamBuffer
& buf
);
214 size_t Read(void *buffer
, size_t size
);
215 size_t Read(wxStreamBuffer
*buf
);
216 size_t Write(const void *buffer
, size_t size
);
217 size_t Write(wxStreamBuffer
*buf
);
220 void PutChar(char c
);
222 off_t
Seek(off_t pos
, wxSeekMode mode
);
228 void SetBufferIO(char *buffer_start
, char *buffer_end
);
229 void SetBufferIO(size_t bufsize
);
230 char *GetBufferStart() const { return m_buffer_start
; }
231 char *GetBufferEnd() const { return m_buffer_end
; }
232 char *GetBufferPos() const { return m_buffer_pos
; }
233 off_t
GetIntPosition() const { return m_buffer_pos
-m_buffer_start
; }
234 void SetIntPosition(off_t pos
) { m_buffer_pos
= m_buffer_start
+pos
; }
235 size_t GetLastAccess() const { return m_buffer_end
-m_buffer_start
; }
237 void Fixed(bool fixed
) { m_fixed
= fixed
; }
238 void Flushable(bool f
) { m_flushable
= f
; }
242 size_t GetDataLeft();
247 wxStreamBase
*Stream() { return m_stream
; }
250 void GetFromBuffer(void *buffer
, size_t size
);
251 void PutToBuffer(const void *buffer
, size_t size
);
254 char *m_buffer_start
, *m_buffer_end
, *m_buffer_pos
;
255 size_t m_buffer_size
;
258 size_t m_wbacksize
, m_wbackcur
;
260 bool m_fixed
, m_flushable
;
262 wxStreamBase
*m_stream
;
264 bool m_destroybuf
, m_destroystream
;
267 // ---------------------------------------------------------------------------
269 // ---------------------------------------------------------------------------
271 class wxBufferedInputStream
: public wxFilterInputStream
{
273 wxBufferedInputStream(wxInputStream
& stream
);
274 ~wxBufferedInputStream();
276 wxInputStream
& Read(void *buffer
, size_t size
);
278 // Position functions
279 off_t
SeekI(off_t pos
, wxSeekMode mode
= wxFromStart
);
282 wxStreamBuffer
*InputStreamBuffer() const { return m_i_streambuf
; }
285 size_t OnSysRead(void *buffer
, size_t bufsize
);
286 off_t
OnSysSeek(off_t seek
, wxSeekMode mode
);
287 off_t
OnSysTell() const;
290 wxStreamBuffer
*m_i_streambuf
;
293 class wxBufferedOutputStream
: public wxFilterOutputStream
{
295 wxBufferedOutputStream(wxOutputStream
& stream
);
296 ~wxBufferedOutputStream();
298 wxOutputStream
& Write(const void *buffer
, size_t size
);
300 // Position functions
301 off_t
SeekO(off_t pos
, wxSeekMode mode
= wxFromStart
);
306 wxStreamBuffer
*OutputStreamBuffer() const { return m_o_streambuf
; }
309 size_t OnSysWrite(const void *buffer
, size_t bufsize
);
310 off_t
OnSysSeek(off_t seek
, wxSeekMode mode
);
311 off_t
OnSysTell() const;
314 wxStreamBuffer
*m_o_streambuf
;