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