]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: datstrm.cpp | |
3 | // Purpose: Data stream classes | |
4 | // Author: Guilhem Lavaux | |
5 | // Modified by: Mickael Gilabert | |
6 | // Created: 28/06/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Guilhem Lavaux | |
9 | // Licence: wxWindows licence | |
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 | #if wxUSE_UNICODE | |
32 | wxDataInputStream::wxDataInputStream(wxInputStream& s, wxMBConv& conv) | |
33 | : m_input(&s), m_be_order(FALSE), m_conv(conv) | |
34 | #else | |
35 | wxDataInputStream::wxDataInputStream(wxInputStream& s) | |
36 | : m_input(&s), m_be_order(FALSE) | |
37 | #endif | |
38 | { | |
39 | } | |
40 | ||
41 | wxDataInputStream::~wxDataInputStream() | |
42 | { | |
43 | } | |
44 | ||
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 | ||
57 | wxUint32 wxDataInputStream::Read32() | |
58 | { | |
59 | wxUint32 i32; | |
60 | ||
61 | m_input->Read(&i32, 4); | |
62 | ||
63 | if (m_be_order) | |
64 | return wxUINT32_SWAP_ON_LE(i32); | |
65 | else | |
66 | return wxUINT32_SWAP_ON_BE(i32); | |
67 | } | |
68 | ||
69 | wxUint16 wxDataInputStream::Read16() | |
70 | { | |
71 | wxUint16 i16; | |
72 | ||
73 | m_input->Read(&i16, 2); | |
74 | ||
75 | if (m_be_order) | |
76 | return wxUINT16_SWAP_ON_LE(i16); | |
77 | else | |
78 | return wxUINT16_SWAP_ON_BE(i16); | |
79 | } | |
80 | ||
81 | wxUint8 wxDataInputStream::Read8() | |
82 | { | |
83 | wxUint8 buf; | |
84 | ||
85 | m_input->Read(&buf, 1); | |
86 | return (wxUint8)buf; | |
87 | } | |
88 | ||
89 | // Must be at global scope for VC++ 5 | |
90 | extern "C" double ConvertFromIeeeExtended(const unsigned char *bytes); | |
91 | ||
92 | double wxDataInputStream::ReadDouble() | |
93 | { | |
94 | #if wxUSE_APPLE_IEEE | |
95 | char buf[10]; | |
96 | ||
97 | m_input->Read(buf, 10); | |
98 | return ConvertFromIeeeExtended((unsigned char *)buf); | |
99 | #else | |
100 | return 0.0; | |
101 | #endif | |
102 | } | |
103 | ||
104 | wxString wxDataInputStream::ReadString() | |
105 | { | |
106 | size_t len; | |
107 | ||
108 | len = Read32(); | |
109 | ||
110 | if (len > 0) | |
111 | { | |
112 | #if wxUSE_UNICODE | |
113 | char *tmp = new char[len + 1]; | |
114 | m_input->Read(tmp, len); | |
115 | tmp[len] = 0; | |
116 | wxString ret( (const wxChar*) m_conv.cMB2WX(tmp) ); | |
117 | delete[] tmp; | |
118 | #else | |
119 | wxString ret; | |
120 | m_input->Read( ret.GetWriteBuf(len), len); | |
121 | ret.UngetWriteBuf(); | |
122 | #endif | |
123 | return ret; | |
124 | } | |
125 | else | |
126 | return wxEmptyString; | |
127 | } | |
128 | ||
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 | ||
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 | ||
250 | wxDataInputStream& wxDataInputStream::operator>>(wxUint64& i) | |
251 | { | |
252 | i = Read64(); | |
253 | return *this; | |
254 | } | |
255 | ||
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 | } | |
267 | ||
268 | // --------------------------------------------------------------------------- | |
269 | // wxDataOutputStream | |
270 | // --------------------------------------------------------------------------- | |
271 | ||
272 | #if wxUSE_UNICODE | |
273 | wxDataOutputStream::wxDataOutputStream(wxOutputStream& s, wxMBConv& conv) | |
274 | : m_output(&s), m_be_order(FALSE), m_conv(conv) | |
275 | #else | |
276 | wxDataOutputStream::wxDataOutputStream(wxOutputStream& s) | |
277 | : m_output(&s), m_be_order(FALSE) | |
278 | #endif | |
279 | { | |
280 | } | |
281 | ||
282 | wxDataOutputStream::~wxDataOutputStream() | |
283 | { | |
284 | } | |
285 | ||
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 | ||
297 | void wxDataOutputStream::Write32(wxUint32 i) | |
298 | { | |
299 | wxUint32 i32; | |
300 | ||
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); | |
306 | } | |
307 | ||
308 | void wxDataOutputStream::Write16(wxUint16 i) | |
309 | { | |
310 | wxUint16 i16; | |
311 | ||
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); | |
318 | } | |
319 | ||
320 | void wxDataOutputStream::Write8(wxUint8 i) | |
321 | { | |
322 | m_output->Write(&i, 1); | |
323 | } | |
324 | ||
325 | void wxDataOutputStream::WriteString(const wxString& string) | |
326 | { | |
327 | #if wxUSE_UNICODE | |
328 | const wxWX2MBbuf buf = string.mb_str(m_conv); | |
329 | #else | |
330 | const wxWX2MBbuf buf = string.mb_str(); | |
331 | #endif | |
332 | size_t len = strlen(buf); | |
333 | Write32(len); | |
334 | if (len > 0) | |
335 | m_output->Write(buf, len); | |
336 | } | |
337 | ||
338 | // Must be at global scope for VC++ 5 | |
339 | extern "C" void ConvertToIeeeExtended(double num, unsigned char *bytes); | |
340 | ||
341 | void wxDataOutputStream::WriteDouble(double d) | |
342 | { | |
343 | char buf[10]; | |
344 | ||
345 | #if wxUSE_APPLE_IEEE | |
346 | ConvertToIeeeExtended(d, (unsigned char *)buf); | |
347 | #else | |
348 | #ifndef __VMS__ | |
349 | # pragma warning "wxDataOutputStream::WriteDouble() not using IeeeExtended - will not work!" | |
350 | #endif | |
351 | buf[0] = '\0'; | |
352 | #endif | |
353 | m_output->Write(buf, 10); | |
354 | } | |
355 | ||
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 | ||
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 | ||
442 | wxDataOutputStream& wxDataOutputStream::operator<<(const wxString& string) | |
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 | ||
484 | wxDataOutputStream& wxDataOutputStream::operator<<(wxUint64 i) | |
485 | { | |
486 | Write64(i); | |
487 | return *this; | |
488 | } | |
489 | ||
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; | |
500 | } | |
501 | ||
502 | #endif | |
503 | // wxUSE_STREAMS | |
504 |