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