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
46 #if WXWIN_COMPATIBILITY_2_2
47 #define wxStream_NOERROR wxSTREAM_NOERROR
48 #define wxStream_EOF wxSTREAM_EOF
49 #define wxStream_WRITE_ERR wxSTREAM_WRITE_ERROR
50 #define wxStream_READ_ERR wxSTREAM_READ_ERROR
52 #define wxSTREAM_NO_ERR wxSTREAM_NO_ERROR
53 #define wxSTREAM_NOERROR wxSTREAM_NO_ERROR
54 #define wxSTREAM_WRITE_ERR wxSTREAM_WRITE_ERROR
55 #define wxSTREAM_READ_ERR wxSTREAM_READ_ERROR
56 #endif // WXWIN_COMPATIBILITY_2_2
58 // ============================================================================
59 // base stream classes: wxInputStream and wxOutputStream
60 // ============================================================================
62 // ---------------------------------------------------------------------------
63 // wxStreamBase: common (but non virtual!) base for all stream classes
64 // ---------------------------------------------------------------------------
66 class WXDLLIMPEXP_BASE wxStreamBase
70 virtual ~wxStreamBase();
73 wxStreamError
GetLastError() const { return m_lasterror
; }
74 bool IsOk() const { return GetLastError() == wxSTREAM_NO_ERROR
; }
75 bool operator!() const { return !IsOk(); }
77 // reset the stream state
78 void Reset() { m_lasterror
= wxSTREAM_NO_ERROR
; }
80 // this doesn't make sense for all streams, always test its return value
81 virtual size_t GetSize() const;
82 virtual wxFileOffset
GetLength() const { return wxInvalidOffset
; }
84 // returns true if the streams supports seeking to arbitrary offsets
85 virtual bool IsSeekable() const { return false; }
87 #if WXWIN_COMPATIBILITY_2_2
88 // deprecated, for compatibility only
89 wxDEPRECATED( wxStreamError
LastError() const );
90 wxDEPRECATED( size_t StreamSize() const );
91 #endif // WXWIN_COMPATIBILITY_2_2
94 // Reserved for future use
95 virtual void ReservedStreamFunc1() {}
96 virtual void ReservedStreamFunc2() {}
97 virtual void ReservedStreamFunc3() {}
98 virtual void ReservedStreamFunc4() {}
99 virtual void ReservedStreamFunc5() {}
100 virtual void ReservedStreamFunc6() {}
101 virtual void ReservedStreamFunc7() {}
102 virtual void ReservedStreamFunc8() {}
103 virtual void ReservedStreamFunc9() {}
106 virtual wxFileOffset
OnSysSeek(wxFileOffset seek
, wxSeekMode mode
);
107 virtual wxFileOffset
OnSysTell() const;
110 wxStreamError m_lasterror
;
112 friend class wxStreamBuffer
;
114 DECLARE_NO_COPY_CLASS(wxStreamBase
)
117 // ----------------------------------------------------------------------------
118 // wxInputStream: base class for the input streams
119 // ----------------------------------------------------------------------------
121 class WXDLLIMPEXP_BASE wxInputStream
: public wxStreamBase
124 // ctor and dtor, nothing exciting
126 virtual ~wxInputStream();
132 // return a character from the stream without removing it, i.e. it will
133 // still be returned by the next call to GetC()
135 // blocks until something appears in the stream if necessary, if nothing
136 // ever does (i.e. EOF) LastRead() will return 0 (and the return value is
137 // undefined), otherwise 1
140 // return one character from the stream, blocking until it appears if
143 // if EOF, return value is undefined and LastRead() will return 0 and not 1
146 // read at most the given number of bytes from the stream
148 // there are 2 possible situations here: either there is nothing at all in
149 // the stream right now in which case Read() blocks until something appears
150 // (use CanRead() to avoid this) or there is already some data available in
151 // the stream and then Read() doesn't block but returns just the data it
152 // can read without waiting for more
154 // in any case, if there are not enough bytes in the stream right now,
155 // LastRead() value will be less than size but greater than 0. If it is 0,
156 // it means that EOF has been reached.
157 virtual wxInputStream
& Read(void *buffer
, size_t size
);
159 // copy the entire contents of this stream into streamOut, stopping only
160 // when EOF is reached or an error occurs
161 wxInputStream
& Read(wxOutputStream
& streamOut
);
167 // returns the number of bytes read by the last call to Read(), GetC() or
170 // this should be used to discover whether that call succeeded in reading
171 // all the requested data or not
172 virtual size_t LastRead() const { return wxStreamBase::m_lastcount
; }
174 // returns true if some data is available in the stream right now, so that
175 // calling Read() wouldn't block
176 virtual bool CanRead() const;
178 // is the stream at EOF?
180 // note that this cannot be really implemented for all streams and
181 // CanRead() is more reliable than Eof()
182 virtual bool Eof() const;
188 // put back the specified number of bytes into the stream, they will be
189 // fetched by the next call to the read functions
191 // returns the number of bytes really stuffed back
192 size_t Ungetch(const void *buffer
, size_t size
);
194 // put back the specified character in the stream
196 // returns true if ok, false on error
197 bool Ungetch(char c
);
200 // position functions
201 // ------------------
203 // move the stream pointer to the given position (if the stream supports
206 // returns wxInvalidOffset on error
207 virtual wxFileOffset
SeekI(wxFileOffset pos
, wxSeekMode mode
= wxFromStart
);
209 // return the current position of the stream pointer or wxInvalidOffset
210 virtual wxFileOffset
TellI() const;
213 // stream-like operators
214 // ---------------------
216 wxInputStream
& operator>>(wxOutputStream
& out
) { return Read(out
); }
217 wxInputStream
& operator>>(__wxInputManip func
) { return func(*this); }
220 // do read up to size bytes of data into the provided buffer
222 // this method should return 0 if EOF has been reached or an error occurred
223 // (m_lasterror should be set accordingly as well) or the number of bytes
225 virtual size_t OnSysRead(void *buffer
, size_t size
) = 0;
227 // write-back buffer support
228 // -------------------------
230 // return the pointer to a buffer big enough to hold sizeNeeded bytes
231 char *AllocSpaceWBack(size_t sizeNeeded
);
233 // read up to size data from the write back buffer, return the number of
235 size_t GetWBack(void *buf
, size_t size
);
237 // write back buffer or NULL if none
240 // the size of the buffer
243 // the current position in the buffer
246 friend class wxStreamBuffer
;
248 DECLARE_NO_COPY_CLASS(wxInputStream
)
251 // ----------------------------------------------------------------------------
252 // wxOutputStream: base for the output streams
253 // ----------------------------------------------------------------------------
255 class WXDLLIMPEXP_BASE wxOutputStream
: public wxStreamBase
259 virtual ~wxOutputStream();
262 virtual wxOutputStream
& Write(const void *buffer
, size_t size
);
263 wxOutputStream
& Write(wxInputStream
& stream_in
);
265 virtual wxFileOffset
SeekO(wxFileOffset pos
, wxSeekMode mode
= wxFromStart
);
266 virtual wxFileOffset
TellO() const;
268 virtual size_t LastWrite() const { return wxStreamBase::m_lastcount
; }
271 virtual bool Close() { return true; }
273 wxOutputStream
& operator<<(wxInputStream
& out
) { return Write(out
); }
274 wxOutputStream
& operator<<( __wxOutputManip func
) { return func(*this); }
277 // to be implemented in the derived classes (it should have been pure
279 virtual size_t OnSysWrite(const void *buffer
, size_t bufsize
);
281 friend class wxStreamBuffer
;
283 DECLARE_NO_COPY_CLASS(wxOutputStream
)
286 // ============================================================================
287 // helper stream classes
288 // ============================================================================
290 // ---------------------------------------------------------------------------
291 // A stream for measuring streamed output
292 // ---------------------------------------------------------------------------
294 class WXDLLIMPEXP_BASE wxCountingOutputStream
: public wxOutputStream
297 wxCountingOutputStream();
299 wxFileOffset
GetLength() const;
300 bool Ok() const { return true; }
303 virtual size_t OnSysWrite(const void *buffer
, size_t size
);
304 virtual wxFileOffset
OnSysSeek(wxFileOffset pos
, wxSeekMode mode
);
305 virtual wxFileOffset
OnSysTell() const;
309 DECLARE_NO_COPY_CLASS(wxCountingOutputStream
)
312 // ---------------------------------------------------------------------------
314 // ---------------------------------------------------------------------------
316 class WXDLLIMPEXP_BASE wxFilterInputStream
: public wxInputStream
319 wxFilterInputStream();
320 wxFilterInputStream(wxInputStream
& stream
);
321 virtual ~wxFilterInputStream();
323 char Peek() { return m_parent_i_stream
->Peek(); }
325 wxFileOffset
GetLength() const { return m_parent_i_stream
->GetLength(); }
327 wxInputStream
*GetFilterInputStream() const { return m_parent_i_stream
; }
330 wxInputStream
*m_parent_i_stream
;
332 DECLARE_NO_COPY_CLASS(wxFilterInputStream
)
335 class WXDLLIMPEXP_BASE wxFilterOutputStream
: public wxOutputStream
338 wxFilterOutputStream();
339 wxFilterOutputStream(wxOutputStream
& stream
);
340 virtual ~wxFilterOutputStream();
342 wxFileOffset
GetLength() const { return m_parent_o_stream
->GetLength(); }
344 wxOutputStream
*GetFilterOutputStream() const { return m_parent_o_stream
; }
347 wxOutputStream
*m_parent_o_stream
;
349 DECLARE_NO_COPY_CLASS(wxFilterOutputStream
)
352 // ============================================================================
354 // ============================================================================
356 // ---------------------------------------------------------------------------
357 // Stream buffer: this class can be derived from and passed to
358 // wxBufferedStreams to implement custom buffering
359 // ---------------------------------------------------------------------------
361 class WXDLLIMPEXP_BASE wxStreamBuffer
371 wxStreamBuffer(wxStreamBase
& stream
, BufMode mode
);
372 wxStreamBuffer(const wxStreamBuffer
& buf
);
373 virtual ~wxStreamBuffer();
376 virtual size_t Read(void *buffer
, size_t size
);
377 size_t Read(wxStreamBuffer
*buf
);
378 virtual size_t Write(const void *buffer
, size_t size
);
379 size_t Write(wxStreamBuffer
*buf
);
382 virtual char GetChar();
383 virtual void PutChar(char c
);
384 virtual wxFileOffset
Tell() const;
385 virtual wxFileOffset
Seek(wxFileOffset pos
, wxSeekMode mode
);
390 // NB: the buffer must always be allocated with malloc() if takeOwn is
391 // true as it will be deallocated by free()
392 void SetBufferIO(void *start
, void *end
, bool takeOwnership
= false);
393 void SetBufferIO(void *start
, size_t len
, bool takeOwnership
= false);
394 void SetBufferIO(size_t bufsize
);
395 void *GetBufferStart() const { return m_buffer_start
; }
396 void *GetBufferEnd() const { return m_buffer_end
; }
397 void *GetBufferPos() const { return m_buffer_pos
; }
398 size_t GetBufferSize() const { return m_buffer_size
; }
399 size_t GetIntPosition() const { return m_buffer_pos
- m_buffer_start
; }
400 void SetIntPosition(size_t pos
) { m_buffer_pos
= m_buffer_start
+ pos
; }
401 size_t GetLastAccess() const { return m_buffer_end
- m_buffer_start
; }
402 size_t GetBytesLeft() const { return m_buffer_end
- m_buffer_pos
; }
404 void Fixed(bool fixed
) { m_fixed
= fixed
; }
405 void Flushable(bool f
) { m_flushable
= f
; }
409 size_t GetDataLeft();
412 wxStreamBase
*GetStream() const { return m_stream
; }
413 bool HasBuffer() const { return m_buffer_size
!= 0; }
415 bool IsFixed() const { return m_fixed
; }
416 bool IsFlushable() const { return m_flushable
; }
418 // only for input/output buffers respectively, returns NULL otherwise
419 wxInputStream
*GetInputStream() const;
420 wxOutputStream
*GetOutputStream() const;
422 // deprecated, for compatibility only
423 wxStreamBase
*Stream() { return m_stream
; }
425 // this constructs a dummy wxStreamBuffer, used by (and exists for)
426 // wxMemoryStreams only, don't use!
427 wxStreamBuffer(BufMode mode
);
430 void GetFromBuffer(void *buffer
, size_t size
);
431 void PutToBuffer(const void *buffer
, size_t size
);
433 // set the last error to the specified value if we didn't have it before
434 void SetError(wxStreamError err
);
436 // common part of several ctors
439 // init buffer variables to be empty
442 // free the buffer (always safe to call)
445 // the buffer itself: the pointers to its start and end and the current
446 // position in the buffer
447 char *m_buffer_start
,
452 // FIXME: isn't it the same as m_buffer_end - m_buffer_start? (VZ)
453 size_t m_buffer_size
;
455 // the stream we're associated with
456 wxStreamBase
*m_stream
;
462 bool m_destroybuf
, // deallocate buffer?
468 // DECLARE_NO_COPY_CLASS(wxStreamBuffer)
469 // because copy constructor is explicitly declared above;
470 // but no copy assignment operator is defined, so declare
471 // it private to prevent the compiler from defining it:
472 wxStreamBuffer
& operator=(const wxStreamBuffer
&);
475 // ---------------------------------------------------------------------------
476 // wxBufferedInputStream
477 // ---------------------------------------------------------------------------
479 class WXDLLIMPEXP_BASE wxBufferedInputStream
: public wxFilterInputStream
482 // if a non NULL buffer is given to the stream, it will be deleted by it
483 wxBufferedInputStream(wxInputStream
& stream
,
484 wxStreamBuffer
*buffer
= NULL
);
485 virtual ~wxBufferedInputStream();
488 wxInputStream
& Read(void *buffer
, size_t size
);
490 // Position functions
491 wxFileOffset
SeekI(wxFileOffset pos
, wxSeekMode mode
= wxFromStart
);
492 wxFileOffset
TellI() const;
493 bool IsSeekable() const { return m_parent_i_stream
->IsSeekable(); }
495 // the buffer given to the stream will be deleted by it
496 void SetInputStreamBuffer(wxStreamBuffer
*buffer
);
497 wxStreamBuffer
*GetInputStreamBuffer() const { return m_i_streambuf
; }
499 // deprecated, for compatibility only
500 wxStreamBuffer
*InputStreamBuffer() const { return m_i_streambuf
; }
503 virtual size_t OnSysRead(void *buffer
, size_t bufsize
);
504 virtual wxFileOffset
OnSysSeek(wxFileOffset seek
, wxSeekMode mode
);
505 virtual wxFileOffset
OnSysTell() const;
507 wxStreamBuffer
*m_i_streambuf
;
509 DECLARE_NO_COPY_CLASS(wxBufferedInputStream
)
512 // ----------------------------------------------------------------------------
513 // wxBufferedOutputStream
514 // ----------------------------------------------------------------------------
516 class WXDLLIMPEXP_BASE wxBufferedOutputStream
: public wxFilterOutputStream
519 // if a non NULL buffer is given to the stream, it will be deleted by it
520 wxBufferedOutputStream(wxOutputStream
& stream
,
521 wxStreamBuffer
*buffer
= NULL
);
522 virtual ~wxBufferedOutputStream();
524 wxOutputStream
& Write(const void *buffer
, size_t size
);
526 // Position functions
527 wxFileOffset
SeekO(wxFileOffset pos
, wxSeekMode mode
= wxFromStart
);
528 wxFileOffset
TellO() const;
529 bool IsSeekable() const { return m_parent_o_stream
->IsSeekable(); }
534 wxFileOffset
GetLength() const;
536 // the buffer given to the stream will be deleted by it
537 void SetOutputStreamBuffer(wxStreamBuffer
*buffer
);
538 wxStreamBuffer
*GetOutputStreamBuffer() const { return m_o_streambuf
; }
540 // deprecated, for compatibility only
541 wxStreamBuffer
*OutputStreamBuffer() const { return m_o_streambuf
; }
544 virtual size_t OnSysWrite(const void *buffer
, size_t bufsize
);
545 virtual wxFileOffset
OnSysSeek(wxFileOffset seek
, wxSeekMode mode
);
546 virtual wxFileOffset
OnSysTell() const;
548 wxStreamBuffer
*m_o_streambuf
;
550 DECLARE_NO_COPY_CLASS(wxBufferedOutputStream
)
553 #endif // wxUSE_STREAMS
555 #endif // _WX_WXSTREAM_H__