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
47 wxSTREAM_NO_ERROR
= 0,
48 wxSTREAM_NO_ERR
= wxSTREAM_NO_ERROR
,
49 wxSTREAM_NOERROR
= wxSTREAM_NO_ERROR
,
54 wxSTREAM_WRITE_ERR
= wxSTREAM_WRITE_ERROR
,
57 wxSTREAM_READ_ERR
= wxSTREAM_READ_ERROR
61 class WXDLLEXPORT wxStreamBase
65 virtual ~wxStreamBase();
67 bool operator!() const { return (LastError() != wxSTREAM_NOERROR
); }
68 wxStreamError
LastError() const { return m_lasterror
; }
69 virtual size_t GetSize() const { return ~((size_t)0); }
70 size_t StreamSize() const { return GetSize(); }
73 virtual size_t OnSysRead(void *buffer
, size_t bufsize
);
74 virtual size_t OnSysWrite(const void *buffer
, size_t bufsize
);
75 virtual off_t
OnSysSeek(off_t seek
, wxSeekMode mode
);
76 virtual off_t
OnSysTell() const;
78 friend class wxStreamBuffer
;
81 wxStreamError m_lasterror
;
84 class WXDLLEXPORT wxInputStream
: public wxStreamBase
88 virtual ~wxInputStream();
93 virtual wxInputStream
& Read(void *buffer
, size_t size
);
94 wxInputStream
& Read(wxOutputStream
& stream_out
);
97 virtual off_t
SeekI(off_t pos
, wxSeekMode mode
= wxFromStart
);
98 virtual off_t
TellI() const;
101 virtual size_t LastRead() { return wxStreamBase::m_lastcount
; }
104 size_t Ungetch(const void *buffer
, size_t size
);
105 bool Ungetch(char c
);
108 wxInputStream
& operator>>(wxOutputStream
& out
) { return Read(out
); }
110 wxInputStream
& operator>>(wxObject
*& obj
);
112 wxInputStream
& operator>>( __wxInputManip func
) { return func(*this); }
120 char *AllocSpaceWBack(size_t needed_size
);
121 size_t GetWBack(char *buf
, size_t bsize
);
124 class WXDLLEXPORT wxOutputStream
: public wxStreamBase
128 virtual ~wxOutputStream();
131 virtual wxOutputStream
& Write(const void *buffer
, size_t size
);
132 wxOutputStream
& Write(wxInputStream
& stream_in
);
134 virtual off_t
SeekO(off_t pos
, wxSeekMode mode
= wxFromStart
);
135 virtual off_t
TellO() const;
137 virtual size_t LastWrite() const { return wxStreamBase::m_lastcount
; }
141 wxOutputStream
& operator<<(wxInputStream
& out
) { return Write(out
); }
143 wxOutputStream
& operator<<(wxObject
& obj
);
145 wxOutputStream
& operator<<( __wxOutputManip func
) { return func(*this); }
148 // ---------------------------------------------------------------------------
149 // A stream for measuring streamed output
150 // ---------------------------------------------------------------------------
152 class WXDLLEXPORT wxCountingOutputStream
: public wxOutputStream
155 wxCountingOutputStream();
157 size_t GetSize() const;
158 bool Ok() const { return TRUE
; }
161 size_t OnSysWrite(const void *buffer
, size_t size
);
162 off_t
OnSysSeek(off_t pos
, wxSeekMode mode
);
163 off_t
OnSysTell() const;
168 // ---------------------------------------------------------------------------
170 // ---------------------------------------------------------------------------
172 class WXDLLEXPORT wxFilterInputStream
: public wxInputStream
175 wxFilterInputStream();
176 wxFilterInputStream(wxInputStream
& stream
);
177 ~wxFilterInputStream();
179 char Peek() { return m_parent_i_stream
->Peek(); }
181 size_t GetSize() const { return m_parent_i_stream
->GetSize(); }
184 wxInputStream
*m_parent_i_stream
;
187 class WXDLLEXPORT wxFilterOutputStream
: public wxOutputStream
190 wxFilterOutputStream();
191 wxFilterOutputStream(wxOutputStream
& stream
);
192 ~wxFilterOutputStream();
194 size_t GetSize() const { return m_parent_o_stream
->GetSize(); }
197 wxOutputStream
*m_parent_o_stream
;
200 // ---------------------------------------------------------------------------
202 // ---------------------------------------------------------------------------
204 class WXDLLEXPORT wxStreamBuffer
208 read
= 0, write
, read_write
211 wxStreamBuffer(wxStreamBase
& stream
, BufMode mode
);
212 wxStreamBuffer(BufMode mode
);
213 wxStreamBuffer(const wxStreamBuffer
& buf
);
217 size_t Read(void *buffer
, size_t size
);
218 size_t Read(wxStreamBuffer
*buf
);
219 size_t Write(const void *buffer
, size_t size
);
220 size_t Write(wxStreamBuffer
*buf
);
224 void PutChar(char c
);
226 off_t
Seek(off_t pos
, wxSeekMode mode
);
230 void SetBufferIO(char *buffer_start
, char *buffer_end
);
231 void SetBufferIO(size_t bufsize
);
232 char *GetBufferStart() const { return m_buffer_start
; }
233 char *GetBufferEnd() const { return m_buffer_end
; }
234 char *GetBufferPos() const { return m_buffer_pos
; }
235 off_t
GetIntPosition() const { return m_buffer_pos
-m_buffer_start
; }
236 void SetIntPosition(off_t pos
) { m_buffer_pos
= m_buffer_start
+pos
; }
237 size_t GetLastAccess() const { return m_buffer_end
-m_buffer_start
; }
239 void Fixed(bool fixed
) { m_fixed
= fixed
; }
240 void Flushable(bool f
) { m_flushable
= f
; }
244 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
);
253 char *m_buffer_start
, *m_buffer_end
, *m_buffer_pos
;
254 size_t m_buffer_size
;
256 bool m_fixed
, m_flushable
;
258 wxStreamBase
*m_stream
;
260 bool m_destroybuf
, m_destroystream
;
263 // ---------------------------------------------------------------------------
265 // ---------------------------------------------------------------------------
267 class WXDLLEXPORT wxBufferedInputStream
: public wxFilterInputStream
270 wxBufferedInputStream(wxInputStream
& stream
);
271 ~wxBufferedInputStream();
274 wxInputStream
& Read(void *buffer
, size_t size
);
276 // Position functions
277 off_t
SeekI(off_t pos
, wxSeekMode mode
= wxFromStart
);
280 wxStreamBuffer
*InputStreamBuffer() const { return m_i_streambuf
; }
283 size_t OnSysRead(void *buffer
, size_t bufsize
);
284 off_t
OnSysSeek(off_t seek
, wxSeekMode mode
);
285 off_t
OnSysTell() const;
287 wxStreamBuffer
*m_i_streambuf
;
290 class WXDLLEXPORT wxBufferedOutputStream
: public wxFilterOutputStream
293 wxBufferedOutputStream(wxOutputStream
& stream
);
294 ~wxBufferedOutputStream();
296 wxOutputStream
& Write(const void *buffer
, size_t size
);
298 // Position functions
299 off_t
SeekO(off_t pos
, wxSeekMode mode
= wxFromStart
);
304 size_t GetSize() const;
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;
313 wxStreamBuffer
*m_o_streambuf
;