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