]> git.saurik.com Git - wxWidgets.git/blob - include/wx/stream.h
1. wxMotif toolbar works; the toggle buttons have the same width as the other
[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 void PutC(char c);
123 virtual wxOutputStream& Write(const void *buffer, size_t size);
124 wxOutputStream& Write(wxInputStream& stream_in);
125
126 virtual off_t SeekO(off_t pos, wxSeekMode mode = wxFromStart);
127 virtual off_t TellO() const;
128
129 virtual size_t LastWrite() const { return wxStreamBase::m_lastcount; }
130
131 virtual void Sync();
132
133 wxOutputStream& operator<<(wxInputStream& out) { return Write(out); }
134 #if wxUSE_SERIAL
135 wxOutputStream& operator<<(wxObject& obj);
136 #endif
137 wxOutputStream& operator<<( __wxOutputManip func) { return func(*this); }
138 };
139
140 // ---------------------------------------------------------------------------
141 // A stream for measuring streamed output
142 // ---------------------------------------------------------------------------
143
144 class wxCountingOutputStream: public wxOutputStream {
145 public:
146 wxCountingOutputStream();
147
148 size_t GetSize() const;
149 bool Ok() const { return TRUE; }
150
151 protected:
152
153 size_t OnSysWrite(const void *buffer, size_t size);
154 off_t OnSysSeek(off_t pos, wxSeekMode mode);
155 off_t OnSysTell() const;
156
157 protected:
158 size_t m_currentPos;
159 };
160
161
162 // ---------------------------------------------------------------------------
163 // "Filter" streams
164 // ---------------------------------------------------------------------------
165
166 class WXDLLEXPORT wxFilterInputStream: public wxInputStream {
167 public:
168 wxFilterInputStream();
169 wxFilterInputStream(wxInputStream& stream);
170 ~wxFilterInputStream();
171
172 char Peek() { return m_parent_i_stream->Peek(); }
173
174 size_t GetSize() const { return m_parent_i_stream->GetSize(); }
175
176 protected:
177 wxInputStream *m_parent_i_stream;
178 };
179
180 class WXDLLEXPORT wxFilterOutputStream: public wxOutputStream {
181 public:
182 wxFilterOutputStream();
183 wxFilterOutputStream(wxOutputStream& stream);
184 ~wxFilterOutputStream();
185
186 size_t GetSize() const { return m_parent_o_stream->GetSize(); }
187
188 protected:
189 wxOutputStream *m_parent_o_stream;
190 };
191
192 // ---------------------------------------------------------------------------
193 // Stream buffer
194 // ---------------------------------------------------------------------------
195
196 class WXDLLEXPORT wxStreamBuffer {
197 public:
198 typedef enum {
199 read = 0, write, read_write
200 } BufMode;
201
202 // -----------
203 // ctor & dtor
204 // -----------
205 wxStreamBuffer(wxStreamBase& stream, BufMode mode);
206 wxStreamBuffer(BufMode mode);
207 wxStreamBuffer(const wxStreamBuffer& buf);
208 ~wxStreamBuffer();
209
210 // -----------
211 // Filtered IO
212 // -----------
213 size_t Read(void *buffer, size_t size);
214 size_t Read(wxStreamBuffer *buf);
215 size_t Write(const void *buffer, size_t size);
216 size_t Write(wxStreamBuffer *buf);
217
218 char Peek();
219 char GetChar();
220 void PutChar(char c);
221 off_t Tell() const;
222 off_t Seek(off_t pos, wxSeekMode mode);
223
224 // --------------
225 // Buffer control
226 // --------------
227 void ResetBuffer();
228 void SetBufferIO(char *buffer_start, char *buffer_end);
229 void SetBufferIO(size_t bufsize);
230 char *GetBufferStart() const { return m_buffer_start; }
231 char *GetBufferEnd() const { return m_buffer_end; }
232 char *GetBufferPos() const { return m_buffer_pos; }
233 off_t GetIntPosition() const { return m_buffer_pos-m_buffer_start; }
234 void SetIntPosition(off_t pos) { m_buffer_pos = m_buffer_start+pos; }
235 size_t GetLastAccess() const { return m_buffer_end-m_buffer_start; }
236
237 void Fixed(bool fixed) { m_fixed = fixed; }
238 void Flushable(bool f) { m_flushable = f; }
239
240 bool FlushBuffer();
241 bool FillBuffer();
242 size_t GetDataLeft();
243
244 // --------------
245 // Administration
246 // --------------
247 wxStreamBase *Stream() { return m_stream; }
248
249 protected:
250 void GetFromBuffer(void *buffer, size_t size);
251 void PutToBuffer(const void *buffer, size_t size);
252
253 protected:
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 wxBufferedInputStream: public wxFilterInputStream {
269 public:
270 wxBufferedInputStream(wxInputStream& stream);
271 ~wxBufferedInputStream();
272
273 char Peek();
274 wxInputStream& Read(void *buffer, size_t size);
275
276 // Position functions
277 off_t SeekI(off_t pos, wxSeekMode mode = wxFromStart);
278 off_t TellI() const;
279
280 wxStreamBuffer *InputStreamBuffer() const { return m_i_streambuf; }
281
282 protected:
283 size_t OnSysRead(void *buffer, size_t bufsize);
284 off_t OnSysSeek(off_t seek, wxSeekMode mode);
285 off_t OnSysTell() const;
286
287 protected:
288 wxStreamBuffer *m_i_streambuf;
289 };
290
291 class wxBufferedOutputStream: public wxFilterOutputStream {
292 public:
293 wxBufferedOutputStream(wxOutputStream& stream);
294 ~wxBufferedOutputStream();
295
296 wxOutputStream& Write(const void *buffer, size_t size);
297
298 // Position functions
299 off_t SeekO(off_t pos, wxSeekMode mode = wxFromStart);
300 off_t TellO() const;
301
302 void Sync();
303
304 wxStreamBuffer *OutputStreamBuffer() const { return m_o_streambuf; }
305
306 protected:
307 size_t OnSysWrite(const void *buffer, size_t bufsize);
308 off_t OnSysSeek(off_t seek, wxSeekMode mode);
309 off_t OnSysTell() const;
310
311 protected:
312 wxStreamBuffer *m_o_streambuf;
313 };
314
315 #endif
316 // wxUSE_STREAMS
317
318 #endif
319 // _WX_WXSTREAM_H__