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