]> git.saurik.com Git - wxWidgets.git/blob - include/wx/stream.h
Doc updates; new grid disabled for BC++/WIN16 (too much global data)
[wxWidgets.git] / include / wx / stream.h
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
12 #ifndef _WX_WXSTREAM_H__
13 #define _WX_WXSTREAM_H__
14
15 #ifdef __GNUG__
16 #pragma interface
17 #endif
18
19 #include "wx/defs.h"
20
21 #if wxUSE_STREAMS
22
23 #include <stdio.h>
24 #include "wx/object.h"
25 #include "wx/string.h"
26 #include "wx/filefn.h" // for off_t, wxInvalidOffset and wxSeekMode
27
28 class WXDLLEXPORT wxStreamBase;
29 class WXDLLEXPORT wxInputStream;
30 class WXDLLEXPORT wxOutputStream;
31
32 typedef wxInputStream& (*__wxInputManip)(wxInputStream&);
33 typedef wxOutputStream& (*__wxOutputManip)(wxOutputStream&);
34
35 WXDLLEXPORT wxOutputStream& wxEndL(wxOutputStream& o_stream);
36
37 // ---------------------------------------------------------------------------
38 // wxStream: base classes
39 // ---------------------------------------------------------------------------
40
41 typedef enum
42 {
43 wxSTREAM_NO_ERROR = 0,
44 wxSTREAM_NO_ERR = wxSTREAM_NO_ERROR,
45 wxSTREAM_NOERROR = wxSTREAM_NO_ERROR,
46
47 wxSTREAM_EOF,
48
49 wxSTREAM_WRITE_ERROR,
50 wxSTREAM_WRITE_ERR = wxSTREAM_WRITE_ERROR,
51
52 wxSTREAM_READ_ERROR,
53 wxSTREAM_READ_ERR = wxSTREAM_READ_ERROR
54
55 } wxStreamError;
56
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
63 class WXDLLEXPORT wxStreamBase
64 {
65 public:
66 wxStreamBase();
67 virtual ~wxStreamBase();
68
69 // error testing
70 wxStreamError LastError() const { return m_lasterror; }
71 wxStreamError GetLastError() const { return m_lasterror; }
72 bool IsOk() const { return LastError() == wxSTREAM_NOERROR; }
73 bool operator!() const { return LastError() != wxSTREAM_NOERROR; }
74
75 virtual size_t GetSize() const { return ~((size_t)0); }
76 size_t StreamSize() const { return GetSize(); }
77
78 protected:
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;
83
84 friend class wxStreamBuffer;
85
86 size_t m_lastcount;
87 wxStreamError m_lasterror;
88 };
89
90 class WXDLLEXPORT wxInputStream: public wxStreamBase
91 {
92 public:
93 wxInputStream();
94 virtual ~wxInputStream();
95
96 // is the stream at EOF?
97 virtual bool Eof() const;
98
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);
104
105 // Position functions
106 virtual off_t SeekI(off_t pos, wxSeekMode mode = wxFromStart);
107 virtual off_t TellI() const;
108
109 // State functions
110 virtual size_t LastRead() { return wxStreamBase::m_lastcount; }
111
112 // Ungetch
113 size_t Ungetch(const void *buffer, size_t size);
114 bool Ungetch(char c);
115
116 // Operators
117 wxInputStream& operator>>(wxOutputStream& out) { return Read(out); }
118 #if wxUSE_SERIAL
119 wxInputStream& operator>>(wxObject *& obj);
120 #endif
121 wxInputStream& operator>>( __wxInputManip func) { return func(*this); }
122
123 protected:
124 // Ungetch managers
125 char *m_wback;
126 size_t m_wbacksize;
127 size_t m_wbackcur;
128
129 char *AllocSpaceWBack(size_t needed_size);
130 size_t GetWBack(char *buf, size_t bsize);
131 };
132
133 class WXDLLEXPORT wxOutputStream: public wxStreamBase
134 {
135 public:
136 wxOutputStream();
137 virtual ~wxOutputStream();
138
139 void PutC(char c);
140 virtual wxOutputStream& Write(const void *buffer, size_t size);
141 wxOutputStream& Write(wxInputStream& stream_in);
142
143 virtual off_t SeekO(off_t pos, wxSeekMode mode = wxFromStart);
144 virtual off_t TellO() const;
145
146 virtual size_t LastWrite() const { return wxStreamBase::m_lastcount; }
147
148 virtual void Sync();
149
150 wxOutputStream& operator<<(wxInputStream& out) { return Write(out); }
151 #if wxUSE_SERIAL
152 wxOutputStream& operator<<(wxObject& obj);
153 #endif
154 wxOutputStream& operator<<( __wxOutputManip func) { return func(*this); }
155 };
156
157 // ---------------------------------------------------------------------------
158 // A stream for measuring streamed output
159 // ---------------------------------------------------------------------------
160
161 class WXDLLEXPORT wxCountingOutputStream: public wxOutputStream
162 {
163 public:
164 wxCountingOutputStream();
165
166 size_t GetSize() const;
167 bool Ok() const { return TRUE; }
168
169 protected:
170 size_t OnSysWrite(const void *buffer, size_t size);
171 off_t OnSysSeek(off_t pos, wxSeekMode mode);
172 off_t OnSysTell() const;
173
174 size_t m_currentPos;
175 };
176
177 // ---------------------------------------------------------------------------
178 // "Filter" streams
179 // ---------------------------------------------------------------------------
180
181 class WXDLLEXPORT wxFilterInputStream: public wxInputStream
182 {
183 public:
184 wxFilterInputStream();
185 wxFilterInputStream(wxInputStream& stream);
186 ~wxFilterInputStream();
187
188 char Peek() { return m_parent_i_stream->Peek(); }
189
190 size_t GetSize() const { return m_parent_i_stream->GetSize(); }
191
192 protected:
193 wxInputStream *m_parent_i_stream;
194 };
195
196 class WXDLLEXPORT wxFilterOutputStream: public wxOutputStream
197 {
198 public:
199 wxFilterOutputStream();
200 wxFilterOutputStream(wxOutputStream& stream);
201 ~wxFilterOutputStream();
202
203 size_t GetSize() const { return m_parent_o_stream->GetSize(); }
204
205 protected:
206 wxOutputStream *m_parent_o_stream;
207 };
208
209 // ---------------------------------------------------------------------------
210 // Stream buffer
211 // ---------------------------------------------------------------------------
212
213 class WXDLLEXPORT wxStreamBuffer
214 {
215 public:
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
258 protected:
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;
270 };
271
272 // ---------------------------------------------------------------------------
273 // wxBufferedStreams
274 // ---------------------------------------------------------------------------
275
276 class WXDLLEXPORT wxBufferedInputStream: public wxFilterInputStream
277 {
278 public:
279 wxBufferedInputStream(wxInputStream& stream);
280 ~wxBufferedInputStream();
281
282 char Peek();
283 wxInputStream& Read(void *buffer, size_t size);
284
285 // Position functions
286 off_t SeekI(off_t pos, wxSeekMode mode = wxFromStart);
287 off_t TellI() const;
288
289 wxStreamBuffer *InputStreamBuffer() const { return m_i_streambuf; }
290
291 protected:
292 size_t OnSysRead(void *buffer, size_t bufsize);
293 off_t OnSysSeek(off_t seek, wxSeekMode mode);
294 off_t OnSysTell() const;
295
296 wxStreamBuffer *m_i_streambuf;
297 };
298
299 class WXDLLEXPORT wxBufferedOutputStream: public wxFilterOutputStream
300 {
301 public:
302 wxBufferedOutputStream(wxOutputStream& stream);
303 ~wxBufferedOutputStream();
304
305 wxOutputStream& Write(const void *buffer, size_t size);
306
307 // Position functions
308 off_t SeekO(off_t pos, wxSeekMode mode = wxFromStart);
309 off_t TellO() const;
310
311 void Sync();
312
313 size_t GetSize() const;
314
315 wxStreamBuffer *OutputStreamBuffer() const { return m_o_streambuf; }
316
317 protected:
318 size_t OnSysWrite(const void *buffer, size_t bufsize);
319 off_t OnSysSeek(off_t seek, wxSeekMode mode);
320 off_t OnSysTell() const;
321
322 wxStreamBuffer *m_o_streambuf;
323 };
324
325 #endif
326 // wxUSE_STREAMS
327
328 #endif
329 // _WX_WXSTREAM_H__