]> git.saurik.com Git - wxWidgets.git/blame - include/wx/stream.h
have CGImage Support in all OSX builds
[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
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"
30984dea 26#include "wx/filefn.h" // for wxFileOffset, wxInvalidOffset and wxSeekMode
1678ad78 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
588066b7
VZ
85 virtual size_t GetSize() const;
86 virtual wxFileOffset GetLength() const { return wxInvalidOffset; }
47fc03d2 87
3c70014d
MW
88 // returns true if the streams supports seeking to arbitrary offsets
89 virtual bool IsSeekable() const { return false; }
90
2b5f62a0 91#if WXWIN_COMPATIBILITY_2_2
47fc03d2 92 // deprecated, for compatibility only
2d67974d
WS
93 wxDEPRECATED( wxStreamError LastError() const );
94 wxDEPRECATED( size_t StreamSize() const );
2b5f62a0 95#endif // WXWIN_COMPATIBILITY_2_2
75ed1d15 96
c7a9fa36 97protected:
4004775e
RL
98 virtual wxFileOffset OnSysSeek(wxFileOffset seek, wxSeekMode mode);
99 virtual wxFileOffset OnSysTell() const;
75ed1d15 100
c7a9fa36
RR
101 size_t m_lastcount;
102 wxStreamError m_lasterror;
2b5f62a0
VZ
103
104 friend class wxStreamBuffer;
22f3361e 105
00e7a427 106 DECLARE_NO_COPY_CLASS(wxStreamBase)
75ed1d15
GL
107};
108
2b5f62a0
VZ
109// ----------------------------------------------------------------------------
110// wxInputStream: base class for the input streams
111// ----------------------------------------------------------------------------
112
bddd7a8d 113class WXDLLIMPEXP_BASE wxInputStream : public wxStreamBase
c7a9fa36
RR
114{
115public:
2b5f62a0 116 // ctor and dtor, nothing exciting
c7a9fa36
RR
117 wxInputStream();
118 virtual ~wxInputStream();
32fc4afb 119
cd6ce4a9 120
c7a9fa36 121 // IO functions
2b5f62a0
VZ
122 // ------------
123
124 // return a character from the stream without removing it, i.e. it will
125 // still be returned by the next call to GetC()
126 //
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
c7a9fa36 130 virtual char Peek();
2b5f62a0
VZ
131
132 // return one character from the stream, blocking until it appears if
133 // necessary
134 //
135 // if EOF, return value is undefined and LastRead() will return 0 and not 1
c7a9fa36 136 char GetC();
2b5f62a0
VZ
137
138 // read at most the given number of bytes from the stream
139 //
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
145 //
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.
c7a9fa36 149 virtual wxInputStream& Read(void *buffer, size_t size);
32fc4afb 150
2b5f62a0
VZ
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);
154
155
156 // status functions
157 // ----------------
158
159 // returns the number of bytes read by the last call to Read(), GetC() or
160 // Peek()
161 //
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; }
165
d775fa82 166 // returns true if some data is available in the stream right now, so that
2b5f62a0
VZ
167 // calling Read() wouldn't block
168 virtual bool CanRead() const;
32fc4afb 169
2b5f62a0
VZ
170 // is the stream at EOF?
171 //
172 // note that this cannot be really implemented for all streams and
173 // CanRead() is more reliable than Eof()
174 virtual bool Eof() const;
175
176
177 // write back buffer
178 // -----------------
fae05df5 179
2b5f62a0
VZ
180 // put back the specified number of bytes into the stream, they will be
181 // fetched by the next call to the read functions
182 //
183 // returns the number of bytes really stuffed back
c7a9fa36 184 size_t Ungetch(const void *buffer, size_t size);
2b5f62a0
VZ
185
186 // put back the specified character in the stream
187 //
d775fa82 188 // returns true if ok, false on error
c7a9fa36 189 bool Ungetch(char c);
0cd9bfe8 190
2b5f62a0
VZ
191
192 // position functions
193 // ------------------
194
195 // move the stream pointer to the given position (if the stream supports
196 // it)
197 //
198 // returns wxInvalidOffset on error
4004775e 199 virtual wxFileOffset SeekI(wxFileOffset pos, wxSeekMode mode = wxFromStart);
2b5f62a0
VZ
200
201 // return the current position of the stream pointer or wxInvalidOffset
4004775e 202 virtual wxFileOffset TellI() const;
2b5f62a0
VZ
203
204
205 // stream-like operators
206 // ---------------------
207
c7a9fa36 208 wxInputStream& operator>>(wxOutputStream& out) { return Read(out); }
2b5f62a0 209 wxInputStream& operator>>(__wxInputManip func) { return func(*this); }
fae05df5 210
c7a9fa36 211protected:
2b5f62a0
VZ
212 // do read up to size bytes of data into the provided buffer
213 //
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
216 // read
217 virtual size_t OnSysRead(void *buffer, size_t size) = 0;
218
219 // write-back buffer support
220 // -------------------------
221
222 // return the pointer to a buffer big enough to hold sizeNeeded bytes
223 char *AllocSpaceWBack(size_t sizeNeeded);
224
225 // read up to size data from the write back buffer, return the number of
226 // bytes read
227 size_t GetWBack(void *buf, size_t size);
47fc03d2 228
2b5f62a0 229 // write back buffer or NULL if none
c7a9fa36 230 char *m_wback;
2b5f62a0
VZ
231
232 // the size of the buffer
c7a9fa36 233 size_t m_wbacksize;
fae05df5 234
2b5f62a0
VZ
235 // the current position in the buffer
236 size_t m_wbackcur;
47fc03d2
VZ
237
238 friend class wxStreamBuffer;
fc7a2a60
VZ
239
240 DECLARE_NO_COPY_CLASS(wxInputStream)
32fc4afb
GL
241};
242
2b5f62a0
VZ
243// ----------------------------------------------------------------------------
244// wxOutputStream: base for the output streams
245// ----------------------------------------------------------------------------
246
bddd7a8d 247class WXDLLIMPEXP_BASE wxOutputStream : public wxStreamBase
c7a9fa36
RR
248{
249public:
250 wxOutputStream();
251 virtual ~wxOutputStream();
32fc4afb 252
c7a9fa36
RR
253 void PutC(char c);
254 virtual wxOutputStream& Write(const void *buffer, size_t size);
255 wxOutputStream& Write(wxInputStream& stream_in);
32fc4afb 256
4004775e
RL
257 virtual wxFileOffset SeekO(wxFileOffset pos, wxSeekMode mode = wxFromStart);
258 virtual wxFileOffset TellO() const;
1678ad78 259
c7a9fa36 260 virtual size_t LastWrite() const { return wxStreamBase::m_lastcount; }
32fc4afb 261
c7a9fa36 262 virtual void Sync();
8f0ff178 263 virtual bool Close() { return true; }
32fc4afb 264
c7a9fa36 265 wxOutputStream& operator<<(wxInputStream& out) { return Write(out); }
c7a9fa36 266 wxOutputStream& operator<<( __wxOutputManip func) { return func(*this); }
47fc03d2
VZ
267
268protected:
269 // to be implemented in the derived classes (it should have been pure
270 // virtual)
271 virtual size_t OnSysWrite(const void *buffer, size_t bufsize);
272
273 friend class wxStreamBuffer;
fc7a2a60
VZ
274
275 DECLARE_NO_COPY_CLASS(wxOutputStream)
32fc4afb
GL
276};
277
2b5f62a0
VZ
278// ============================================================================
279// helper stream classes
280// ============================================================================
281
e2acb9ae
RR
282// ---------------------------------------------------------------------------
283// A stream for measuring streamed output
284// ---------------------------------------------------------------------------
285
bddd7a8d 286class WXDLLIMPEXP_BASE wxCountingOutputStream : public wxOutputStream
c7a9fa36
RR
287{
288public:
289 wxCountingOutputStream();
e2acb9ae 290
588066b7 291 wxFileOffset GetLength() const;
d775fa82 292 bool Ok() const { return true; }
e2acb9ae 293
c7a9fa36 294protected:
47fc03d2 295 virtual size_t OnSysWrite(const void *buffer, size_t size);
4004775e
RL
296 virtual wxFileOffset OnSysSeek(wxFileOffset pos, wxSeekMode mode);
297 virtual wxFileOffset OnSysTell() const;
e2acb9ae 298
c7a9fa36 299 size_t m_currentPos;
fc7a2a60
VZ
300
301 DECLARE_NO_COPY_CLASS(wxCountingOutputStream)
e2acb9ae
RR
302};
303
f4ada568
GL
304// ---------------------------------------------------------------------------
305// "Filter" streams
306// ---------------------------------------------------------------------------
32fc4afb 307
bddd7a8d 308class WXDLLIMPEXP_BASE wxFilterInputStream : public wxInputStream
c7a9fa36
RR
309{
310public:
311 wxFilterInputStream();
312 wxFilterInputStream(wxInputStream& stream);
47fc03d2 313 virtual ~wxFilterInputStream();
32fc4afb 314
c7a9fa36 315 char Peek() { return m_parent_i_stream->Peek(); }
32fc4afb 316
588066b7 317 wxFileOffset GetLength() const { return m_parent_i_stream->GetLength(); }
84b46c35 318
47fc03d2
VZ
319 wxInputStream *GetFilterInputStream() const { return m_parent_i_stream; }
320
c7a9fa36
RR
321protected:
322 wxInputStream *m_parent_i_stream;
22f3361e
VZ
323
324 DECLARE_NO_COPY_CLASS(wxFilterInputStream)
32fc4afb
GL
325};
326
bddd7a8d 327class WXDLLIMPEXP_BASE wxFilterOutputStream : public wxOutputStream
c7a9fa36
RR
328{
329public:
330 wxFilterOutputStream();
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
c7a9fa36
RR
338protected:
339 wxOutputStream *m_parent_o_stream;
22f3361e
VZ
340
341 DECLARE_NO_COPY_CLASS(wxFilterOutputStream)
32fc4afb
GL
342};
343
2b5f62a0
VZ
344// ============================================================================
345// buffered streams
346// ============================================================================
347
fae05df5 348// ---------------------------------------------------------------------------
47fc03d2
VZ
349// Stream buffer: this class can be derived from and passed to
350// wxBufferedStreams to implement custom buffering
fae05df5
GL
351// ---------------------------------------------------------------------------
352
bddd7a8d 353class WXDLLIMPEXP_BASE wxStreamBuffer
c7a9fa36
RR
354{
355public:
49e399d8
VZ
356 enum BufMode
357 {
358 read,
359 write,
360 read_write
361 };
c7a9fa36
RR
362
363 wxStreamBuffer(wxStreamBase& stream, BufMode mode);
c7a9fa36 364 wxStreamBuffer(const wxStreamBuffer& buf);
47fc03d2 365 virtual ~wxStreamBuffer();
c7a9fa36
RR
366
367 // Filtered IO
47fc03d2 368 virtual size_t Read(void *buffer, size_t size);
c7a9fa36 369 size_t Read(wxStreamBuffer *buf);
47fc03d2 370 virtual size_t Write(const void *buffer, size_t size);
c7a9fa36
RR
371 size_t Write(wxStreamBuffer *buf);
372
47fc03d2
VZ
373 virtual char Peek();
374 virtual char GetChar();
375 virtual void PutChar(char c);
4004775e
RL
376 virtual wxFileOffset Tell() const;
377 virtual wxFileOffset Seek(wxFileOffset pos, wxSeekMode mode);
c7a9fa36
RR
378
379 // Buffer control
380 void ResetBuffer();
49e399d8
VZ
381
382 // NB: the buffer must always be allocated with malloc() if takeOwn is
d775fa82
WS
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);
c7a9fa36 386 void SetBufferIO(size_t bufsize);
49e399d8
VZ
387 void *GetBufferStart() const { return m_buffer_start; }
388 void *GetBufferEnd() const { return m_buffer_end; }
389 void *GetBufferPos() const { return m_buffer_pos; }
67c8c225 390 size_t GetBufferSize() const { return m_buffer_size; }
49e399d8
VZ
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; }
c7a9fa36
RR
395
396 void Fixed(bool fixed) { m_fixed = fixed; }
397 void Flushable(bool f) { m_flushable = f; }
398
399 bool FlushBuffer();
400 bool FillBuffer();
401 size_t GetDataLeft();
402
49e399d8
VZ
403 // misc accessors
404 wxStreamBase *GetStream() const { return m_stream; }
405 bool HasBuffer() const { return m_buffer_size != 0; }
406
407 bool IsFixed() const { return m_fixed; }
408 bool IsFlushable() const { return m_flushable; }
409
47fc03d2
VZ
410 // only for input/output buffers respectively, returns NULL otherwise
411 wxInputStream *GetInputStream() const;
412 wxOutputStream *GetOutputStream() const;
413
49e399d8 414 // deprecated, for compatibility only
c7a9fa36
RR
415 wxStreamBase *Stream() { return m_stream; }
416
2b5f62a0
VZ
417 // this constructs a dummy wxStreamBuffer, used by (and exists for)
418 // wxMemoryStreams only, don't use!
419 wxStreamBuffer(BufMode mode);
420
c7a9fa36
RR
421protected:
422 void GetFromBuffer(void *buffer, size_t size);
423 void PutToBuffer(const void *buffer, size_t size);
424
49e399d8
VZ
425 // set the last error to the specified value if we didn't have it before
426 void SetError(wxStreamError err);
427
428 // common part of several ctors
429 void Init();
c7a9fa36 430
49e399d8
VZ
431 // init buffer variables to be empty
432 void InitBuffer();
c7a9fa36 433
49e399d8
VZ
434 // free the buffer (always safe to call)
435 void FreeBuffer();
436
437 // the buffer itself: the pointers to its start and end and the current
438 // position in the buffer
439 char *m_buffer_start,
440 *m_buffer_end,
441 *m_buffer_pos;
442
443 // the buffer size
444 // FIXME: isn't it the same as m_buffer_end - m_buffer_start? (VZ)
445 size_t m_buffer_size;
446
447 // the stream we're associated with
c7a9fa36 448 wxStreamBase *m_stream;
49e399d8
VZ
449
450 // its mode
c7a9fa36 451 BufMode m_mode;
49e399d8
VZ
452
453 // flags
454 bool m_destroybuf, // deallocate buffer?
49e399d8
VZ
455 m_fixed,
456 m_flushable;
22f3361e
VZ
457
458private:
459// Cannot use
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&);
fae05df5
GL
465};
466
467// ---------------------------------------------------------------------------
2b5f62a0 468// wxBufferedInputStream
fae05df5
GL
469// ---------------------------------------------------------------------------
470
bddd7a8d 471class WXDLLIMPEXP_BASE wxBufferedInputStream : public wxFilterInputStream
c7a9fa36
RR
472{
473public:
47fc03d2
VZ
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();
fae05df5 478
c7a9fa36
RR
479 char Peek();
480 wxInputStream& Read(void *buffer, size_t size);
657d2097 481
c7a9fa36 482 // Position functions
4004775e
RL
483 wxFileOffset SeekI(wxFileOffset pos, wxSeekMode mode = wxFromStart);
484 wxFileOffset TellI() const;
3c70014d 485 bool IsSeekable() const { return m_parent_i_stream->IsSeekable(); }
fae05df5 486
47fc03d2
VZ
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; }
490
491 // deprecated, for compatibility only
c7a9fa36 492 wxStreamBuffer *InputStreamBuffer() const { return m_i_streambuf; }
fae05df5 493
c7a9fa36 494protected:
47fc03d2 495 virtual size_t OnSysRead(void *buffer, size_t bufsize);
4004775e
RL
496 virtual wxFileOffset OnSysSeek(wxFileOffset seek, wxSeekMode mode);
497 virtual wxFileOffset OnSysTell() const;
fae05df5 498
c7a9fa36 499 wxStreamBuffer *m_i_streambuf;
22f3361e
VZ
500
501 DECLARE_NO_COPY_CLASS(wxBufferedInputStream)
fae05df5
GL
502};
503
2b5f62a0
VZ
504// ----------------------------------------------------------------------------
505// wxBufferedOutputStream
506// ----------------------------------------------------------------------------
507
bddd7a8d 508class WXDLLIMPEXP_BASE wxBufferedOutputStream : public wxFilterOutputStream
c7a9fa36
RR
509{
510public:
47fc03d2
VZ
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();
fae05df5 515
c7a9fa36 516 wxOutputStream& Write(const void *buffer, size_t size);
657d2097 517
c7a9fa36 518 // Position functions
4004775e
RL
519 wxFileOffset SeekO(wxFileOffset pos, wxSeekMode mode = wxFromStart);
520 wxFileOffset TellO() const;
3c70014d 521 bool IsSeekable() const { return m_parent_o_stream->IsSeekable(); }
fae05df5 522
c7a9fa36 523 void Sync();
8f0ff178 524 bool Close();
fae05df5 525
588066b7 526 wxFileOffset GetLength() const;
657d2097 527
47fc03d2
VZ
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; }
531
532 // deprecated, for compatibility only
c7a9fa36 533 wxStreamBuffer *OutputStreamBuffer() const { return m_o_streambuf; }
fae05df5 534
c7a9fa36 535protected:
47fc03d2 536 virtual size_t OnSysWrite(const void *buffer, size_t bufsize);
4004775e
RL
537 virtual wxFileOffset OnSysSeek(wxFileOffset seek, wxSeekMode mode);
538 virtual wxFileOffset OnSysTell() const;
fae05df5 539
c7a9fa36 540 wxStreamBuffer *m_o_streambuf;
22f3361e
VZ
541
542 DECLARE_NO_COPY_CLASS(wxBufferedOutputStream)
fae05df5
GL
543};
544
47fc03d2
VZ
545#endif // wxUSE_STREAMS
546
547#endif // _WX_WXSTREAM_H__
ce4169a4 548