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__
20 #include "wx/object.h"
21 #include "wx/string.h"
22 #include "wx/filefn.h" // for wxFileOffset, wxInvalidOffset and wxSeekMode
24 class WXDLLIMPEXP_BASE wxStreamBase
;
25 class WXDLLIMPEXP_BASE wxInputStream
;
26 class WXDLLIMPEXP_BASE wxOutputStream
;
28 typedef wxInputStream
& (*__wxInputManip
)(wxInputStream
&);
29 typedef wxOutputStream
& (*__wxOutputManip
)(wxOutputStream
&);
31 WXDLLIMPEXP_BASE wxOutputStream
& wxEndL(wxOutputStream
& o_stream
);
33 // ----------------------------------------------------------------------------
35 // ----------------------------------------------------------------------------
39 wxSTREAM_NO_ERROR
= 0, // stream is in good state
40 wxSTREAM_EOF
, // EOF reached in Read() or similar
41 wxSTREAM_WRITE_ERROR
, // generic write error
42 wxSTREAM_READ_ERROR
// generic read error
47 // ============================================================================
48 // base stream classes: wxInputStream and wxOutputStream
49 // ============================================================================
51 // ---------------------------------------------------------------------------
52 // wxStreamBase: common (but non virtual!) base for all stream classes
53 // ---------------------------------------------------------------------------
55 class WXDLLIMPEXP_BASE wxStreamBase
59 virtual ~wxStreamBase();
62 wxStreamError
GetLastError() const { return m_lasterror
; }
63 virtual bool IsOk() const { return GetLastError() == wxSTREAM_NO_ERROR
; }
64 bool operator!() const { return !IsOk(); }
66 // reset the stream state
67 void Reset() { m_lasterror
= wxSTREAM_NO_ERROR
; }
69 // this doesn't make sense for all streams, always test its return value
70 virtual size_t GetSize() const;
71 virtual wxFileOffset
GetLength() const { return wxInvalidOffset
; }
73 // returns true if the streams supports seeking to arbitrary offsets
74 virtual bool IsSeekable() const { return false; }
77 virtual wxFileOffset
OnSysSeek(wxFileOffset seek
, wxSeekMode mode
);
78 virtual wxFileOffset
OnSysTell() const;
81 wxStreamError m_lasterror
;
83 friend class wxStreamBuffer
;
85 DECLARE_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 // copy the entire contents of this stream into streamOut, stopping only
131 // when EOF is reached or an error occurs
132 wxInputStream
& Read(wxOutputStream
& streamOut
);
138 // returns the number of bytes read by the last call to Read(), GetC() or
141 // this should be used to discover whether that call succeeded in reading
142 // all the requested data or not
143 virtual size_t LastRead() const { return wxStreamBase::m_lastcount
; }
145 // returns true if some data is available in the stream right now, so that
146 // calling Read() wouldn't block
147 virtual bool CanRead() const;
149 // is the stream at EOF?
151 // note that this cannot be really implemented for all streams and
152 // CanRead() is more reliable than Eof()
153 virtual bool Eof() const;
159 // put back the specified number of bytes into the stream, they will be
160 // fetched by the next call to the read functions
162 // returns the number of bytes really stuffed back
163 size_t Ungetch(const void *buffer
, size_t size
);
165 // put back the specified character in the stream
167 // returns true if ok, false on error
168 bool Ungetch(char c
);
171 // position functions
172 // ------------------
174 // move the stream pointer to the given position (if the stream supports
177 // returns wxInvalidOffset on error
178 virtual wxFileOffset
SeekI(wxFileOffset pos
, wxSeekMode mode
= wxFromStart
);
180 // return the current position of the stream pointer or wxInvalidOffset
181 virtual wxFileOffset
TellI() const;
184 // stream-like operators
185 // ---------------------
187 wxInputStream
& operator>>(wxOutputStream
& out
) { return Read(out
); }
188 wxInputStream
& operator>>(__wxInputManip func
) { return func(*this); }
191 // do read up to size bytes of data into the provided buffer
193 // this method should return 0 if EOF has been reached or an error occurred
194 // (m_lasterror should be set accordingly as well) or the number of bytes
196 virtual size_t OnSysRead(void *buffer
, size_t size
) = 0;
198 // write-back buffer support
199 // -------------------------
201 // return the pointer to a buffer big enough to hold sizeNeeded bytes
202 char *AllocSpaceWBack(size_t sizeNeeded
);
204 // read up to size data from the write back buffer, return the number of
206 size_t GetWBack(void *buf
, size_t size
);
208 // write back buffer or NULL if none
211 // the size of the buffer
214 // the current position in the buffer
217 friend class wxStreamBuffer
;
219 DECLARE_NO_COPY_CLASS(wxInputStream
)
222 // ----------------------------------------------------------------------------
223 // wxOutputStream: base for the output streams
224 // ----------------------------------------------------------------------------
226 class WXDLLIMPEXP_BASE wxOutputStream
: public wxStreamBase
230 virtual ~wxOutputStream();
233 virtual wxOutputStream
& Write(const void *buffer
, size_t size
);
234 wxOutputStream
& Write(wxInputStream
& stream_in
);
236 virtual wxFileOffset
SeekO(wxFileOffset pos
, wxSeekMode mode
= wxFromStart
);
237 virtual wxFileOffset
TellO() const;
239 virtual size_t LastWrite() const { return wxStreamBase::m_lastcount
; }
242 virtual bool Close() { return true; }
244 wxOutputStream
& operator<<(wxInputStream
& out
) { return Write(out
); }
245 wxOutputStream
& operator<<( __wxOutputManip func
) { return func(*this); }
248 // to be implemented in the derived classes (it should have been pure
250 virtual size_t OnSysWrite(const void *buffer
, size_t bufsize
);
252 friend class wxStreamBuffer
;
254 DECLARE_NO_COPY_CLASS(wxOutputStream
)
257 // ============================================================================
258 // helper stream classes
259 // ============================================================================
261 // ---------------------------------------------------------------------------
262 // A stream for measuring streamed output
263 // ---------------------------------------------------------------------------
265 class WXDLLIMPEXP_BASE wxCountingOutputStream
: public wxOutputStream
268 wxCountingOutputStream();
270 wxFileOffset
GetLength() const;
271 bool Ok() const { return IsOk(); }
272 bool IsOk() const { return true; }
275 virtual size_t OnSysWrite(const void *buffer
, size_t size
);
276 virtual wxFileOffset
OnSysSeek(wxFileOffset pos
, wxSeekMode mode
);
277 virtual wxFileOffset
OnSysTell() const;
281 DECLARE_NO_COPY_CLASS(wxCountingOutputStream
)
284 // ---------------------------------------------------------------------------
286 // ---------------------------------------------------------------------------
288 class WXDLLIMPEXP_BASE wxFilterInputStream
: public wxInputStream
291 wxFilterInputStream();
292 wxFilterInputStream(wxInputStream
& stream
);
293 wxFilterInputStream(wxInputStream
*stream
);
294 virtual ~wxFilterInputStream();
296 char Peek() { return m_parent_i_stream
->Peek(); }
298 wxFileOffset
GetLength() const { return m_parent_i_stream
->GetLength(); }
300 wxInputStream
*GetFilterInputStream() const { return m_parent_i_stream
; }
303 wxInputStream
*m_parent_i_stream
;
306 DECLARE_NO_COPY_CLASS(wxFilterInputStream
)
309 class WXDLLIMPEXP_BASE wxFilterOutputStream
: public wxOutputStream
312 wxFilterOutputStream();
313 wxFilterOutputStream(wxOutputStream
& stream
);
314 wxFilterOutputStream(wxOutputStream
*stream
);
315 virtual ~wxFilterOutputStream();
317 wxFileOffset
GetLength() const { return m_parent_o_stream
->GetLength(); }
319 wxOutputStream
*GetFilterOutputStream() const { return m_parent_o_stream
; }
324 wxOutputStream
*m_parent_o_stream
;
327 DECLARE_NO_COPY_CLASS(wxFilterOutputStream
)
330 enum wxStreamProtocolType
332 wxSTREAM_PROTOCOL
, // wxFileSystem protocol (should be only one)
333 wxSTREAM_MIMETYPE
, // MIME types the stream handles
334 wxSTREAM_ENCODING
, // The HTTP Content-Encodings the stream handles
335 wxSTREAM_FILEEXT
// File extensions the stream handles
338 void WXDLLIMPEXP_BASE
wxUseFilterClasses();
340 class WXDLLIMPEXP_BASE wxFilterClassFactoryBase
: public wxObject
343 virtual ~wxFilterClassFactoryBase() { }
345 wxString
GetProtocol() const { return wxString(*GetProtocols()); }
346 wxString
PopExtension(const wxString
& location
) const;
348 virtual const wxChar
* const *GetProtocols(wxStreamProtocolType type
349 = wxSTREAM_PROTOCOL
) const = 0;
351 bool CanHandle(const wxChar
*protocol
,
352 wxStreamProtocolType type
353 = wxSTREAM_PROTOCOL
) const;
356 wxString::size_type
FindExtension(const wxChar
*location
) const;
358 DECLARE_ABSTRACT_CLASS(wxFilterClassFactoryBase
)
361 class WXDLLIMPEXP_BASE wxFilterClassFactory
: public wxFilterClassFactoryBase
364 virtual ~wxFilterClassFactory() { }
366 virtual wxFilterInputStream
*NewStream(wxInputStream
& stream
) const = 0;
367 virtual wxFilterOutputStream
*NewStream(wxOutputStream
& stream
) const = 0;
368 virtual wxFilterInputStream
*NewStream(wxInputStream
*stream
) const = 0;
369 virtual wxFilterOutputStream
*NewStream(wxOutputStream
*stream
) const = 0;
371 static const wxFilterClassFactory
*Find(const wxChar
*protocol
,
372 wxStreamProtocolType type
373 = wxSTREAM_PROTOCOL
);
375 static const wxFilterClassFactory
*GetFirst();
376 const wxFilterClassFactory
*GetNext() const { return m_next
; }
378 void PushFront() { Remove(); m_next
= sm_first
; sm_first
= this; }
382 wxFilterClassFactory() : m_next(this) { }
384 wxFilterClassFactory
& operator=(const wxFilterClassFactory
&)
388 static wxFilterClassFactory
*sm_first
;
389 wxFilterClassFactory
*m_next
;
391 DECLARE_ABSTRACT_CLASS(wxFilterClassFactory
)
394 // ============================================================================
396 // ============================================================================
398 // ---------------------------------------------------------------------------
399 // Stream buffer: this class can be derived from and passed to
400 // wxBufferedStreams to implement custom buffering
401 // ---------------------------------------------------------------------------
403 class WXDLLIMPEXP_BASE wxStreamBuffer
413 wxStreamBuffer(wxStreamBase
& stream
, BufMode mode
);
414 wxStreamBuffer(const wxStreamBuffer
& buf
);
415 virtual ~wxStreamBuffer();
418 virtual size_t Read(void *buffer
, size_t size
);
419 size_t Read(wxStreamBuffer
*buf
);
420 virtual size_t Write(const void *buffer
, size_t size
);
421 size_t Write(wxStreamBuffer
*buf
);
424 virtual char GetChar();
425 virtual void PutChar(char c
);
426 virtual wxFileOffset
Tell() const;
427 virtual wxFileOffset
Seek(wxFileOffset pos
, wxSeekMode mode
);
432 // NB: the buffer must always be allocated with malloc() if takeOwn is
433 // true as it will be deallocated by free()
434 void SetBufferIO(void *start
, void *end
, bool takeOwnership
= false);
435 void SetBufferIO(void *start
, size_t len
, bool takeOwnership
= false);
436 void SetBufferIO(size_t bufsize
);
437 void *GetBufferStart() const { return m_buffer_start
; }
438 void *GetBufferEnd() const { return m_buffer_end
; }
439 void *GetBufferPos() const { return m_buffer_pos
; }
440 size_t GetBufferSize() const { return m_buffer_size
; }
441 size_t GetIntPosition() const { return m_buffer_pos
- m_buffer_start
; }
442 void SetIntPosition(size_t pos
) { m_buffer_pos
= m_buffer_start
+ pos
; }
443 size_t GetLastAccess() const { return m_buffer_end
- m_buffer_start
; }
444 size_t GetBytesLeft() const { return m_buffer_end
- m_buffer_pos
; }
446 void Fixed(bool fixed
) { m_fixed
= fixed
; }
447 void Flushable(bool f
) { m_flushable
= f
; }
451 size_t GetDataLeft();
454 wxStreamBase
*GetStream() const { return m_stream
; }
455 bool HasBuffer() const { return m_buffer_size
!= 0; }
457 bool IsFixed() const { return m_fixed
; }
458 bool IsFlushable() const { return m_flushable
; }
460 // only for input/output buffers respectively, returns NULL otherwise
461 wxInputStream
*GetInputStream() const;
462 wxOutputStream
*GetOutputStream() const;
464 #if WXWIN_COMPATIBILITY_2_6
465 // deprecated, for compatibility only
466 wxDEPRECATED( wxStreamBase
*Stream() );
467 #endif // WXWIN_COMPATIBILITY_2_6
469 // this constructs a dummy wxStreamBuffer, used by (and exists for)
470 // wxMemoryStreams only, don't use!
471 wxStreamBuffer(BufMode mode
);
474 void GetFromBuffer(void *buffer
, size_t size
);
475 void PutToBuffer(const void *buffer
, size_t size
);
477 // set the last error to the specified value if we didn't have it before
478 void SetError(wxStreamError err
);
480 // common part of several ctors
483 // init buffer variables to be empty
486 // free the buffer (always safe to call)
489 // the buffer itself: the pointers to its start and end and the current
490 // position in the buffer
491 char *m_buffer_start
,
496 // FIXME: isn't it the same as m_buffer_end - m_buffer_start? (VZ)
497 size_t m_buffer_size
;
499 // the stream we're associated with
500 wxStreamBase
*m_stream
;
506 bool m_destroybuf
, // deallocate buffer?
512 // DECLARE_NO_COPY_CLASS(wxStreamBuffer)
513 // because copy constructor is explicitly declared above;
514 // but no copy assignment operator is defined, so declare
515 // it private to prevent the compiler from defining it:
516 wxStreamBuffer
& operator=(const wxStreamBuffer
&);
519 // ---------------------------------------------------------------------------
520 // wxBufferedInputStream
521 // ---------------------------------------------------------------------------
523 class WXDLLIMPEXP_BASE wxBufferedInputStream
: public wxFilterInputStream
526 // if a non NULL buffer is given to the stream, it will be deleted by it
527 wxBufferedInputStream(wxInputStream
& stream
,
528 wxStreamBuffer
*buffer
= NULL
);
529 virtual ~wxBufferedInputStream();
532 wxInputStream
& Read(void *buffer
, size_t size
);
534 // Position functions
535 wxFileOffset
SeekI(wxFileOffset pos
, wxSeekMode mode
= wxFromStart
);
536 wxFileOffset
TellI() const;
537 bool IsSeekable() const { return m_parent_i_stream
->IsSeekable(); }
539 // the buffer given to the stream will be deleted by it
540 void SetInputStreamBuffer(wxStreamBuffer
*buffer
);
541 wxStreamBuffer
*GetInputStreamBuffer() const { return m_i_streambuf
; }
543 #if WXWIN_COMPATIBILITY_2_6
544 // deprecated, for compatibility only
545 wxDEPRECATED( wxStreamBuffer
*InputStreamBuffer() const );
546 #endif // WXWIN_COMPATIBILITY_2_6
549 virtual size_t OnSysRead(void *buffer
, size_t bufsize
);
550 virtual wxFileOffset
OnSysSeek(wxFileOffset seek
, wxSeekMode mode
);
551 virtual wxFileOffset
OnSysTell() const;
553 wxStreamBuffer
*m_i_streambuf
;
555 DECLARE_NO_COPY_CLASS(wxBufferedInputStream
)
558 // ----------------------------------------------------------------------------
559 // wxBufferedOutputStream
560 // ----------------------------------------------------------------------------
562 class WXDLLIMPEXP_BASE wxBufferedOutputStream
: public wxFilterOutputStream
565 // if a non NULL buffer is given to the stream, it will be deleted by it
566 wxBufferedOutputStream(wxOutputStream
& stream
,
567 wxStreamBuffer
*buffer
= NULL
);
568 virtual ~wxBufferedOutputStream();
570 wxOutputStream
& Write(const void *buffer
, size_t size
);
572 // Position functions
573 wxFileOffset
SeekO(wxFileOffset pos
, wxSeekMode mode
= wxFromStart
);
574 wxFileOffset
TellO() const;
575 bool IsSeekable() const { return m_parent_o_stream
->IsSeekable(); }
580 wxFileOffset
GetLength() const;
582 // the buffer given to the stream will be deleted by it
583 void SetOutputStreamBuffer(wxStreamBuffer
*buffer
);
584 wxStreamBuffer
*GetOutputStreamBuffer() const { return m_o_streambuf
; }
586 #if WXWIN_COMPATIBILITY_2_6
587 // deprecated, for compatibility only
588 wxDEPRECATED( wxStreamBuffer
*OutputStreamBuffer() const );
589 #endif // WXWIN_COMPATIBILITY_2_6
592 virtual size_t OnSysWrite(const void *buffer
, size_t bufsize
);
593 virtual wxFileOffset
OnSysSeek(wxFileOffset seek
, wxSeekMode mode
);
594 virtual wxFileOffset
OnSysTell() const;
596 wxStreamBuffer
*m_o_streambuf
;
598 DECLARE_NO_COPY_CLASS(wxBufferedOutputStream
)
601 #if WXWIN_COMPATIBILITY_2_6
602 inline wxStreamBase
*wxStreamBuffer::Stream() { return m_stream
; }
603 inline wxStreamBuffer
*wxBufferedInputStream::InputStreamBuffer() const { return m_i_streambuf
; }
604 inline wxStreamBuffer
*wxBufferedOutputStream::OutputStreamBuffer() const { return m_o_streambuf
; }
605 #endif // WXWIN_COMPATIBILITY_2_6
607 #endif // wxUSE_STREAMS
609 #endif // _WX_WXSTREAM_H__