]> git.saurik.com Git - wxWidgets.git/blame - include/wx/stream.h
Don't use wxCriticalSection in wxWakeUpPipeMT if wxUSE_THREADS==0.
[wxWidgets.git] / include / wx / stream.h
CommitLineData
32fc4afb 1/////////////////////////////////////////////////////////////////////////////
49e399d8 2// Name: wx/stream.h
2b5f62a0
VZ
3// Purpose: stream classes
4// Author: Guilhem Lavaux, Guillermo Rodriguez Garcia, Vadim Zeitlin
32fc4afb
GL
5// Modified by:
6// Created: 11/07/98
7// RCS-ID: $Id$
8// Copyright: (c) Guilhem Lavaux
65571936 9// Licence: wxWindows licence
32fc4afb
GL
10/////////////////////////////////////////////////////////////////////////////
11
34138703
JS
12#ifndef _WX_WXSTREAM_H__
13#define _WX_WXSTREAM_H__
32fc4afb 14
ce4169a4
RR
15#include "wx/defs.h"
16
17#if wxUSE_STREAMS
18
32fc4afb 19#include <stdio.h>
45ea509a
VZ
20#include "wx/object.h"
21#include "wx/string.h"
30984dea 22#include "wx/filefn.h" // for wxFileOffset, wxInvalidOffset and wxSeekMode
1678ad78 23
b5dbe15d
VS
24class WXDLLIMPEXP_FWD_BASE wxStreamBase;
25class WXDLLIMPEXP_FWD_BASE wxInputStream;
26class WXDLLIMPEXP_FWD_BASE wxOutputStream;
1678ad78
GL
27
28typedef wxInputStream& (*__wxInputManip)(wxInputStream&);
29typedef wxOutputStream& (*__wxOutputManip)(wxOutputStream&);
30
bddd7a8d 31WXDLLIMPEXP_BASE wxOutputStream& wxEndL(wxOutputStream& o_stream);
1678ad78 32
2b5f62a0
VZ
33// ----------------------------------------------------------------------------
34// constants
35// ----------------------------------------------------------------------------
f4ada568 36
49e399d8 37enum wxStreamError
cd6ce4a9 38{
2b5f62a0
VZ
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
49e399d8 43};
75ed1d15 44
6ea48c51
MW
45const int wxEOF = -1;
46
2b5f62a0
VZ
47// ============================================================================
48// base stream classes: wxInputStream and wxOutputStream
49// ============================================================================
50
51// ---------------------------------------------------------------------------
52// wxStreamBase: common (but non virtual!) base for all stream classes
53// ---------------------------------------------------------------------------
cd6ce4a9 54
6285e36b 55class WXDLLIMPEXP_BASE wxStreamBase : public wxObject
c7a9fa36
RR
56{
57public:
58 wxStreamBase();
59 virtual ~wxStreamBase();
75ed1d15 60
cd6ce4a9 61 // error testing
cd6ce4a9 62 wxStreamError GetLastError() const { return m_lasterror; }
d65fd4e9 63 virtual bool IsOk() const { return GetLastError() == wxSTREAM_NO_ERROR; }
2b5f62a0 64 bool operator!() const { return !IsOk(); }
cd6ce4a9 65
2b5f62a0 66 // reset the stream state
90693f47 67 void Reset(wxStreamError error = wxSTREAM_NO_ERROR) { m_lasterror = error; }
2b5f62a0 68
f6e7cd0a 69 // this doesn't make sense for all streams, always test its return value
588066b7
VZ
70 virtual size_t GetSize() const;
71 virtual wxFileOffset GetLength() const { return wxInvalidOffset; }
47fc03d2 72
3c70014d
MW
73 // returns true if the streams supports seeking to arbitrary offsets
74 virtual bool IsSeekable() const { return false; }
75
c7a9fa36 76protected:
4004775e
RL
77 virtual wxFileOffset OnSysSeek(wxFileOffset seek, wxSeekMode mode);
78 virtual wxFileOffset OnSysTell() const;
75ed1d15 79
c7a9fa36
RR
80 size_t m_lastcount;
81 wxStreamError m_lasterror;
2b5f62a0
VZ
82
83 friend class wxStreamBuffer;
22f3361e 84
6285e36b 85 DECLARE_ABSTRACT_CLASS(wxStreamBase)
c0c133e1 86 wxDECLARE_NO_COPY_CLASS(wxStreamBase);
75ed1d15
GL
87};
88
2b5f62a0
VZ
89// ----------------------------------------------------------------------------
90// wxInputStream: base class for the input streams
91// ----------------------------------------------------------------------------
92
bddd7a8d 93class WXDLLIMPEXP_BASE wxInputStream : public wxStreamBase
c7a9fa36
RR
94{
95public:
2b5f62a0 96 // ctor and dtor, nothing exciting
c7a9fa36
RR
97 wxInputStream();
98 virtual ~wxInputStream();
32fc4afb 99
cd6ce4a9 100
c7a9fa36 101 // IO functions
2b5f62a0
VZ
102 // ------------
103
104 // return a character from the stream without removing it, i.e. it will
105 // still be returned by the next call to GetC()
106 //
107 // blocks until something appears in the stream if necessary, if nothing
108 // ever does (i.e. EOF) LastRead() will return 0 (and the return value is
109 // undefined), otherwise 1
c7a9fa36 110 virtual char Peek();
2b5f62a0 111
6ea48c51 112 // return one byte from the stream, blocking until it appears if
2b5f62a0
VZ
113 // necessary
114 //
6ea48c51
MW
115 // on success returns a value between 0 - 255, or wxEOF on EOF or error.
116 int GetC();
2b5f62a0
VZ
117
118 // read at most the given number of bytes from the stream
119 //
120 // there are 2 possible situations here: either there is nothing at all in
121 // the stream right now in which case Read() blocks until something appears
122 // (use CanRead() to avoid this) or there is already some data available in
123 // the stream and then Read() doesn't block but returns just the data it
124 // can read without waiting for more
125 //
126 // in any case, if there are not enough bytes in the stream right now,
127 // LastRead() value will be less than size but greater than 0. If it is 0,
128 // it means that EOF has been reached.
c7a9fa36 129 virtual wxInputStream& Read(void *buffer, size_t size);
32fc4afb 130
cc437b96
VZ
131 // Read exactly the given number of bytes, unlike Read(), which may read
132 // less than the requested amount of data without returning an error, this
133 // method either reads all the data or returns false.
134 bool ReadAll(void *buffer, size_t size);
135
2b5f62a0
VZ
136 // copy the entire contents of this stream into streamOut, stopping only
137 // when EOF is reached or an error occurs
138 wxInputStream& Read(wxOutputStream& streamOut);
139
140
141 // status functions
142 // ----------------
143
144 // returns the number of bytes read by the last call to Read(), GetC() or
145 // Peek()
146 //
147 // this should be used to discover whether that call succeeded in reading
148 // all the requested data or not
149 virtual size_t LastRead() const { return wxStreamBase::m_lastcount; }
150
d775fa82 151 // returns true if some data is available in the stream right now, so that
2b5f62a0
VZ
152 // calling Read() wouldn't block
153 virtual bool CanRead() const;
32fc4afb 154
2b5f62a0
VZ
155 // is the stream at EOF?
156 //
157 // note that this cannot be really implemented for all streams and
158 // CanRead() is more reliable than Eof()
159 virtual bool Eof() const;
160
161
162 // write back buffer
163 // -----------------
fae05df5 164
2b5f62a0
VZ
165 // put back the specified number of bytes into the stream, they will be
166 // fetched by the next call to the read functions
167 //
168 // returns the number of bytes really stuffed back
c7a9fa36 169 size_t Ungetch(const void *buffer, size_t size);
2b5f62a0
VZ
170
171 // put back the specified character in the stream
172 //
d775fa82 173 // returns true if ok, false on error
c7a9fa36 174 bool Ungetch(char c);
0cd9bfe8 175
2b5f62a0
VZ
176
177 // position functions
178 // ------------------
179
180 // move the stream pointer to the given position (if the stream supports
181 // it)
182 //
183 // returns wxInvalidOffset on error
4004775e 184 virtual wxFileOffset SeekI(wxFileOffset pos, wxSeekMode mode = wxFromStart);
2b5f62a0
VZ
185
186 // return the current position of the stream pointer or wxInvalidOffset
4004775e 187 virtual wxFileOffset TellI() const;
2b5f62a0
VZ
188
189
190 // stream-like operators
191 // ---------------------
192
c7a9fa36 193 wxInputStream& operator>>(wxOutputStream& out) { return Read(out); }
2b5f62a0 194 wxInputStream& operator>>(__wxInputManip func) { return func(*this); }
fae05df5 195
c7a9fa36 196protected:
2b5f62a0
VZ
197 // do read up to size bytes of data into the provided buffer
198 //
3103e8a9 199 // this method should return 0 if EOF has been reached or an error occurred
2b5f62a0
VZ
200 // (m_lasterror should be set accordingly as well) or the number of bytes
201 // read
202 virtual size_t OnSysRead(void *buffer, size_t size) = 0;
203
204 // write-back buffer support
205 // -------------------------
206
207 // return the pointer to a buffer big enough to hold sizeNeeded bytes
208 char *AllocSpaceWBack(size_t sizeNeeded);
209
210 // read up to size data from the write back buffer, return the number of
211 // bytes read
212 size_t GetWBack(void *buf, size_t size);
47fc03d2 213
2b5f62a0 214 // write back buffer or NULL if none
c7a9fa36 215 char *m_wback;
2b5f62a0
VZ
216
217 // the size of the buffer
c7a9fa36 218 size_t m_wbacksize;
fae05df5 219
2b5f62a0
VZ
220 // the current position in the buffer
221 size_t m_wbackcur;
47fc03d2
VZ
222
223 friend class wxStreamBuffer;
fc7a2a60 224
6285e36b 225 DECLARE_ABSTRACT_CLASS(wxInputStream)
c0c133e1 226 wxDECLARE_NO_COPY_CLASS(wxInputStream);
32fc4afb
GL
227};
228
2b5f62a0
VZ
229// ----------------------------------------------------------------------------
230// wxOutputStream: base for the output streams
231// ----------------------------------------------------------------------------
232
bddd7a8d 233class WXDLLIMPEXP_BASE wxOutputStream : public wxStreamBase
c7a9fa36
RR
234{
235public:
236 wxOutputStream();
237 virtual ~wxOutputStream();
32fc4afb 238
c7a9fa36
RR
239 void PutC(char c);
240 virtual wxOutputStream& Write(const void *buffer, size_t size);
cc437b96
VZ
241
242 // This is ReadAll() equivalent for Write(): it either writes exactly the
243 // given number of bytes or returns false, unlike Write() which can write
244 // less data than requested but still return without error.
245 bool WriteAll(const void *buffer, size_t size);
246
c7a9fa36 247 wxOutputStream& Write(wxInputStream& stream_in);
32fc4afb 248
4004775e
RL
249 virtual wxFileOffset SeekO(wxFileOffset pos, wxSeekMode mode = wxFromStart);
250 virtual wxFileOffset TellO() const;
1678ad78 251
c7a9fa36 252 virtual size_t LastWrite() const { return wxStreamBase::m_lastcount; }
32fc4afb 253
c7a9fa36 254 virtual void Sync();
8f0ff178 255 virtual bool Close() { return true; }
32fc4afb 256
c7a9fa36 257 wxOutputStream& operator<<(wxInputStream& out) { return Write(out); }
c7a9fa36 258 wxOutputStream& operator<<( __wxOutputManip func) { return func(*this); }
47fc03d2
VZ
259
260protected:
261 // to be implemented in the derived classes (it should have been pure
262 // virtual)
263 virtual size_t OnSysWrite(const void *buffer, size_t bufsize);
264
265 friend class wxStreamBuffer;
fc7a2a60 266
6285e36b 267 DECLARE_ABSTRACT_CLASS(wxOutputStream)
c0c133e1 268 wxDECLARE_NO_COPY_CLASS(wxOutputStream);
32fc4afb
GL
269};
270
2b5f62a0
VZ
271// ============================================================================
272// helper stream classes
273// ============================================================================
274
e2acb9ae
RR
275// ---------------------------------------------------------------------------
276// A stream for measuring streamed output
277// ---------------------------------------------------------------------------
278
bddd7a8d 279class WXDLLIMPEXP_BASE wxCountingOutputStream : public wxOutputStream
c7a9fa36
RR
280{
281public:
282 wxCountingOutputStream();
e2acb9ae 283
9bc3af3e 284 virtual wxFileOffset GetLength() const;
b7cacb43 285 bool Ok() const { return IsOk(); }
9bc3af3e 286 virtual bool IsOk() const { return true; }
e2acb9ae 287
c7a9fa36 288protected:
47fc03d2 289 virtual size_t OnSysWrite(const void *buffer, size_t size);
4004775e
RL
290 virtual wxFileOffset OnSysSeek(wxFileOffset pos, wxSeekMode mode);
291 virtual wxFileOffset OnSysTell() const;
e2acb9ae 292
9bc3af3e
VZ
293 size_t m_currentPos,
294 m_lastPos;
fc7a2a60 295
6285e36b 296 DECLARE_DYNAMIC_CLASS(wxCountingOutputStream)
c0c133e1 297 wxDECLARE_NO_COPY_CLASS(wxCountingOutputStream);
e2acb9ae
RR
298};
299
f4ada568
GL
300// ---------------------------------------------------------------------------
301// "Filter" streams
302// ---------------------------------------------------------------------------
32fc4afb 303
bddd7a8d 304class WXDLLIMPEXP_BASE wxFilterInputStream : public wxInputStream
c7a9fa36
RR
305{
306public:
307 wxFilterInputStream();
308 wxFilterInputStream(wxInputStream& stream);
166c3ef0 309 wxFilterInputStream(wxInputStream *stream);
47fc03d2 310 virtual ~wxFilterInputStream();
32fc4afb 311
c7a9fa36 312 char Peek() { return m_parent_i_stream->Peek(); }
32fc4afb 313
588066b7 314 wxFileOffset GetLength() const { return m_parent_i_stream->GetLength(); }
84b46c35 315
47fc03d2
VZ
316 wxInputStream *GetFilterInputStream() const { return m_parent_i_stream; }
317
c7a9fa36
RR
318protected:
319 wxInputStream *m_parent_i_stream;
166c3ef0 320 bool m_owns;
22f3361e 321
6285e36b 322 DECLARE_ABSTRACT_CLASS(wxFilterInputStream)
c0c133e1 323 wxDECLARE_NO_COPY_CLASS(wxFilterInputStream);
32fc4afb
GL
324};
325
bddd7a8d 326class WXDLLIMPEXP_BASE wxFilterOutputStream : public wxOutputStream
c7a9fa36
RR
327{
328public:
329 wxFilterOutputStream();
330 wxFilterOutputStream(wxOutputStream& stream);
166c3ef0 331 wxFilterOutputStream(wxOutputStream *stream);
47fc03d2 332 virtual ~wxFilterOutputStream();
1678ad78 333
588066b7 334 wxFileOffset GetLength() const { return m_parent_o_stream->GetLength(); }
84b46c35 335
47fc03d2
VZ
336 wxOutputStream *GetFilterOutputStream() const { return m_parent_o_stream; }
337
166c3ef0
MW
338 bool Close();
339
c7a9fa36
RR
340protected:
341 wxOutputStream *m_parent_o_stream;
166c3ef0 342 bool m_owns;
22f3361e 343
6285e36b 344 DECLARE_ABSTRACT_CLASS(wxFilterOutputStream)
c0c133e1 345 wxDECLARE_NO_COPY_CLASS(wxFilterOutputStream);
32fc4afb
GL
346};
347
166c3ef0
MW
348enum wxStreamProtocolType
349{
489a164c
MW
350 wxSTREAM_PROTOCOL, // wxFileSystem protocol (should be only one)
351 wxSTREAM_MIMETYPE, // MIME types the stream handles
352 wxSTREAM_ENCODING, // The HTTP Content-Encodings the stream handles
353 wxSTREAM_FILEEXT // File extensions the stream handles
166c3ef0
MW
354};
355
356void WXDLLIMPEXP_BASE wxUseFilterClasses();
357
58211774 358class WXDLLIMPEXP_BASE wxFilterClassFactoryBase : public wxObject
166c3ef0
MW
359{
360public:
58211774 361 virtual ~wxFilterClassFactoryBase() { }
166c3ef0
MW
362
363 wxString GetProtocol() const { return wxString(*GetProtocols()); }
a8d2cf4e 364 wxString PopExtension(const wxString& location) const;
166c3ef0
MW
365
366 virtual const wxChar * const *GetProtocols(wxStreamProtocolType type
367 = wxSTREAM_PROTOCOL) const = 0;
368
86501081 369 bool CanHandle(const wxString& protocol,
166c3ef0
MW
370 wxStreamProtocolType type
371 = wxSTREAM_PROTOCOL) const;
372
58211774 373protected:
86501081 374 wxString::size_type FindExtension(const wxString& location) const;
58211774
MW
375
376 DECLARE_ABSTRACT_CLASS(wxFilterClassFactoryBase)
377};
378
379class WXDLLIMPEXP_BASE wxFilterClassFactory : public wxFilterClassFactoryBase
380{
381public:
382 virtual ~wxFilterClassFactory() { }
383
384 virtual wxFilterInputStream *NewStream(wxInputStream& stream) const = 0;
385 virtual wxFilterOutputStream *NewStream(wxOutputStream& stream) const = 0;
386 virtual wxFilterInputStream *NewStream(wxInputStream *stream) const = 0;
387 virtual wxFilterOutputStream *NewStream(wxOutputStream *stream) const = 0;
388
86501081 389 static const wxFilterClassFactory *Find(const wxString& protocol,
166c3ef0
MW
390 wxStreamProtocolType type
391 = wxSTREAM_PROTOCOL);
392
393 static const wxFilterClassFactory *GetFirst();
394 const wxFilterClassFactory *GetNext() const { return m_next; }
395
396 void PushFront() { Remove(); m_next = sm_first; sm_first = this; }
397 void Remove();
398
399protected:
400 wxFilterClassFactory() : m_next(this) { }
401
402 wxFilterClassFactory& operator=(const wxFilterClassFactory&)
403 { return *this; }
404
405private:
406 static wxFilterClassFactory *sm_first;
407 wxFilterClassFactory *m_next;
408
409 DECLARE_ABSTRACT_CLASS(wxFilterClassFactory)
410};
411
2b5f62a0
VZ
412// ============================================================================
413// buffered streams
414// ============================================================================
415
fae05df5 416// ---------------------------------------------------------------------------
47fc03d2
VZ
417// Stream buffer: this class can be derived from and passed to
418// wxBufferedStreams to implement custom buffering
fae05df5
GL
419// ---------------------------------------------------------------------------
420
bddd7a8d 421class WXDLLIMPEXP_BASE wxStreamBuffer
c7a9fa36
RR
422{
423public:
49e399d8
VZ
424 enum BufMode
425 {
426 read,
427 write,
428 read_write
429 };
c7a9fa36 430
f42c1512
VZ
431 wxStreamBuffer(wxStreamBase& stream, BufMode mode)
432 {
433 InitWithStream(stream, mode);
434 }
435
36f062d3 436 wxStreamBuffer(size_t bufsize, wxInputStream& stream)
f42c1512
VZ
437 {
438 InitWithStream(stream, read);
439 SetBufferIO(bufsize);
440 }
441
36f062d3 442 wxStreamBuffer(size_t bufsize, wxOutputStream& stream)
f42c1512
VZ
443 {
444 InitWithStream(stream, write);
445 SetBufferIO(bufsize);
446 }
447
c7a9fa36 448 wxStreamBuffer(const wxStreamBuffer& buf);
47fc03d2 449 virtual ~wxStreamBuffer();
c7a9fa36
RR
450
451 // Filtered IO
47fc03d2 452 virtual size_t Read(void *buffer, size_t size);
c7a9fa36 453 size_t Read(wxStreamBuffer *buf);
47fc03d2 454 virtual size_t Write(const void *buffer, size_t size);
c7a9fa36
RR
455 size_t Write(wxStreamBuffer *buf);
456
47fc03d2
VZ
457 virtual char Peek();
458 virtual char GetChar();
459 virtual void PutChar(char c);
4004775e
RL
460 virtual wxFileOffset Tell() const;
461 virtual wxFileOffset Seek(wxFileOffset pos, wxSeekMode mode);
c7a9fa36
RR
462
463 // Buffer control
464 void ResetBuffer();
df18cc7a 465 void Truncate();
49e399d8
VZ
466
467 // NB: the buffer must always be allocated with malloc() if takeOwn is
d775fa82
WS
468 // true as it will be deallocated by free()
469 void SetBufferIO(void *start, void *end, bool takeOwnership = false);
470 void SetBufferIO(void *start, size_t len, bool takeOwnership = false);
c7a9fa36 471 void SetBufferIO(size_t bufsize);
49e399d8
VZ
472 void *GetBufferStart() const { return m_buffer_start; }
473 void *GetBufferEnd() const { return m_buffer_end; }
474 void *GetBufferPos() const { return m_buffer_pos; }
b23bc769 475 size_t GetBufferSize() const { return m_buffer_end - m_buffer_start; }
49e399d8
VZ
476 size_t GetIntPosition() const { return m_buffer_pos - m_buffer_start; }
477 void SetIntPosition(size_t pos) { m_buffer_pos = m_buffer_start + pos; }
478 size_t GetLastAccess() const { return m_buffer_end - m_buffer_start; }
479 size_t GetBytesLeft() const { return m_buffer_end - m_buffer_pos; }
c7a9fa36
RR
480
481 void Fixed(bool fixed) { m_fixed = fixed; }
482 void Flushable(bool f) { m_flushable = f; }
483
484 bool FlushBuffer();
485 bool FillBuffer();
486 size_t GetDataLeft();
487
49e399d8
VZ
488 // misc accessors
489 wxStreamBase *GetStream() const { return m_stream; }
b23bc769 490 bool HasBuffer() const { return m_buffer_start != m_buffer_end; }
49e399d8
VZ
491
492 bool IsFixed() const { return m_fixed; }
493 bool IsFlushable() const { return m_flushable; }
494
47fc03d2
VZ
495 // only for input/output buffers respectively, returns NULL otherwise
496 wxInputStream *GetInputStream() const;
497 wxOutputStream *GetOutputStream() const;
498
40ff126a 499#if WXWIN_COMPATIBILITY_2_6
49e399d8 500 // deprecated, for compatibility only
40ff126a
WS
501 wxDEPRECATED( wxStreamBase *Stream() );
502#endif // WXWIN_COMPATIBILITY_2_6
c7a9fa36 503
2b5f62a0
VZ
504 // this constructs a dummy wxStreamBuffer, used by (and exists for)
505 // wxMemoryStreams only, don't use!
506 wxStreamBuffer(BufMode mode);
507
c7a9fa36
RR
508protected:
509 void GetFromBuffer(void *buffer, size_t size);
510 void PutToBuffer(const void *buffer, size_t size);
511
49e399d8
VZ
512 // set the last error to the specified value if we didn't have it before
513 void SetError(wxStreamError err);
514
515 // common part of several ctors
516 void Init();
c7a9fa36 517
f42c1512
VZ
518 // common part of ctors taking wxStreamBase parameter
519 void InitWithStream(wxStreamBase& stream, BufMode mode);
520
49e399d8
VZ
521 // init buffer variables to be empty
522 void InitBuffer();
c7a9fa36 523
49e399d8
VZ
524 // free the buffer (always safe to call)
525 void FreeBuffer();
526
527 // the buffer itself: the pointers to its start and end and the current
528 // position in the buffer
529 char *m_buffer_start,
530 *m_buffer_end,
531 *m_buffer_pos;
532
49e399d8 533 // the stream we're associated with
c7a9fa36 534 wxStreamBase *m_stream;
49e399d8
VZ
535
536 // its mode
c7a9fa36 537 BufMode m_mode;
49e399d8
VZ
538
539 // flags
540 bool m_destroybuf, // deallocate buffer?
49e399d8
VZ
541 m_fixed,
542 m_flushable;
22f3361e 543
f42c1512 544
c0c133e1 545 wxDECLARE_NO_ASSIGN_CLASS(wxStreamBuffer);
fae05df5
GL
546};
547
548// ---------------------------------------------------------------------------
2b5f62a0 549// wxBufferedInputStream
fae05df5
GL
550// ---------------------------------------------------------------------------
551
bddd7a8d 552class WXDLLIMPEXP_BASE wxBufferedInputStream : public wxFilterInputStream
c7a9fa36
RR
553{
554public:
f42c1512
VZ
555 // create a buffered stream on top of the specified low-level stream
556 //
557 // if a non NULL buffer is given to the stream, it will be deleted by it,
558 // otherwise a default 1KB buffer will be used
47fc03d2
VZ
559 wxBufferedInputStream(wxInputStream& stream,
560 wxStreamBuffer *buffer = NULL);
f42c1512
VZ
561
562 // ctor allowing to specify the buffer size, it's just a more convenient
563 // alternative to creating wxStreamBuffer, calling its SetBufferIO(bufsize)
564 // and using the ctor above
565 wxBufferedInputStream(wxInputStream& stream, size_t bufsize);
566
567
47fc03d2 568 virtual ~wxBufferedInputStream();
fae05df5 569
c7a9fa36
RR
570 char Peek();
571 wxInputStream& Read(void *buffer, size_t size);
657d2097 572
c7a9fa36 573 // Position functions
4004775e
RL
574 wxFileOffset SeekI(wxFileOffset pos, wxSeekMode mode = wxFromStart);
575 wxFileOffset TellI() const;
3c70014d 576 bool IsSeekable() const { return m_parent_i_stream->IsSeekable(); }
fae05df5 577
47fc03d2
VZ
578 // the buffer given to the stream will be deleted by it
579 void SetInputStreamBuffer(wxStreamBuffer *buffer);
580 wxStreamBuffer *GetInputStreamBuffer() const { return m_i_streambuf; }
581
40ff126a 582#if WXWIN_COMPATIBILITY_2_6
47fc03d2 583 // deprecated, for compatibility only
40ff126a
WS
584 wxDEPRECATED( wxStreamBuffer *InputStreamBuffer() const );
585#endif // WXWIN_COMPATIBILITY_2_6
fae05df5 586
c7a9fa36 587protected:
47fc03d2 588 virtual size_t OnSysRead(void *buffer, size_t bufsize);
4004775e
RL
589 virtual wxFileOffset OnSysSeek(wxFileOffset seek, wxSeekMode mode);
590 virtual wxFileOffset OnSysTell() const;
fae05df5 591
c7a9fa36 592 wxStreamBuffer *m_i_streambuf;
22f3361e 593
c0c133e1 594 wxDECLARE_NO_COPY_CLASS(wxBufferedInputStream);
fae05df5
GL
595};
596
2b5f62a0
VZ
597// ----------------------------------------------------------------------------
598// wxBufferedOutputStream
599// ----------------------------------------------------------------------------
600
bddd7a8d 601class WXDLLIMPEXP_BASE wxBufferedOutputStream : public wxFilterOutputStream
c7a9fa36
RR
602{
603public:
f42c1512
VZ
604 // create a buffered stream on top of the specified low-level stream
605 //
606 // if a non NULL buffer is given to the stream, it will be deleted by it,
607 // otherwise a default 1KB buffer will be used
47fc03d2
VZ
608 wxBufferedOutputStream(wxOutputStream& stream,
609 wxStreamBuffer *buffer = NULL);
f42c1512
VZ
610
611 // ctor allowing to specify the buffer size, it's just a more convenient
612 // alternative to creating wxStreamBuffer, calling its SetBufferIO(bufsize)
613 // and using the ctor above
614 wxBufferedOutputStream(wxOutputStream& stream, size_t bufsize);
615
47fc03d2 616 virtual ~wxBufferedOutputStream();
fae05df5 617
c7a9fa36 618 wxOutputStream& Write(const void *buffer, size_t size);
657d2097 619
c7a9fa36 620 // Position functions
4004775e
RL
621 wxFileOffset SeekO(wxFileOffset pos, wxSeekMode mode = wxFromStart);
622 wxFileOffset TellO() const;
3c70014d 623 bool IsSeekable() const { return m_parent_o_stream->IsSeekable(); }
fae05df5 624
c7a9fa36 625 void Sync();
8f0ff178 626 bool Close();
fae05df5 627
588066b7 628 wxFileOffset GetLength() const;
657d2097 629
47fc03d2
VZ
630 // the buffer given to the stream will be deleted by it
631 void SetOutputStreamBuffer(wxStreamBuffer *buffer);
632 wxStreamBuffer *GetOutputStreamBuffer() const { return m_o_streambuf; }
633
40ff126a 634#if WXWIN_COMPATIBILITY_2_6
47fc03d2 635 // deprecated, for compatibility only
40ff126a
WS
636 wxDEPRECATED( wxStreamBuffer *OutputStreamBuffer() const );
637#endif // WXWIN_COMPATIBILITY_2_6
fae05df5 638
c7a9fa36 639protected:
47fc03d2 640 virtual size_t OnSysWrite(const void *buffer, size_t bufsize);
4004775e
RL
641 virtual wxFileOffset OnSysSeek(wxFileOffset seek, wxSeekMode mode);
642 virtual wxFileOffset OnSysTell() const;
fae05df5 643
c7a9fa36 644 wxStreamBuffer *m_o_streambuf;
22f3361e 645
c0c133e1 646 wxDECLARE_NO_COPY_CLASS(wxBufferedOutputStream);
fae05df5
GL
647};
648
40ff126a
WS
649#if WXWIN_COMPATIBILITY_2_6
650 inline wxStreamBase *wxStreamBuffer::Stream() { return m_stream; }
651 inline wxStreamBuffer *wxBufferedInputStream::InputStreamBuffer() const { return m_i_streambuf; }
652 inline wxStreamBuffer *wxBufferedOutputStream::OutputStreamBuffer() const { return m_o_streambuf; }
653#endif // WXWIN_COMPATIBILITY_2_6
654
f5ef4d69
VZ
655// ---------------------------------------------------------------------------
656// wxWrapperInputStream: forwards all IO to another stream.
657// ---------------------------------------------------------------------------
658
659class WXDLLIMPEXP_BASE wxWrapperInputStream : public wxFilterInputStream
660{
661public:
662 // Constructor fully initializing the stream. The overload taking pointer
663 // takes ownership of the parent stream, the one taking reference does not.
664 //
665 // Notice that this class also has a default ctor but it's protected as the
666 // derived class is supposed to take care of calling InitParentStream() if
667 // it's used.
668 wxWrapperInputStream(wxInputStream& stream);
669 wxWrapperInputStream(wxInputStream* stream);
670
671 // Override the base class methods to forward to the wrapped stream.
672 virtual wxFileOffset GetLength() const;
673 virtual bool IsSeekable() const;
674
675protected:
676 virtual size_t OnSysRead(void *buffer, size_t size);
677 virtual wxFileOffset OnSysSeek(wxFileOffset pos, wxSeekMode mode);
678 virtual wxFileOffset OnSysTell() const;
679
680 // Ensure that our own last error is the same as that of the real stream.
681 //
682 // This method is const because the error must be updated even from const
683 // methods (in other words, it really should have been mutable in the first
684 // place).
685 void SynchronizeLastError() const
686 {
687 const_cast<wxWrapperInputStream*>(this)->
688 Reset(m_parent_i_stream->GetLastError());
689 }
690
691 // Default constructor, use InitParentStream() later.
692 wxWrapperInputStream();
693
694 // Set up the wrapped stream for an object initialized using the default
695 // constructor. The ownership logic is the same as above.
696 void InitParentStream(wxInputStream& stream);
697 void InitParentStream(wxInputStream* stream);
698
699 wxDECLARE_NO_COPY_CLASS(wxWrapperInputStream);
700};
701
702
47fc03d2
VZ
703#endif // wxUSE_STREAMS
704
705#endif // _WX_WXSTREAM_H__