]> git.saurik.com Git - wxWidgets.git/blame - include/wx/stream.h
moved the rest of XPMs to /art
[wxWidgets.git] / include / wx / stream.h
CommitLineData
32fc4afb 1/////////////////////////////////////////////////////////////////////////////
49e399d8 2// Name: wx/stream.h
32fc4afb
GL
3// Purpose: "wxWindows stream" base classes
4// Author: Guilhem Lavaux
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
GL
14
15#ifdef __GNUG__
79c3e0e1 16#pragma interface
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
f4ada568
GL
37// ---------------------------------------------------------------------------
38// wxStream: base classes
39// ---------------------------------------------------------------------------
40
49e399d8 41enum wxStreamError
cd6ce4a9 42{
47fc03d2
VZ
43 wxSTREAM_NO_ERROR = 0,
44 wxSTREAM_NO_ERR = wxSTREAM_NO_ERROR,
45 wxSTREAM_NOERROR = wxSTREAM_NO_ERROR,
657d2097 46
47fc03d2 47 wxSTREAM_EOF,
657d2097 48
47fc03d2
VZ
49 wxSTREAM_WRITE_ERROR,
50 wxSTREAM_WRITE_ERR = wxSTREAM_WRITE_ERROR,
657d2097 51
47fc03d2
VZ
52 wxSTREAM_READ_ERROR,
53 wxSTREAM_READ_ERR = wxSTREAM_READ_ERROR
49e399d8 54};
75ed1d15 55
cd6ce4a9
VZ
56// compatibility
57#define wxStream_NOERROR wxSTREAM_NOERROR
58#define wxStream_EOF wxSTREAM_EOF
59#define wxStream_WRITE_ERR wxSTREAM_WRITE_ERROR
60#define wxStream_READ_ERR wxSTREAM_READ_ERROR
61
657d2097 62class WXDLLEXPORT wxStreamBase
c7a9fa36
RR
63{
64public:
65 wxStreamBase();
66 virtual ~wxStreamBase();
75ed1d15 67
cd6ce4a9 68 // error testing
cd6ce4a9
VZ
69 wxStreamError GetLastError() const { return m_lasterror; }
70 bool IsOk() const { return LastError() == wxSTREAM_NOERROR; }
71 bool operator!() const { return LastError() != wxSTREAM_NOERROR; }
72
47fc03d2
VZ
73 virtual size_t GetSize() const { return 0; }
74
75 // deprecated, for compatibility only
76 wxStreamError LastError() const { return m_lasterror; }
c7a9fa36 77 size_t StreamSize() const { return GetSize(); }
75ed1d15 78
c7a9fa36 79protected:
47fc03d2
VZ
80 // VZ: these functions are really pure virtual and shouldn't be declared
81 // in the base class because it creates ambiguties in stream classes
82 // deriving from both wxInputStream and wxOutputStream
83#if 0
c7a9fa36
RR
84 virtual size_t OnSysRead(void *buffer, size_t bufsize);
85 virtual size_t OnSysWrite(const void *buffer, size_t bufsize);
47fc03d2
VZ
86#endif
87
c7a9fa36
RR
88 virtual off_t OnSysSeek(off_t seek, wxSeekMode mode);
89 virtual off_t OnSysTell() const;
75ed1d15 90
c7a9fa36 91 friend class wxStreamBuffer;
75ed1d15 92
c7a9fa36
RR
93 size_t m_lastcount;
94 wxStreamError m_lasterror;
75ed1d15
GL
95};
96
47fc03d2 97class WXDLLEXPORT wxInputStream : /* virtual */ public wxStreamBase
c7a9fa36
RR
98{
99public:
100 wxInputStream();
101 virtual ~wxInputStream();
32fc4afb 102
cd6ce4a9
VZ
103 // is the stream at EOF?
104 virtual bool Eof() const;
105
c7a9fa36
RR
106 // IO functions
107 virtual char Peek();
108 char GetC();
109 virtual wxInputStream& Read(void *buffer, size_t size);
110 wxInputStream& Read(wxOutputStream& stream_out);
32fc4afb 111
c7a9fa36
RR
112 // Position functions
113 virtual off_t SeekI(off_t pos, wxSeekMode mode = wxFromStart);
114 virtual off_t TellI() const;
32fc4afb 115
c7a9fa36
RR
116 // State functions
117 virtual size_t LastRead() { return wxStreamBase::m_lastcount; }
fae05df5 118
c7a9fa36
RR
119 // Ungetch
120 size_t Ungetch(const void *buffer, size_t size);
121 bool Ungetch(char c);
0cd9bfe8 122
c7a9fa36
RR
123 // Operators
124 wxInputStream& operator>>(wxOutputStream& out) { return Read(out); }
c7a9fa36 125 wxInputStream& operator>>( __wxInputManip func) { return func(*this); }
fae05df5 126
c7a9fa36 127protected:
47fc03d2
VZ
128 // to be implemented in the derived classes (it should have been pure
129 // virtual)
130 virtual size_t OnSysRead(void *buffer, size_t bufsize);
131
c7a9fa36
RR
132 // Ungetch managers
133 char *m_wback;
134 size_t m_wbacksize;
135 size_t m_wbackcur;
fae05df5 136
c7a9fa36 137 char *AllocSpaceWBack(size_t needed_size);
49e399d8 138 size_t GetWBack(void *buf, size_t bsize);
47fc03d2
VZ
139
140 friend class wxStreamBuffer;
32fc4afb
GL
141};
142
47fc03d2 143class WXDLLEXPORT wxOutputStream : /* virtual */ public wxStreamBase
c7a9fa36
RR
144{
145public:
146 wxOutputStream();
147 virtual ~wxOutputStream();
32fc4afb 148
c7a9fa36
RR
149 void PutC(char c);
150 virtual wxOutputStream& Write(const void *buffer, size_t size);
151 wxOutputStream& Write(wxInputStream& stream_in);
32fc4afb 152
c7a9fa36
RR
153 virtual off_t SeekO(off_t pos, wxSeekMode mode = wxFromStart);
154 virtual off_t TellO() const;
1678ad78 155
c7a9fa36 156 virtual size_t LastWrite() const { return wxStreamBase::m_lastcount; }
32fc4afb 157
c7a9fa36 158 virtual void Sync();
32fc4afb 159
c7a9fa36 160 wxOutputStream& operator<<(wxInputStream& out) { return Write(out); }
c7a9fa36 161 wxOutputStream& operator<<( __wxOutputManip func) { return func(*this); }
47fc03d2
VZ
162
163protected:
164 // to be implemented in the derived classes (it should have been pure
165 // virtual)
166 virtual size_t OnSysWrite(const void *buffer, size_t bufsize);
167
168 friend class wxStreamBuffer;
32fc4afb
GL
169};
170
e2acb9ae
RR
171// ---------------------------------------------------------------------------
172// A stream for measuring streamed output
173// ---------------------------------------------------------------------------
174
47fc03d2 175class WXDLLEXPORT wxCountingOutputStream : public wxOutputStream
c7a9fa36
RR
176{
177public:
178 wxCountingOutputStream();
e2acb9ae 179
c7a9fa36
RR
180 size_t GetSize() const;
181 bool Ok() const { return TRUE; }
e2acb9ae 182
c7a9fa36 183protected:
47fc03d2
VZ
184 virtual size_t OnSysWrite(const void *buffer, size_t size);
185 virtual off_t OnSysSeek(off_t pos, wxSeekMode mode);
186 virtual off_t OnSysTell() const;
e2acb9ae 187
c7a9fa36 188 size_t m_currentPos;
e2acb9ae
RR
189};
190
f4ada568
GL
191// ---------------------------------------------------------------------------
192// "Filter" streams
193// ---------------------------------------------------------------------------
32fc4afb 194
47fc03d2 195class WXDLLEXPORT wxFilterInputStream : public wxInputStream
c7a9fa36
RR
196{
197public:
198 wxFilterInputStream();
199 wxFilterInputStream(wxInputStream& stream);
47fc03d2 200 virtual ~wxFilterInputStream();
32fc4afb 201
c7a9fa36 202 char Peek() { return m_parent_i_stream->Peek(); }
32fc4afb 203
c7a9fa36 204 size_t GetSize() const { return m_parent_i_stream->GetSize(); }
84b46c35 205
47fc03d2
VZ
206 wxInputStream *GetFilterInputStream() const { return m_parent_i_stream; }
207
c7a9fa36
RR
208protected:
209 wxInputStream *m_parent_i_stream;
32fc4afb
GL
210};
211
47fc03d2 212class WXDLLEXPORT wxFilterOutputStream : public wxOutputStream
c7a9fa36
RR
213{
214public:
215 wxFilterOutputStream();
216 wxFilterOutputStream(wxOutputStream& stream);
47fc03d2 217 virtual ~wxFilterOutputStream();
1678ad78 218
c7a9fa36 219 size_t GetSize() const { return m_parent_o_stream->GetSize(); }
84b46c35 220
47fc03d2
VZ
221 wxOutputStream *GetFilterOutputStream() const { return m_parent_o_stream; }
222
c7a9fa36
RR
223protected:
224 wxOutputStream *m_parent_o_stream;
32fc4afb
GL
225};
226
fae05df5 227// ---------------------------------------------------------------------------
47fc03d2
VZ
228// Stream buffer: this class can be derived from and passed to
229// wxBufferedStreams to implement custom buffering
fae05df5
GL
230// ---------------------------------------------------------------------------
231
657d2097 232class WXDLLEXPORT wxStreamBuffer
c7a9fa36
RR
233{
234public:
49e399d8
VZ
235 enum BufMode
236 {
237 read,
238 write,
239 read_write
240 };
c7a9fa36
RR
241
242 wxStreamBuffer(wxStreamBase& stream, BufMode mode);
243 wxStreamBuffer(BufMode mode);
244 wxStreamBuffer(const wxStreamBuffer& buf);
47fc03d2 245 virtual ~wxStreamBuffer();
c7a9fa36
RR
246
247 // Filtered IO
47fc03d2 248 virtual size_t Read(void *buffer, size_t size);
c7a9fa36 249 size_t Read(wxStreamBuffer *buf);
47fc03d2 250 virtual size_t Write(const void *buffer, size_t size);
c7a9fa36
RR
251 size_t Write(wxStreamBuffer *buf);
252
47fc03d2
VZ
253 virtual char Peek();
254 virtual char GetChar();
255 virtual void PutChar(char c);
256 virtual off_t Tell() const;
257 virtual off_t Seek(off_t pos, wxSeekMode mode);
c7a9fa36
RR
258
259 // Buffer control
260 void ResetBuffer();
49e399d8
VZ
261
262 // NB: the buffer must always be allocated with malloc() if takeOwn is
263 // TRUE as it will be deallocated by free()
264 void SetBufferIO(void *start, void *end, bool takeOwnership = FALSE);
67c8c225 265 void SetBufferIO(void *start, size_t len, bool takeOwnership = FALSE);
c7a9fa36 266 void SetBufferIO(size_t bufsize);
49e399d8
VZ
267 void *GetBufferStart() const { return m_buffer_start; }
268 void *GetBufferEnd() const { return m_buffer_end; }
269 void *GetBufferPos() const { return m_buffer_pos; }
67c8c225 270 size_t GetBufferSize() const { return m_buffer_size; }
49e399d8
VZ
271 size_t GetIntPosition() const { return m_buffer_pos - m_buffer_start; }
272 void SetIntPosition(size_t pos) { m_buffer_pos = m_buffer_start + pos; }
273 size_t GetLastAccess() const { return m_buffer_end - m_buffer_start; }
274 size_t GetBytesLeft() const { return m_buffer_end - m_buffer_pos; }
c7a9fa36
RR
275
276 void Fixed(bool fixed) { m_fixed = fixed; }
277 void Flushable(bool f) { m_flushable = f; }
278
279 bool FlushBuffer();
280 bool FillBuffer();
281 size_t GetDataLeft();
282
49e399d8
VZ
283 // misc accessors
284 wxStreamBase *GetStream() const { return m_stream; }
285 bool HasBuffer() const { return m_buffer_size != 0; }
286
287 bool IsFixed() const { return m_fixed; }
288 bool IsFlushable() const { return m_flushable; }
289
47fc03d2
VZ
290 // only for input/output buffers respectively, returns NULL otherwise
291 wxInputStream *GetInputStream() const;
292 wxOutputStream *GetOutputStream() const;
293
49e399d8 294 // deprecated, for compatibility only
c7a9fa36
RR
295 wxStreamBase *Stream() { return m_stream; }
296
297protected:
298 void GetFromBuffer(void *buffer, size_t size);
299 void PutToBuffer(const void *buffer, size_t size);
300
49e399d8
VZ
301 // set the last error to the specified value if we didn't have it before
302 void SetError(wxStreamError err);
303
304 // common part of several ctors
305 void Init();
c7a9fa36 306
49e399d8
VZ
307 // init buffer variables to be empty
308 void InitBuffer();
c7a9fa36 309
49e399d8
VZ
310 // free the buffer (always safe to call)
311 void FreeBuffer();
312
313 // the buffer itself: the pointers to its start and end and the current
314 // position in the buffer
315 char *m_buffer_start,
316 *m_buffer_end,
317 *m_buffer_pos;
318
319 // the buffer size
320 // FIXME: isn't it the same as m_buffer_end - m_buffer_start? (VZ)
321 size_t m_buffer_size;
322
323 // the stream we're associated with
c7a9fa36 324 wxStreamBase *m_stream;
49e399d8
VZ
325
326 // its mode
c7a9fa36 327 BufMode m_mode;
49e399d8
VZ
328
329 // flags
330 bool m_destroybuf, // deallocate buffer?
331 m_destroystream, // delete associated stream?
332 m_fixed,
333 m_flushable;
fae05df5
GL
334};
335
336// ---------------------------------------------------------------------------
337// wxBufferedStreams
338// ---------------------------------------------------------------------------
339
47fc03d2 340class WXDLLEXPORT wxBufferedInputStream : public wxFilterInputStream
c7a9fa36
RR
341{
342public:
47fc03d2
VZ
343 // if a non NULL buffer is given to the stream, it will be deleted by it
344 wxBufferedInputStream(wxInputStream& stream,
345 wxStreamBuffer *buffer = NULL);
346 virtual ~wxBufferedInputStream();
fae05df5 347
c7a9fa36
RR
348 char Peek();
349 wxInputStream& Read(void *buffer, size_t size);
657d2097 350
c7a9fa36
RR
351 // Position functions
352 off_t SeekI(off_t pos, wxSeekMode mode = wxFromStart);
353 off_t TellI() const;
fae05df5 354
47fc03d2
VZ
355 // the buffer given to the stream will be deleted by it
356 void SetInputStreamBuffer(wxStreamBuffer *buffer);
357 wxStreamBuffer *GetInputStreamBuffer() const { return m_i_streambuf; }
358
359 // deprecated, for compatibility only
c7a9fa36 360 wxStreamBuffer *InputStreamBuffer() const { return m_i_streambuf; }
fae05df5 361
c7a9fa36 362protected:
47fc03d2
VZ
363 virtual size_t OnSysRead(void *buffer, size_t bufsize);
364 virtual off_t OnSysSeek(off_t seek, wxSeekMode mode);
365 virtual off_t OnSysTell() const;
fae05df5 366
c7a9fa36 367 wxStreamBuffer *m_i_streambuf;
fae05df5
GL
368};
369
47fc03d2 370class WXDLLEXPORT wxBufferedOutputStream : public wxFilterOutputStream
c7a9fa36
RR
371{
372public:
47fc03d2
VZ
373 // if a non NULL buffer is given to the stream, it will be deleted by it
374 wxBufferedOutputStream(wxOutputStream& stream,
375 wxStreamBuffer *buffer = NULL);
376 virtual ~wxBufferedOutputStream();
fae05df5 377
c7a9fa36 378 wxOutputStream& Write(const void *buffer, size_t size);
657d2097 379
c7a9fa36
RR
380 // Position functions
381 off_t SeekO(off_t pos, wxSeekMode mode = wxFromStart);
382 off_t TellO() const;
fae05df5 383
c7a9fa36 384 void Sync();
fae05df5 385
c7a9fa36 386 size_t GetSize() const;
657d2097 387
47fc03d2
VZ
388 // the buffer given to the stream will be deleted by it
389 void SetOutputStreamBuffer(wxStreamBuffer *buffer);
390 wxStreamBuffer *GetOutputStreamBuffer() const { return m_o_streambuf; }
391
392 // deprecated, for compatibility only
c7a9fa36 393 wxStreamBuffer *OutputStreamBuffer() const { return m_o_streambuf; }
fae05df5 394
c7a9fa36 395protected:
47fc03d2
VZ
396 virtual size_t OnSysWrite(const void *buffer, size_t bufsize);
397 virtual off_t OnSysSeek(off_t seek, wxSeekMode mode);
398 virtual off_t OnSysTell() const;
fae05df5 399
c7a9fa36 400 wxStreamBuffer *m_o_streambuf;
fae05df5
GL
401};
402
47fc03d2
VZ
403#endif // wxUSE_STREAMS
404
405#endif // _WX_WXSTREAM_H__
ce4169a4 406