]>
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 | ||
f4ada568 GL |
118 | virtual size_t DoRead(void *buffer, size_t size) { return 0; } |
119 | virtual off_t DoSeekInput(off_t pos, wxSeekMode mode) | |
120 | { return wxInvalidOffset; } | |
121 | virtual off_t DoTellInput() const | |
122 | { return wxInvalidOffset; } | |
1678ad78 GL |
123 | |
124 | protected: | |
125 | bool m_eof, m_i_destroybuf; | |
126 | size_t m_lastread; | |
127 | wxStreamBuffer *m_i_streambuf; | |
32fc4afb GL |
128 | }; |
129 | ||
1678ad78 | 130 | class WXDLLEXPORT wxOutputStream { |
32fc4afb GL |
131 | public: |
132 | wxOutputStream(); | |
133 | virtual ~wxOutputStream(); | |
134 | ||
1678ad78 | 135 | virtual wxOutputStream& Write(const void *buffer, size_t size); |
32fc4afb GL |
136 | wxOutputStream& Write(wxInputStream& stream_in); |
137 | ||
1678ad78 GL |
138 | virtual off_t SeekO(off_t pos, wxSeekMode mode = wxFromStart); |
139 | virtual off_t TellO() const; | |
140 | ||
141 | virtual bool Bad() const { return m_bad; } | |
142 | virtual size_t LastWrite() const { return m_lastwrite; } | |
143 | wxStreamBuffer *OutputStreamBuffer() { return m_o_streambuf; } | |
32fc4afb | 144 | |
1678ad78 | 145 | virtual void Sync(); |
32fc4afb | 146 | |
1678ad78 GL |
147 | wxOutputStream& operator<<(wxInputStream& out) { return Write(out); } |
148 | wxOutputStream& operator<<(const char *string); | |
149 | wxOutputStream& operator<<(wxString& string); | |
150 | wxOutputStream& operator<<(char c); | |
151 | wxOutputStream& operator<<(short i); | |
152 | wxOutputStream& operator<<(int i); | |
153 | wxOutputStream& operator<<(long i); | |
154 | wxOutputStream& operator<<(double f); | |
fcc6dddd | 155 | #if USE_SERIAL |
123a7fdd | 156 | wxOutputStream& operator<<(wxObject& obj); |
fcc6dddd | 157 | #endif |
1678ad78 GL |
158 | |
159 | wxOutputStream& operator<<(float f) { return operator<<((double)f); } | |
160 | wxOutputStream& operator<<(unsigned char c) { return operator<<((char)c); } | |
161 | wxOutputStream& operator<<(unsigned short i) { return operator<<((short)i); } | |
162 | wxOutputStream& operator<<(unsigned int i) { return operator<<((int)i); } | |
163 | wxOutputStream& operator<<(unsigned long i) { return operator<<((long)i); } | |
1678ad78 GL |
164 | wxOutputStream& operator<<( __wxOutputManip func) { return func(*this); } |
165 | ||
166 | protected: | |
167 | friend class wxStreamBuffer; | |
168 | friend class wxFilterOutputStream; | |
169 | ||
170 | wxOutputStream(wxStreamBuffer *buffer); | |
171 | ||
f4ada568 GL |
172 | virtual size_t DoWrite(const void *buffer, size_t size) { return 0; } |
173 | virtual off_t DoSeekOutput(off_t pos, wxSeekMode mode) | |
174 | { return wxInvalidOffset; } | |
175 | virtual off_t DoTellOutput() const | |
176 | { return wxInvalidOffset; } | |
1678ad78 GL |
177 | |
178 | protected: | |
179 | bool m_bad, m_o_destroybuf; | |
180 | size_t m_lastwrite; | |
181 | wxStreamBuffer *m_o_streambuf; | |
32fc4afb GL |
182 | }; |
183 | ||
f4ada568 GL |
184 | class wxStream: virtual public wxInputStream, |
185 | virtual public wxOutputStream | |
186 | { | |
187 | public: | |
188 | wxStream(); | |
189 | }; | |
190 | ||
191 | // --------------------------------------------------------------------------- | |
192 | // "Filter" streams | |
193 | // --------------------------------------------------------------------------- | |
32fc4afb | 194 | |
f4ada568 | 195 | class WXDLLEXPORT wxFilterInputStream: virtual public wxInputStream { |
32fc4afb | 196 | public: |
f4ada568 | 197 | wxFilterInputStream(); |
32fc4afb | 198 | wxFilterInputStream(wxInputStream& stream); |
f4ada568 | 199 | ~wxFilterInputStream(); |
32fc4afb | 200 | |
f4ada568 | 201 | char Peek() { return m_parent_i_stream->Peek(); } |
32fc4afb | 202 | |
f4ada568 GL |
203 | bool Eof() const { return m_parent_i_stream->Eof(); } |
204 | size_t LastRead() const { return m_parent_i_stream->LastRead(); } | |
205 | off_t TellI() const { return m_parent_i_stream->TellI(); } | |
79c3e0e1 | 206 | |
1678ad78 | 207 | protected: |
f4ada568 GL |
208 | size_t DoRead(void *buffer, size_t size); |
209 | off_t DoSeekInput(off_t pos, wxSeekMode mode); | |
210 | off_t DoTellInput() const; | |
1678ad78 | 211 | |
32fc4afb GL |
212 | protected: |
213 | wxInputStream *m_parent_i_stream; | |
214 | }; | |
215 | ||
f4ada568 | 216 | class WXDLLEXPORT wxFilterOutputStream: virtual public wxOutputStream { |
32fc4afb | 217 | public: |
f4ada568 | 218 | wxFilterOutputStream(); |
32fc4afb GL |
219 | wxFilterOutputStream(wxOutputStream& stream); |
220 | virtual ~wxFilterOutputStream(); | |
221 | ||
f4ada568 GL |
222 | bool Bad() const { return m_parent_o_stream->Bad(); } |
223 | size_t LastWrite() const { return m_parent_o_stream->LastWrite(); } | |
224 | off_t TellO() const { return m_parent_o_stream->TellO(); } | |
32fc4afb | 225 | |
1678ad78 | 226 | protected: |
1678ad78 GL |
227 | // The forward is implicitely done by wxStreamBuffer. |
228 | ||
f4ada568 GL |
229 | size_t DoWrite(const void *buffer, size_t size); |
230 | off_t DoSeekOutput(off_t pos, wxSeekMode mode); | |
231 | off_t DoTellOutput() const; | |
1678ad78 | 232 | |
32fc4afb GL |
233 | protected: |
234 | wxOutputStream *m_parent_o_stream; | |
235 | }; | |
236 | ||
f4ada568 GL |
237 | class WXDLLEXPORT wxFilterStream: public wxStream, |
238 | virtual public wxFilterInputStream, | |
239 | virtual public wxFilterOutputStream { | |
240 | public: | |
241 | wxFilterStream(wxStream& stream); | |
242 | wxFilterStream(); | |
243 | }; | |
244 | ||
32fc4afb | 245 | #endif |