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