]>
Commit | Line | Data |
---|---|---|
32fc4afb GL |
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 | ||
34138703 JS |
12 | #ifndef _WX_WXSTREAM_H__ |
13 | #define _WX_WXSTREAM_H__ | |
32fc4afb GL |
14 | |
15 | #ifdef __GNUG__ | |
79c3e0e1 | 16 | #pragma interface |
32fc4afb GL |
17 | #endif |
18 | ||
ce4169a4 RR |
19 | #include "wx/defs.h" |
20 | ||
21 | #if wxUSE_STREAMS | |
22 | ||
32fc4afb | 23 | #include <stdio.h> |
45ea509a VZ |
24 | #include "wx/object.h" |
25 | #include "wx/string.h" | |
1678ad78 GL |
26 | #include "wx/filefn.h" // for off_t, wxInvalidOffset and wxSeekMode |
27 | ||
75ed1d15 | 28 | class WXDLLEXPORT wxStreamBase; |
1678ad78 GL |
29 | class WXDLLEXPORT wxInputStream; |
30 | class WXDLLEXPORT wxOutputStream; | |
31 | ||
32 | typedef wxInputStream& (*__wxInputManip)(wxInputStream&); | |
33 | typedef wxOutputStream& (*__wxOutputManip)(wxOutputStream&); | |
34 | ||
184b5d99 | 35 | WXDLLEXPORT wxOutputStream& wxEndL(wxOutputStream& o_stream); |
1678ad78 | 36 | |
f4ada568 GL |
37 | // --------------------------------------------------------------------------- |
38 | // Stream buffer | |
39 | // --------------------------------------------------------------------------- | |
40 | ||
1678ad78 GL |
41 | class WXDLLEXPORT wxStreamBuffer { |
42 | public: | |
75ed1d15 | 43 | typedef enum { |
8ef6a930 | 44 | read = 0, write, read_write |
75ed1d15 GL |
45 | } BufMode; |
46 | ||
47 | // ----------- | |
48 | // ctor & dtor | |
49 | // ----------- | |
50 | wxStreamBuffer(wxStreamBase& stream, BufMode mode); | |
84b46c35 GL |
51 | wxStreamBuffer(BufMode mode); |
52 | wxStreamBuffer(const wxStreamBuffer& buf); | |
1678ad78 GL |
53 | ~wxStreamBuffer(); |
54 | ||
75ed1d15 GL |
55 | // ----------- |
56 | // Filtered IO | |
57 | // ----------- | |
8ef6a930 GL |
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); | |
75ed1d15 | 64 | bool WriteBack(char c); |
84b46c35 GL |
65 | char GetChar(); |
66 | void PutChar(char c); | |
75ed1d15 GL |
67 | off_t Tell() const; |
68 | off_t Seek(off_t pos, wxSeekMode mode); | |
69 | ||
70 | // -------------- | |
71 | // Buffer control | |
72 | // -------------- | |
73 | void ResetBuffer(); | |
1678ad78 GL |
74 | void SetBufferIO(char *buffer_start, char *buffer_end); |
75 | void SetBufferIO(size_t bufsize); | |
75ed1d15 GL |
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; } | |
84b46c35 | 82 | |
75ed1d15 | 83 | void Fixed(bool fixed) { m_fixed = fixed; } |
84b46c35 | 84 | void Flushable(bool f) { m_flushable = f; } |
75ed1d15 GL |
85 | |
86 | bool FlushBuffer(); | |
87 | bool FillBuffer(); | |
8ef6a930 GL |
88 | size_t GetDataLeft(); |
89 | ||
90 | // -------------- | |
91 | // Administration | |
92 | // -------------- | |
93 | wxStreamBase *Stream() { return m_stream; } | |
75ed1d15 GL |
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); | |
1678ad78 GL |
101 | |
102 | protected: | |
103 | char *m_buffer_start, *m_buffer_end, *m_buffer_pos; | |
104 | size_t m_buffer_size; | |
105 | ||
75ed1d15 GL |
106 | char *m_wback; |
107 | size_t m_wbacksize, m_wbackcur; | |
108 | ||
84b46c35 | 109 | bool m_fixed, m_flushable; |
75ed1d15 GL |
110 | |
111 | wxStreamBase *m_stream; | |
112 | BufMode m_mode; | |
8ef6a930 | 113 | bool m_destroybuf, m_destroystream; |
1678ad78 | 114 | }; |
eda3efe2 | 115 | |
f4ada568 GL |
116 | // --------------------------------------------------------------------------- |
117 | // wxStream: base classes | |
118 | // --------------------------------------------------------------------------- | |
119 | ||
75ed1d15 | 120 | typedef enum { |
926c550d GL |
121 | wxStream_NOERROR = 0, |
122 | wxStream_EOF, | |
123 | wxStream_WRITE_ERR, | |
124 | wxStream_READ_ERR | |
75ed1d15 GL |
125 | } wxStreamError; |
126 | ||
127 | class WXDLLEXPORT wxStreamBase { | |
128 | public: | |
129 | wxStreamBase(); | |
130 | virtual ~wxStreamBase(); | |
131 | ||
84b46c35 GL |
132 | wxStreamError LastError() const { return m_lasterror; } |
133 | virtual size_t StreamSize() const { return ~((size_t)0); } | |
75ed1d15 GL |
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); | |
84b46c35 | 141 | virtual off_t OnSysTell() const; |
75ed1d15 GL |
142 | |
143 | protected: | |
144 | size_t m_lastcount; | |
145 | wxStreamError m_lasterror; | |
146 | }; | |
147 | ||
148 | class WXDLLEXPORT wxInputStream: public wxStreamBase { | |
32fc4afb GL |
149 | public: |
150 | wxInputStream(); | |
75ed1d15 | 151 | wxInputStream(wxStreamBuffer *sbuf); |
32fc4afb GL |
152 | virtual ~wxInputStream(); |
153 | ||
1678ad78 | 154 | // IO functions |
75ed1d15 GL |
155 | virtual char Peek(); |
156 | char GetC(); | |
41895a05 | 157 | virtual wxInputStream& Read(void *buffer, size_t size); |
32fc4afb GL |
158 | wxInputStream& Read(wxOutputStream& stream_out); |
159 | ||
1678ad78 | 160 | // Position functions |
75ed1d15 GL |
161 | off_t SeekI(off_t pos, wxSeekMode mode = wxFromStart); |
162 | off_t TellI() const; | |
32fc4afb | 163 | |
1678ad78 | 164 | // State functions |
1678ad78 | 165 | wxStreamBuffer *InputStreamBuffer() { return m_i_streambuf; } |
75ed1d15 | 166 | size_t LastRead() { return wxStreamBase::m_lastcount; } |
0cd9bfe8 | 167 | |
1678ad78 | 168 | // Operators |
0cd9bfe8 | 169 | wxInputStream& operator>>(wxOutputStream& out) { return Read(out); } |
1678ad78 GL |
170 | wxInputStream& operator>>(wxString& line); |
171 | wxInputStream& operator>>(char& c); | |
172 | wxInputStream& operator>>(short& i); | |
123a7fdd | 173 | wxInputStream& operator>>(int& i); |
1678ad78 | 174 | wxInputStream& operator>>(long& i); |
75ed1d15 | 175 | wxInputStream& operator>>(double& i); |
47d67540 | 176 | #if wxUSE_SERIAL |
123a7fdd | 177 | wxInputStream& operator>>(wxObject *& obj); |
fcc6dddd | 178 | #endif |
123a7fdd | 179 | |
75ed1d15 | 180 | wxInputStream& operator>>(float& f) { double d; operator>>((double&)d); f = (float)d; return *this; } |
123a7fdd GL |
181 | wxInputStream& operator>>(unsigned char& c) { return operator>>((char&)c); } |
182 | wxInputStream& operator>>(unsigned short& i) { return operator>>((short&)i); } | |
183 | wxInputStream& operator>>(unsigned int& i) { return operator>>((int&)i); } | |
184 | wxInputStream& operator>>(unsigned long& i) { return operator>>((long&)i); } | |
1678ad78 GL |
185 | wxInputStream& operator>>( __wxInputManip func) { return func(*this); } |
186 | ||
187 | protected: | |
75ed1d15 | 188 | bool m_i_destroybuf; |
1678ad78 | 189 | wxStreamBuffer *m_i_streambuf; |
32fc4afb GL |
190 | }; |
191 | ||
75ed1d15 | 192 | class WXDLLEXPORT wxOutputStream: public wxStreamBase { |
32fc4afb GL |
193 | public: |
194 | wxOutputStream(); | |
75ed1d15 | 195 | wxOutputStream(wxStreamBuffer *sbuf); |
32fc4afb GL |
196 | virtual ~wxOutputStream(); |
197 | ||
41895a05 | 198 | virtual wxOutputStream& Write(const void *buffer, size_t size); |
32fc4afb GL |
199 | wxOutputStream& Write(wxInputStream& stream_in); |
200 | ||
75ed1d15 GL |
201 | off_t SeekO(off_t pos, wxSeekMode mode = wxFromStart); |
202 | off_t TellO() const; | |
1678ad78 | 203 | |
75ed1d15 | 204 | size_t LastWrite() const { return wxStreamBase::m_lastcount; } |
1678ad78 | 205 | wxStreamBuffer *OutputStreamBuffer() { return m_o_streambuf; } |
32fc4afb | 206 | |
75ed1d15 | 207 | void Sync(); |
32fc4afb | 208 | |
1678ad78 GL |
209 | wxOutputStream& operator<<(wxInputStream& out) { return Write(out); } |
210 | wxOutputStream& operator<<(const char *string); | |
211 | wxOutputStream& operator<<(wxString& string); | |
212 | wxOutputStream& operator<<(char c); | |
213 | wxOutputStream& operator<<(short i); | |
214 | wxOutputStream& operator<<(int i); | |
215 | wxOutputStream& operator<<(long i); | |
216 | wxOutputStream& operator<<(double f); | |
47d67540 | 217 | #if wxUSE_SERIAL |
123a7fdd | 218 | wxOutputStream& operator<<(wxObject& obj); |
fcc6dddd | 219 | #endif |
1678ad78 GL |
220 | |
221 | wxOutputStream& operator<<(float f) { return operator<<((double)f); } | |
222 | wxOutputStream& operator<<(unsigned char c) { return operator<<((char)c); } | |
223 | wxOutputStream& operator<<(unsigned short i) { return operator<<((short)i); } | |
224 | wxOutputStream& operator<<(unsigned int i) { return operator<<((int)i); } | |
225 | wxOutputStream& operator<<(unsigned long i) { return operator<<((long)i); } | |
1678ad78 GL |
226 | wxOutputStream& operator<<( __wxOutputManip func) { return func(*this); } |
227 | ||
228 | protected: | |
75ed1d15 | 229 | bool m_o_destroybuf; |
1678ad78 | 230 | wxStreamBuffer *m_o_streambuf; |
32fc4afb GL |
231 | }; |
232 | ||
f4ada568 GL |
233 | // --------------------------------------------------------------------------- |
234 | // "Filter" streams | |
235 | // --------------------------------------------------------------------------- | |
32fc4afb | 236 | |
75ed1d15 | 237 | class WXDLLEXPORT wxFilterInputStream: public wxInputStream { |
32fc4afb | 238 | public: |
f4ada568 | 239 | wxFilterInputStream(); |
32fc4afb | 240 | wxFilterInputStream(wxInputStream& stream); |
f4ada568 | 241 | ~wxFilterInputStream(); |
32fc4afb | 242 | |
f4ada568 | 243 | char Peek() { return m_parent_i_stream->Peek(); } |
32fc4afb | 244 | |
84b46c35 GL |
245 | wxStreamError LastError() const { return m_parent_i_stream->LastError(); } |
246 | size_t StreamSize() const { return m_parent_i_stream->StreamSize(); } | |
247 | ||
32fc4afb GL |
248 | protected: |
249 | wxInputStream *m_parent_i_stream; | |
250 | }; | |
251 | ||
75ed1d15 | 252 | class WXDLLEXPORT wxFilterOutputStream: public wxOutputStream { |
32fc4afb | 253 | public: |
f4ada568 | 254 | wxFilterOutputStream(); |
32fc4afb | 255 | wxFilterOutputStream(wxOutputStream& stream); |
75ed1d15 | 256 | ~wxFilterOutputStream(); |
1678ad78 | 257 | |
84b46c35 GL |
258 | wxStreamError LastError() const { return m_parent_o_stream->LastError(); } |
259 | size_t StreamSize() const { return m_parent_o_stream->StreamSize(); } | |
260 | ||
32fc4afb GL |
261 | protected: |
262 | wxOutputStream *m_parent_o_stream; | |
263 | }; | |
264 | ||
265 | #endif | |
ce4169a4 RR |
266 | // wxUSE_STREAMS |
267 | ||
268 | #endif | |
269 | // _WX_WXSTREAM_H__ |