]> git.saurik.com Git - wxWidgets.git/blob - include/wx/stream.h
Rewrote wxRadioBox (recompile)
[wxWidgets.git] / include / wx / stream.h
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 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 // ---------------------------------------------------------------------------
33 // Stream buffer
34 // ---------------------------------------------------------------------------
35
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 };
69
70 // ---------------------------------------------------------------------------
71 // wxStream: base classes
72 // ---------------------------------------------------------------------------
73
74 class WXDLLEXPORT wxInputStream {
75 public:
76 wxInputStream();
77 virtual ~wxInputStream();
78
79 // IO functions
80 virtual char Peek() { return 0; }
81 virtual char GetC();
82 virtual wxInputStream& Read(void *buffer, size_t size);
83 wxInputStream& Read(wxOutputStream& stream_out);
84
85 // Position functions
86 virtual off_t SeekI(off_t pos, wxSeekMode mode = wxFromStart);
87 virtual off_t TellI() const;
88
89 // State functions
90 bool Eof() const { return m_eof; }
91 size_t LastRead() { return m_lastread; }
92 wxStreamBuffer *InputStreamBuffer() { return m_i_streambuf; }
93
94 // Operators
95 wxInputStream& operator>>(wxOutputStream& out) { return Read(out); }
96 wxInputStream& operator>>(wxString& line);
97 wxInputStream& operator>>(char& c);
98 wxInputStream& operator>>(short& i);
99 wxInputStream& operator>>(int& i);
100 wxInputStream& operator>>(long& i);
101 wxInputStream& operator>>(float& i);
102 #if USE_SERIAL
103 wxInputStream& operator>>(wxObject *& obj);
104 #endif
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); }
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
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) )
121 { return wxInvalidOffset; }
122 virtual off_t DoTellInput() const
123 { return wxInvalidOffset; }
124
125 protected:
126 bool m_eof, m_i_destroybuf;
127 size_t m_lastread;
128 wxStreamBuffer *m_i_streambuf;
129 };
130
131 class WXDLLEXPORT wxOutputStream {
132 public:
133 wxOutputStream();
134 virtual ~wxOutputStream();
135
136 virtual wxOutputStream& Write(const void *buffer, size_t size);
137 wxOutputStream& Write(wxInputStream& stream_in);
138
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; }
145
146 virtual void Sync();
147
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);
156 #if USE_SERIAL
157 wxOutputStream& operator<<(wxObject& obj);
158 #endif
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); }
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
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) )
176 { return wxInvalidOffset; }
177 virtual off_t DoTellOutput() const
178 { return wxInvalidOffset; }
179
180 protected:
181 bool m_bad, m_o_destroybuf;
182 size_t m_lastwrite;
183 wxStreamBuffer *m_o_streambuf;
184 };
185
186 class wxStream: virtual public wxInputStream,
187 virtual public wxOutputStream
188 {
189 public:
190 wxStream();
191 };
192
193 // ---------------------------------------------------------------------------
194 // "Filter" streams
195 // ---------------------------------------------------------------------------
196
197 class WXDLLEXPORT wxFilterInputStream: virtual public wxInputStream {
198 public:
199 wxFilterInputStream();
200 wxFilterInputStream(wxInputStream& stream);
201 ~wxFilterInputStream();
202
203 char Peek() { return m_parent_i_stream->Peek(); }
204
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(); }
208
209 protected:
210 size_t DoRead(void *buffer, size_t size);
211 off_t DoSeekInput(off_t pos, wxSeekMode mode);
212 off_t DoTellInput() const;
213
214 protected:
215 wxInputStream *m_parent_i_stream;
216 };
217
218 class WXDLLEXPORT wxFilterOutputStream: virtual public wxOutputStream {
219 public:
220 wxFilterOutputStream();
221 wxFilterOutputStream(wxOutputStream& stream);
222 virtual ~wxFilterOutputStream();
223
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(); }
227
228 protected:
229 // The forward is implicitely done by wxStreamBuffer.
230
231 size_t DoWrite(const void *buffer, size_t size);
232 off_t DoSeekOutput(off_t pos, wxSeekMode mode);
233 off_t DoTellOutput() const;
234
235 protected:
236 wxOutputStream *m_parent_o_stream;
237 };
238
239 class WXDLLEXPORT wxFilterStream: public wxStream,
240 virtual public wxFilterInputStream,
241 virtual public wxFilterOutputStream {
242 public:
243 wxFilterStream(wxStream& stream);
244 wxFilterStream();
245 };
246
247 #endif