1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: stream classes
4 // Author: Guilhem Lavaux, Guillermo Rodriguez Garcia, Vadim Zeitlin
7 // Copyright: (c) Guilhem Lavaux
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #ifndef _WX_WXSTREAM_H__
12 #define _WX_WXSTREAM_H__
19 #include "wx/object.h"
20 #include "wx/string.h"
21 #include "wx/filefn.h" // for wxFileOffset, wxInvalidOffset and wxSeekMode
23 class WXDLLIMPEXP_FWD_BASE wxStreamBase
;
24 class WXDLLIMPEXP_FWD_BASE wxInputStream
;
25 class WXDLLIMPEXP_FWD_BASE wxOutputStream
;
27 typedef wxInputStream
& (*__wxInputManip
)(wxInputStream
&);
28 typedef wxOutputStream
& (*__wxOutputManip
)(wxOutputStream
&);
30 WXDLLIMPEXP_BASE wxOutputStream
& wxEndL(wxOutputStream
& o_stream
);
32 // ----------------------------------------------------------------------------
34 // ----------------------------------------------------------------------------
38 wxSTREAM_NO_ERROR
= 0, // stream is in good state
39 wxSTREAM_EOF
, // EOF reached in Read() or similar
40 wxSTREAM_WRITE_ERROR
, // generic write error
41 wxSTREAM_READ_ERROR
// generic read error
46 // ============================================================================
47 // base stream classes: wxInputStream and wxOutputStream
48 // ============================================================================
50 // ---------------------------------------------------------------------------
51 // wxStreamBase: common (but non virtual!) base for all stream classes
52 // ---------------------------------------------------------------------------
54 class WXDLLIMPEXP_BASE wxStreamBase
: public wxObject
58 virtual ~wxStreamBase();
61 wxStreamError
GetLastError() const { return m_lasterror
; }
62 virtual bool IsOk() const { return GetLastError() == wxSTREAM_NO_ERROR
; }
63 bool operator!() const { return !IsOk(); }
65 // reset the stream state
66 void Reset(wxStreamError error
= wxSTREAM_NO_ERROR
) { m_lasterror
= error
; }
68 // this doesn't make sense for all streams, always test its return value
69 virtual size_t GetSize() const;
70 virtual wxFileOffset
GetLength() const { return wxInvalidOffset
; }
72 // returns true if the streams supports seeking to arbitrary offsets
73 virtual bool IsSeekable() const { return false; }
76 virtual wxFileOffset
OnSysSeek(wxFileOffset seek
, wxSeekMode mode
);
77 virtual wxFileOffset
OnSysTell() const;
80 wxStreamError m_lasterror
;
82 friend class wxStreamBuffer
;
84 DECLARE_ABSTRACT_CLASS(wxStreamBase
)
85 wxDECLARE_NO_COPY_CLASS(wxStreamBase
);
88 // ----------------------------------------------------------------------------
89 // wxInputStream: base class for the input streams
90 // ----------------------------------------------------------------------------
92 class WXDLLIMPEXP_BASE wxInputStream
: public wxStreamBase
95 // ctor and dtor, nothing exciting
97 virtual ~wxInputStream();
103 // return a character from the stream without removing it, i.e. it will
104 // still be returned by the next call to GetC()
106 // blocks until something appears in the stream if necessary, if nothing
107 // ever does (i.e. EOF) LastRead() will return 0 (and the return value is
108 // undefined), otherwise 1
111 // return one byte from the stream, blocking until it appears if
114 // on success returns a value between 0 - 255, or wxEOF on EOF or error.
117 // read at most the given number of bytes from the stream
119 // there are 2 possible situations here: either there is nothing at all in
120 // the stream right now in which case Read() blocks until something appears
121 // (use CanRead() to avoid this) or there is already some data available in
122 // the stream and then Read() doesn't block but returns just the data it
123 // can read without waiting for more
125 // in any case, if there are not enough bytes in the stream right now,
126 // LastRead() value will be less than size but greater than 0. If it is 0,
127 // it means that EOF has been reached.
128 virtual wxInputStream
& Read(void *buffer
, size_t size
);
130 // Read exactly the given number of bytes, unlike Read(), which may read
131 // less than the requested amount of data without returning an error, this
132 // method either reads all the data or returns false.
133 bool ReadAll(void *buffer
, size_t size
);
135 // copy the entire contents of this stream into streamOut, stopping only
136 // when EOF is reached or an error occurs
137 wxInputStream
& Read(wxOutputStream
& streamOut
);
143 // returns the number of bytes read by the last call to Read(), GetC() or
146 // this should be used to discover whether that call succeeded in reading
147 // all the requested data or not
148 virtual size_t LastRead() const { return wxStreamBase::m_lastcount
; }
150 // returns true if some data is available in the stream right now, so that
151 // calling Read() wouldn't block
152 virtual bool CanRead() const;
154 // is the stream at EOF?
156 // note that this cannot be really implemented for all streams and
157 // CanRead() is more reliable than Eof()
158 virtual bool Eof() const;
164 // put back the specified number of bytes into the stream, they will be
165 // fetched by the next call to the read functions
167 // returns the number of bytes really stuffed back
168 size_t Ungetch(const void *buffer
, size_t size
);
170 // put back the specified character in the stream
172 // returns true if ok, false on error
173 bool Ungetch(char c
);
176 // position functions
177 // ------------------
179 // move the stream pointer to the given position (if the stream supports
182 // returns wxInvalidOffset on error
183 virtual wxFileOffset
SeekI(wxFileOffset pos
, wxSeekMode mode
= wxFromStart
);
185 // return the current position of the stream pointer or wxInvalidOffset
186 virtual wxFileOffset
TellI() const;
189 // stream-like operators
190 // ---------------------
192 wxInputStream
& operator>>(wxOutputStream
& out
) { return Read(out
); }
193 wxInputStream
& operator>>(__wxInputManip func
) { return func(*this); }
196 // do read up to size bytes of data into the provided buffer
198 // this method should return 0 if EOF has been reached or an error occurred
199 // (m_lasterror should be set accordingly as well) or the number of bytes
201 virtual size_t OnSysRead(void *buffer
, size_t size
) = 0;
203 // write-back buffer support
204 // -------------------------
206 // return the pointer to a buffer big enough to hold sizeNeeded bytes
207 char *AllocSpaceWBack(size_t sizeNeeded
);
209 // read up to size data from the write back buffer, return the number of
211 size_t GetWBack(void *buf
, size_t size
);
213 // write back buffer or NULL if none
216 // the size of the buffer
219 // the current position in the buffer
222 friend class wxStreamBuffer
;
224 DECLARE_ABSTRACT_CLASS(wxInputStream
)
225 wxDECLARE_NO_COPY_CLASS(wxInputStream
);
228 // ----------------------------------------------------------------------------
229 // wxOutputStream: base for the output streams
230 // ----------------------------------------------------------------------------
232 class WXDLLIMPEXP_BASE wxOutputStream
: public wxStreamBase
236 virtual ~wxOutputStream();
239 virtual wxOutputStream
& Write(const void *buffer
, size_t size
);
241 // This is ReadAll() equivalent for Write(): it either writes exactly the
242 // given number of bytes or returns false, unlike Write() which can write
243 // less data than requested but still return without error.
244 bool WriteAll(const void *buffer
, size_t size
);
246 wxOutputStream
& Write(wxInputStream
& stream_in
);
248 virtual wxFileOffset
SeekO(wxFileOffset pos
, wxSeekMode mode
= wxFromStart
);
249 virtual wxFileOffset
TellO() const;
251 virtual size_t LastWrite() const { return wxStreamBase::m_lastcount
; }
254 virtual bool Close() { return true; }
256 wxOutputStream
& operator<<(wxInputStream
& out
) { return Write(out
); }
257 wxOutputStream
& operator<<( __wxOutputManip func
) { return func(*this); }
260 // to be implemented in the derived classes (it should have been pure
262 virtual size_t OnSysWrite(const void *buffer
, size_t bufsize
);
264 friend class wxStreamBuffer
;
266 DECLARE_ABSTRACT_CLASS(wxOutputStream
)
267 wxDECLARE_NO_COPY_CLASS(wxOutputStream
);
270 // ============================================================================
271 // helper stream classes
272 // ============================================================================
274 // ---------------------------------------------------------------------------
275 // A stream for measuring streamed output
276 // ---------------------------------------------------------------------------
278 class WXDLLIMPEXP_BASE wxCountingOutputStream
: public wxOutputStream
281 wxCountingOutputStream();
283 virtual wxFileOffset
GetLength() const;
284 bool Ok() const { return IsOk(); }
285 virtual bool IsOk() const { return true; }
288 virtual size_t OnSysWrite(const void *buffer
, size_t size
);
289 virtual wxFileOffset
OnSysSeek(wxFileOffset pos
, wxSeekMode mode
);
290 virtual wxFileOffset
OnSysTell() const;
295 DECLARE_DYNAMIC_CLASS(wxCountingOutputStream
)
296 wxDECLARE_NO_COPY_CLASS(wxCountingOutputStream
);
299 // ---------------------------------------------------------------------------
301 // ---------------------------------------------------------------------------
303 class WXDLLIMPEXP_BASE wxFilterInputStream
: public wxInputStream
306 wxFilterInputStream();
307 wxFilterInputStream(wxInputStream
& stream
);
308 wxFilterInputStream(wxInputStream
*stream
);
309 virtual ~wxFilterInputStream();
311 char Peek() { return m_parent_i_stream
->Peek(); }
313 wxFileOffset
GetLength() const { return m_parent_i_stream
->GetLength(); }
315 wxInputStream
*GetFilterInputStream() const { return m_parent_i_stream
; }
318 wxInputStream
*m_parent_i_stream
;
321 DECLARE_ABSTRACT_CLASS(wxFilterInputStream
)
322 wxDECLARE_NO_COPY_CLASS(wxFilterInputStream
);
325 class WXDLLIMPEXP_BASE wxFilterOutputStream
: public wxOutputStream
328 wxFilterOutputStream();
329 wxFilterOutputStream(wxOutputStream
& stream
);
330 wxFilterOutputStream(wxOutputStream
*stream
);
331 virtual ~wxFilterOutputStream();
333 wxFileOffset
GetLength() const { return m_parent_o_stream
->GetLength(); }
335 wxOutputStream
*GetFilterOutputStream() const { return m_parent_o_stream
; }
340 wxOutputStream
*m_parent_o_stream
;
343 DECLARE_ABSTRACT_CLASS(wxFilterOutputStream
)
344 wxDECLARE_NO_COPY_CLASS(wxFilterOutputStream
);
347 enum wxStreamProtocolType
349 wxSTREAM_PROTOCOL
, // wxFileSystem protocol (should be only one)
350 wxSTREAM_MIMETYPE
, // MIME types the stream handles
351 wxSTREAM_ENCODING
, // The HTTP Content-Encodings the stream handles
352 wxSTREAM_FILEEXT
// File extensions the stream handles
355 void WXDLLIMPEXP_BASE
wxUseFilterClasses();
357 class WXDLLIMPEXP_BASE wxFilterClassFactoryBase
: public wxObject
360 virtual ~wxFilterClassFactoryBase() { }
362 wxString
GetProtocol() const { return wxString(*GetProtocols()); }
363 wxString
PopExtension(const wxString
& location
) const;
365 virtual const wxChar
* const *GetProtocols(wxStreamProtocolType type
366 = wxSTREAM_PROTOCOL
) const = 0;
368 bool CanHandle(const wxString
& protocol
,
369 wxStreamProtocolType type
370 = wxSTREAM_PROTOCOL
) const;
373 wxString::size_type
FindExtension(const wxString
& location
) const;
375 DECLARE_ABSTRACT_CLASS(wxFilterClassFactoryBase
)
378 class WXDLLIMPEXP_BASE wxFilterClassFactory
: public wxFilterClassFactoryBase
381 virtual ~wxFilterClassFactory() { }
383 virtual wxFilterInputStream
*NewStream(wxInputStream
& stream
) const = 0;
384 virtual wxFilterOutputStream
*NewStream(wxOutputStream
& stream
) const = 0;
385 virtual wxFilterInputStream
*NewStream(wxInputStream
*stream
) const = 0;
386 virtual wxFilterOutputStream
*NewStream(wxOutputStream
*stream
) const = 0;
388 static const wxFilterClassFactory
*Find(const wxString
& protocol
,
389 wxStreamProtocolType type
390 = wxSTREAM_PROTOCOL
);
392 static const wxFilterClassFactory
*GetFirst();
393 const wxFilterClassFactory
*GetNext() const { return m_next
; }
395 void PushFront() { Remove(); m_next
= sm_first
; sm_first
= this; }
399 wxFilterClassFactory() : m_next(this) { }
401 wxFilterClassFactory
& operator=(const wxFilterClassFactory
&)
405 static wxFilterClassFactory
*sm_first
;
406 wxFilterClassFactory
*m_next
;
408 DECLARE_ABSTRACT_CLASS(wxFilterClassFactory
)
411 // ============================================================================
413 // ============================================================================
415 // ---------------------------------------------------------------------------
416 // Stream buffer: this class can be derived from and passed to
417 // wxBufferedStreams to implement custom buffering
418 // ---------------------------------------------------------------------------
420 class WXDLLIMPEXP_BASE wxStreamBuffer
430 wxStreamBuffer(wxStreamBase
& stream
, BufMode mode
)
432 InitWithStream(stream
, mode
);
435 wxStreamBuffer(size_t bufsize
, wxInputStream
& stream
)
437 InitWithStream(stream
, read
);
438 SetBufferIO(bufsize
);
441 wxStreamBuffer(size_t bufsize
, wxOutputStream
& stream
)
443 InitWithStream(stream
, write
);
444 SetBufferIO(bufsize
);
447 wxStreamBuffer(const wxStreamBuffer
& buf
);
448 virtual ~wxStreamBuffer();
451 virtual size_t Read(void *buffer
, size_t size
);
452 size_t Read(wxStreamBuffer
*buf
);
453 virtual size_t Write(const void *buffer
, size_t size
);
454 size_t Write(wxStreamBuffer
*buf
);
457 virtual char GetChar();
458 virtual void PutChar(char c
);
459 virtual wxFileOffset
Tell() const;
460 virtual wxFileOffset
Seek(wxFileOffset pos
, wxSeekMode mode
);
466 // NB: the buffer must always be allocated with malloc() if takeOwn is
467 // true as it will be deallocated by free()
468 void SetBufferIO(void *start
, void *end
, bool takeOwnership
= false);
469 void SetBufferIO(void *start
, size_t len
, bool takeOwnership
= false);
470 void SetBufferIO(size_t bufsize
);
471 void *GetBufferStart() const { return m_buffer_start
; }
472 void *GetBufferEnd() const { return m_buffer_end
; }
473 void *GetBufferPos() const { return m_buffer_pos
; }
474 size_t GetBufferSize() const { return m_buffer_end
- m_buffer_start
; }
475 size_t GetIntPosition() const { return m_buffer_pos
- m_buffer_start
; }
476 void SetIntPosition(size_t pos
) { m_buffer_pos
= m_buffer_start
+ pos
; }
477 size_t GetLastAccess() const { return m_buffer_end
- m_buffer_start
; }
478 size_t GetBytesLeft() const { return m_buffer_end
- m_buffer_pos
; }
480 void Fixed(bool fixed
) { m_fixed
= fixed
; }
481 void Flushable(bool f
) { m_flushable
= f
; }
485 size_t GetDataLeft();
488 wxStreamBase
*GetStream() const { return m_stream
; }
489 bool HasBuffer() const { return m_buffer_start
!= m_buffer_end
; }
491 bool IsFixed() const { return m_fixed
; }
492 bool IsFlushable() const { return m_flushable
; }
494 // only for input/output buffers respectively, returns NULL otherwise
495 wxInputStream
*GetInputStream() const;
496 wxOutputStream
*GetOutputStream() const;
498 #if WXWIN_COMPATIBILITY_2_6
499 // deprecated, for compatibility only
500 wxDEPRECATED( wxStreamBase
*Stream() );
501 #endif // WXWIN_COMPATIBILITY_2_6
503 // this constructs a dummy wxStreamBuffer, used by (and exists for)
504 // wxMemoryStreams only, don't use!
505 wxStreamBuffer(BufMode mode
);
508 void GetFromBuffer(void *buffer
, size_t size
);
509 void PutToBuffer(const void *buffer
, size_t size
);
511 // set the last error to the specified value if we didn't have it before
512 void SetError(wxStreamError err
);
514 // common part of several ctors
517 // common part of ctors taking wxStreamBase parameter
518 void InitWithStream(wxStreamBase
& stream
, BufMode mode
);
520 // init buffer variables to be empty
523 // free the buffer (always safe to call)
526 // the buffer itself: the pointers to its start and end and the current
527 // position in the buffer
528 char *m_buffer_start
,
532 // the stream we're associated with
533 wxStreamBase
*m_stream
;
539 bool m_destroybuf
, // deallocate buffer?
544 wxDECLARE_NO_ASSIGN_CLASS(wxStreamBuffer
);
547 // ---------------------------------------------------------------------------
548 // wxBufferedInputStream
549 // ---------------------------------------------------------------------------
551 class WXDLLIMPEXP_BASE wxBufferedInputStream
: public wxFilterInputStream
554 // create a buffered stream on top of the specified low-level stream
556 // if a non NULL buffer is given to the stream, it will be deleted by it,
557 // otherwise a default 1KB buffer will be used
558 wxBufferedInputStream(wxInputStream
& stream
,
559 wxStreamBuffer
*buffer
= NULL
);
561 // ctor allowing to specify the buffer size, it's just a more convenient
562 // alternative to creating wxStreamBuffer, calling its SetBufferIO(bufsize)
563 // and using the ctor above
564 wxBufferedInputStream(wxInputStream
& stream
, size_t bufsize
);
567 virtual ~wxBufferedInputStream();
570 wxInputStream
& Read(void *buffer
, size_t size
);
572 // Position functions
573 wxFileOffset
SeekI(wxFileOffset pos
, wxSeekMode mode
= wxFromStart
);
574 wxFileOffset
TellI() const;
575 bool IsSeekable() const { return m_parent_i_stream
->IsSeekable(); }
577 // the buffer given to the stream will be deleted by it
578 void SetInputStreamBuffer(wxStreamBuffer
*buffer
);
579 wxStreamBuffer
*GetInputStreamBuffer() const { return m_i_streambuf
; }
581 #if WXWIN_COMPATIBILITY_2_6
582 // deprecated, for compatibility only
583 wxDEPRECATED( wxStreamBuffer
*InputStreamBuffer() const );
584 #endif // WXWIN_COMPATIBILITY_2_6
587 virtual size_t OnSysRead(void *buffer
, size_t bufsize
);
588 virtual wxFileOffset
OnSysSeek(wxFileOffset seek
, wxSeekMode mode
);
589 virtual wxFileOffset
OnSysTell() const;
591 wxStreamBuffer
*m_i_streambuf
;
593 wxDECLARE_NO_COPY_CLASS(wxBufferedInputStream
);
596 // ----------------------------------------------------------------------------
597 // wxBufferedOutputStream
598 // ----------------------------------------------------------------------------
600 class WXDLLIMPEXP_BASE wxBufferedOutputStream
: public wxFilterOutputStream
603 // create a buffered stream on top of the specified low-level stream
605 // if a non NULL buffer is given to the stream, it will be deleted by it,
606 // otherwise a default 1KB buffer will be used
607 wxBufferedOutputStream(wxOutputStream
& stream
,
608 wxStreamBuffer
*buffer
= NULL
);
610 // ctor allowing to specify the buffer size, it's just a more convenient
611 // alternative to creating wxStreamBuffer, calling its SetBufferIO(bufsize)
612 // and using the ctor above
613 wxBufferedOutputStream(wxOutputStream
& stream
, size_t bufsize
);
615 virtual ~wxBufferedOutputStream();
617 wxOutputStream
& Write(const void *buffer
, size_t size
);
619 // Position functions
620 wxFileOffset
SeekO(wxFileOffset pos
, wxSeekMode mode
= wxFromStart
);
621 wxFileOffset
TellO() const;
622 bool IsSeekable() const { return m_parent_o_stream
->IsSeekable(); }
627 wxFileOffset
GetLength() const;
629 // the buffer given to the stream will be deleted by it
630 void SetOutputStreamBuffer(wxStreamBuffer
*buffer
);
631 wxStreamBuffer
*GetOutputStreamBuffer() const { return m_o_streambuf
; }
633 #if WXWIN_COMPATIBILITY_2_6
634 // deprecated, for compatibility only
635 wxDEPRECATED( wxStreamBuffer
*OutputStreamBuffer() const );
636 #endif // WXWIN_COMPATIBILITY_2_6
639 virtual size_t OnSysWrite(const void *buffer
, size_t bufsize
);
640 virtual wxFileOffset
OnSysSeek(wxFileOffset seek
, wxSeekMode mode
);
641 virtual wxFileOffset
OnSysTell() const;
643 wxStreamBuffer
*m_o_streambuf
;
645 wxDECLARE_NO_COPY_CLASS(wxBufferedOutputStream
);
648 #if WXWIN_COMPATIBILITY_2_6
649 inline wxStreamBase
*wxStreamBuffer::Stream() { return m_stream
; }
650 inline wxStreamBuffer
*wxBufferedInputStream::InputStreamBuffer() const { return m_i_streambuf
; }
651 inline wxStreamBuffer
*wxBufferedOutputStream::OutputStreamBuffer() const { return m_o_streambuf
; }
652 #endif // WXWIN_COMPATIBILITY_2_6
654 // ---------------------------------------------------------------------------
655 // wxWrapperInputStream: forwards all IO to another stream.
656 // ---------------------------------------------------------------------------
658 class WXDLLIMPEXP_BASE wxWrapperInputStream
: public wxFilterInputStream
661 // Constructor fully initializing the stream. The overload taking pointer
662 // takes ownership of the parent stream, the one taking reference does not.
664 // Notice that this class also has a default ctor but it's protected as the
665 // derived class is supposed to take care of calling InitParentStream() if
667 wxWrapperInputStream(wxInputStream
& stream
);
668 wxWrapperInputStream(wxInputStream
* stream
);
670 // Override the base class methods to forward to the wrapped stream.
671 virtual wxFileOffset
GetLength() const;
672 virtual bool IsSeekable() const;
675 virtual size_t OnSysRead(void *buffer
, size_t size
);
676 virtual wxFileOffset
OnSysSeek(wxFileOffset pos
, wxSeekMode mode
);
677 virtual wxFileOffset
OnSysTell() const;
679 // Ensure that our own last error is the same as that of the real stream.
681 // This method is const because the error must be updated even from const
682 // methods (in other words, it really should have been mutable in the first
684 void SynchronizeLastError() const
686 const_cast<wxWrapperInputStream
*>(this)->
687 Reset(m_parent_i_stream
->GetLastError());
690 // Default constructor, use InitParentStream() later.
691 wxWrapperInputStream();
693 // Set up the wrapped stream for an object initialized using the default
694 // constructor. The ownership logic is the same as above.
695 void InitParentStream(wxInputStream
& stream
);
696 void InitParentStream(wxInputStream
* stream
);
698 wxDECLARE_NO_COPY_CLASS(wxWrapperInputStream
);
702 #endif // wxUSE_STREAMS
704 #endif // _WX_WXSTREAM_H__