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