]> git.saurik.com Git - wxWidgets.git/blame - include/wx/stream.h
renamed wxWave to wxSound
[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
371a5b4e 9// Licence: wxWindows licence
32fc4afb
GL
10/////////////////////////////////////////////////////////////////////////////
11
34138703
JS
12#ifndef _WX_WXSTREAM_H__
13#define _WX_WXSTREAM_H__
32fc4afb 14
12028905 15#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
af49c4b8 16#pragma interface "stream.h"
32fc4afb
GL
17#endif
18
ce4169a4
RR
19#include "wx/defs.h"
20
21#if wxUSE_STREAMS
22
32fc4afb 23#include <stdio.h>
45ea509a
VZ
24#include "wx/object.h"
25#include "wx/string.h"
1678ad78
GL
26#include "wx/filefn.h" // for off_t, wxInvalidOffset and wxSeekMode
27
bddd7a8d
VZ
28class WXDLLIMPEXP_BASE wxStreamBase;
29class WXDLLIMPEXP_BASE wxInputStream;
30class WXDLLIMPEXP_BASE wxOutputStream;
1678ad78
GL
31
32typedef wxInputStream& (*__wxInputManip)(wxInputStream&);
33typedef wxOutputStream& (*__wxOutputManip)(wxOutputStream&);
34
bddd7a8d 35WXDLLIMPEXP_BASE wxOutputStream& wxEndL(wxOutputStream& o_stream);
1678ad78 36
2b5f62a0
VZ
37// ----------------------------------------------------------------------------
38// constants
39// ----------------------------------------------------------------------------
f4ada568 40
49e399d8 41enum wxStreamError
cd6ce4a9 42{
2b5f62a0
VZ
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
49e399d8 47};
75ed1d15 48
cd6ce4a9 49// compatibility
2b5f62a0
VZ
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
55
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
61
62// ============================================================================
63// base stream classes: wxInputStream and wxOutputStream
64// ============================================================================
65
66// ---------------------------------------------------------------------------
67// wxStreamBase: common (but non virtual!) base for all stream classes
68// ---------------------------------------------------------------------------
cd6ce4a9 69
bddd7a8d 70class WXDLLIMPEXP_BASE wxStreamBase
c7a9fa36
RR
71{
72public:
73 wxStreamBase();
74 virtual ~wxStreamBase();
75ed1d15 75
cd6ce4a9 76 // error testing
cd6ce4a9 77 wxStreamError GetLastError() const { return m_lasterror; }
2b5f62a0
VZ
78 bool IsOk() const { return GetLastError() == wxSTREAM_NO_ERROR; }
79 bool operator!() const { return !IsOk(); }
cd6ce4a9 80
2b5f62a0
VZ
81 // reset the stream state
82 void Reset() { m_lasterror = wxSTREAM_NO_ERROR; }
83
f6e7cd0a 84 // this doesn't make sense for all streams, always test its return value
47fc03d2
VZ
85 virtual size_t GetSize() const { return 0; }
86
2b5f62a0 87#if WXWIN_COMPATIBILITY_2_2
47fc03d2
VZ
88 // deprecated, for compatibility only
89 wxStreamError LastError() const { return m_lasterror; }
c7a9fa36 90 size_t StreamSize() const { return GetSize(); }
2b5f62a0 91#endif // WXWIN_COMPATIBILITY_2_2
75ed1d15 92
c7a9fa36 93protected:
c7a9fa36
RR
94 virtual off_t OnSysSeek(off_t seek, wxSeekMode mode);
95 virtual off_t OnSysTell() const;
75ed1d15 96
c7a9fa36
RR
97 size_t m_lastcount;
98 wxStreamError m_lasterror;
2b5f62a0
VZ
99
100 friend class wxStreamBuffer;
22f3361e 101
00e7a427 102 DECLARE_NO_COPY_CLASS(wxStreamBase)
75ed1d15
GL
103};
104
2b5f62a0
VZ
105// ----------------------------------------------------------------------------
106// wxInputStream: base class for the input streams
107// ----------------------------------------------------------------------------
108
bddd7a8d 109class WXDLLIMPEXP_BASE wxInputStream : public wxStreamBase
c7a9fa36
RR
110{
111public:
2b5f62a0 112 // ctor and dtor, nothing exciting
c7a9fa36
RR
113 wxInputStream();
114 virtual ~wxInputStream();
32fc4afb 115
cd6ce4a9 116
c7a9fa36 117 // IO functions
2b5f62a0
VZ
118 // ------------
119
120 // return a character from the stream without removing it, i.e. it will
121 // still be returned by the next call to GetC()
122 //
123 // blocks until something appears in the stream if necessary, if nothing
124 // ever does (i.e. EOF) LastRead() will return 0 (and the return value is
125 // undefined), otherwise 1
c7a9fa36 126 virtual char Peek();
2b5f62a0
VZ
127
128 // return one character from the stream, blocking until it appears if
129 // necessary
130 //
131 // if EOF, return value is undefined and LastRead() will return 0 and not 1
c7a9fa36 132 char GetC();
2b5f62a0
VZ
133
134 // read at most the given number of bytes from the stream
135 //
136 // there are 2 possible situations here: either there is nothing at all in
137 // the stream right now in which case Read() blocks until something appears
138 // (use CanRead() to avoid this) or there is already some data available in
139 // the stream and then Read() doesn't block but returns just the data it
140 // can read without waiting for more
141 //
142 // in any case, if there are not enough bytes in the stream right now,
143 // LastRead() value will be less than size but greater than 0. If it is 0,
144 // it means that EOF has been reached.
c7a9fa36 145 virtual wxInputStream& Read(void *buffer, size_t size);
32fc4afb 146
2b5f62a0
VZ
147 // copy the entire contents of this stream into streamOut, stopping only
148 // when EOF is reached or an error occurs
149 wxInputStream& Read(wxOutputStream& streamOut);
150
151
152 // status functions
153 // ----------------
154
155 // returns the number of bytes read by the last call to Read(), GetC() or
156 // Peek()
157 //
158 // this should be used to discover whether that call succeeded in reading
159 // all the requested data or not
160 virtual size_t LastRead() const { return wxStreamBase::m_lastcount; }
161
162 // returns TRUE if some data is available in the stream right now, so that
163 // calling Read() wouldn't block
164 virtual bool CanRead() const;
32fc4afb 165
2b5f62a0
VZ
166 // is the stream at EOF?
167 //
168 // note that this cannot be really implemented for all streams and
169 // CanRead() is more reliable than Eof()
170 virtual bool Eof() const;
171
172
173 // write back buffer
174 // -----------------
fae05df5 175
2b5f62a0
VZ
176 // put back the specified number of bytes into the stream, they will be
177 // fetched by the next call to the read functions
178 //
179 // returns the number of bytes really stuffed back
c7a9fa36 180 size_t Ungetch(const void *buffer, size_t size);
2b5f62a0
VZ
181
182 // put back the specified character in the stream
183 //
184 // returns TRUE if ok, FALSE on error
c7a9fa36 185 bool Ungetch(char c);
0cd9bfe8 186
2b5f62a0
VZ
187
188 // position functions
189 // ------------------
190
191 // move the stream pointer to the given position (if the stream supports
192 // it)
193 //
194 // returns wxInvalidOffset on error
195 virtual off_t SeekI(off_t pos, wxSeekMode mode = wxFromStart);
196
197 // return the current position of the stream pointer or wxInvalidOffset
198 virtual off_t TellI() const;
199
200
201 // stream-like operators
202 // ---------------------
203
c7a9fa36 204 wxInputStream& operator>>(wxOutputStream& out) { return Read(out); }
2b5f62a0 205 wxInputStream& operator>>(__wxInputManip func) { return func(*this); }
fae05df5 206
c7a9fa36 207protected:
2b5f62a0
VZ
208 // do read up to size bytes of data into the provided buffer
209 //
210 // this method should return 0 if EOF has been reached or an error occured
211 // (m_lasterror should be set accordingly as well) or the number of bytes
212 // read
213 virtual size_t OnSysRead(void *buffer, size_t size) = 0;
214
215 // write-back buffer support
216 // -------------------------
217
218 // return the pointer to a buffer big enough to hold sizeNeeded bytes
219 char *AllocSpaceWBack(size_t sizeNeeded);
220
221 // read up to size data from the write back buffer, return the number of
222 // bytes read
223 size_t GetWBack(void *buf, size_t size);
47fc03d2 224
2b5f62a0 225 // write back buffer or NULL if none
c7a9fa36 226 char *m_wback;
2b5f62a0
VZ
227
228 // the size of the buffer
c7a9fa36 229 size_t m_wbacksize;
fae05df5 230
2b5f62a0
VZ
231 // the current position in the buffer
232 size_t m_wbackcur;
47fc03d2
VZ
233
234 friend class wxStreamBuffer;
fc7a2a60
VZ
235
236 DECLARE_NO_COPY_CLASS(wxInputStream)
32fc4afb
GL
237};
238
2b5f62a0
VZ
239// ----------------------------------------------------------------------------
240// wxOutputStream: base for the output streams
241// ----------------------------------------------------------------------------
242
bddd7a8d 243class WXDLLIMPEXP_BASE wxOutputStream : public wxStreamBase
c7a9fa36
RR
244{
245public:
246 wxOutputStream();
247 virtual ~wxOutputStream();
32fc4afb 248
c7a9fa36
RR
249 void PutC(char c);
250 virtual wxOutputStream& Write(const void *buffer, size_t size);
251 wxOutputStream& Write(wxInputStream& stream_in);
32fc4afb 252
c7a9fa36
RR
253 virtual off_t SeekO(off_t pos, wxSeekMode mode = wxFromStart);
254 virtual off_t TellO() const;
1678ad78 255
c7a9fa36 256 virtual size_t LastWrite() const { return wxStreamBase::m_lastcount; }
32fc4afb 257
c7a9fa36 258 virtual void Sync();
32fc4afb 259
c7a9fa36 260 wxOutputStream& operator<<(wxInputStream& out) { return Write(out); }
c7a9fa36 261 wxOutputStream& operator<<( __wxOutputManip func) { return func(*this); }
47fc03d2
VZ
262
263protected:
264 // to be implemented in the derived classes (it should have been pure
265 // virtual)
266 virtual size_t OnSysWrite(const void *buffer, size_t bufsize);
267
268 friend class wxStreamBuffer;
fc7a2a60
VZ
269
270 DECLARE_NO_COPY_CLASS(wxOutputStream)
32fc4afb
GL
271};
272
2b5f62a0
VZ
273// ============================================================================
274// helper stream classes
275// ============================================================================
276
e2acb9ae
RR
277// ---------------------------------------------------------------------------
278// A stream for measuring streamed output
279// ---------------------------------------------------------------------------
280
bddd7a8d 281class WXDLLIMPEXP_BASE wxCountingOutputStream : public wxOutputStream
c7a9fa36
RR
282{
283public:
284 wxCountingOutputStream();
e2acb9ae 285
c7a9fa36
RR
286 size_t GetSize() const;
287 bool Ok() const { return TRUE; }
e2acb9ae 288
c7a9fa36 289protected:
47fc03d2
VZ
290 virtual size_t OnSysWrite(const void *buffer, size_t size);
291 virtual off_t OnSysSeek(off_t pos, wxSeekMode mode);
292 virtual off_t OnSysTell() const;
e2acb9ae 293
c7a9fa36 294 size_t m_currentPos;
fc7a2a60
VZ
295
296 DECLARE_NO_COPY_CLASS(wxCountingOutputStream)
e2acb9ae
RR
297};
298
f4ada568
GL
299// ---------------------------------------------------------------------------
300// "Filter" streams
301// ---------------------------------------------------------------------------
32fc4afb 302
bddd7a8d 303class WXDLLIMPEXP_BASE wxFilterInputStream : public wxInputStream
c7a9fa36
RR
304{
305public:
306 wxFilterInputStream();
307 wxFilterInputStream(wxInputStream& stream);
47fc03d2 308 virtual ~wxFilterInputStream();
32fc4afb 309
c7a9fa36 310 char Peek() { return m_parent_i_stream->Peek(); }
32fc4afb 311
c7a9fa36 312 size_t GetSize() const { return m_parent_i_stream->GetSize(); }
84b46c35 313
47fc03d2
VZ
314 wxInputStream *GetFilterInputStream() const { return m_parent_i_stream; }
315
c7a9fa36
RR
316protected:
317 wxInputStream *m_parent_i_stream;
22f3361e
VZ
318
319 DECLARE_NO_COPY_CLASS(wxFilterInputStream)
32fc4afb
GL
320};
321
bddd7a8d 322class WXDLLIMPEXP_BASE wxFilterOutputStream : public wxOutputStream
c7a9fa36
RR
323{
324public:
325 wxFilterOutputStream();
326 wxFilterOutputStream(wxOutputStream& stream);
47fc03d2 327 virtual ~wxFilterOutputStream();
1678ad78 328
c7a9fa36 329 size_t GetSize() const { return m_parent_o_stream->GetSize(); }
84b46c35 330
47fc03d2
VZ
331 wxOutputStream *GetFilterOutputStream() const { return m_parent_o_stream; }
332
c7a9fa36
RR
333protected:
334 wxOutputStream *m_parent_o_stream;
22f3361e
VZ
335
336 DECLARE_NO_COPY_CLASS(wxFilterOutputStream)
32fc4afb
GL
337};
338
2b5f62a0
VZ
339// ============================================================================
340// buffered streams
341// ============================================================================
342
fae05df5 343// ---------------------------------------------------------------------------
47fc03d2
VZ
344// Stream buffer: this class can be derived from and passed to
345// wxBufferedStreams to implement custom buffering
fae05df5
GL
346// ---------------------------------------------------------------------------
347
bddd7a8d 348class WXDLLIMPEXP_BASE wxStreamBuffer
c7a9fa36
RR
349{
350public:
49e399d8
VZ
351 enum BufMode
352 {
353 read,
354 write,
355 read_write
356 };
c7a9fa36
RR
357
358 wxStreamBuffer(wxStreamBase& stream, BufMode mode);
c7a9fa36 359 wxStreamBuffer(const wxStreamBuffer& buf);
47fc03d2 360 virtual ~wxStreamBuffer();
c7a9fa36
RR
361
362 // Filtered IO
47fc03d2 363 virtual size_t Read(void *buffer, size_t size);
c7a9fa36 364 size_t Read(wxStreamBuffer *buf);
47fc03d2 365 virtual size_t Write(const void *buffer, size_t size);
c7a9fa36
RR
366 size_t Write(wxStreamBuffer *buf);
367
47fc03d2
VZ
368 virtual char Peek();
369 virtual char GetChar();
370 virtual void PutChar(char c);
371 virtual off_t Tell() const;
372 virtual off_t Seek(off_t pos, wxSeekMode mode);
c7a9fa36
RR
373
374 // Buffer control
375 void ResetBuffer();
49e399d8
VZ
376
377 // NB: the buffer must always be allocated with malloc() if takeOwn is
378 // TRUE as it will be deallocated by free()
379 void SetBufferIO(void *start, void *end, bool takeOwnership = FALSE);
67c8c225 380 void SetBufferIO(void *start, size_t len, bool takeOwnership = FALSE);
c7a9fa36 381 void SetBufferIO(size_t bufsize);
49e399d8
VZ
382 void *GetBufferStart() const { return m_buffer_start; }
383 void *GetBufferEnd() const { return m_buffer_end; }
384 void *GetBufferPos() const { return m_buffer_pos; }
67c8c225 385 size_t GetBufferSize() const { return m_buffer_size; }
49e399d8
VZ
386 size_t GetIntPosition() const { return m_buffer_pos - m_buffer_start; }
387 void SetIntPosition(size_t pos) { m_buffer_pos = m_buffer_start + pos; }
388 size_t GetLastAccess() const { return m_buffer_end - m_buffer_start; }
389 size_t GetBytesLeft() const { return m_buffer_end - m_buffer_pos; }
c7a9fa36
RR
390
391 void Fixed(bool fixed) { m_fixed = fixed; }
392 void Flushable(bool f) { m_flushable = f; }
393
394 bool FlushBuffer();
395 bool FillBuffer();
396 size_t GetDataLeft();
397
49e399d8
VZ
398 // misc accessors
399 wxStreamBase *GetStream() const { return m_stream; }
400 bool HasBuffer() const { return m_buffer_size != 0; }
401
402 bool IsFixed() const { return m_fixed; }
403 bool IsFlushable() const { return m_flushable; }
404
47fc03d2
VZ
405 // only for input/output buffers respectively, returns NULL otherwise
406 wxInputStream *GetInputStream() const;
407 wxOutputStream *GetOutputStream() const;
408
49e399d8 409 // deprecated, for compatibility only
c7a9fa36
RR
410 wxStreamBase *Stream() { return m_stream; }
411
2b5f62a0
VZ
412 // this constructs a dummy wxStreamBuffer, used by (and exists for)
413 // wxMemoryStreams only, don't use!
414 wxStreamBuffer(BufMode mode);
415
c7a9fa36
RR
416protected:
417 void GetFromBuffer(void *buffer, size_t size);
418 void PutToBuffer(const void *buffer, size_t size);
419
49e399d8
VZ
420 // set the last error to the specified value if we didn't have it before
421 void SetError(wxStreamError err);
422
423 // common part of several ctors
424 void Init();
c7a9fa36 425
49e399d8
VZ
426 // init buffer variables to be empty
427 void InitBuffer();
c7a9fa36 428
49e399d8
VZ
429 // free the buffer (always safe to call)
430 void FreeBuffer();
431
432 // the buffer itself: the pointers to its start and end and the current
433 // position in the buffer
434 char *m_buffer_start,
435 *m_buffer_end,
436 *m_buffer_pos;
437
438 // the buffer size
439 // FIXME: isn't it the same as m_buffer_end - m_buffer_start? (VZ)
440 size_t m_buffer_size;
441
442 // the stream we're associated with
c7a9fa36 443 wxStreamBase *m_stream;
49e399d8
VZ
444
445 // its mode
c7a9fa36 446 BufMode m_mode;
49e399d8
VZ
447
448 // flags
449 bool m_destroybuf, // deallocate buffer?
49e399d8
VZ
450 m_fixed,
451 m_flushable;
22f3361e
VZ
452
453private:
454// Cannot use
455// DECLARE_NO_COPY_CLASS(wxStreamBuffer)
456// because copy constructor is explicitly declared above;
457// but no copy assignment operator is defined, so declare
458// it private to prevent the compiler from defining it:
459 wxStreamBuffer& operator=(const wxStreamBuffer&);
fae05df5
GL
460};
461
462// ---------------------------------------------------------------------------
2b5f62a0 463// wxBufferedInputStream
fae05df5
GL
464// ---------------------------------------------------------------------------
465
bddd7a8d 466class WXDLLIMPEXP_BASE wxBufferedInputStream : public wxFilterInputStream
c7a9fa36
RR
467{
468public:
47fc03d2
VZ
469 // if a non NULL buffer is given to the stream, it will be deleted by it
470 wxBufferedInputStream(wxInputStream& stream,
471 wxStreamBuffer *buffer = NULL);
472 virtual ~wxBufferedInputStream();
fae05df5 473
c7a9fa36
RR
474 char Peek();
475 wxInputStream& Read(void *buffer, size_t size);
657d2097 476
c7a9fa36
RR
477 // Position functions
478 off_t SeekI(off_t pos, wxSeekMode mode = wxFromStart);
479 off_t TellI() const;
fae05df5 480
47fc03d2
VZ
481 // the buffer given to the stream will be deleted by it
482 void SetInputStreamBuffer(wxStreamBuffer *buffer);
483 wxStreamBuffer *GetInputStreamBuffer() const { return m_i_streambuf; }
484
485 // deprecated, for compatibility only
c7a9fa36 486 wxStreamBuffer *InputStreamBuffer() const { return m_i_streambuf; }
fae05df5 487
c7a9fa36 488protected:
47fc03d2
VZ
489 virtual size_t OnSysRead(void *buffer, size_t bufsize);
490 virtual off_t OnSysSeek(off_t seek, wxSeekMode mode);
491 virtual off_t OnSysTell() const;
fae05df5 492
c7a9fa36 493 wxStreamBuffer *m_i_streambuf;
22f3361e
VZ
494
495 DECLARE_NO_COPY_CLASS(wxBufferedInputStream)
fae05df5
GL
496};
497
2b5f62a0
VZ
498// ----------------------------------------------------------------------------
499// wxBufferedOutputStream
500// ----------------------------------------------------------------------------
501
bddd7a8d 502class WXDLLIMPEXP_BASE wxBufferedOutputStream : public wxFilterOutputStream
c7a9fa36
RR
503{
504public:
47fc03d2
VZ
505 // if a non NULL buffer is given to the stream, it will be deleted by it
506 wxBufferedOutputStream(wxOutputStream& stream,
507 wxStreamBuffer *buffer = NULL);
508 virtual ~wxBufferedOutputStream();
fae05df5 509
c7a9fa36 510 wxOutputStream& Write(const void *buffer, size_t size);
657d2097 511
c7a9fa36
RR
512 // Position functions
513 off_t SeekO(off_t pos, wxSeekMode mode = wxFromStart);
514 off_t TellO() const;
fae05df5 515
c7a9fa36 516 void Sync();
fae05df5 517
c7a9fa36 518 size_t GetSize() const;
657d2097 519
47fc03d2
VZ
520 // the buffer given to the stream will be deleted by it
521 void SetOutputStreamBuffer(wxStreamBuffer *buffer);
522 wxStreamBuffer *GetOutputStreamBuffer() const { return m_o_streambuf; }
523
524 // deprecated, for compatibility only
c7a9fa36 525 wxStreamBuffer *OutputStreamBuffer() const { return m_o_streambuf; }
fae05df5 526
c7a9fa36 527protected:
47fc03d2
VZ
528 virtual size_t OnSysWrite(const void *buffer, size_t bufsize);
529 virtual off_t OnSysSeek(off_t seek, wxSeekMode mode);
530 virtual off_t OnSysTell() const;
fae05df5 531
c7a9fa36 532 wxStreamBuffer *m_o_streambuf;
22f3361e
VZ
533
534 DECLARE_NO_COPY_CLASS(wxBufferedOutputStream)
fae05df5
GL
535};
536
47fc03d2
VZ
537#endif // wxUSE_STREAMS
538
539#endif // _WX_WXSTREAM_H__
ce4169a4 540