]> git.saurik.com Git - wxWidgets.git/blame - include/wx/stream.h
patches from Ricky Gonzales <gonzales@pyramid3.net>:
[wxWidgets.git] / include / wx / stream.h
CommitLineData
32fc4afb
GL
1/////////////////////////////////////////////////////////////////////////////
2// Name: stream.h
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
cd6ce4a9
VZ
41typedef enum
42{
c7a9fa36
RR
43 wxSTREAM_NO_ERROR = 0,
44 wxSTREAM_NO_ERR = wxSTREAM_NO_ERROR,
45 wxSTREAM_NOERROR = wxSTREAM_NO_ERROR,
657d2097 46
c7a9fa36 47 wxSTREAM_EOF,
657d2097 48
c7a9fa36
RR
49 wxSTREAM_WRITE_ERROR,
50 wxSTREAM_WRITE_ERR = wxSTREAM_WRITE_ERROR,
657d2097 51
c7a9fa36 52 wxSTREAM_READ_ERROR,
657d2097
DW
53 wxSTREAM_READ_ERR = wxSTREAM_READ_ERROR
54
75ed1d15
GL
55} wxStreamError;
56
cd6ce4a9
VZ
57// compatibility
58#define wxStream_NOERROR wxSTREAM_NOERROR
59#define wxStream_EOF wxSTREAM_EOF
60#define wxStream_WRITE_ERR wxSTREAM_WRITE_ERROR
61#define wxStream_READ_ERR wxSTREAM_READ_ERROR
62
657d2097 63class WXDLLEXPORT wxStreamBase
c7a9fa36
RR
64{
65public:
66 wxStreamBase();
67 virtual ~wxStreamBase();
75ed1d15 68
cd6ce4a9 69 // error testing
c7a9fa36 70 wxStreamError LastError() const { return m_lasterror; }
cd6ce4a9
VZ
71 wxStreamError GetLastError() const { return m_lasterror; }
72 bool IsOk() const { return LastError() == wxSTREAM_NOERROR; }
73 bool operator!() const { return LastError() != wxSTREAM_NOERROR; }
74
c7a9fa36
RR
75 virtual size_t GetSize() const { return ~((size_t)0); }
76 size_t StreamSize() const { return GetSize(); }
75ed1d15 77
c7a9fa36
RR
78protected:
79 virtual size_t OnSysRead(void *buffer, size_t bufsize);
80 virtual size_t OnSysWrite(const void *buffer, size_t bufsize);
81 virtual off_t OnSysSeek(off_t seek, wxSeekMode mode);
82 virtual off_t OnSysTell() const;
75ed1d15 83
c7a9fa36 84 friend class wxStreamBuffer;
75ed1d15 85
c7a9fa36
RR
86 size_t m_lastcount;
87 wxStreamError m_lasterror;
75ed1d15
GL
88};
89
657d2097 90class WXDLLEXPORT wxInputStream: public wxStreamBase
c7a9fa36
RR
91{
92public:
93 wxInputStream();
94 virtual ~wxInputStream();
32fc4afb 95
cd6ce4a9
VZ
96 // is the stream at EOF?
97 virtual bool Eof() const;
98
c7a9fa36
RR
99 // IO functions
100 virtual char Peek();
101 char GetC();
102 virtual wxInputStream& Read(void *buffer, size_t size);
103 wxInputStream& Read(wxOutputStream& stream_out);
32fc4afb 104
c7a9fa36
RR
105 // Position functions
106 virtual off_t SeekI(off_t pos, wxSeekMode mode = wxFromStart);
107 virtual off_t TellI() const;
32fc4afb 108
c7a9fa36
RR
109 // State functions
110 virtual size_t LastRead() { return wxStreamBase::m_lastcount; }
fae05df5 111
c7a9fa36
RR
112 // Ungetch
113 size_t Ungetch(const void *buffer, size_t size);
114 bool Ungetch(char c);
0cd9bfe8 115
c7a9fa36
RR
116 // Operators
117 wxInputStream& operator>>(wxOutputStream& out) { return Read(out); }
47d67540 118#if wxUSE_SERIAL
c7a9fa36 119 wxInputStream& operator>>(wxObject *& obj);
fcc6dddd 120#endif
c7a9fa36 121 wxInputStream& operator>>( __wxInputManip func) { return func(*this); }
fae05df5 122
c7a9fa36
RR
123protected:
124 // Ungetch managers
125 char *m_wback;
126 size_t m_wbacksize;
127 size_t m_wbackcur;
fae05df5 128
c7a9fa36
RR
129 char *AllocSpaceWBack(size_t needed_size);
130 size_t GetWBack(char *buf, size_t bsize);
32fc4afb
GL
131};
132
657d2097 133class WXDLLEXPORT wxOutputStream: public wxStreamBase
c7a9fa36
RR
134{
135public:
136 wxOutputStream();
137 virtual ~wxOutputStream();
32fc4afb 138
c7a9fa36
RR
139 void PutC(char c);
140 virtual wxOutputStream& Write(const void *buffer, size_t size);
141 wxOutputStream& Write(wxInputStream& stream_in);
32fc4afb 142
c7a9fa36
RR
143 virtual off_t SeekO(off_t pos, wxSeekMode mode = wxFromStart);
144 virtual off_t TellO() const;
1678ad78 145
c7a9fa36 146 virtual size_t LastWrite() const { return wxStreamBase::m_lastcount; }
32fc4afb 147
c7a9fa36 148 virtual void Sync();
32fc4afb 149
c7a9fa36 150 wxOutputStream& operator<<(wxInputStream& out) { return Write(out); }
47d67540 151#if wxUSE_SERIAL
c7a9fa36 152 wxOutputStream& operator<<(wxObject& obj);
fcc6dddd 153#endif
c7a9fa36 154 wxOutputStream& operator<<( __wxOutputManip func) { return func(*this); }
32fc4afb
GL
155};
156
e2acb9ae
RR
157// ---------------------------------------------------------------------------
158// A stream for measuring streamed output
159// ---------------------------------------------------------------------------
160
88413fec 161class WXDLLEXPORT wxCountingOutputStream: public wxOutputStream
c7a9fa36
RR
162{
163public:
164 wxCountingOutputStream();
e2acb9ae 165
c7a9fa36
RR
166 size_t GetSize() const;
167 bool Ok() const { return TRUE; }
e2acb9ae 168
c7a9fa36
RR
169protected:
170 size_t OnSysWrite(const void *buffer, size_t size);
171 off_t OnSysSeek(off_t pos, wxSeekMode mode);
172 off_t OnSysTell() const;
e2acb9ae 173
c7a9fa36 174 size_t m_currentPos;
e2acb9ae
RR
175};
176
f4ada568
GL
177// ---------------------------------------------------------------------------
178// "Filter" streams
179// ---------------------------------------------------------------------------
32fc4afb 180
657d2097 181class WXDLLEXPORT wxFilterInputStream: public wxInputStream
c7a9fa36
RR
182{
183public:
184 wxFilterInputStream();
185 wxFilterInputStream(wxInputStream& stream);
186 ~wxFilterInputStream();
32fc4afb 187
c7a9fa36 188 char Peek() { return m_parent_i_stream->Peek(); }
32fc4afb 189
c7a9fa36 190 size_t GetSize() const { return m_parent_i_stream->GetSize(); }
84b46c35 191
c7a9fa36
RR
192protected:
193 wxInputStream *m_parent_i_stream;
32fc4afb
GL
194};
195
657d2097 196class WXDLLEXPORT wxFilterOutputStream: public wxOutputStream
c7a9fa36
RR
197{
198public:
199 wxFilterOutputStream();
200 wxFilterOutputStream(wxOutputStream& stream);
201 ~wxFilterOutputStream();
1678ad78 202
c7a9fa36 203 size_t GetSize() const { return m_parent_o_stream->GetSize(); }
84b46c35 204
c7a9fa36
RR
205protected:
206 wxOutputStream *m_parent_o_stream;
32fc4afb
GL
207};
208
fae05df5
GL
209// ---------------------------------------------------------------------------
210// Stream buffer
211// ---------------------------------------------------------------------------
212
657d2097 213class WXDLLEXPORT wxStreamBuffer
c7a9fa36
RR
214{
215public:
216 typedef enum {
217 read = 0, write, read_write
218 } BufMode;
219
220 wxStreamBuffer(wxStreamBase& stream, BufMode mode);
221 wxStreamBuffer(BufMode mode);
222 wxStreamBuffer(const wxStreamBuffer& buf);
223 ~wxStreamBuffer();
224
225 // Filtered IO
226 size_t Read(void *buffer, size_t size);
227 size_t Read(wxStreamBuffer *buf);
228 size_t Write(const void *buffer, size_t size);
229 size_t Write(wxStreamBuffer *buf);
230
231 char Peek();
232 char GetChar();
233 void PutChar(char c);
234 off_t Tell() const;
235 off_t Seek(off_t pos, wxSeekMode mode);
236
237 // Buffer control
238 void ResetBuffer();
239 void SetBufferIO(char *buffer_start, char *buffer_end);
240 void SetBufferIO(size_t bufsize);
241 char *GetBufferStart() const { return m_buffer_start; }
242 char *GetBufferEnd() const { return m_buffer_end; }
243 char *GetBufferPos() const { return m_buffer_pos; }
244 off_t GetIntPosition() const { return m_buffer_pos-m_buffer_start; }
245 void SetIntPosition(off_t pos) { m_buffer_pos = m_buffer_start+pos; }
246 size_t GetLastAccess() const { return m_buffer_end-m_buffer_start; }
247
248 void Fixed(bool fixed) { m_fixed = fixed; }
249 void Flushable(bool f) { m_flushable = f; }
250
251 bool FlushBuffer();
252 bool FillBuffer();
253 size_t GetDataLeft();
254
255 // Misc.
256 wxStreamBase *Stream() { return m_stream; }
257
258protected:
259 void GetFromBuffer(void *buffer, size_t size);
260 void PutToBuffer(const void *buffer, size_t size);
261
262 char *m_buffer_start, *m_buffer_end, *m_buffer_pos;
263 size_t m_buffer_size;
264
265 bool m_fixed, m_flushable;
266
267 wxStreamBase *m_stream;
268 BufMode m_mode;
269 bool m_destroybuf, m_destroystream;
fae05df5
GL
270};
271
272// ---------------------------------------------------------------------------
273// wxBufferedStreams
274// ---------------------------------------------------------------------------
275
24e148ad 276class WXDLLEXPORT wxBufferedInputStream: public wxFilterInputStream
c7a9fa36
RR
277{
278public:
279 wxBufferedInputStream(wxInputStream& stream);
280 ~wxBufferedInputStream();
fae05df5 281
c7a9fa36
RR
282 char Peek();
283 wxInputStream& Read(void *buffer, size_t size);
657d2097 284
c7a9fa36
RR
285 // Position functions
286 off_t SeekI(off_t pos, wxSeekMode mode = wxFromStart);
287 off_t TellI() const;
fae05df5 288
c7a9fa36 289 wxStreamBuffer *InputStreamBuffer() const { return m_i_streambuf; }
fae05df5 290
c7a9fa36
RR
291protected:
292 size_t OnSysRead(void *buffer, size_t bufsize);
293 off_t OnSysSeek(off_t seek, wxSeekMode mode);
657d2097 294 off_t OnSysTell() const;
fae05df5 295
c7a9fa36 296 wxStreamBuffer *m_i_streambuf;
fae05df5
GL
297};
298
24e148ad 299class WXDLLEXPORT wxBufferedOutputStream: public wxFilterOutputStream
c7a9fa36
RR
300{
301public:
302 wxBufferedOutputStream(wxOutputStream& stream);
303 ~wxBufferedOutputStream();
fae05df5 304
c7a9fa36 305 wxOutputStream& Write(const void *buffer, size_t size);
657d2097 306
c7a9fa36
RR
307 // Position functions
308 off_t SeekO(off_t pos, wxSeekMode mode = wxFromStart);
309 off_t TellO() const;
fae05df5 310
c7a9fa36 311 void Sync();
fae05df5 312
c7a9fa36 313 size_t GetSize() const;
657d2097 314
c7a9fa36 315 wxStreamBuffer *OutputStreamBuffer() const { return m_o_streambuf; }
fae05df5 316
c7a9fa36
RR
317protected:
318 size_t OnSysWrite(const void *buffer, size_t bufsize);
319 off_t OnSysSeek(off_t seek, wxSeekMode mode);
657d2097 320 off_t OnSysTell() const;
fae05df5 321
c7a9fa36 322 wxStreamBuffer *m_o_streambuf;
fae05df5
GL
323};
324
32fc4afb 325#endif
ce4169a4
RR
326 // wxUSE_STREAMS
327
328#endif
329 // _WX_WXSTREAM_H__