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