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