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
57 #define wxStream_NOERROR wxSTREAM_NOERROR
58 #define wxStream_EOF wxSTREAM_EOF
59 #define wxStream_WRITE_ERR wxSTREAM_WRITE_ERROR
60 #define wxStream_READ_ERR wxSTREAM_READ_ERROR
62 class WXDLLEXPORT wxStreamBase
66 virtual ~wxStreamBase();
69 wxStreamError
GetLastError() const { return m_lasterror
; }
70 bool IsOk() const { return LastError() == wxSTREAM_NOERROR
; }
71 bool operator!() const { return LastError() != wxSTREAM_NOERROR
; }
73 virtual size_t GetSize() const { return 0; }
75 // deprecated, for compatibility only
76 wxStreamError
LastError() const { return m_lasterror
; }
77 size_t StreamSize() const { return GetSize(); }
80 // VZ: these functions are really pure virtual and shouldn't be declared
81 // in the base class because it creates ambiguties in stream classes
82 // deriving from both wxInputStream and wxOutputStream
84 virtual size_t OnSysRead(void *buffer
, size_t bufsize
);
85 virtual size_t OnSysWrite(const void *buffer
, size_t bufsize
);
88 virtual off_t
OnSysSeek(off_t seek
, wxSeekMode mode
);
89 virtual off_t
OnSysTell() const;
91 friend class wxStreamBuffer
;
94 wxStreamError m_lasterror
;
97 class WXDLLEXPORT wxInputStream
: /* virtual */ public wxStreamBase
101 virtual ~wxInputStream();
103 // is the stream at EOF?
104 virtual bool Eof() const;
109 virtual wxInputStream
& Read(void *buffer
, size_t size
);
110 wxInputStream
& Read(wxOutputStream
& stream_out
);
112 // Position functions
113 virtual off_t
SeekI(off_t pos
, wxSeekMode mode
= wxFromStart
);
114 virtual off_t
TellI() const;
117 virtual size_t LastRead() { return wxStreamBase::m_lastcount
; }
120 size_t Ungetch(const void *buffer
, size_t size
);
121 bool Ungetch(char c
);
124 wxInputStream
& operator>>(wxOutputStream
& out
) { return Read(out
); }
126 wxInputStream
& operator>>(wxObject
*& obj
);
128 wxInputStream
& operator>>( __wxInputManip func
) { return func(*this); }
131 // to be implemented in the derived classes (it should have been pure
133 virtual size_t OnSysRead(void *buffer
, size_t bufsize
);
140 char *AllocSpaceWBack(size_t needed_size
);
141 size_t GetWBack(void *buf
, size_t bsize
);
143 friend class wxStreamBuffer
;
146 class WXDLLEXPORT wxOutputStream
: /* virtual */ public wxStreamBase
150 virtual ~wxOutputStream();
153 virtual wxOutputStream
& Write(const void *buffer
, size_t size
);
154 wxOutputStream
& Write(wxInputStream
& stream_in
);
156 virtual off_t
SeekO(off_t pos
, wxSeekMode mode
= wxFromStart
);
157 virtual off_t
TellO() const;
159 virtual size_t LastWrite() const { return wxStreamBase::m_lastcount
; }
163 wxOutputStream
& operator<<(wxInputStream
& out
) { return Write(out
); }
165 wxOutputStream
& operator<<(wxObject
& obj
);
167 wxOutputStream
& operator<<( __wxOutputManip func
) { return func(*this); }
170 // to be implemented in the derived classes (it should have been pure
172 virtual size_t OnSysWrite(const void *buffer
, size_t bufsize
);
174 friend class wxStreamBuffer
;
177 // ---------------------------------------------------------------------------
178 // A stream for measuring streamed output
179 // ---------------------------------------------------------------------------
181 class WXDLLEXPORT wxCountingOutputStream
: public wxOutputStream
184 wxCountingOutputStream();
186 size_t GetSize() const;
187 bool Ok() const { return TRUE
; }
190 virtual size_t OnSysWrite(const void *buffer
, size_t size
);
191 virtual off_t
OnSysSeek(off_t pos
, wxSeekMode mode
);
192 virtual off_t
OnSysTell() const;
197 // ---------------------------------------------------------------------------
199 // ---------------------------------------------------------------------------
201 class WXDLLEXPORT wxFilterInputStream
: public wxInputStream
204 wxFilterInputStream();
205 wxFilterInputStream(wxInputStream
& stream
);
206 virtual ~wxFilterInputStream();
208 char Peek() { return m_parent_i_stream
->Peek(); }
210 size_t GetSize() const { return m_parent_i_stream
->GetSize(); }
212 wxInputStream
*GetFilterInputStream() const { return m_parent_i_stream
; }
215 wxInputStream
*m_parent_i_stream
;
218 class WXDLLEXPORT wxFilterOutputStream
: public wxOutputStream
221 wxFilterOutputStream();
222 wxFilterOutputStream(wxOutputStream
& stream
);
223 virtual ~wxFilterOutputStream();
225 size_t GetSize() const { return m_parent_o_stream
->GetSize(); }
227 wxOutputStream
*GetFilterOutputStream() const { return m_parent_o_stream
; }
230 wxOutputStream
*m_parent_o_stream
;
233 // ---------------------------------------------------------------------------
234 // Stream buffer: this class can be derived from and passed to
235 // wxBufferedStreams to implement custom buffering
236 // ---------------------------------------------------------------------------
238 class WXDLLEXPORT wxStreamBuffer
248 wxStreamBuffer(wxStreamBase
& stream
, BufMode mode
);
249 wxStreamBuffer(BufMode mode
);
250 wxStreamBuffer(const wxStreamBuffer
& buf
);
251 virtual ~wxStreamBuffer();
254 virtual size_t Read(void *buffer
, size_t size
);
255 size_t Read(wxStreamBuffer
*buf
);
256 virtual size_t Write(const void *buffer
, size_t size
);
257 size_t Write(wxStreamBuffer
*buf
);
260 virtual char GetChar();
261 virtual void PutChar(char c
);
262 virtual off_t
Tell() const;
263 virtual off_t
Seek(off_t pos
, wxSeekMode mode
);
268 // NB: the buffer must always be allocated with malloc() if takeOwn is
269 // TRUE as it will be deallocated by free()
270 void SetBufferIO(void *start
, void *end
, bool takeOwnership
= FALSE
);
271 void SetBufferIO(void *start
, size_t len
, bool takeOwnership
= FALSE
);
272 void SetBufferIO(size_t bufsize
);
273 void *GetBufferStart() const { return m_buffer_start
; }
274 void *GetBufferEnd() const { return m_buffer_end
; }
275 void *GetBufferPos() const { return m_buffer_pos
; }
276 size_t GetBufferSize() const { return m_buffer_size
; }
277 size_t GetIntPosition() const { return m_buffer_pos
- m_buffer_start
; }
278 void SetIntPosition(size_t pos
) { m_buffer_pos
= m_buffer_start
+ pos
; }
279 size_t GetLastAccess() const { return m_buffer_end
- m_buffer_start
; }
280 size_t GetBytesLeft() const { return m_buffer_end
- m_buffer_pos
; }
282 void Fixed(bool fixed
) { m_fixed
= fixed
; }
283 void Flushable(bool f
) { m_flushable
= f
; }
287 size_t GetDataLeft();
290 wxStreamBase
*GetStream() const { return m_stream
; }
291 bool HasBuffer() const { return m_buffer_size
!= 0; }
293 bool IsFixed() const { return m_fixed
; }
294 bool IsFlushable() const { return m_flushable
; }
296 // only for input/output buffers respectively, returns NULL otherwise
297 wxInputStream
*GetInputStream() const;
298 wxOutputStream
*GetOutputStream() const;
300 // deprecated, for compatibility only
301 wxStreamBase
*Stream() { return m_stream
; }
304 void GetFromBuffer(void *buffer
, size_t size
);
305 void PutToBuffer(const void *buffer
, size_t size
);
307 // set the last error to the specified value if we didn't have it before
308 void SetError(wxStreamError err
);
310 // common part of several ctors
313 // init buffer variables to be empty
316 // free the buffer (always safe to call)
319 // the buffer itself: the pointers to its start and end and the current
320 // position in the buffer
321 char *m_buffer_start
,
326 // FIXME: isn't it the same as m_buffer_end - m_buffer_start? (VZ)
327 size_t m_buffer_size
;
329 // the stream we're associated with
330 wxStreamBase
*m_stream
;
336 bool m_destroybuf
, // deallocate buffer?
337 m_destroystream
, // delete associated stream?
342 // ---------------------------------------------------------------------------
344 // ---------------------------------------------------------------------------
346 class WXDLLEXPORT wxBufferedInputStream
: public wxFilterInputStream
349 // if a non NULL buffer is given to the stream, it will be deleted by it
350 wxBufferedInputStream(wxInputStream
& stream
,
351 wxStreamBuffer
*buffer
= NULL
);
352 virtual ~wxBufferedInputStream();
355 wxInputStream
& Read(void *buffer
, size_t size
);
357 // Position functions
358 off_t
SeekI(off_t pos
, wxSeekMode mode
= wxFromStart
);
361 // the buffer given to the stream will be deleted by it
362 void SetInputStreamBuffer(wxStreamBuffer
*buffer
);
363 wxStreamBuffer
*GetInputStreamBuffer() const { return m_i_streambuf
; }
365 // deprecated, for compatibility only
366 wxStreamBuffer
*InputStreamBuffer() const { return m_i_streambuf
; }
369 virtual size_t OnSysRead(void *buffer
, size_t bufsize
);
370 virtual off_t
OnSysSeek(off_t seek
, wxSeekMode mode
);
371 virtual off_t
OnSysTell() const;
373 wxStreamBuffer
*m_i_streambuf
;
376 class WXDLLEXPORT wxBufferedOutputStream
: public wxFilterOutputStream
379 // if a non NULL buffer is given to the stream, it will be deleted by it
380 wxBufferedOutputStream(wxOutputStream
& stream
,
381 wxStreamBuffer
*buffer
= NULL
);
382 virtual ~wxBufferedOutputStream();
384 wxOutputStream
& Write(const void *buffer
, size_t size
);
386 // Position functions
387 off_t
SeekO(off_t pos
, wxSeekMode mode
= wxFromStart
);
392 size_t GetSize() const;
394 // the buffer given to the stream will be deleted by it
395 void SetOutputStreamBuffer(wxStreamBuffer
*buffer
);
396 wxStreamBuffer
*GetOutputStreamBuffer() const { return m_o_streambuf
; }
398 // deprecated, for compatibility only
399 wxStreamBuffer
*OutputStreamBuffer() const { return m_o_streambuf
; }
402 virtual size_t OnSysWrite(const void *buffer
, size_t bufsize
);
403 virtual off_t
OnSysSeek(off_t seek
, wxSeekMode mode
);
404 virtual off_t
OnSysTell() const;
406 wxStreamBuffer
*m_o_streambuf
;
409 #endif // wxUSE_STREAMS
411 #endif // _WX_WXSTREAM_H__