]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: datstrm.cpp | |
3 | // Purpose: Data stream classes | |
4 | // Author: Guilhem Lavaux | |
5 | // Modified by: | |
6 | // Created: 28/06/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Guilhem Lavaux | |
9 | // Licence: wxWindows license | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "datstrm.h" | |
14 | #endif | |
15 | ||
16 | // For compilers that support precompilation, includes "wx.h". | |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __BORLANDC__ | |
20 | #pragma hdrstop | |
21 | #endif | |
22 | ||
23 | #if wxUSE_STREAMS | |
24 | ||
25 | #include "wx/datstrm.h" | |
26 | ||
27 | // --------------------------------------------------------------------------- | |
28 | // wxDataInputStream | |
29 | // --------------------------------------------------------------------------- | |
30 | ||
31 | wxDataInputStream::wxDataInputStream(wxInputStream& s) | |
32 | : m_input(&s) | |
33 | { | |
34 | } | |
35 | ||
36 | wxDataInputStream::~wxDataInputStream() | |
37 | { | |
38 | } | |
39 | ||
40 | wxUint32 wxDataInputStream::Read32() | |
41 | { | |
42 | char buf[4]; | |
43 | ||
44 | m_input->Read(buf, 4); | |
45 | ||
46 | return (wxUint32)buf[0] | | |
47 | ((wxUint32)buf[1] << 8) | | |
48 | ((wxUint32)buf[2] << 16) | | |
49 | ((wxUint32)buf[3] << 24); | |
50 | } | |
51 | ||
52 | wxUint16 wxDataInputStream::Read16() | |
53 | { | |
54 | char buf[2]; | |
55 | ||
56 | m_input->Read(buf, 2); | |
57 | ||
58 | return (wxUint16)buf[0] | | |
59 | ((wxUint16)buf[1] << 8); | |
60 | } | |
61 | ||
62 | wxUint8 wxDataInputStream::Read8() | |
63 | { | |
64 | wxUint8 buf; | |
65 | ||
66 | m_input->Read((char *)&buf, 1); | |
67 | return (wxUint8)buf; | |
68 | } | |
69 | ||
70 | // Must be at global scope for VC++ 5 | |
71 | extern "C" double ConvertFromIeeeExtended(const unsigned char *bytes); | |
72 | ||
73 | double wxDataInputStream::ReadDouble() | |
74 | { | |
75 | #if wxUSE_APPLE_IEEE | |
76 | char buf[10]; | |
77 | ||
78 | m_input->Read(buf, 10); | |
79 | return ConvertFromIeeeExtended((unsigned char *)buf); | |
80 | #else | |
81 | return 0.0; | |
82 | #endif | |
83 | } | |
84 | ||
85 | wxString wxDataInputStream::ReadString() | |
86 | { | |
87 | wxString wx_string; | |
88 | char *string; | |
89 | unsigned long len; | |
90 | ||
91 | len = Read32(); | |
92 | string = new char[len+1]; | |
93 | ||
94 | m_input->Read(string, len); | |
95 | ||
96 | string[len] = 0; | |
97 | wx_string = string; | |
98 | delete string; | |
99 | ||
100 | return wx_string; | |
101 | } | |
102 | ||
103 | wxDataInputStream& wxDataInputStream::operator>>(wxString& s) | |
104 | { | |
105 | s = ReadString(); | |
106 | return *this; | |
107 | } | |
108 | ||
109 | wxDataInputStream& wxDataInputStream::operator>>(wxInt8& c) | |
110 | { | |
111 | c = (wxInt8)Read8(); | |
112 | return *this; | |
113 | } | |
114 | ||
115 | wxDataInputStream& wxDataInputStream::operator>>(wxInt16& i) | |
116 | { | |
117 | i = (wxInt16)Read16(); | |
118 | return *this; | |
119 | } | |
120 | ||
121 | wxDataInputStream& wxDataInputStream::operator>>(wxInt32& i) | |
122 | { | |
123 | i = (wxInt32)Read32(); | |
124 | return *this; | |
125 | } | |
126 | ||
127 | wxDataInputStream& wxDataInputStream::operator>>(wxUint8& c) | |
128 | { | |
129 | c = Read8(); | |
130 | return *this; | |
131 | } | |
132 | ||
133 | wxDataInputStream& wxDataInputStream::operator>>(wxUint16& i) | |
134 | { | |
135 | i = Read16(); | |
136 | return *this; | |
137 | } | |
138 | ||
139 | wxDataInputStream& wxDataInputStream::operator>>(wxUint32& i) | |
140 | { | |
141 | i = Read32(); | |
142 | return *this; | |
143 | } | |
144 | ||
145 | wxDataInputStream& wxDataInputStream::operator>>(double& i) | |
146 | { | |
147 | i = ReadDouble(); | |
148 | return *this; | |
149 | } | |
150 | ||
151 | wxDataInputStream& wxDataInputStream::operator>>(float& f) | |
152 | { | |
153 | f = (float)ReadDouble(); | |
154 | return *this; | |
155 | } | |
156 | ||
157 | // --------------------------------------------------------------------------- | |
158 | // wxDataOutputStream | |
159 | // --------------------------------------------------------------------------- | |
160 | ||
161 | wxDataOutputStream::wxDataOutputStream(wxOutputStream& s) | |
162 | : m_output(&s) | |
163 | { | |
164 | } | |
165 | ||
166 | wxDataOutputStream::~wxDataOutputStream() | |
167 | { | |
168 | } | |
169 | ||
170 | void wxDataOutputStream::Write32(wxUint32 i) | |
171 | { | |
172 | char buf[4]; | |
173 | ||
174 | buf[0] = i & 0xff; | |
175 | buf[1] = (i >> 8) & 0xff; | |
176 | buf[2] = (i >> 16) & 0xff; | |
177 | buf[3] = (i >> 24) & 0xff; | |
178 | m_output->Write(buf, 4); | |
179 | } | |
180 | ||
181 | void wxDataOutputStream::Write16(wxUint16 i) | |
182 | { | |
183 | char buf[2]; | |
184 | ||
185 | buf[0] = i & 0xff; | |
186 | buf[1] = (i >> 8) & 0xff; | |
187 | m_output->Write(buf, 2); | |
188 | } | |
189 | ||
190 | void wxDataOutputStream::Write8(wxUint8 i) | |
191 | { | |
192 | m_output->Write(&i, 1); | |
193 | } | |
194 | ||
195 | void wxDataOutputStream::WriteString(const wxString& string) | |
196 | { | |
197 | const wxWX2MBbuf buf = string.mb_str(); | |
198 | Write32(string.Length()); | |
199 | m_output->Write(buf, string.Len()); | |
200 | } | |
201 | ||
202 | // Must be at global scope for VC++ 5 | |
203 | extern "C" void ConvertToIeeeExtended(double num, unsigned char *bytes); | |
204 | ||
205 | void wxDataOutputStream::WriteDouble(double d) | |
206 | { | |
207 | char buf[10]; | |
208 | ||
209 | #if wxUSE_APPLE_IEEE | |
210 | ConvertToIeeeExtended(d, (unsigned char *)buf); | |
211 | #else | |
212 | # pragma warning "wxDataOutputStream::WriteDouble() not using IeeeExtended - will not work!" | |
213 | buf[0] = '\0'; | |
214 | #endif | |
215 | m_output->Write(buf, 10); | |
216 | } | |
217 | ||
218 | wxDataOutputStream& wxDataOutputStream::operator<<(const wxChar *string) | |
219 | { | |
220 | Write32(wxStrlen(string)); | |
221 | m_output->Write((const char *)string, wxStrlen(string)*sizeof(wxChar)); | |
222 | return *this; | |
223 | } | |
224 | ||
225 | wxDataOutputStream& wxDataOutputStream::operator<<(wxString& string) | |
226 | { | |
227 | WriteString(string); | |
228 | return *this; | |
229 | } | |
230 | ||
231 | wxDataOutputStream& wxDataOutputStream::operator<<(wxInt8 c) | |
232 | { | |
233 | Write8((wxUint8)c); | |
234 | return *this; | |
235 | } | |
236 | ||
237 | wxDataOutputStream& wxDataOutputStream::operator<<(wxInt16 i) | |
238 | { | |
239 | Write16((wxUint16)i); | |
240 | return *this; | |
241 | } | |
242 | ||
243 | wxDataOutputStream& wxDataOutputStream::operator<<(wxInt32 i) | |
244 | { | |
245 | Write32((wxUint32)i); | |
246 | return *this; | |
247 | } | |
248 | ||
249 | wxDataOutputStream& wxDataOutputStream::operator<<(wxUint8 c) | |
250 | { | |
251 | Write8(c); | |
252 | return *this; | |
253 | } | |
254 | ||
255 | wxDataOutputStream& wxDataOutputStream::operator<<(wxUint16 i) | |
256 | { | |
257 | Write16(i); | |
258 | return *this; | |
259 | } | |
260 | ||
261 | wxDataOutputStream& wxDataOutputStream::operator<<(wxUint32 i) | |
262 | { | |
263 | Write32(i); | |
264 | return *this; | |
265 | } | |
266 | ||
267 | wxDataOutputStream& wxDataOutputStream::operator<<(double f) | |
268 | { | |
269 | WriteDouble(f); | |
270 | return *this; | |
271 | } | |
272 | ||
273 | wxDataOutputStream& wxDataOutputStream::operator<<(float f) | |
274 | { | |
275 | WriteDouble((double)f); | |
276 | return *this; | |
277 | } | |
278 | ||
279 | #endif | |
280 | // wxUSE_STREAMS | |
281 |