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