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