1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: stream classes
4 // Author: Guilhem Lavaux, Guillermo Rodriguez Garcia, Vadim Zeitlin
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_WXSTREAM_H__
13 #define _WX_WXSTREAM_H__
15 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
16 #pragma interface "stream.h"
24 #include "wx/object.h"
25 #include "wx/string.h"
26 #include "wx/filefn.h" // for off_t, wxInvalidOffset and wxSeekMode
28 class WXDLLIMPEXP_BASE wxStreamBase
;
29 class WXDLLIMPEXP_BASE wxInputStream
;
30 class WXDLLIMPEXP_BASE wxOutputStream
;
32 typedef wxInputStream
& (*__wxInputManip
)(wxInputStream
&);
33 typedef wxOutputStream
& (*__wxOutputManip
)(wxOutputStream
&);
35 WXDLLIMPEXP_BASE wxOutputStream
& wxEndL(wxOutputStream
& o_stream
);
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
43 wxSTREAM_NO_ERROR
= 0, // stream is in good state
44 wxSTREAM_EOF
, // EOF reached in Read() or similar
45 wxSTREAM_WRITE_ERROR
, // generic write error
46 wxSTREAM_READ_ERROR
// generic read error
50 #if WXWIN_COMPATIBILITY_2_2
51 #define wxStream_NOERROR wxSTREAM_NOERROR
52 #define wxStream_EOF wxSTREAM_EOF
53 #define wxStream_WRITE_ERR wxSTREAM_WRITE_ERROR
54 #define wxStream_READ_ERR wxSTREAM_READ_ERROR
56 #define wxSTREAM_NO_ERR wxSTREAM_NO_ERROR
57 #define wxSTREAM_NOERROR wxSTREAM_NO_ERROR
58 #define wxSTREAM_WRITE_ERR wxSTREAM_WRITE_ERROR
59 #define wxSTREAM_READ_ERR wxSTREAM_READ_ERROR
60 #endif // WXWIN_COMPATIBILITY_2_2
62 // ============================================================================
63 // base stream classes: wxInputStream and wxOutputStream
64 // ============================================================================
66 // ---------------------------------------------------------------------------
67 // wxStreamBase: common (but non virtual!) base for all stream classes
68 // ---------------------------------------------------------------------------
70 class WXDLLIMPEXP_BASE wxStreamBase
74 virtual ~wxStreamBase();
77 wxStreamError
GetLastError() const { return m_lasterror
; }
78 bool IsOk() const { return GetLastError() == wxSTREAM_NO_ERROR
; }
79 bool operator!() const { return !IsOk(); }
81 // reset the stream state
82 void Reset() { m_lasterror
= wxSTREAM_NO_ERROR
; }
84 // this doesn't make sense for all streams, always test its return value
85 virtual size_t GetSize() const { return 0; }
87 #if WXWIN_COMPATIBILITY_2_2
88 // deprecated, for compatibility only
89 wxStreamError
LastError() const { return m_lasterror
; }
90 size_t StreamSize() const { return GetSize(); }
91 #endif // WXWIN_COMPATIBILITY_2_2
94 virtual off_t
OnSysSeek(off_t seek
, wxSeekMode mode
);
95 virtual off_t
OnSysTell() const;
98 wxStreamError m_lasterror
;
100 friend class wxStreamBuffer
;
102 DECLARE_NO_COPY_CLASS(wxStreamBase
)
105 // ----------------------------------------------------------------------------
106 // wxInputStream: base class for the input streams
107 // ----------------------------------------------------------------------------
109 class WXDLLIMPEXP_BASE wxInputStream
: public wxStreamBase
112 // ctor and dtor, nothing exciting
114 virtual ~wxInputStream();
120 // return a character from the stream without removing it, i.e. it will
121 // still be returned by the next call to GetC()
123 // blocks until something appears in the stream if necessary, if nothing
124 // ever does (i.e. EOF) LastRead() will return 0 (and the return value is
125 // undefined), otherwise 1
128 // return one character from the stream, blocking until it appears if
131 // if EOF, return value is undefined and LastRead() will return 0 and not 1
134 // read at most the given number of bytes from the stream
136 // there are 2 possible situations here: either there is nothing at all in
137 // the stream right now in which case Read() blocks until something appears
138 // (use CanRead() to avoid this) or there is already some data available in
139 // the stream and then Read() doesn't block but returns just the data it
140 // can read without waiting for more
142 // in any case, if there are not enough bytes in the stream right now,
143 // LastRead() value will be less than size but greater than 0. If it is 0,
144 // it means that EOF has been reached.
145 virtual wxInputStream
& Read(void *buffer
, size_t size
);
147 // copy the entire contents of this stream into streamOut, stopping only
148 // when EOF is reached or an error occurs
149 wxInputStream
& Read(wxOutputStream
& streamOut
);
155 // returns the number of bytes read by the last call to Read(), GetC() or
158 // this should be used to discover whether that call succeeded in reading
159 // all the requested data or not
160 virtual size_t LastRead() const { return wxStreamBase::m_lastcount
; }
162 // returns TRUE if some data is available in the stream right now, so that
163 // calling Read() wouldn't block
164 virtual bool CanRead() const;
166 // is the stream at EOF?
168 // note that this cannot be really implemented for all streams and
169 // CanRead() is more reliable than Eof()
170 virtual bool Eof() const;
176 // put back the specified number of bytes into the stream, they will be
177 // fetched by the next call to the read functions
179 // returns the number of bytes really stuffed back
180 size_t Ungetch(const void *buffer
, size_t size
);
182 // put back the specified character in the stream
184 // returns TRUE if ok, FALSE on error
185 bool Ungetch(char c
);
188 // position functions
189 // ------------------
191 // move the stream pointer to the given position (if the stream supports
194 // returns wxInvalidOffset on error
195 virtual off_t
SeekI(off_t pos
, wxSeekMode mode
= wxFromStart
);
197 // return the current position of the stream pointer or wxInvalidOffset
198 virtual off_t
TellI() const;
201 // stream-like operators
202 // ---------------------
204 wxInputStream
& operator>>(wxOutputStream
& out
) { return Read(out
); }
205 wxInputStream
& operator>>(__wxInputManip func
) { return func(*this); }
208 // do read up to size bytes of data into the provided buffer
210 // this method should return 0 if EOF has been reached or an error occured
211 // (m_lasterror should be set accordingly as well) or the number of bytes
213 virtual size_t OnSysRead(void *buffer
, size_t size
) = 0;
215 // write-back buffer support
216 // -------------------------
218 // return the pointer to a buffer big enough to hold sizeNeeded bytes
219 char *AllocSpaceWBack(size_t sizeNeeded
);
221 // read up to size data from the write back buffer, return the number of
223 size_t GetWBack(void *buf
, size_t size
);
225 // write back buffer or NULL if none
228 // the size of the buffer
231 // the current position in the buffer
234 friend class wxStreamBuffer
;
236 DECLARE_NO_COPY_CLASS(wxInputStream
)
239 // ----------------------------------------------------------------------------
240 // wxOutputStream: base for the output streams
241 // ----------------------------------------------------------------------------
243 class WXDLLIMPEXP_BASE wxOutputStream
: public wxStreamBase
247 virtual ~wxOutputStream();
250 virtual wxOutputStream
& Write(const void *buffer
, size_t size
);
251 wxOutputStream
& Write(wxInputStream
& stream_in
);
253 virtual off_t
SeekO(off_t pos
, wxSeekMode mode
= wxFromStart
);
254 virtual off_t
TellO() const;
256 virtual size_t LastWrite() const { return wxStreamBase::m_lastcount
; }
260 wxOutputStream
& operator<<(wxInputStream
& out
) { return Write(out
); }
261 wxOutputStream
& operator<<( __wxOutputManip func
) { return func(*this); }
264 // to be implemented in the derived classes (it should have been pure
266 virtual size_t OnSysWrite(const void *buffer
, size_t bufsize
);
268 friend class wxStreamBuffer
;
270 DECLARE_NO_COPY_CLASS(wxOutputStream
)
273 // ============================================================================
274 // helper stream classes
275 // ============================================================================
277 // ---------------------------------------------------------------------------
278 // A stream for measuring streamed output
279 // ---------------------------------------------------------------------------
281 class WXDLLIMPEXP_BASE wxCountingOutputStream
: public wxOutputStream
284 wxCountingOutputStream();
286 size_t GetSize() const;
287 bool Ok() const { return TRUE
; }
290 virtual size_t OnSysWrite(const void *buffer
, size_t size
);
291 virtual off_t
OnSysSeek(off_t pos
, wxSeekMode mode
);
292 virtual off_t
OnSysTell() const;
296 DECLARE_NO_COPY_CLASS(wxCountingOutputStream
)
299 // ---------------------------------------------------------------------------
301 // ---------------------------------------------------------------------------
303 class WXDLLIMPEXP_BASE wxFilterInputStream
: public wxInputStream
306 wxFilterInputStream();
307 wxFilterInputStream(wxInputStream
& stream
);
308 virtual ~wxFilterInputStream();
310 char Peek() { return m_parent_i_stream
->Peek(); }
312 size_t GetSize() const { return m_parent_i_stream
->GetSize(); }
314 wxInputStream
*GetFilterInputStream() const { return m_parent_i_stream
; }
317 wxInputStream
*m_parent_i_stream
;
319 DECLARE_NO_COPY_CLASS(wxFilterInputStream
)
322 class WXDLLIMPEXP_BASE wxFilterOutputStream
: public wxOutputStream
325 wxFilterOutputStream();
326 wxFilterOutputStream(wxOutputStream
& stream
);
327 virtual ~wxFilterOutputStream();
329 size_t GetSize() const { return m_parent_o_stream
->GetSize(); }
331 wxOutputStream
*GetFilterOutputStream() const { return m_parent_o_stream
; }
334 wxOutputStream
*m_parent_o_stream
;
336 DECLARE_NO_COPY_CLASS(wxFilterOutputStream
)
339 // ============================================================================
341 // ============================================================================
343 // ---------------------------------------------------------------------------
344 // Stream buffer: this class can be derived from and passed to
345 // wxBufferedStreams to implement custom buffering
346 // ---------------------------------------------------------------------------
348 class WXDLLIMPEXP_BASE wxStreamBuffer
358 wxStreamBuffer(wxStreamBase
& stream
, BufMode mode
);
359 wxStreamBuffer(const wxStreamBuffer
& buf
);
360 virtual ~wxStreamBuffer();
363 virtual size_t Read(void *buffer
, size_t size
);
364 size_t Read(wxStreamBuffer
*buf
);
365 virtual size_t Write(const void *buffer
, size_t size
);
366 size_t Write(wxStreamBuffer
*buf
);
369 virtual char GetChar();
370 virtual void PutChar(char c
);
371 virtual off_t
Tell() const;
372 virtual off_t
Seek(off_t pos
, wxSeekMode mode
);
377 // NB: the buffer must always be allocated with malloc() if takeOwn is
378 // TRUE as it will be deallocated by free()
379 void SetBufferIO(void *start
, void *end
, bool takeOwnership
= FALSE
);
380 void SetBufferIO(void *start
, size_t len
, bool takeOwnership
= FALSE
);
381 void SetBufferIO(size_t bufsize
);
382 void *GetBufferStart() const { return m_buffer_start
; }
383 void *GetBufferEnd() const { return m_buffer_end
; }
384 void *GetBufferPos() const { return m_buffer_pos
; }
385 size_t GetBufferSize() const { return m_buffer_size
; }
386 size_t GetIntPosition() const { return m_buffer_pos
- m_buffer_start
; }
387 void SetIntPosition(size_t pos
) { m_buffer_pos
= m_buffer_start
+ pos
; }
388 size_t GetLastAccess() const { return m_buffer_end
- m_buffer_start
; }
389 size_t GetBytesLeft() const { return m_buffer_end
- m_buffer_pos
; }
391 void Fixed(bool fixed
) { m_fixed
= fixed
; }
392 void Flushable(bool f
) { m_flushable
= f
; }
396 size_t GetDataLeft();
399 wxStreamBase
*GetStream() const { return m_stream
; }
400 bool HasBuffer() const { return m_buffer_size
!= 0; }
402 bool IsFixed() const { return m_fixed
; }
403 bool IsFlushable() const { return m_flushable
; }
405 // only for input/output buffers respectively, returns NULL otherwise
406 wxInputStream
*GetInputStream() const;
407 wxOutputStream
*GetOutputStream() const;
409 // deprecated, for compatibility only
410 wxStreamBase
*Stream() { return m_stream
; }
412 // this constructs a dummy wxStreamBuffer, used by (and exists for)
413 // wxMemoryStreams only, don't use!
414 wxStreamBuffer(BufMode mode
);
417 void GetFromBuffer(void *buffer
, size_t size
);
418 void PutToBuffer(const void *buffer
, size_t size
);
420 // set the last error to the specified value if we didn't have it before
421 void SetError(wxStreamError err
);
423 // common part of several ctors
426 // init buffer variables to be empty
429 // free the buffer (always safe to call)
432 // the buffer itself: the pointers to its start and end and the current
433 // position in the buffer
434 char *m_buffer_start
,
439 // FIXME: isn't it the same as m_buffer_end - m_buffer_start? (VZ)
440 size_t m_buffer_size
;
442 // the stream we're associated with
443 wxStreamBase
*m_stream
;
449 bool m_destroybuf
, // deallocate buffer?
455 // DECLARE_NO_COPY_CLASS(wxStreamBuffer)
456 // because copy constructor is explicitly declared above;
457 // but no copy assignment operator is defined, so declare
458 // it private to prevent the compiler from defining it:
459 wxStreamBuffer
& operator=(const wxStreamBuffer
&);
462 // ---------------------------------------------------------------------------
463 // wxBufferedInputStream
464 // ---------------------------------------------------------------------------
466 class WXDLLIMPEXP_BASE wxBufferedInputStream
: public wxFilterInputStream
469 // if a non NULL buffer is given to the stream, it will be deleted by it
470 wxBufferedInputStream(wxInputStream
& stream
,
471 wxStreamBuffer
*buffer
= NULL
);
472 virtual ~wxBufferedInputStream();
475 wxInputStream
& Read(void *buffer
, size_t size
);
477 // Position functions
478 off_t
SeekI(off_t pos
, wxSeekMode mode
= wxFromStart
);
481 // the buffer given to the stream will be deleted by it
482 void SetInputStreamBuffer(wxStreamBuffer
*buffer
);
483 wxStreamBuffer
*GetInputStreamBuffer() const { return m_i_streambuf
; }
485 // deprecated, for compatibility only
486 wxStreamBuffer
*InputStreamBuffer() const { return m_i_streambuf
; }
489 virtual size_t OnSysRead(void *buffer
, size_t bufsize
);
490 virtual off_t
OnSysSeek(off_t seek
, wxSeekMode mode
);
491 virtual off_t
OnSysTell() const;
493 wxStreamBuffer
*m_i_streambuf
;
495 DECLARE_NO_COPY_CLASS(wxBufferedInputStream
)
498 // ----------------------------------------------------------------------------
499 // wxBufferedOutputStream
500 // ----------------------------------------------------------------------------
502 class WXDLLIMPEXP_BASE wxBufferedOutputStream
: public wxFilterOutputStream
505 // if a non NULL buffer is given to the stream, it will be deleted by it
506 wxBufferedOutputStream(wxOutputStream
& stream
,
507 wxStreamBuffer
*buffer
= NULL
);
508 virtual ~wxBufferedOutputStream();
510 wxOutputStream
& Write(const void *buffer
, size_t size
);
512 // Position functions
513 off_t
SeekO(off_t pos
, wxSeekMode mode
= wxFromStart
);
518 size_t GetSize() const;
520 // the buffer given to the stream will be deleted by it
521 void SetOutputStreamBuffer(wxStreamBuffer
*buffer
);
522 wxStreamBuffer
*GetOutputStreamBuffer() const { return m_o_streambuf
; }
524 // deprecated, for compatibility only
525 wxStreamBuffer
*OutputStreamBuffer() const { return m_o_streambuf
; }
528 virtual size_t OnSysWrite(const void *buffer
, size_t bufsize
);
529 virtual off_t
OnSysSeek(off_t seek
, wxSeekMode mode
);
530 virtual off_t
OnSysTell() const;
532 wxStreamBuffer
*m_o_streambuf
;
534 DECLARE_NO_COPY_CLASS(wxBufferedOutputStream
)
537 #endif // wxUSE_STREAMS
539 #endif // _WX_WXSTREAM_H__