]> git.saurik.com Git - wxWidgets.git/blob - include/wx/stream.h
added wxLogWindow::GetFrame()
[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 __WXSTREAM_H__
13 #define __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 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 };
65
66 /*
67 * wxStream: base classes
68 */
69 class WXDLLEXPORT wxInputStream {
70 public:
71 wxInputStream();
72 virtual ~wxInputStream();
73
74 // IO functions
75 virtual char Peek() = 0;
76 virtual char GetC();
77 virtual wxInputStream& Read(void *buffer, size_t size);
78 wxInputStream& Read(wxOutputStream& stream_out);
79
80 // Position functions
81 off_t SeekI(off_t pos, wxSeekMode mode = wxFromStart);
82 off_t TellI() const;
83
84 // State functions
85 bool Eof() const { return m_eof; }
86 size_t LastRead() { return m_lastread; }
87 wxStreamBuffer *InputStreamBuffer() { return m_i_streambuf; }
88
89 // Operators
90 wxInputStream& operator>>(wxOutputStream& out) { return Read(out); }
91 wxInputStream& operator>>(wxString& line);
92 wxInputStream& operator>>(char& c);
93 wxInputStream& operator>>(short& i);
94 wxInputStream& operator>>(long& i);
95 wxInputStream& operator>>(float& i);
96 wxInputStream& operator>>( __wxInputManip func) { return func(*this); }
97
98 protected:
99 friend class wxStreamBuffer;
100 friend class wxFilterInputStream;
101
102 wxInputStream(wxStreamBuffer *buffer);
103
104 virtual size_t DoRead(void *buffer, size_t size) = 0;
105 virtual off_t DoSeekInput(off_t pos, wxSeekMode mode) = 0;
106 virtual off_t DoTellInput() const = 0;
107
108 protected:
109 bool m_eof, m_i_destroybuf;
110 size_t m_lastread;
111 wxStreamBuffer *m_i_streambuf;
112 };
113
114 class WXDLLEXPORT wxOutputStream {
115 public:
116 wxOutputStream();
117 virtual ~wxOutputStream();
118
119 virtual wxOutputStream& Write(const void *buffer, size_t size);
120 wxOutputStream& Write(wxInputStream& stream_in);
121
122 virtual off_t SeekO(off_t pos, wxSeekMode mode = wxFromStart);
123 virtual off_t TellO() const;
124
125 virtual bool Bad() const { return m_bad; }
126 virtual size_t LastWrite() const { return m_lastwrite; }
127 wxStreamBuffer *OutputStreamBuffer() { return m_o_streambuf; }
128
129 virtual void Sync();
130
131 wxOutputStream& operator<<(wxInputStream& out) { return Write(out); }
132 wxOutputStream& operator<<(const char *string);
133 wxOutputStream& operator<<(wxString& string);
134 wxOutputStream& operator<<(char c);
135 wxOutputStream& operator<<(short i);
136 wxOutputStream& operator<<(int i);
137 wxOutputStream& operator<<(long i);
138 wxOutputStream& operator<<(double f);
139
140 wxOutputStream& operator<<(float f) { return operator<<((double)f); }
141 wxOutputStream& operator<<(unsigned char c) { return operator<<((char)c); }
142 wxOutputStream& operator<<(unsigned short i) { return operator<<((short)i); }
143 wxOutputStream& operator<<(unsigned int i) { return operator<<((int)i); }
144 wxOutputStream& operator<<(unsigned long i) { return operator<<((long)i); }
145
146 wxOutputStream& operator<<( __wxOutputManip func) { return func(*this); }
147
148 protected:
149 friend class wxStreamBuffer;
150 friend class wxFilterOutputStream;
151
152 wxOutputStream(wxStreamBuffer *buffer);
153
154 virtual size_t DoWrite(const void *buffer, size_t size) = 0;
155 virtual off_t DoSeekOutput(off_t pos, wxSeekMode mode) = 0;
156 virtual off_t DoTellOutput() const = 0;
157
158 protected:
159 bool m_bad, m_o_destroybuf;
160 size_t m_lastwrite;
161 wxStreamBuffer *m_o_streambuf;
162 };
163
164 /*
165 * "Filter" streams
166 */
167
168 class WXDLLEXPORT wxFilterInputStream: public wxInputStream {
169 public:
170 wxFilterInputStream(wxInputStream& stream);
171 virtual ~wxFilterInputStream();
172
173 virtual char Peek() { return m_parent_i_stream->Peek(); }
174
175 virtual bool Eof() const { return m_parent_i_stream->Eof(); }
176 virtual size_t LastRead() const { return m_parent_i_stream->LastRead(); }
177
178 protected:
179 virtual size_t DoRead(void *buffer, size_t size);
180 virtual off_t DoSeekInput(off_t pos, wxSeekMode mode);
181 virtual off_t DoTellInput() const;
182
183 protected:
184 wxInputStream *m_parent_i_stream;
185 };
186
187 class WXDLLEXPORT wxFilterOutputStream: public wxOutputStream {
188 public:
189 wxFilterOutputStream(wxOutputStream& stream);
190 virtual ~wxFilterOutputStream();
191
192 virtual bool Bad() const { return m_parent_o_stream->Bad(); }
193 virtual size_t LastWrite() const { return m_parent_o_stream->LastWrite(); }
194
195 protected:
196
197 // The forward is implicitely done by wxStreamBuffer.
198
199 virtual size_t DoWrite(const void *buffer, size_t size);
200 virtual off_t DoSeekOutput(off_t pos, wxSeekMode mode);
201 virtual off_t DoTellOutput() const;
202
203 protected:
204 wxOutputStream *m_parent_o_stream;
205 };
206
207 #endif