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
45 // ============================================================================
46 // base stream classes: wxInputStream and wxOutputStream
47 // ============================================================================
49 // ---------------------------------------------------------------------------
50 // wxStreamBase: common (but non virtual!) base for all stream classes
51 // ---------------------------------------------------------------------------
53 class WXDLLIMPEXP_BASE wxStreamBase
57 virtual ~wxStreamBase();
60 wxStreamError
GetLastError() const { return m_lasterror
; }
61 virtual bool IsOk() const { return GetLastError() == wxSTREAM_NO_ERROR
; }
62 bool operator!() const { return !IsOk(); }
64 // reset the stream state
65 void Reset() { m_lasterror
= wxSTREAM_NO_ERROR
; }
67 // this doesn't make sense for all streams, always test its return value
68 virtual size_t GetSize() const;
69 virtual wxFileOffset
GetLength() const { return wxInvalidOffset
; }
71 // returns true if the streams supports seeking to arbitrary offsets
72 virtual bool IsSeekable() const { return false; }
75 virtual wxFileOffset
OnSysSeek(wxFileOffset seek
, wxSeekMode mode
);
76 virtual wxFileOffset
OnSysTell() const;
79 wxStreamError m_lasterror
;
81 friend class wxStreamBuffer
;
83 DECLARE_NO_COPY_CLASS(wxStreamBase
)
86 // ----------------------------------------------------------------------------
87 // wxInputStream: base class for the input streams
88 // ----------------------------------------------------------------------------
90 class WXDLLIMPEXP_BASE wxInputStream
: public wxStreamBase
93 // ctor and dtor, nothing exciting
95 virtual ~wxInputStream();
101 // return a character from the stream without removing it, i.e. it will
102 // still be returned by the next call to GetC()
104 // blocks until something appears in the stream if necessary, if nothing
105 // ever does (i.e. EOF) LastRead() will return 0 (and the return value is
106 // undefined), otherwise 1
109 // return one character from the stream, blocking until it appears if
112 // if EOF, return value is undefined and LastRead() will return 0 and not 1
115 // read at most the given number of bytes from the stream
117 // there are 2 possible situations here: either there is nothing at all in
118 // the stream right now in which case Read() blocks until something appears
119 // (use CanRead() to avoid this) or there is already some data available in
120 // the stream and then Read() doesn't block but returns just the data it
121 // can read without waiting for more
123 // in any case, if there are not enough bytes in the stream right now,
124 // LastRead() value will be less than size but greater than 0. If it is 0,
125 // it means that EOF has been reached.
126 virtual wxInputStream
& Read(void *buffer
, size_t size
);
128 // copy the entire contents of this stream into streamOut, stopping only
129 // when EOF is reached or an error occurs
130 wxInputStream
& Read(wxOutputStream
& streamOut
);
136 // returns the number of bytes read by the last call to Read(), GetC() or
139 // this should be used to discover whether that call succeeded in reading
140 // all the requested data or not
141 virtual size_t LastRead() const { return wxStreamBase::m_lastcount
; }
143 // returns true if some data is available in the stream right now, so that
144 // calling Read() wouldn't block
145 virtual bool CanRead() const;
147 // is the stream at EOF?
149 // note that this cannot be really implemented for all streams and
150 // CanRead() is more reliable than Eof()
151 virtual bool Eof() const;
157 // put back the specified number of bytes into the stream, they will be
158 // fetched by the next call to the read functions
160 // returns the number of bytes really stuffed back
161 size_t Ungetch(const void *buffer
, size_t size
);
163 // put back the specified character in the stream
165 // returns true if ok, false on error
166 bool Ungetch(char c
);
169 // position functions
170 // ------------------
172 // move the stream pointer to the given position (if the stream supports
175 // returns wxInvalidOffset on error
176 virtual wxFileOffset
SeekI(wxFileOffset pos
, wxSeekMode mode
= wxFromStart
);
178 // return the current position of the stream pointer or wxInvalidOffset
179 virtual wxFileOffset
TellI() const;
182 // stream-like operators
183 // ---------------------
185 wxInputStream
& operator>>(wxOutputStream
& out
) { return Read(out
); }
186 wxInputStream
& operator>>(__wxInputManip func
) { return func(*this); }
189 // do read up to size bytes of data into the provided buffer
191 // this method should return 0 if EOF has been reached or an error occurred
192 // (m_lasterror should be set accordingly as well) or the number of bytes
194 virtual size_t OnSysRead(void *buffer
, size_t size
) = 0;
196 // write-back buffer support
197 // -------------------------
199 // return the pointer to a buffer big enough to hold sizeNeeded bytes
200 char *AllocSpaceWBack(size_t sizeNeeded
);
202 // read up to size data from the write back buffer, return the number of
204 size_t GetWBack(void *buf
, size_t size
);
206 // write back buffer or NULL if none
209 // the size of the buffer
212 // the current position in the buffer
215 friend class wxStreamBuffer
;
217 DECLARE_NO_COPY_CLASS(wxInputStream
)
220 // ----------------------------------------------------------------------------
221 // wxOutputStream: base for the output streams
222 // ----------------------------------------------------------------------------
224 class WXDLLIMPEXP_BASE wxOutputStream
: public wxStreamBase
228 virtual ~wxOutputStream();
231 virtual wxOutputStream
& Write(const void *buffer
, size_t size
);
232 wxOutputStream
& Write(wxInputStream
& stream_in
);
234 virtual wxFileOffset
SeekO(wxFileOffset pos
, wxSeekMode mode
= wxFromStart
);
235 virtual wxFileOffset
TellO() const;
237 virtual size_t LastWrite() const { return wxStreamBase::m_lastcount
; }
240 virtual bool Close() { return true; }
242 wxOutputStream
& operator<<(wxInputStream
& out
) { return Write(out
); }
243 wxOutputStream
& operator<<( __wxOutputManip func
) { return func(*this); }
246 // to be implemented in the derived classes (it should have been pure
248 virtual size_t OnSysWrite(const void *buffer
, size_t bufsize
);
250 friend class wxStreamBuffer
;
252 DECLARE_NO_COPY_CLASS(wxOutputStream
)
255 // ============================================================================
256 // helper stream classes
257 // ============================================================================
259 // ---------------------------------------------------------------------------
260 // A stream for measuring streamed output
261 // ---------------------------------------------------------------------------
263 class WXDLLIMPEXP_BASE wxCountingOutputStream
: public wxOutputStream
266 wxCountingOutputStream();
268 wxFileOffset
GetLength() const;
269 bool Ok() const { return IsOk(); }
270 bool IsOk() const { return true; }
273 virtual size_t OnSysWrite(const void *buffer
, size_t size
);
274 virtual wxFileOffset
OnSysSeek(wxFileOffset pos
, wxSeekMode mode
);
275 virtual wxFileOffset
OnSysTell() const;
279 DECLARE_NO_COPY_CLASS(wxCountingOutputStream
)
282 // ---------------------------------------------------------------------------
284 // ---------------------------------------------------------------------------
286 class WXDLLIMPEXP_BASE wxFilterInputStream
: public wxInputStream
289 wxFilterInputStream();
290 wxFilterInputStream(wxInputStream
& stream
);
291 virtual ~wxFilterInputStream();
293 char Peek() { return m_parent_i_stream
->Peek(); }
295 wxFileOffset
GetLength() const { return m_parent_i_stream
->GetLength(); }
297 wxInputStream
*GetFilterInputStream() const { return m_parent_i_stream
; }
300 wxInputStream
*m_parent_i_stream
;
302 DECLARE_NO_COPY_CLASS(wxFilterInputStream
)
305 class WXDLLIMPEXP_BASE wxFilterOutputStream
: public wxOutputStream
308 wxFilterOutputStream();
309 wxFilterOutputStream(wxOutputStream
& stream
);
310 virtual ~wxFilterOutputStream();
312 wxFileOffset
GetLength() const { return m_parent_o_stream
->GetLength(); }
314 wxOutputStream
*GetFilterOutputStream() const { return m_parent_o_stream
; }
317 wxOutputStream
*m_parent_o_stream
;
319 DECLARE_NO_COPY_CLASS(wxFilterOutputStream
)
322 // ============================================================================
324 // ============================================================================
326 // ---------------------------------------------------------------------------
327 // Stream buffer: this class can be derived from and passed to
328 // wxBufferedStreams to implement custom buffering
329 // ---------------------------------------------------------------------------
331 class WXDLLIMPEXP_BASE wxStreamBuffer
341 wxStreamBuffer(wxStreamBase
& stream
, BufMode mode
);
342 wxStreamBuffer(const wxStreamBuffer
& buf
);
343 virtual ~wxStreamBuffer();
346 virtual size_t Read(void *buffer
, size_t size
);
347 size_t Read(wxStreamBuffer
*buf
);
348 virtual size_t Write(const void *buffer
, size_t size
);
349 size_t Write(wxStreamBuffer
*buf
);
352 virtual char GetChar();
353 virtual void PutChar(char c
);
354 virtual wxFileOffset
Tell() const;
355 virtual wxFileOffset
Seek(wxFileOffset pos
, wxSeekMode mode
);
360 // NB: the buffer must always be allocated with malloc() if takeOwn is
361 // true as it will be deallocated by free()
362 void SetBufferIO(void *start
, void *end
, bool takeOwnership
= false);
363 void SetBufferIO(void *start
, size_t len
, bool takeOwnership
= false);
364 void SetBufferIO(size_t bufsize
);
365 void *GetBufferStart() const { return m_buffer_start
; }
366 void *GetBufferEnd() const { return m_buffer_end
; }
367 void *GetBufferPos() const { return m_buffer_pos
; }
368 size_t GetBufferSize() const { return m_buffer_size
; }
369 size_t GetIntPosition() const { return m_buffer_pos
- m_buffer_start
; }
370 void SetIntPosition(size_t pos
) { m_buffer_pos
= m_buffer_start
+ pos
; }
371 size_t GetLastAccess() const { return m_buffer_end
- m_buffer_start
; }
372 size_t GetBytesLeft() const { return m_buffer_end
- m_buffer_pos
; }
374 void Fixed(bool fixed
) { m_fixed
= fixed
; }
375 void Flushable(bool f
) { m_flushable
= f
; }
379 size_t GetDataLeft();
382 wxStreamBase
*GetStream() const { return m_stream
; }
383 bool HasBuffer() const { return m_buffer_size
!= 0; }
385 bool IsFixed() const { return m_fixed
; }
386 bool IsFlushable() const { return m_flushable
; }
388 // only for input/output buffers respectively, returns NULL otherwise
389 wxInputStream
*GetInputStream() const;
390 wxOutputStream
*GetOutputStream() const;
392 #if WXWIN_COMPATIBILITY_2_6
393 // deprecated, for compatibility only
394 wxDEPRECATED( wxStreamBase
*Stream() );
395 #endif // WXWIN_COMPATIBILITY_2_6
397 // this constructs a dummy wxStreamBuffer, used by (and exists for)
398 // wxMemoryStreams only, don't use!
399 wxStreamBuffer(BufMode mode
);
402 void GetFromBuffer(void *buffer
, size_t size
);
403 void PutToBuffer(const void *buffer
, size_t size
);
405 // set the last error to the specified value if we didn't have it before
406 void SetError(wxStreamError err
);
408 // common part of several ctors
411 // init buffer variables to be empty
414 // free the buffer (always safe to call)
417 // the buffer itself: the pointers to its start and end and the current
418 // position in the buffer
419 char *m_buffer_start
,
424 // FIXME: isn't it the same as m_buffer_end - m_buffer_start? (VZ)
425 size_t m_buffer_size
;
427 // the stream we're associated with
428 wxStreamBase
*m_stream
;
434 bool m_destroybuf
, // deallocate buffer?
440 // DECLARE_NO_COPY_CLASS(wxStreamBuffer)
441 // because copy constructor is explicitly declared above;
442 // but no copy assignment operator is defined, so declare
443 // it private to prevent the compiler from defining it:
444 wxStreamBuffer
& operator=(const wxStreamBuffer
&);
447 // ---------------------------------------------------------------------------
448 // wxBufferedInputStream
449 // ---------------------------------------------------------------------------
451 class WXDLLIMPEXP_BASE wxBufferedInputStream
: public wxFilterInputStream
454 // if a non NULL buffer is given to the stream, it will be deleted by it
455 wxBufferedInputStream(wxInputStream
& stream
,
456 wxStreamBuffer
*buffer
= NULL
);
457 virtual ~wxBufferedInputStream();
460 wxInputStream
& Read(void *buffer
, size_t size
);
462 // Position functions
463 wxFileOffset
SeekI(wxFileOffset pos
, wxSeekMode mode
= wxFromStart
);
464 wxFileOffset
TellI() const;
465 bool IsSeekable() const { return m_parent_i_stream
->IsSeekable(); }
467 // the buffer given to the stream will be deleted by it
468 void SetInputStreamBuffer(wxStreamBuffer
*buffer
);
469 wxStreamBuffer
*GetInputStreamBuffer() const { return m_i_streambuf
; }
471 #if WXWIN_COMPATIBILITY_2_6
472 // deprecated, for compatibility only
473 wxDEPRECATED( wxStreamBuffer
*InputStreamBuffer() const );
474 #endif // WXWIN_COMPATIBILITY_2_6
477 virtual size_t OnSysRead(void *buffer
, size_t bufsize
);
478 virtual wxFileOffset
OnSysSeek(wxFileOffset seek
, wxSeekMode mode
);
479 virtual wxFileOffset
OnSysTell() const;
481 wxStreamBuffer
*m_i_streambuf
;
483 DECLARE_NO_COPY_CLASS(wxBufferedInputStream
)
486 // ----------------------------------------------------------------------------
487 // wxBufferedOutputStream
488 // ----------------------------------------------------------------------------
490 class WXDLLIMPEXP_BASE wxBufferedOutputStream
: public wxFilterOutputStream
493 // if a non NULL buffer is given to the stream, it will be deleted by it
494 wxBufferedOutputStream(wxOutputStream
& stream
,
495 wxStreamBuffer
*buffer
= NULL
);
496 virtual ~wxBufferedOutputStream();
498 wxOutputStream
& Write(const void *buffer
, size_t size
);
500 // Position functions
501 wxFileOffset
SeekO(wxFileOffset pos
, wxSeekMode mode
= wxFromStart
);
502 wxFileOffset
TellO() const;
503 bool IsSeekable() const { return m_parent_o_stream
->IsSeekable(); }
508 wxFileOffset
GetLength() const;
510 // the buffer given to the stream will be deleted by it
511 void SetOutputStreamBuffer(wxStreamBuffer
*buffer
);
512 wxStreamBuffer
*GetOutputStreamBuffer() const { return m_o_streambuf
; }
514 #if WXWIN_COMPATIBILITY_2_6
515 // deprecated, for compatibility only
516 wxDEPRECATED( wxStreamBuffer
*OutputStreamBuffer() const );
517 #endif // WXWIN_COMPATIBILITY_2_6
520 virtual size_t OnSysWrite(const void *buffer
, size_t bufsize
);
521 virtual wxFileOffset
OnSysSeek(wxFileOffset seek
, wxSeekMode mode
);
522 virtual wxFileOffset
OnSysTell() const;
524 wxStreamBuffer
*m_o_streambuf
;
526 DECLARE_NO_COPY_CLASS(wxBufferedOutputStream
)
529 #if WXWIN_COMPATIBILITY_2_6
530 inline wxStreamBase
*wxStreamBuffer::Stream() { return m_stream
; }
531 inline wxStreamBuffer
*wxBufferedInputStream::InputStreamBuffer() const { return m_i_streambuf
; }
532 inline wxStreamBuffer
*wxBufferedOutputStream::OutputStreamBuffer() const { return m_o_streambuf
; }
533 #endif // WXWIN_COMPATIBILITY_2_6
535 #endif // wxUSE_STREAMS
537 #endif // _WX_WXSTREAM_H__