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 wxFileOffset, 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;
86 virtual wxFileOffset
GetLength() const { return wxInvalidOffset
; }
88 // returns true if the streams supports seeking to arbitrary offsets
89 virtual bool IsSeekable() const { return false; }
91 #if WXWIN_COMPATIBILITY_2_2
92 // deprecated, for compatibility only
93 wxDEPRECATED( wxStreamError
LastError() const );
94 wxDEPRECATED( size_t StreamSize() const );
95 #endif // WXWIN_COMPATIBILITY_2_2
98 virtual wxFileOffset
OnSysSeek(wxFileOffset seek
, wxSeekMode mode
);
99 virtual wxFileOffset
OnSysTell() const;
102 wxStreamError m_lasterror
;
104 friend class wxStreamBuffer
;
106 DECLARE_NO_COPY_CLASS(wxStreamBase
)
109 // ----------------------------------------------------------------------------
110 // wxInputStream: base class for the input streams
111 // ----------------------------------------------------------------------------
113 class WXDLLIMPEXP_BASE wxInputStream
: public wxStreamBase
116 // ctor and dtor, nothing exciting
118 virtual ~wxInputStream();
124 // return a character from the stream without removing it, i.e. it will
125 // still be returned by the next call to GetC()
127 // blocks until something appears in the stream if necessary, if nothing
128 // ever does (i.e. EOF) LastRead() will return 0 (and the return value is
129 // undefined), otherwise 1
132 // return one character from the stream, blocking until it appears if
135 // if EOF, return value is undefined and LastRead() will return 0 and not 1
138 // read at most the given number of bytes from the stream
140 // there are 2 possible situations here: either there is nothing at all in
141 // the stream right now in which case Read() blocks until something appears
142 // (use CanRead() to avoid this) or there is already some data available in
143 // the stream and then Read() doesn't block but returns just the data it
144 // can read without waiting for more
146 // in any case, if there are not enough bytes in the stream right now,
147 // LastRead() value will be less than size but greater than 0. If it is 0,
148 // it means that EOF has been reached.
149 virtual wxInputStream
& Read(void *buffer
, size_t size
);
151 // copy the entire contents of this stream into streamOut, stopping only
152 // when EOF is reached or an error occurs
153 wxInputStream
& Read(wxOutputStream
& streamOut
);
159 // returns the number of bytes read by the last call to Read(), GetC() or
162 // this should be used to discover whether that call succeeded in reading
163 // all the requested data or not
164 virtual size_t LastRead() const { return wxStreamBase::m_lastcount
; }
166 // returns true if some data is available in the stream right now, so that
167 // calling Read() wouldn't block
168 virtual bool CanRead() const;
170 // is the stream at EOF?
172 // note that this cannot be really implemented for all streams and
173 // CanRead() is more reliable than Eof()
174 virtual bool Eof() const;
180 // put back the specified number of bytes into the stream, they will be
181 // fetched by the next call to the read functions
183 // returns the number of bytes really stuffed back
184 size_t Ungetch(const void *buffer
, size_t size
);
186 // put back the specified character in the stream
188 // returns true if ok, false on error
189 bool Ungetch(char c
);
192 // position functions
193 // ------------------
195 // move the stream pointer to the given position (if the stream supports
198 // returns wxInvalidOffset on error
199 virtual wxFileOffset
SeekI(wxFileOffset pos
, wxSeekMode mode
= wxFromStart
);
201 // return the current position of the stream pointer or wxInvalidOffset
202 virtual wxFileOffset
TellI() const;
205 // stream-like operators
206 // ---------------------
208 wxInputStream
& operator>>(wxOutputStream
& out
) { return Read(out
); }
209 wxInputStream
& operator>>(__wxInputManip func
) { return func(*this); }
212 // do read up to size bytes of data into the provided buffer
214 // this method should return 0 if EOF has been reached or an error occured
215 // (m_lasterror should be set accordingly as well) or the number of bytes
217 virtual size_t OnSysRead(void *buffer
, size_t size
) = 0;
219 // write-back buffer support
220 // -------------------------
222 // return the pointer to a buffer big enough to hold sizeNeeded bytes
223 char *AllocSpaceWBack(size_t sizeNeeded
);
225 // read up to size data from the write back buffer, return the number of
227 size_t GetWBack(void *buf
, size_t size
);
229 // write back buffer or NULL if none
232 // the size of the buffer
235 // the current position in the buffer
238 friend class wxStreamBuffer
;
240 DECLARE_NO_COPY_CLASS(wxInputStream
)
243 // ----------------------------------------------------------------------------
244 // wxOutputStream: base for the output streams
245 // ----------------------------------------------------------------------------
247 class WXDLLIMPEXP_BASE wxOutputStream
: public wxStreamBase
251 virtual ~wxOutputStream();
254 virtual wxOutputStream
& Write(const void *buffer
, size_t size
);
255 wxOutputStream
& Write(wxInputStream
& stream_in
);
257 virtual wxFileOffset
SeekO(wxFileOffset pos
, wxSeekMode mode
= wxFromStart
);
258 virtual wxFileOffset
TellO() const;
260 virtual size_t LastWrite() const { return wxStreamBase::m_lastcount
; }
263 virtual bool Close() { return true; }
265 wxOutputStream
& operator<<(wxInputStream
& out
) { return Write(out
); }
266 wxOutputStream
& operator<<( __wxOutputManip func
) { return func(*this); }
269 // to be implemented in the derived classes (it should have been pure
271 virtual size_t OnSysWrite(const void *buffer
, size_t bufsize
);
273 friend class wxStreamBuffer
;
275 DECLARE_NO_COPY_CLASS(wxOutputStream
)
278 // ============================================================================
279 // helper stream classes
280 // ============================================================================
282 // ---------------------------------------------------------------------------
283 // A stream for measuring streamed output
284 // ---------------------------------------------------------------------------
286 class WXDLLIMPEXP_BASE wxCountingOutputStream
: public wxOutputStream
289 wxCountingOutputStream();
291 wxFileOffset
GetLength() const;
292 bool Ok() const { return true; }
295 virtual size_t OnSysWrite(const void *buffer
, size_t size
);
296 virtual wxFileOffset
OnSysSeek(wxFileOffset pos
, wxSeekMode mode
);
297 virtual wxFileOffset
OnSysTell() const;
301 DECLARE_NO_COPY_CLASS(wxCountingOutputStream
)
304 // ---------------------------------------------------------------------------
306 // ---------------------------------------------------------------------------
308 class WXDLLIMPEXP_BASE wxFilterInputStream
: public wxInputStream
311 wxFilterInputStream();
312 wxFilterInputStream(wxInputStream
& stream
);
313 virtual ~wxFilterInputStream();
315 char Peek() { return m_parent_i_stream
->Peek(); }
317 wxFileOffset
GetLength() const { return m_parent_i_stream
->GetLength(); }
319 wxInputStream
*GetFilterInputStream() const { return m_parent_i_stream
; }
322 wxInputStream
*m_parent_i_stream
;
324 DECLARE_NO_COPY_CLASS(wxFilterInputStream
)
327 class WXDLLIMPEXP_BASE wxFilterOutputStream
: public wxOutputStream
330 wxFilterOutputStream();
331 wxFilterOutputStream(wxOutputStream
& stream
);
332 virtual ~wxFilterOutputStream();
334 wxFileOffset
GetLength() const { return m_parent_o_stream
->GetLength(); }
336 wxOutputStream
*GetFilterOutputStream() const { return m_parent_o_stream
; }
339 wxOutputStream
*m_parent_o_stream
;
341 DECLARE_NO_COPY_CLASS(wxFilterOutputStream
)
344 // ============================================================================
346 // ============================================================================
348 // ---------------------------------------------------------------------------
349 // Stream buffer: this class can be derived from and passed to
350 // wxBufferedStreams to implement custom buffering
351 // ---------------------------------------------------------------------------
353 class WXDLLIMPEXP_BASE wxStreamBuffer
363 wxStreamBuffer(wxStreamBase
& stream
, BufMode mode
);
364 wxStreamBuffer(const wxStreamBuffer
& buf
);
365 virtual ~wxStreamBuffer();
368 virtual size_t Read(void *buffer
, size_t size
);
369 size_t Read(wxStreamBuffer
*buf
);
370 virtual size_t Write(const void *buffer
, size_t size
);
371 size_t Write(wxStreamBuffer
*buf
);
374 virtual char GetChar();
375 virtual void PutChar(char c
);
376 virtual wxFileOffset
Tell() const;
377 virtual wxFileOffset
Seek(wxFileOffset pos
, wxSeekMode mode
);
382 // NB: the buffer must always be allocated with malloc() if takeOwn is
383 // true as it will be deallocated by free()
384 void SetBufferIO(void *start
, void *end
, bool takeOwnership
= false);
385 void SetBufferIO(void *start
, size_t len
, bool takeOwnership
= false);
386 void SetBufferIO(size_t bufsize
);
387 void *GetBufferStart() const { return m_buffer_start
; }
388 void *GetBufferEnd() const { return m_buffer_end
; }
389 void *GetBufferPos() const { return m_buffer_pos
; }
390 size_t GetBufferSize() const { return m_buffer_size
; }
391 size_t GetIntPosition() const { return m_buffer_pos
- m_buffer_start
; }
392 void SetIntPosition(size_t pos
) { m_buffer_pos
= m_buffer_start
+ pos
; }
393 size_t GetLastAccess() const { return m_buffer_end
- m_buffer_start
; }
394 size_t GetBytesLeft() const { return m_buffer_end
- m_buffer_pos
; }
396 void Fixed(bool fixed
) { m_fixed
= fixed
; }
397 void Flushable(bool f
) { m_flushable
= f
; }
401 size_t GetDataLeft();
404 wxStreamBase
*GetStream() const { return m_stream
; }
405 bool HasBuffer() const { return m_buffer_size
!= 0; }
407 bool IsFixed() const { return m_fixed
; }
408 bool IsFlushable() const { return m_flushable
; }
410 // only for input/output buffers respectively, returns NULL otherwise
411 wxInputStream
*GetInputStream() const;
412 wxOutputStream
*GetOutputStream() const;
414 // deprecated, for compatibility only
415 wxStreamBase
*Stream() { return m_stream
; }
417 // this constructs a dummy wxStreamBuffer, used by (and exists for)
418 // wxMemoryStreams only, don't use!
419 wxStreamBuffer(BufMode mode
);
422 void GetFromBuffer(void *buffer
, size_t size
);
423 void PutToBuffer(const void *buffer
, size_t size
);
425 // set the last error to the specified value if we didn't have it before
426 void SetError(wxStreamError err
);
428 // common part of several ctors
431 // init buffer variables to be empty
434 // free the buffer (always safe to call)
437 // the buffer itself: the pointers to its start and end and the current
438 // position in the buffer
439 char *m_buffer_start
,
444 // FIXME: isn't it the same as m_buffer_end - m_buffer_start? (VZ)
445 size_t m_buffer_size
;
447 // the stream we're associated with
448 wxStreamBase
*m_stream
;
454 bool m_destroybuf
, // deallocate buffer?
460 // DECLARE_NO_COPY_CLASS(wxStreamBuffer)
461 // because copy constructor is explicitly declared above;
462 // but no copy assignment operator is defined, so declare
463 // it private to prevent the compiler from defining it:
464 wxStreamBuffer
& operator=(const wxStreamBuffer
&);
467 // ---------------------------------------------------------------------------
468 // wxBufferedInputStream
469 // ---------------------------------------------------------------------------
471 class WXDLLIMPEXP_BASE wxBufferedInputStream
: public wxFilterInputStream
474 // if a non NULL buffer is given to the stream, it will be deleted by it
475 wxBufferedInputStream(wxInputStream
& stream
,
476 wxStreamBuffer
*buffer
= NULL
);
477 virtual ~wxBufferedInputStream();
480 wxInputStream
& Read(void *buffer
, size_t size
);
482 // Position functions
483 wxFileOffset
SeekI(wxFileOffset pos
, wxSeekMode mode
= wxFromStart
);
484 wxFileOffset
TellI() const;
485 bool IsSeekable() const { return m_parent_i_stream
->IsSeekable(); }
487 // the buffer given to the stream will be deleted by it
488 void SetInputStreamBuffer(wxStreamBuffer
*buffer
);
489 wxStreamBuffer
*GetInputStreamBuffer() const { return m_i_streambuf
; }
491 // deprecated, for compatibility only
492 wxStreamBuffer
*InputStreamBuffer() const { return m_i_streambuf
; }
495 virtual size_t OnSysRead(void *buffer
, size_t bufsize
);
496 virtual wxFileOffset
OnSysSeek(wxFileOffset seek
, wxSeekMode mode
);
497 virtual wxFileOffset
OnSysTell() const;
499 wxStreamBuffer
*m_i_streambuf
;
501 DECLARE_NO_COPY_CLASS(wxBufferedInputStream
)
504 // ----------------------------------------------------------------------------
505 // wxBufferedOutputStream
506 // ----------------------------------------------------------------------------
508 class WXDLLIMPEXP_BASE wxBufferedOutputStream
: public wxFilterOutputStream
511 // if a non NULL buffer is given to the stream, it will be deleted by it
512 wxBufferedOutputStream(wxOutputStream
& stream
,
513 wxStreamBuffer
*buffer
= NULL
);
514 virtual ~wxBufferedOutputStream();
516 wxOutputStream
& Write(const void *buffer
, size_t size
);
518 // Position functions
519 wxFileOffset
SeekO(wxFileOffset pos
, wxSeekMode mode
= wxFromStart
);
520 wxFileOffset
TellO() const;
521 bool IsSeekable() const { return m_parent_o_stream
->IsSeekable(); }
526 wxFileOffset
GetLength() const;
528 // the buffer given to the stream will be deleted by it
529 void SetOutputStreamBuffer(wxStreamBuffer
*buffer
);
530 wxStreamBuffer
*GetOutputStreamBuffer() const { return m_o_streambuf
; }
532 // deprecated, for compatibility only
533 wxStreamBuffer
*OutputStreamBuffer() const { return m_o_streambuf
; }
536 virtual size_t OnSysWrite(const void *buffer
, size_t bufsize
);
537 virtual wxFileOffset
OnSysSeek(wxFileOffset seek
, wxSeekMode mode
);
538 virtual wxFileOffset
OnSysTell() const;
540 wxStreamBuffer
*m_o_streambuf
;
542 DECLARE_NO_COPY_CLASS(wxBufferedOutputStream
)
545 #endif // wxUSE_STREAMS
547 #endif // _WX_WXSTREAM_H__