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 // ---------------------------------------------------------------------------
43 wxSTREAM_NO_ERROR
= 0,
44 wxSTREAM_NO_ERR
= wxSTREAM_NO_ERROR
,
45 wxSTREAM_NOERROR
= wxSTREAM_NO_ERROR
,
50 wxSTREAM_WRITE_ERR
= wxSTREAM_WRITE_ERROR
,
53 wxSTREAM_READ_ERR
= wxSTREAM_READ_ERROR
58 #define wxStream_NOERROR wxSTREAM_NOERROR
59 #define wxStream_EOF wxSTREAM_EOF
60 #define wxStream_WRITE_ERR wxSTREAM_WRITE_ERROR
61 #define wxStream_READ_ERR wxSTREAM_READ_ERROR
63 class WXDLLEXPORT wxStreamBase
67 virtual ~wxStreamBase();
70 wxStreamError
LastError() const { return m_lasterror
; }
71 wxStreamError
GetLastError() const { return m_lasterror
; }
72 bool IsOk() const { return LastError() == wxSTREAM_NOERROR
; }
73 bool operator!() const { return LastError() != wxSTREAM_NOERROR
; }
75 virtual size_t GetSize() const { return ~((size_t)0); }
76 size_t StreamSize() const { return GetSize(); }
79 virtual size_t OnSysRead(void *buffer
, size_t bufsize
);
80 virtual size_t OnSysWrite(const void *buffer
, size_t bufsize
);
81 virtual off_t
OnSysSeek(off_t seek
, wxSeekMode mode
);
82 virtual off_t
OnSysTell() const;
84 friend class wxStreamBuffer
;
87 wxStreamError m_lasterror
;
90 class WXDLLEXPORT wxInputStream
: public wxStreamBase
94 virtual ~wxInputStream();
96 // is the stream at EOF?
97 virtual bool Eof() const;
102 virtual wxInputStream
& Read(void *buffer
, size_t size
);
103 wxInputStream
& Read(wxOutputStream
& stream_out
);
105 // Position functions
106 virtual off_t
SeekI(off_t pos
, wxSeekMode mode
= wxFromStart
);
107 virtual off_t
TellI() const;
110 virtual size_t LastRead() { return wxStreamBase::m_lastcount
; }
113 size_t Ungetch(const void *buffer
, size_t size
);
114 bool Ungetch(char c
);
117 wxInputStream
& operator>>(wxOutputStream
& out
) { return Read(out
); }
119 wxInputStream
& operator>>(wxObject
*& obj
);
121 wxInputStream
& operator>>( __wxInputManip func
) { return func(*this); }
129 char *AllocSpaceWBack(size_t needed_size
);
130 size_t GetWBack(char *buf
, size_t bsize
);
133 class WXDLLEXPORT wxOutputStream
: public wxStreamBase
137 virtual ~wxOutputStream();
140 virtual wxOutputStream
& Write(const void *buffer
, size_t size
);
141 wxOutputStream
& Write(wxInputStream
& stream_in
);
143 virtual off_t
SeekO(off_t pos
, wxSeekMode mode
= wxFromStart
);
144 virtual off_t
TellO() const;
146 virtual size_t LastWrite() const { return wxStreamBase::m_lastcount
; }
150 wxOutputStream
& operator<<(wxInputStream
& out
) { return Write(out
); }
152 wxOutputStream
& operator<<(wxObject
& obj
);
154 wxOutputStream
& operator<<( __wxOutputManip func
) { return func(*this); }
157 // ---------------------------------------------------------------------------
158 // A stream for measuring streamed output
159 // ---------------------------------------------------------------------------
161 class WXDLLEXPORT wxCountingOutputStream
: public wxOutputStream
164 wxCountingOutputStream();
166 size_t GetSize() const;
167 bool Ok() const { return TRUE
; }
170 size_t OnSysWrite(const void *buffer
, size_t size
);
171 off_t
OnSysSeek(off_t pos
, wxSeekMode mode
);
172 off_t
OnSysTell() const;
177 // ---------------------------------------------------------------------------
179 // ---------------------------------------------------------------------------
181 class WXDLLEXPORT wxFilterInputStream
: public wxInputStream
184 wxFilterInputStream();
185 wxFilterInputStream(wxInputStream
& stream
);
186 ~wxFilterInputStream();
188 char Peek() { return m_parent_i_stream
->Peek(); }
190 size_t GetSize() const { return m_parent_i_stream
->GetSize(); }
193 wxInputStream
*m_parent_i_stream
;
196 class WXDLLEXPORT wxFilterOutputStream
: public wxOutputStream
199 wxFilterOutputStream();
200 wxFilterOutputStream(wxOutputStream
& stream
);
201 ~wxFilterOutputStream();
203 size_t GetSize() const { return m_parent_o_stream
->GetSize(); }
206 wxOutputStream
*m_parent_o_stream
;
209 // ---------------------------------------------------------------------------
211 // ---------------------------------------------------------------------------
213 class WXDLLEXPORT wxStreamBuffer
217 read
= 0, write
, read_write
220 wxStreamBuffer(wxStreamBase
& stream
, BufMode mode
);
221 wxStreamBuffer(BufMode mode
);
222 wxStreamBuffer(const wxStreamBuffer
& buf
);
226 size_t Read(void *buffer
, size_t size
);
227 size_t Read(wxStreamBuffer
*buf
);
228 size_t Write(const void *buffer
, size_t size
);
229 size_t Write(wxStreamBuffer
*buf
);
233 void PutChar(char c
);
235 off_t
Seek(off_t pos
, wxSeekMode mode
);
239 void SetBufferIO(char *buffer_start
, char *buffer_end
);
240 void SetBufferIO(size_t bufsize
);
241 char *GetBufferStart() const { return m_buffer_start
; }
242 char *GetBufferEnd() const { return m_buffer_end
; }
243 char *GetBufferPos() const { return m_buffer_pos
; }
244 off_t
GetIntPosition() const { return m_buffer_pos
-m_buffer_start
; }
245 void SetIntPosition(off_t pos
) { m_buffer_pos
= m_buffer_start
+pos
; }
246 size_t GetLastAccess() const { return m_buffer_end
-m_buffer_start
; }
248 void Fixed(bool fixed
) { m_fixed
= fixed
; }
249 void Flushable(bool f
) { m_flushable
= f
; }
253 size_t GetDataLeft();
256 wxStreamBase
*Stream() { return m_stream
; }
259 void GetFromBuffer(void *buffer
, size_t size
);
260 void PutToBuffer(const void *buffer
, size_t size
);
262 char *m_buffer_start
, *m_buffer_end
, *m_buffer_pos
;
263 size_t m_buffer_size
;
265 bool m_fixed
, m_flushable
;
267 wxStreamBase
*m_stream
;
269 bool m_destroybuf
, m_destroystream
;
272 // ---------------------------------------------------------------------------
274 // ---------------------------------------------------------------------------
276 class WXDLLEXPORT wxBufferedInputStream
: public wxFilterInputStream
279 wxBufferedInputStream(wxInputStream
& stream
);
280 ~wxBufferedInputStream();
283 wxInputStream
& Read(void *buffer
, size_t size
);
285 // Position functions
286 off_t
SeekI(off_t pos
, wxSeekMode mode
= wxFromStart
);
289 wxStreamBuffer
*InputStreamBuffer() const { return m_i_streambuf
; }
292 size_t OnSysRead(void *buffer
, size_t bufsize
);
293 off_t
OnSysSeek(off_t seek
, wxSeekMode mode
);
294 off_t
OnSysTell() const;
296 wxStreamBuffer
*m_i_streambuf
;
299 class WXDLLEXPORT wxBufferedOutputStream
: public wxFilterOutputStream
302 wxBufferedOutputStream(wxOutputStream
& stream
);
303 ~wxBufferedOutputStream();
305 wxOutputStream
& Write(const void *buffer
, size_t size
);
307 // Position functions
308 off_t
SeekO(off_t pos
, wxSeekMode mode
= wxFromStart
);
313 size_t GetSize() const;
315 wxStreamBuffer
*OutputStreamBuffer() const { return m_o_streambuf
; }
318 size_t OnSysWrite(const void *buffer
, size_t bufsize
);
319 off_t
OnSysSeek(off_t seek
, wxSeekMode mode
);
320 off_t
OnSysTell() const;
322 wxStreamBuffer
*m_o_streambuf
;