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 #if WXWIN_COMPATIBILITY_2_2
89 // deprecated, for compatibility only
90 wxDEPRECATED( wxStreamError
LastError() const );
91 wxDEPRECATED( size_t StreamSize() const );
92 #endif // WXWIN_COMPATIBILITY_2_2
95 virtual wxFileOffset
OnSysSeek(wxFileOffset seek
, wxSeekMode mode
);
96 virtual wxFileOffset
OnSysTell() const;
99 wxStreamError m_lasterror
;
101 friend class wxStreamBuffer
;
103 DECLARE_NO_COPY_CLASS(wxStreamBase
)
106 // ----------------------------------------------------------------------------
107 // wxInputStream: base class for the input streams
108 // ----------------------------------------------------------------------------
110 class WXDLLIMPEXP_BASE wxInputStream
: public wxStreamBase
113 // ctor and dtor, nothing exciting
115 virtual ~wxInputStream();
121 // return a character from the stream without removing it, i.e. it will
122 // still be returned by the next call to GetC()
124 // blocks until something appears in the stream if necessary, if nothing
125 // ever does (i.e. EOF) LastRead() will return 0 (and the return value is
126 // undefined), otherwise 1
129 // return one character from the stream, blocking until it appears if
132 // if EOF, return value is undefined and LastRead() will return 0 and not 1
135 // read at most the given number of bytes from the stream
137 // there are 2 possible situations here: either there is nothing at all in
138 // the stream right now in which case Read() blocks until something appears
139 // (use CanRead() to avoid this) or there is already some data available in
140 // the stream and then Read() doesn't block but returns just the data it
141 // can read without waiting for more
143 // in any case, if there are not enough bytes in the stream right now,
144 // LastRead() value will be less than size but greater than 0. If it is 0,
145 // it means that EOF has been reached.
146 virtual wxInputStream
& Read(void *buffer
, size_t size
);
148 // copy the entire contents of this stream into streamOut, stopping only
149 // when EOF is reached or an error occurs
150 wxInputStream
& Read(wxOutputStream
& streamOut
);
156 // returns the number of bytes read by the last call to Read(), GetC() or
159 // this should be used to discover whether that call succeeded in reading
160 // all the requested data or not
161 virtual size_t LastRead() const { return wxStreamBase::m_lastcount
; }
163 // returns true if some data is available in the stream right now, so that
164 // calling Read() wouldn't block
165 virtual bool CanRead() const;
167 // is the stream at EOF?
169 // note that this cannot be really implemented for all streams and
170 // CanRead() is more reliable than Eof()
171 virtual bool Eof() const;
177 // put back the specified number of bytes into the stream, they will be
178 // fetched by the next call to the read functions
180 // returns the number of bytes really stuffed back
181 size_t Ungetch(const void *buffer
, size_t size
);
183 // put back the specified character in the stream
185 // returns true if ok, false on error
186 bool Ungetch(char c
);
189 // position functions
190 // ------------------
192 // move the stream pointer to the given position (if the stream supports
195 // returns wxInvalidOffset on error
196 virtual wxFileOffset
SeekI(wxFileOffset pos
, wxSeekMode mode
= wxFromStart
);
198 // return the current position of the stream pointer or wxInvalidOffset
199 virtual wxFileOffset
TellI() const;
202 // stream-like operators
203 // ---------------------
205 wxInputStream
& operator>>(wxOutputStream
& out
) { return Read(out
); }
206 wxInputStream
& operator>>(__wxInputManip func
) { return func(*this); }
209 // do read up to size bytes of data into the provided buffer
211 // this method should return 0 if EOF has been reached or an error occured
212 // (m_lasterror should be set accordingly as well) or the number of bytes
214 virtual size_t OnSysRead(void *buffer
, size_t size
) = 0;
216 // write-back buffer support
217 // -------------------------
219 // return the pointer to a buffer big enough to hold sizeNeeded bytes
220 char *AllocSpaceWBack(size_t sizeNeeded
);
222 // read up to size data from the write back buffer, return the number of
224 size_t GetWBack(void *buf
, size_t size
);
226 // write back buffer or NULL if none
229 // the size of the buffer
232 // the current position in the buffer
235 friend class wxStreamBuffer
;
237 DECLARE_NO_COPY_CLASS(wxInputStream
)
240 // ----------------------------------------------------------------------------
241 // wxOutputStream: base for the output streams
242 // ----------------------------------------------------------------------------
244 class WXDLLIMPEXP_BASE wxOutputStream
: public wxStreamBase
248 virtual ~wxOutputStream();
251 virtual wxOutputStream
& Write(const void *buffer
, size_t size
);
252 wxOutputStream
& Write(wxInputStream
& stream_in
);
254 virtual wxFileOffset
SeekO(wxFileOffset pos
, wxSeekMode mode
= wxFromStart
);
255 virtual wxFileOffset
TellO() const;
257 virtual size_t LastWrite() const { return wxStreamBase::m_lastcount
; }
260 virtual bool Close() { return true; }
262 wxOutputStream
& operator<<(wxInputStream
& out
) { return Write(out
); }
263 wxOutputStream
& operator<<( __wxOutputManip func
) { return func(*this); }
266 // to be implemented in the derived classes (it should have been pure
268 virtual size_t OnSysWrite(const void *buffer
, size_t bufsize
);
270 friend class wxStreamBuffer
;
272 DECLARE_NO_COPY_CLASS(wxOutputStream
)
275 // ============================================================================
276 // helper stream classes
277 // ============================================================================
279 // ---------------------------------------------------------------------------
280 // A stream for measuring streamed output
281 // ---------------------------------------------------------------------------
283 class WXDLLIMPEXP_BASE wxCountingOutputStream
: public wxOutputStream
286 wxCountingOutputStream();
288 wxFileOffset
GetLength() const;
289 bool Ok() const { return true; }
292 virtual size_t OnSysWrite(const void *buffer
, size_t size
);
293 virtual wxFileOffset
OnSysSeek(wxFileOffset pos
, wxSeekMode mode
);
294 virtual wxFileOffset
OnSysTell() const;
298 DECLARE_NO_COPY_CLASS(wxCountingOutputStream
)
301 // ---------------------------------------------------------------------------
303 // ---------------------------------------------------------------------------
305 class WXDLLIMPEXP_BASE wxFilterInputStream
: public wxInputStream
308 wxFilterInputStream();
309 wxFilterInputStream(wxInputStream
& stream
);
310 virtual ~wxFilterInputStream();
312 char Peek() { return m_parent_i_stream
->Peek(); }
314 wxFileOffset
GetLength() const { return m_parent_i_stream
->GetLength(); }
316 wxInputStream
*GetFilterInputStream() const { return m_parent_i_stream
; }
319 wxInputStream
*m_parent_i_stream
;
321 DECLARE_NO_COPY_CLASS(wxFilterInputStream
)
324 class WXDLLIMPEXP_BASE wxFilterOutputStream
: public wxOutputStream
327 wxFilterOutputStream();
328 wxFilterOutputStream(wxOutputStream
& stream
);
329 virtual ~wxFilterOutputStream();
331 wxFileOffset
GetLength() const { return m_parent_o_stream
->GetLength(); }
333 wxOutputStream
*GetFilterOutputStream() const { return m_parent_o_stream
; }
336 wxOutputStream
*m_parent_o_stream
;
338 DECLARE_NO_COPY_CLASS(wxFilterOutputStream
)
341 // ============================================================================
343 // ============================================================================
345 // ---------------------------------------------------------------------------
346 // Stream buffer: this class can be derived from and passed to
347 // wxBufferedStreams to implement custom buffering
348 // ---------------------------------------------------------------------------
350 class WXDLLIMPEXP_BASE wxStreamBuffer
360 wxStreamBuffer(wxStreamBase
& stream
, BufMode mode
);
361 wxStreamBuffer(const wxStreamBuffer
& buf
);
362 virtual ~wxStreamBuffer();
365 virtual size_t Read(void *buffer
, size_t size
);
366 size_t Read(wxStreamBuffer
*buf
);
367 virtual size_t Write(const void *buffer
, size_t size
);
368 size_t Write(wxStreamBuffer
*buf
);
371 virtual char GetChar();
372 virtual void PutChar(char c
);
373 virtual wxFileOffset
Tell() const;
374 virtual wxFileOffset
Seek(wxFileOffset pos
, wxSeekMode mode
);
379 // NB: the buffer must always be allocated with malloc() if takeOwn is
380 // true as it will be deallocated by free()
381 void SetBufferIO(void *start
, void *end
, bool takeOwnership
= false);
382 void SetBufferIO(void *start
, size_t len
, bool takeOwnership
= false);
383 void SetBufferIO(size_t bufsize
);
384 void *GetBufferStart() const { return m_buffer_start
; }
385 void *GetBufferEnd() const { return m_buffer_end
; }
386 void *GetBufferPos() const { return m_buffer_pos
; }
387 size_t GetBufferSize() const { return m_buffer_size
; }
388 size_t GetIntPosition() const { return m_buffer_pos
- m_buffer_start
; }
389 void SetIntPosition(size_t pos
) { m_buffer_pos
= m_buffer_start
+ pos
; }
390 size_t GetLastAccess() const { return m_buffer_end
- m_buffer_start
; }
391 size_t GetBytesLeft() const { return m_buffer_end
- m_buffer_pos
; }
393 void Fixed(bool fixed
) { m_fixed
= fixed
; }
394 void Flushable(bool f
) { m_flushable
= f
; }
398 size_t GetDataLeft();
401 wxStreamBase
*GetStream() const { return m_stream
; }
402 bool HasBuffer() const { return m_buffer_size
!= 0; }
404 bool IsFixed() const { return m_fixed
; }
405 bool IsFlushable() const { return m_flushable
; }
407 // only for input/output buffers respectively, returns NULL otherwise
408 wxInputStream
*GetInputStream() const;
409 wxOutputStream
*GetOutputStream() const;
411 // deprecated, for compatibility only
412 wxStreamBase
*Stream() { return m_stream
; }
414 // this constructs a dummy wxStreamBuffer, used by (and exists for)
415 // wxMemoryStreams only, don't use!
416 wxStreamBuffer(BufMode mode
);
419 void GetFromBuffer(void *buffer
, size_t size
);
420 void PutToBuffer(const void *buffer
, size_t size
);
422 // set the last error to the specified value if we didn't have it before
423 void SetError(wxStreamError err
);
425 // common part of several ctors
428 // init buffer variables to be empty
431 // free the buffer (always safe to call)
434 // the buffer itself: the pointers to its start and end and the current
435 // position in the buffer
436 char *m_buffer_start
,
441 // FIXME: isn't it the same as m_buffer_end - m_buffer_start? (VZ)
442 size_t m_buffer_size
;
444 // the stream we're associated with
445 wxStreamBase
*m_stream
;
451 bool m_destroybuf
, // deallocate buffer?
457 // DECLARE_NO_COPY_CLASS(wxStreamBuffer)
458 // because copy constructor is explicitly declared above;
459 // but no copy assignment operator is defined, so declare
460 // it private to prevent the compiler from defining it:
461 wxStreamBuffer
& operator=(const wxStreamBuffer
&);
464 // ---------------------------------------------------------------------------
465 // wxBufferedInputStream
466 // ---------------------------------------------------------------------------
468 class WXDLLIMPEXP_BASE wxBufferedInputStream
: public wxFilterInputStream
471 // if a non NULL buffer is given to the stream, it will be deleted by it
472 wxBufferedInputStream(wxInputStream
& stream
,
473 wxStreamBuffer
*buffer
= NULL
);
474 virtual ~wxBufferedInputStream();
477 wxInputStream
& Read(void *buffer
, size_t size
);
479 // Position functions
480 wxFileOffset
SeekI(wxFileOffset pos
, wxSeekMode mode
= wxFromStart
);
481 wxFileOffset
TellI() const;
483 // the buffer given to the stream will be deleted by it
484 void SetInputStreamBuffer(wxStreamBuffer
*buffer
);
485 wxStreamBuffer
*GetInputStreamBuffer() const { return m_i_streambuf
; }
487 // deprecated, for compatibility only
488 wxStreamBuffer
*InputStreamBuffer() const { return m_i_streambuf
; }
491 virtual size_t OnSysRead(void *buffer
, size_t bufsize
);
492 virtual wxFileOffset
OnSysSeek(wxFileOffset seek
, wxSeekMode mode
);
493 virtual wxFileOffset
OnSysTell() const;
495 wxStreamBuffer
*m_i_streambuf
;
497 DECLARE_NO_COPY_CLASS(wxBufferedInputStream
)
500 // ----------------------------------------------------------------------------
501 // wxBufferedOutputStream
502 // ----------------------------------------------------------------------------
504 class WXDLLIMPEXP_BASE wxBufferedOutputStream
: public wxFilterOutputStream
507 // if a non NULL buffer is given to the stream, it will be deleted by it
508 wxBufferedOutputStream(wxOutputStream
& stream
,
509 wxStreamBuffer
*buffer
= NULL
);
510 virtual ~wxBufferedOutputStream();
512 wxOutputStream
& Write(const void *buffer
, size_t size
);
514 // Position functions
515 wxFileOffset
SeekO(wxFileOffset pos
, wxSeekMode mode
= wxFromStart
);
516 wxFileOffset
TellO() const;
521 wxFileOffset
GetLength() const;
523 // the buffer given to the stream will be deleted by it
524 void SetOutputStreamBuffer(wxStreamBuffer
*buffer
);
525 wxStreamBuffer
*GetOutputStreamBuffer() const { return m_o_streambuf
; }
527 // deprecated, for compatibility only
528 wxStreamBuffer
*OutputStreamBuffer() const { return m_o_streambuf
; }
531 virtual size_t OnSysWrite(const void *buffer
, size_t bufsize
);
532 virtual wxFileOffset
OnSysSeek(wxFileOffset seek
, wxSeekMode mode
);
533 virtual wxFileOffset
OnSysTell() const;
535 wxStreamBuffer
*m_o_streambuf
;
537 DECLARE_NO_COPY_CLASS(wxBufferedOutputStream
)
540 #endif // wxUSE_STREAMS
542 #endif // _WX_WXSTREAM_H__