]>
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 | ||
19 | #include <stdio.h> | |
45ea509a VZ |
20 | #include "wx/object.h" |
21 | #include "wx/string.h" | |
1678ad78 GL |
22 | #include "wx/filefn.h" // for off_t, wxInvalidOffset and wxSeekMode |
23 | ||
24 | class WXDLLEXPORT wxInputStream; | |
25 | class WXDLLEXPORT wxOutputStream; | |
26 | ||
27 | typedef wxInputStream& (*__wxInputManip)(wxInputStream&); | |
28 | typedef wxOutputStream& (*__wxOutputManip)(wxOutputStream&); | |
29 | ||
30 | wxOutputStream& WXDLLEXPORT wxEndL(wxOutputStream& o_stream); | |
31 | ||
f4ada568 GL |
32 | // --------------------------------------------------------------------------- |
33 | // Stream buffer | |
34 | // --------------------------------------------------------------------------- | |
35 | ||
1678ad78 GL |
36 | class WXDLLEXPORT wxStreamBuffer { |
37 | public: | |
38 | wxStreamBuffer(wxInputStream& stream); | |
39 | wxStreamBuffer(wxOutputStream& stream); | |
40 | ~wxStreamBuffer(); | |
41 | ||
42 | void Read(void *buffer, size_t size); | |
43 | void Write(const void *buffer, size_t size); | |
44 | void WriteBack(char c); | |
45 | ||
46 | void SetBufferIO(char *buffer_start, char *buffer_end); | |
47 | void SetBufferIO(size_t bufsize); | |
48 | void ResetBuffer(); | |
49 | ||
50 | void SetBufferPosition(char *buffer_position) | |
51 | { m_buffer_pos = buffer_position; } | |
52 | void SetIntPosition(size_t pos) | |
53 | { m_buffer_pos = m_buffer_start + pos; } | |
54 | char *GetBufferPosition() const { return m_buffer_pos; } | |
55 | size_t GetIntPosition() const { return m_buffer_pos - m_buffer_start; } | |
56 | ||
57 | char *GetBufferStart() const { return m_buffer_start; } | |
58 | char *GetBufferEnd() const { return m_buffer_end; } | |
59 | size_t GetBufferSize() const { return m_buffer_size; } | |
60 | size_t GetLastAccess() const { return m_buffer_end - m_buffer_start; } | |
61 | ||
62 | protected: | |
63 | char *m_buffer_start, *m_buffer_end, *m_buffer_pos; | |
64 | size_t m_buffer_size; | |
65 | ||
66 | wxInputStream *m_istream; | |
67 | wxOutputStream *m_ostream; | |
68 | }; | |
eda3efe2 | 69 | |
f4ada568 GL |
70 | // --------------------------------------------------------------------------- |
71 | // wxStream: base classes | |
72 | // --------------------------------------------------------------------------- | |
73 | ||
1678ad78 | 74 | class WXDLLEXPORT wxInputStream { |
32fc4afb GL |
75 | public: |
76 | wxInputStream(); | |
77 | virtual ~wxInputStream(); | |
78 | ||
1678ad78 | 79 | // IO functions |
f4ada568 | 80 | virtual char Peek() { return 0; } |
1678ad78 GL |
81 | virtual char GetC(); |
82 | virtual wxInputStream& Read(void *buffer, size_t size); | |
32fc4afb GL |
83 | wxInputStream& Read(wxOutputStream& stream_out); |
84 | ||
1678ad78 | 85 | // Position functions |
7a4b9130 GL |
86 | virtual off_t SeekI(off_t pos, wxSeekMode mode = wxFromStart); |
87 | virtual off_t TellI() const; | |
32fc4afb | 88 | |
1678ad78 GL |
89 | // State functions |
90 | bool Eof() const { return m_eof; } | |
91 | size_t LastRead() { return m_lastread; } | |
92 | wxStreamBuffer *InputStreamBuffer() { return m_i_streambuf; } | |
0cd9bfe8 | 93 | |
1678ad78 | 94 | // Operators |
0cd9bfe8 | 95 | wxInputStream& operator>>(wxOutputStream& out) { return Read(out); } |
1678ad78 GL |
96 | wxInputStream& operator>>(wxString& line); |
97 | wxInputStream& operator>>(char& c); | |
98 | wxInputStream& operator>>(short& i); | |
123a7fdd | 99 | wxInputStream& operator>>(int& i); |
1678ad78 GL |
100 | wxInputStream& operator>>(long& i); |
101 | wxInputStream& operator>>(float& i); | |
fcc6dddd | 102 | #if USE_SERIAL |
123a7fdd | 103 | wxInputStream& operator>>(wxObject *& obj); |
fcc6dddd | 104 | #endif |
123a7fdd GL |
105 | |
106 | wxInputStream& operator>>(unsigned char& c) { return operator>>((char&)c); } | |
107 | wxInputStream& operator>>(unsigned short& i) { return operator>>((short&)i); } | |
108 | wxInputStream& operator>>(unsigned int& i) { return operator>>((int&)i); } | |
109 | wxInputStream& operator>>(unsigned long& i) { return operator>>((long&)i); } | |
1678ad78 GL |
110 | wxInputStream& operator>>( __wxInputManip func) { return func(*this); } |
111 | ||
112 | protected: | |
113 | friend class wxStreamBuffer; | |
114 | friend class wxFilterInputStream; | |
115 | ||
116 | wxInputStream(wxStreamBuffer *buffer); | |
117 | ||
e22036dc RR |
118 | virtual size_t DoRead(void *WXUNUSED(buffer), size_t WXUNUSED(size) ) |
119 | { return 0; } | |
120 | virtual off_t DoSeekInput( off_t WXUNUSED(pos), wxSeekMode WXUNUSED(mode) ) | |
f4ada568 GL |
121 | { return wxInvalidOffset; } |
122 | virtual off_t DoTellInput() const | |
123 | { return wxInvalidOffset; } | |
1678ad78 GL |
124 | |
125 | protected: | |
126 | bool m_eof, m_i_destroybuf; | |
127 | size_t m_lastread; | |
128 | wxStreamBuffer *m_i_streambuf; | |
32fc4afb GL |
129 | }; |
130 | ||
1678ad78 | 131 | class WXDLLEXPORT wxOutputStream { |
32fc4afb GL |
132 | public: |
133 | wxOutputStream(); | |
134 | virtual ~wxOutputStream(); | |
135 | ||
1678ad78 | 136 | virtual wxOutputStream& Write(const void *buffer, size_t size); |
32fc4afb GL |
137 | wxOutputStream& Write(wxInputStream& stream_in); |
138 | ||
1678ad78 GL |
139 | virtual off_t SeekO(off_t pos, wxSeekMode mode = wxFromStart); |
140 | virtual off_t TellO() const; | |
141 | ||
142 | virtual bool Bad() const { return m_bad; } | |
143 | virtual size_t LastWrite() const { return m_lastwrite; } | |
144 | wxStreamBuffer *OutputStreamBuffer() { return m_o_streambuf; } | |
32fc4afb | 145 | |
1678ad78 | 146 | virtual void Sync(); |
32fc4afb | 147 | |
1678ad78 GL |
148 | wxOutputStream& operator<<(wxInputStream& out) { return Write(out); } |
149 | wxOutputStream& operator<<(const char *string); | |
150 | wxOutputStream& operator<<(wxString& string); | |
151 | wxOutputStream& operator<<(char c); | |
152 | wxOutputStream& operator<<(short i); | |
153 | wxOutputStream& operator<<(int i); | |
154 | wxOutputStream& operator<<(long i); | |
155 | wxOutputStream& operator<<(double f); | |
fcc6dddd | 156 | #if USE_SERIAL |
123a7fdd | 157 | wxOutputStream& operator<<(wxObject& obj); |
fcc6dddd | 158 | #endif |
1678ad78 GL |
159 | |
160 | wxOutputStream& operator<<(float f) { return operator<<((double)f); } | |
161 | wxOutputStream& operator<<(unsigned char c) { return operator<<((char)c); } | |
162 | wxOutputStream& operator<<(unsigned short i) { return operator<<((short)i); } | |
163 | wxOutputStream& operator<<(unsigned int i) { return operator<<((int)i); } | |
164 | wxOutputStream& operator<<(unsigned long i) { return operator<<((long)i); } | |
1678ad78 GL |
165 | wxOutputStream& operator<<( __wxOutputManip func) { return func(*this); } |
166 | ||
167 | protected: | |
168 | friend class wxStreamBuffer; | |
169 | friend class wxFilterOutputStream; | |
170 | ||
171 | wxOutputStream(wxStreamBuffer *buffer); | |
172 | ||
e22036dc RR |
173 | virtual size_t DoWrite( const void *WXUNUSED(buffer), size_t WXUNUSED(size) ) |
174 | { return 0; } | |
175 | virtual off_t DoSeekOutput( off_t WXUNUSED(pos), wxSeekMode WXUNUSED(mode) ) | |
f4ada568 GL |
176 | { return wxInvalidOffset; } |
177 | virtual off_t DoTellOutput() const | |
178 | { return wxInvalidOffset; } | |
1678ad78 GL |
179 | |
180 | protected: | |
181 | bool m_bad, m_o_destroybuf; | |
182 | size_t m_lastwrite; | |
183 | wxStreamBuffer *m_o_streambuf; | |
32fc4afb GL |
184 | }; |
185 | ||
c740f496 GL |
186 | class wxStream: public virtual wxInputStream, |
187 | public virtual wxOutputStream | |
f4ada568 GL |
188 | { |
189 | public: | |
190 | wxStream(); | |
191 | }; | |
192 | ||
193 | // --------------------------------------------------------------------------- | |
194 | // "Filter" streams | |
195 | // --------------------------------------------------------------------------- | |
32fc4afb | 196 | |
c740f496 | 197 | class WXDLLEXPORT wxFilterInputStream: public virtual wxInputStream { |
32fc4afb | 198 | public: |
f4ada568 | 199 | wxFilterInputStream(); |
32fc4afb | 200 | wxFilterInputStream(wxInputStream& stream); |
f4ada568 | 201 | ~wxFilterInputStream(); |
32fc4afb | 202 | |
f4ada568 | 203 | char Peek() { return m_parent_i_stream->Peek(); } |
32fc4afb | 204 | |
f4ada568 GL |
205 | bool Eof() const { return m_parent_i_stream->Eof(); } |
206 | size_t LastRead() const { return m_parent_i_stream->LastRead(); } | |
207 | off_t TellI() const { return m_parent_i_stream->TellI(); } | |
79c3e0e1 | 208 | |
1678ad78 | 209 | protected: |
f4ada568 GL |
210 | size_t DoRead(void *buffer, size_t size); |
211 | off_t DoSeekInput(off_t pos, wxSeekMode mode); | |
212 | off_t DoTellInput() const; | |
1678ad78 | 213 | |
32fc4afb GL |
214 | protected: |
215 | wxInputStream *m_parent_i_stream; | |
216 | }; | |
217 | ||
c740f496 | 218 | class WXDLLEXPORT wxFilterOutputStream: public virtual wxOutputStream { |
32fc4afb | 219 | public: |
f4ada568 | 220 | wxFilterOutputStream(); |
32fc4afb GL |
221 | wxFilterOutputStream(wxOutputStream& stream); |
222 | virtual ~wxFilterOutputStream(); | |
223 | ||
f4ada568 GL |
224 | bool Bad() const { return m_parent_o_stream->Bad(); } |
225 | size_t LastWrite() const { return m_parent_o_stream->LastWrite(); } | |
226 | off_t TellO() const { return m_parent_o_stream->TellO(); } | |
32fc4afb | 227 | |
1678ad78 | 228 | protected: |
1678ad78 GL |
229 | // The forward is implicitely done by wxStreamBuffer. |
230 | ||
f4ada568 GL |
231 | size_t DoWrite(const void *buffer, size_t size); |
232 | off_t DoSeekOutput(off_t pos, wxSeekMode mode); | |
233 | off_t DoTellOutput() const; | |
1678ad78 | 234 | |
32fc4afb GL |
235 | protected: |
236 | wxOutputStream *m_parent_o_stream; | |
237 | }; | |
238 | ||
f4ada568 | 239 | class WXDLLEXPORT wxFilterStream: public wxStream, |
c740f496 GL |
240 | public virtual wxFilterInputStream, |
241 | public virtual wxFilterOutputStream { | |
f4ada568 GL |
242 | public: |
243 | wxFilterStream(wxStream& stream); | |
244 | wxFilterStream(); | |
245 | }; | |
246 | ||
32fc4afb | 247 | #endif |