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