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