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