]>
Commit | Line | Data |
---|---|---|
cf447356 GL |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: datstrm.cpp | |
3 | // Purpose: Data stream classes | |
4 | // Author: Guilhem Lavaux | |
53663be8 | 5 | // Modified by: Mickael Gilabert |
cf447356 GL |
6 | // Created: 28/06/98 |
7 | // RCS-ID: $Id$ | |
13111b2a | 8 | // Copyright: (c) Guilhem Lavaux |
55d99c7a | 9 | // Licence: wxWindows licence |
cf447356 GL |
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__ | |
ce4169a4 | 20 | #pragma hdrstop |
cf447356 GL |
21 | #endif |
22 | ||
ce4169a4 RR |
23 | #if wxUSE_STREAMS |
24 | ||
cf447356 | 25 | #include "wx/datstrm.h" |
3cacae09 | 26 | |
f4ada568 GL |
27 | // --------------------------------------------------------------------------- |
28 | // wxDataInputStream | |
29 | // --------------------------------------------------------------------------- | |
30 | ||
a99acbb0 VS |
31 | #if wxUSE_UNICODE |
32 | wxDataInputStream::wxDataInputStream(wxInputStream& s, wxMBConv& conv) | |
33 | : m_input(&s), m_be_order(FALSE), m_conv(conv) | |
34 | #else | |
3d4c6a21 | 35 | wxDataInputStream::wxDataInputStream(wxInputStream& s) |
5a96d2f4 | 36 | : m_input(&s), m_be_order(FALSE) |
a99acbb0 | 37 | #endif |
cf447356 | 38 | { |
cf447356 GL |
39 | } |
40 | ||
3d4c6a21 | 41 | wxDataInputStream::~wxDataInputStream() |
cf447356 | 42 | { |
cf447356 GL |
43 | } |
44 | ||
41b0a113 RL |
45 | wxUint64 wxDataInputStream::Read64() |
46 | { | |
47 | wxUint64 i64; | |
48 | ||
49 | m_input->Read(&i64, 8); | |
50 | ||
51 | if (m_be_order) | |
52 | return wxUINT64_SWAP_ON_LE(i64); | |
53 | else | |
54 | return wxUINT64_SWAP_ON_BE(i64); | |
55 | } | |
56 | ||
7b8bd818 | 57 | wxUint32 wxDataInputStream::Read32() |
cf447356 | 58 | { |
5a96d2f4 | 59 | wxUint32 i32; |
cf447356 | 60 | |
5a96d2f4 | 61 | m_input->Read(&i32, 4); |
cf447356 | 62 | |
5a96d2f4 GL |
63 | if (m_be_order) |
64 | return wxUINT32_SWAP_ON_LE(i32); | |
65 | else | |
66 | return wxUINT32_SWAP_ON_BE(i32); | |
cf447356 GL |
67 | } |
68 | ||
7b8bd818 | 69 | wxUint16 wxDataInputStream::Read16() |
cf447356 | 70 | { |
5a96d2f4 | 71 | wxUint16 i16; |
cf447356 | 72 | |
5a96d2f4 | 73 | m_input->Read(&i16, 2); |
cf447356 | 74 | |
5a96d2f4 GL |
75 | if (m_be_order) |
76 | return wxUINT16_SWAP_ON_LE(i16); | |
77 | else | |
78 | return wxUINT16_SWAP_ON_BE(i16); | |
cf447356 GL |
79 | } |
80 | ||
7b8bd818 | 81 | wxUint8 wxDataInputStream::Read8() |
cf447356 | 82 | { |
7b8bd818 | 83 | wxUint8 buf; |
cf447356 | 84 | |
5a96d2f4 | 85 | m_input->Read(&buf, 1); |
7b8bd818 | 86 | return (wxUint8)buf; |
cf447356 GL |
87 | } |
88 | ||
5260b1c5 JS |
89 | // Must be at global scope for VC++ 5 |
90 | extern "C" double ConvertFromIeeeExtended(const unsigned char *bytes); | |
91 | ||
3d4c6a21 | 92 | double wxDataInputStream::ReadDouble() |
cf447356 | 93 | { |
47d67540 | 94 | #if wxUSE_APPLE_IEEE |
cf447356 GL |
95 | char buf[10]; |
96 | ||
fae05df5 | 97 | m_input->Read(buf, 10); |
cf447356 GL |
98 | return ConvertFromIeeeExtended((unsigned char *)buf); |
99 | #else | |
100 | return 0.0; | |
101 | #endif | |
102 | } | |
103 | ||
3d4c6a21 | 104 | wxString wxDataInputStream::ReadString() |
eafc087e | 105 | { |
13111b2a | 106 | size_t len; |
eafc087e | 107 | |
eafc087e | 108 | len = Read32(); |
eafc087e | 109 | |
39a16cb4 VS |
110 | if (len > 0) |
111 | { | |
2ae47e3f | 112 | #if wxUSE_UNICODE |
39a16cb4 VS |
113 | char *tmp = new char[len + 1]; |
114 | m_input->Read(tmp, len); | |
115 | tmp[len] = 0; | |
2b5f62a0 | 116 | wxString ret( (const wxChar*) m_conv.cMB2WX(tmp) ); |
39a16cb4 | 117 | delete[] tmp; |
2ae47e3f | 118 | #else |
2b5f62a0 VZ |
119 | wxString ret; |
120 | m_input->Read( ret.GetWriteBuf(len), len); | |
121 | ret.UngetWriteBuf(); | |
2ae47e3f | 122 | #endif |
2b5f62a0 | 123 | return ret; |
39a16cb4 | 124 | } |
38caaa61 | 125 | else |
39a16cb4 | 126 | return wxEmptyString; |
eafc087e | 127 | } |
13111b2a | 128 | |
53663be8 VZ |
129 | void wxDataInputStream::Read64(wxUint64 *buffer, size_t size) |
130 | { | |
131 | m_input->Read(buffer, size * 8); | |
132 | ||
133 | if (m_be_order) | |
134 | { | |
135 | for (wxUint32 i=0; i<size; i++) | |
136 | { | |
137 | wxUint64 v = wxUINT64_SWAP_ON_LE(*buffer); | |
138 | *(buffer++) = v; | |
139 | } | |
140 | } | |
141 | else | |
142 | { | |
143 | for (wxUint32 i=0; i<size; i++) | |
144 | { | |
145 | wxUint64 v = wxUINT64_SWAP_ON_BE(*buffer); | |
146 | *(buffer++) = v; | |
147 | } | |
148 | } | |
149 | } | |
150 | ||
151 | void wxDataInputStream::Read32(wxUint32 *buffer, size_t size) | |
152 | { | |
153 | m_input->Read(buffer, size * 4); | |
154 | ||
155 | if (m_be_order) | |
156 | { | |
157 | for (wxUint32 i=0; i<size; i++) | |
158 | { | |
159 | wxUint32 v = wxUINT32_SWAP_ON_LE(*buffer); | |
160 | *(buffer++) = v; | |
161 | } | |
162 | } | |
163 | else | |
164 | { | |
165 | for (wxUint32 i=0; i<size; i++) | |
166 | { | |
167 | wxUint32 v = wxUINT32_SWAP_ON_BE(*buffer); | |
168 | *(buffer++) = v; | |
169 | } | |
170 | } | |
171 | } | |
172 | ||
173 | void wxDataInputStream::Read16(wxUint16 *buffer, size_t size) | |
174 | { | |
175 | m_input->Read(buffer, size * 2); | |
176 | ||
177 | if (m_be_order) | |
178 | { | |
179 | for (wxUint32 i=0; i<size; i++) | |
180 | { | |
181 | wxUint16 v = wxUINT16_SWAP_ON_LE(*buffer); | |
182 | *(buffer++) = v; | |
183 | } | |
184 | } | |
185 | else | |
186 | { | |
187 | for (wxUint32 i=0; i<size; i++) | |
188 | { | |
189 | wxUint16 v = wxUINT16_SWAP_ON_BE(*buffer); | |
190 | *(buffer++) = v; | |
191 | } | |
192 | } | |
193 | } | |
194 | ||
195 | void wxDataInputStream::Read8(wxUint8 *buffer, size_t size) | |
196 | { | |
197 | m_input->Read(buffer, size); | |
198 | } | |
199 | ||
200 | void wxDataInputStream::ReadDouble(double *buffer, size_t size) | |
201 | { | |
202 | for (wxUint32 i=0; i<size; i++) | |
203 | { | |
204 | *(buffer++) = ReadDouble(); | |
205 | } | |
206 | } | |
207 | ||
fae05df5 GL |
208 | wxDataInputStream& wxDataInputStream::operator>>(wxString& s) |
209 | { | |
210 | s = ReadString(); | |
211 | return *this; | |
212 | } | |
213 | ||
214 | wxDataInputStream& wxDataInputStream::operator>>(wxInt8& c) | |
215 | { | |
216 | c = (wxInt8)Read8(); | |
217 | return *this; | |
218 | } | |
219 | ||
220 | wxDataInputStream& wxDataInputStream::operator>>(wxInt16& i) | |
221 | { | |
222 | i = (wxInt16)Read16(); | |
223 | return *this; | |
224 | } | |
225 | ||
226 | wxDataInputStream& wxDataInputStream::operator>>(wxInt32& i) | |
227 | { | |
228 | i = (wxInt32)Read32(); | |
229 | return *this; | |
230 | } | |
231 | ||
232 | wxDataInputStream& wxDataInputStream::operator>>(wxUint8& c) | |
233 | { | |
234 | c = Read8(); | |
235 | return *this; | |
236 | } | |
237 | ||
238 | wxDataInputStream& wxDataInputStream::operator>>(wxUint16& i) | |
239 | { | |
240 | i = Read16(); | |
241 | return *this; | |
242 | } | |
243 | ||
244 | wxDataInputStream& wxDataInputStream::operator>>(wxUint32& i) | |
245 | { | |
246 | i = Read32(); | |
247 | return *this; | |
248 | } | |
249 | ||
41b0a113 RL |
250 | wxDataInputStream& wxDataInputStream::operator>>(wxUint64& i) |
251 | { | |
252 | i = Read64(); | |
253 | return *this; | |
254 | } | |
255 | ||
fae05df5 GL |
256 | wxDataInputStream& wxDataInputStream::operator>>(double& i) |
257 | { | |
258 | i = ReadDouble(); | |
259 | return *this; | |
260 | } | |
261 | ||
262 | wxDataInputStream& wxDataInputStream::operator>>(float& f) | |
263 | { | |
264 | f = (float)ReadDouble(); | |
265 | return *this; | |
266 | } | |
eafc087e | 267 | |
f4ada568 GL |
268 | // --------------------------------------------------------------------------- |
269 | // wxDataOutputStream | |
270 | // --------------------------------------------------------------------------- | |
271 | ||
a99acbb0 VS |
272 | #if wxUSE_UNICODE |
273 | wxDataOutputStream::wxDataOutputStream(wxOutputStream& s, wxMBConv& conv) | |
274 | : m_output(&s), m_be_order(FALSE), m_conv(conv) | |
275 | #else | |
3d4c6a21 | 276 | wxDataOutputStream::wxDataOutputStream(wxOutputStream& s) |
7ff14117 | 277 | : m_output(&s), m_be_order(FALSE) |
a99acbb0 | 278 | #endif |
3d4c6a21 GL |
279 | { |
280 | } | |
281 | ||
f0b07807 KB |
282 | wxDataOutputStream::~wxDataOutputStream() |
283 | { | |
284 | } | |
285 | ||
41b0a113 RL |
286 | void wxDataOutputStream::Write64(wxUint64 i) |
287 | { | |
288 | wxUint64 i64; | |
289 | ||
290 | if (m_be_order) | |
291 | i64 = wxUINT64_SWAP_ON_LE(i); | |
292 | else | |
293 | i64 = wxUINT64_SWAP_ON_BE(i); | |
294 | m_output->Write(&i64, 8); | |
295 | } | |
296 | ||
7b8bd818 | 297 | void wxDataOutputStream::Write32(wxUint32 i) |
cf447356 | 298 | { |
5a96d2f4 | 299 | wxUint32 i32; |
cf447356 | 300 | |
5a96d2f4 GL |
301 | if (m_be_order) |
302 | i32 = wxUINT32_SWAP_ON_LE(i); | |
303 | else | |
304 | i32 = wxUINT32_SWAP_ON_BE(i); | |
305 | m_output->Write(&i32, 4); | |
cf447356 GL |
306 | } |
307 | ||
7b8bd818 | 308 | void wxDataOutputStream::Write16(wxUint16 i) |
cf447356 | 309 | { |
5a96d2f4 | 310 | wxUint16 i16; |
cf447356 | 311 | |
5a96d2f4 GL |
312 | if (m_be_order) |
313 | i16 = wxUINT16_SWAP_ON_LE(i); | |
314 | else | |
315 | i16 = wxUINT16_SWAP_ON_BE(i); | |
316 | ||
317 | m_output->Write(&i16, 2); | |
cf447356 GL |
318 | } |
319 | ||
7b8bd818 | 320 | void wxDataOutputStream::Write8(wxUint8 i) |
cf447356 | 321 | { |
fae05df5 | 322 | m_output->Write(&i, 1); |
cf447356 GL |
323 | } |
324 | ||
3d4c6a21 | 325 | void wxDataOutputStream::WriteString(const wxString& string) |
eafc087e | 326 | { |
a99acbb0 VS |
327 | #if wxUSE_UNICODE |
328 | const wxWX2MBbuf buf = string.mb_str(m_conv); | |
329 | #else | |
c980c992 | 330 | const wxWX2MBbuf buf = string.mb_str(); |
a99acbb0 VS |
331 | #endif |
332 | size_t len = strlen(buf); | |
333 | Write32(len); | |
334 | if (len > 0) | |
335 | m_output->Write(buf, len); | |
cf447356 GL |
336 | } |
337 | ||
5260b1c5 JS |
338 | // Must be at global scope for VC++ 5 |
339 | extern "C" void ConvertToIeeeExtended(double num, unsigned char *bytes); | |
340 | ||
3d4c6a21 | 341 | void wxDataOutputStream::WriteDouble(double d) |
cf447356 | 342 | { |
cf447356 GL |
343 | char buf[10]; |
344 | ||
47d67540 | 345 | #if wxUSE_APPLE_IEEE |
cf447356 | 346 | ConvertToIeeeExtended(d, (unsigned char *)buf); |
0e338ff9 | 347 | #else |
338dd992 JJ |
348 | #ifndef __VMS__ |
349 | # pragma warning "wxDataOutputStream::WriteDouble() not using IeeeExtended - will not work!" | |
350 | #endif | |
351 | buf[0] = '\0'; | |
0e338ff9 | 352 | #endif |
fae05df5 GL |
353 | m_output->Write(buf, 10); |
354 | } | |
355 | ||
53663be8 VZ |
356 | void wxDataOutputStream::Write64(const wxUint64 *buffer, size_t size) |
357 | { | |
358 | if (m_be_order) | |
359 | { | |
360 | for (wxUint32 i=0; i<size ;i++) | |
361 | { | |
362 | wxUint64 i64 = wxUINT64_SWAP_ON_LE(*buffer); | |
363 | buffer++; | |
364 | m_output->Write(&i64, 8); | |
365 | } | |
366 | } | |
367 | else | |
368 | { | |
369 | for (wxUint32 i=0; i<size ;i++) | |
370 | { | |
371 | wxUint64 i64 = wxUINT64_SWAP_ON_BE(*buffer); | |
372 | buffer++; | |
373 | m_output->Write(&i64, 8); | |
374 | } | |
375 | } | |
376 | } | |
377 | ||
378 | void wxDataOutputStream::Write32(const wxUint32 *buffer, size_t size) | |
379 | { | |
380 | if (m_be_order) | |
381 | { | |
382 | for (wxUint32 i=0; i<size ;i++) | |
383 | { | |
384 | wxUint32 i32 = wxUINT32_SWAP_ON_LE(*buffer); | |
385 | buffer++; | |
386 | m_output->Write(&i32, 4); | |
387 | } | |
388 | } | |
389 | else | |
390 | { | |
391 | for (wxUint32 i=0; i<size ;i++) | |
392 | { | |
393 | wxUint32 i32 = wxUINT32_SWAP_ON_BE(*buffer); | |
394 | buffer++; | |
395 | m_output->Write(&i32, 4); | |
396 | } | |
397 | } | |
398 | } | |
399 | ||
400 | void wxDataOutputStream::Write16(const wxUint16 *buffer, size_t size) | |
401 | { | |
402 | if (m_be_order) | |
403 | { | |
404 | for (wxUint32 i=0; i<size ;i++) | |
405 | { | |
406 | wxUint16 i16 = wxUINT16_SWAP_ON_LE(*buffer); | |
407 | buffer++; | |
408 | m_output->Write(&i16, 2); | |
409 | } | |
410 | } | |
411 | else | |
412 | { | |
413 | for (wxUint32 i=0; i<size ;i++) | |
414 | { | |
415 | wxUint16 i16 = wxUINT16_SWAP_ON_BE(*buffer); | |
416 | buffer++; | |
417 | m_output->Write(&i16, 2); | |
418 | } | |
419 | } | |
420 | } | |
421 | ||
422 | void wxDataOutputStream::Write8(const wxUint8 *buffer, size_t size) | |
423 | { | |
424 | m_output->Write(buffer, size); | |
425 | } | |
426 | ||
427 | void wxDataOutputStream::WriteDouble(const double *buffer, size_t size) | |
428 | { | |
429 | for (wxUint32 i=0; i<size; i++) | |
430 | { | |
431 | WriteDouble(*(buffer++)); | |
432 | } | |
433 | } | |
434 | ||
fae05df5 GL |
435 | wxDataOutputStream& wxDataOutputStream::operator<<(const wxChar *string) |
436 | { | |
437 | Write32(wxStrlen(string)); | |
438 | m_output->Write((const char *)string, wxStrlen(string)*sizeof(wxChar)); | |
439 | return *this; | |
440 | } | |
441 | ||
38caaa61 | 442 | wxDataOutputStream& wxDataOutputStream::operator<<(const wxString& string) |
fae05df5 GL |
443 | { |
444 | WriteString(string); | |
445 | return *this; | |
446 | } | |
447 | ||
448 | wxDataOutputStream& wxDataOutputStream::operator<<(wxInt8 c) | |
449 | { | |
450 | Write8((wxUint8)c); | |
451 | return *this; | |
452 | } | |
453 | ||
454 | wxDataOutputStream& wxDataOutputStream::operator<<(wxInt16 i) | |
455 | { | |
456 | Write16((wxUint16)i); | |
457 | return *this; | |
458 | } | |
459 | ||
460 | wxDataOutputStream& wxDataOutputStream::operator<<(wxInt32 i) | |
461 | { | |
462 | Write32((wxUint32)i); | |
463 | return *this; | |
464 | } | |
465 | ||
466 | wxDataOutputStream& wxDataOutputStream::operator<<(wxUint8 c) | |
467 | { | |
468 | Write8(c); | |
469 | return *this; | |
470 | } | |
471 | ||
472 | wxDataOutputStream& wxDataOutputStream::operator<<(wxUint16 i) | |
473 | { | |
474 | Write16(i); | |
475 | return *this; | |
476 | } | |
477 | ||
478 | wxDataOutputStream& wxDataOutputStream::operator<<(wxUint32 i) | |
479 | { | |
480 | Write32(i); | |
481 | return *this; | |
482 | } | |
483 | ||
41b0a113 RL |
484 | wxDataOutputStream& wxDataOutputStream::operator<<(wxUint64 i) |
485 | { | |
486 | Write64(i); | |
487 | return *this; | |
488 | } | |
489 | ||
fae05df5 GL |
490 | wxDataOutputStream& wxDataOutputStream::operator<<(double f) |
491 | { | |
492 | WriteDouble(f); | |
493 | return *this; | |
494 | } | |
495 | ||
496 | wxDataOutputStream& wxDataOutputStream::operator<<(float f) | |
497 | { | |
498 | WriteDouble((double)f); | |
499 | return *this; | |
cf447356 | 500 | } |
ce4169a4 RR |
501 | |
502 | #endif | |
503 | // wxUSE_STREAMS | |
13111b2a | 504 |