]>
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 | ||
75ed1d15 | 24 | class WXDLLEXPORT wxStreamBase; |
1678ad78 GL |
25 | class WXDLLEXPORT wxInputStream; |
26 | class WXDLLEXPORT wxOutputStream; | |
27 | ||
28 | typedef wxInputStream& (*__wxInputManip)(wxInputStream&); | |
29 | typedef wxOutputStream& (*__wxOutputManip)(wxOutputStream&); | |
30 | ||
31 | wxOutputStream& WXDLLEXPORT wxEndL(wxOutputStream& o_stream); | |
32 | ||
f4ada568 GL |
33 | // --------------------------------------------------------------------------- |
34 | // Stream buffer | |
35 | // --------------------------------------------------------------------------- | |
36 | ||
1678ad78 GL |
37 | class WXDLLEXPORT wxStreamBuffer { |
38 | public: | |
75ed1d15 GL |
39 | typedef enum { |
40 | read, write | |
41 | } BufMode; | |
42 | ||
43 | // ----------- | |
44 | // ctor & dtor | |
45 | // ----------- | |
46 | wxStreamBuffer(wxStreamBase& stream, BufMode mode); | |
1678ad78 GL |
47 | ~wxStreamBuffer(); |
48 | ||
75ed1d15 GL |
49 | // ----------- |
50 | // Filtered IO | |
51 | // ----------- | |
1678ad78 GL |
52 | void Read(void *buffer, size_t size); |
53 | void Write(const void *buffer, size_t size); | |
75ed1d15 GL |
54 | bool WriteBack(const char *buffer, size_t size); |
55 | bool WriteBack(char c); | |
56 | off_t Tell() const; | |
57 | off_t Seek(off_t pos, wxSeekMode mode); | |
58 | ||
59 | // -------------- | |
60 | // Buffer control | |
61 | // -------------- | |
62 | void ResetBuffer(); | |
1678ad78 GL |
63 | void SetBufferIO(char *buffer_start, char *buffer_end); |
64 | void SetBufferIO(size_t bufsize); | |
75ed1d15 GL |
65 | char *GetBufferStart() const { return m_buffer_start; } |
66 | char *GetBufferEnd() const { return m_buffer_end; } | |
67 | char *GetBufferPos() const { return m_buffer_pos; } | |
68 | off_t GetIntPosition() const { return m_buffer_pos-m_buffer_start; } | |
69 | void SetIntPosition(off_t pos) { m_buffer_pos = m_buffer_start+pos; } | |
70 | size_t GetLastAccess() const { return m_buffer_end-m_buffer_start; } | |
71 | void Fixed(bool fixed) { m_fixed = fixed; } | |
72 | ||
73 | bool FlushBuffer(); | |
74 | bool FillBuffer(); | |
75 | size_t GetDataLeft() const; | |
76 | ||
77 | protected: | |
78 | char *AllocSpaceWBack(size_t needed_size); | |
79 | size_t GetWBack(char *buf, size_t bsize); | |
80 | ||
81 | void GetFromBuffer(void *buffer, size_t size); | |
82 | void PutToBuffer(const void *buffer, size_t size); | |
1678ad78 GL |
83 | |
84 | protected: | |
85 | char *m_buffer_start, *m_buffer_end, *m_buffer_pos; | |
86 | size_t m_buffer_size; | |
87 | ||
75ed1d15 GL |
88 | char *m_wback; |
89 | size_t m_wbacksize, m_wbackcur; | |
90 | ||
91 | bool m_fixed; | |
92 | ||
93 | wxStreamBase *m_stream; | |
94 | BufMode m_mode; | |
1678ad78 | 95 | }; |
eda3efe2 | 96 | |
f4ada568 GL |
97 | // --------------------------------------------------------------------------- |
98 | // wxStream: base classes | |
99 | // --------------------------------------------------------------------------- | |
100 | ||
75ed1d15 GL |
101 | typedef enum { |
102 | wxStream_NOERROR, | |
103 | wxStream_EOF | |
104 | } wxStreamError; | |
105 | ||
106 | class WXDLLEXPORT wxStreamBase { | |
107 | public: | |
108 | wxStreamBase(); | |
109 | virtual ~wxStreamBase(); | |
110 | ||
111 | wxStreamError LastError() { return m_lasterror; } | |
112 | ||
113 | protected: | |
114 | friend class wxStreamBuffer; | |
115 | ||
116 | virtual size_t OnSysRead(void *buffer, size_t bufsize); | |
117 | virtual size_t OnSysWrite(const void *buffer, size_t bufsize); | |
118 | virtual off_t OnSysSeek(off_t seek, wxSeekMode mode); | |
119 | virtual off_t OnSysTell(); | |
120 | ||
121 | protected: | |
122 | size_t m_lastcount; | |
123 | wxStreamError m_lasterror; | |
124 | }; | |
125 | ||
126 | class WXDLLEXPORT wxInputStream: public wxStreamBase { | |
32fc4afb GL |
127 | public: |
128 | wxInputStream(); | |
75ed1d15 | 129 | wxInputStream(wxStreamBuffer *sbuf); |
32fc4afb GL |
130 | virtual ~wxInputStream(); |
131 | ||
1678ad78 | 132 | // IO functions |
75ed1d15 GL |
133 | virtual char Peek(); |
134 | char GetC(); | |
135 | wxInputStream& Read(void *buffer, size_t size); | |
32fc4afb GL |
136 | wxInputStream& Read(wxOutputStream& stream_out); |
137 | ||
1678ad78 | 138 | // Position functions |
75ed1d15 GL |
139 | off_t SeekI(off_t pos, wxSeekMode mode = wxFromStart); |
140 | off_t TellI() const; | |
32fc4afb | 141 | |
1678ad78 | 142 | // State functions |
1678ad78 | 143 | wxStreamBuffer *InputStreamBuffer() { return m_i_streambuf; } |
75ed1d15 | 144 | size_t LastRead() { return wxStreamBase::m_lastcount; } |
0cd9bfe8 | 145 | |
1678ad78 | 146 | // Operators |
0cd9bfe8 | 147 | wxInputStream& operator>>(wxOutputStream& out) { return Read(out); } |
1678ad78 GL |
148 | wxInputStream& operator>>(wxString& line); |
149 | wxInputStream& operator>>(char& c); | |
150 | wxInputStream& operator>>(short& i); | |
123a7fdd | 151 | wxInputStream& operator>>(int& i); |
1678ad78 | 152 | wxInputStream& operator>>(long& i); |
75ed1d15 | 153 | wxInputStream& operator>>(double& i); |
47d67540 | 154 | #if wxUSE_SERIAL |
123a7fdd | 155 | wxInputStream& operator>>(wxObject *& obj); |
fcc6dddd | 156 | #endif |
123a7fdd | 157 | |
75ed1d15 | 158 | wxInputStream& operator>>(float& f) { double d; operator>>((double&)d); f = (float)d; return *this; } |
123a7fdd GL |
159 | wxInputStream& operator>>(unsigned char& c) { return operator>>((char&)c); } |
160 | wxInputStream& operator>>(unsigned short& i) { return operator>>((short&)i); } | |
161 | wxInputStream& operator>>(unsigned int& i) { return operator>>((int&)i); } | |
162 | wxInputStream& operator>>(unsigned long& i) { return operator>>((long&)i); } | |
1678ad78 GL |
163 | wxInputStream& operator>>( __wxInputManip func) { return func(*this); } |
164 | ||
165 | protected: | |
75ed1d15 | 166 | bool m_i_destroybuf; |
1678ad78 | 167 | wxStreamBuffer *m_i_streambuf; |
32fc4afb GL |
168 | }; |
169 | ||
75ed1d15 | 170 | class WXDLLEXPORT wxOutputStream: public wxStreamBase { |
32fc4afb GL |
171 | public: |
172 | wxOutputStream(); | |
75ed1d15 | 173 | wxOutputStream(wxStreamBuffer *sbuf); |
32fc4afb GL |
174 | virtual ~wxOutputStream(); |
175 | ||
75ed1d15 | 176 | wxOutputStream& Write(const void *buffer, size_t size); |
32fc4afb GL |
177 | wxOutputStream& Write(wxInputStream& stream_in); |
178 | ||
75ed1d15 GL |
179 | off_t SeekO(off_t pos, wxSeekMode mode = wxFromStart); |
180 | off_t TellO() const; | |
1678ad78 | 181 | |
75ed1d15 | 182 | size_t LastWrite() const { return wxStreamBase::m_lastcount; } |
1678ad78 | 183 | wxStreamBuffer *OutputStreamBuffer() { return m_o_streambuf; } |
32fc4afb | 184 | |
75ed1d15 | 185 | void Sync(); |
32fc4afb | 186 | |
1678ad78 GL |
187 | wxOutputStream& operator<<(wxInputStream& out) { return Write(out); } |
188 | wxOutputStream& operator<<(const char *string); | |
189 | wxOutputStream& operator<<(wxString& string); | |
190 | wxOutputStream& operator<<(char c); | |
191 | wxOutputStream& operator<<(short i); | |
192 | wxOutputStream& operator<<(int i); | |
193 | wxOutputStream& operator<<(long i); | |
194 | wxOutputStream& operator<<(double f); | |
47d67540 | 195 | #if wxUSE_SERIAL |
123a7fdd | 196 | wxOutputStream& operator<<(wxObject& obj); |
fcc6dddd | 197 | #endif |
1678ad78 GL |
198 | |
199 | wxOutputStream& operator<<(float f) { return operator<<((double)f); } | |
200 | wxOutputStream& operator<<(unsigned char c) { return operator<<((char)c); } | |
201 | wxOutputStream& operator<<(unsigned short i) { return operator<<((short)i); } | |
202 | wxOutputStream& operator<<(unsigned int i) { return operator<<((int)i); } | |
203 | wxOutputStream& operator<<(unsigned long i) { return operator<<((long)i); } | |
1678ad78 GL |
204 | wxOutputStream& operator<<( __wxOutputManip func) { return func(*this); } |
205 | ||
206 | protected: | |
75ed1d15 | 207 | bool m_o_destroybuf; |
1678ad78 | 208 | wxStreamBuffer *m_o_streambuf; |
32fc4afb GL |
209 | }; |
210 | ||
f4ada568 GL |
211 | // --------------------------------------------------------------------------- |
212 | // "Filter" streams | |
213 | // --------------------------------------------------------------------------- | |
32fc4afb | 214 | |
75ed1d15 | 215 | class WXDLLEXPORT wxFilterInputStream: public wxInputStream { |
32fc4afb | 216 | public: |
f4ada568 | 217 | wxFilterInputStream(); |
32fc4afb | 218 | wxFilterInputStream(wxInputStream& stream); |
f4ada568 | 219 | ~wxFilterInputStream(); |
32fc4afb | 220 | |
f4ada568 | 221 | char Peek() { return m_parent_i_stream->Peek(); } |
32fc4afb | 222 | |
32fc4afb GL |
223 | protected: |
224 | wxInputStream *m_parent_i_stream; | |
225 | }; | |
226 | ||
75ed1d15 | 227 | class WXDLLEXPORT wxFilterOutputStream: public wxOutputStream { |
32fc4afb | 228 | public: |
f4ada568 | 229 | wxFilterOutputStream(); |
32fc4afb | 230 | wxFilterOutputStream(wxOutputStream& stream); |
75ed1d15 | 231 | ~wxFilterOutputStream(); |
1678ad78 | 232 | |
32fc4afb GL |
233 | protected: |
234 | wxOutputStream *m_parent_o_stream; | |
235 | }; | |
236 | ||
237 | #endif |