]> git.saurik.com Git - wxWidgets.git/blame - src/common/datstrm.cpp
don't crash in wx(Flex)GridSizer with division by 0, assert instead; also factored...
[wxWidgets.git] / src / common / datstrm.cpp
CommitLineData
cf447356
GL
1/////////////////////////////////////////////////////////////////////////////
2// Name: datstrm.cpp
3// Purpose: Data stream classes
4// Author: Guilhem Lavaux
5// Modified by:
6// Created: 28/06/98
7// RCS-ID: $Id$
13111b2a 8// Copyright: (c) Guilhem Lavaux
cf447356
GL
9// Licence: wxWindows license
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__
ce4169a4 20 #pragma hdrstop
cf447356
GL
21#endif
22
ce4169a4
RR
23#if wxUSE_STREAMS
24
cf447356 25#include "wx/datstrm.h"
3cacae09 26
f4ada568
GL
27// ---------------------------------------------------------------------------
28// wxDataInputStream
29// ---------------------------------------------------------------------------
30
a99acbb0
VS
31#if wxUSE_UNICODE
32wxDataInputStream::wxDataInputStream(wxInputStream& s, wxMBConv& conv)
33 : m_input(&s), m_be_order(FALSE), m_conv(conv)
34#else
3d4c6a21 35wxDataInputStream::wxDataInputStream(wxInputStream& s)
5a96d2f4 36 : m_input(&s), m_be_order(FALSE)
a99acbb0 37#endif
cf447356 38{
cf447356
GL
39}
40
3d4c6a21 41wxDataInputStream::~wxDataInputStream()
cf447356 42{
cf447356
GL
43}
44
41b0a113
RL
45wxUint64 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
7b8bd818 57wxUint32 wxDataInputStream::Read32()
cf447356 58{
5a96d2f4 59 wxUint32 i32;
cf447356 60
5a96d2f4 61 m_input->Read(&i32, 4);
cf447356 62
5a96d2f4
GL
63 if (m_be_order)
64 return wxUINT32_SWAP_ON_LE(i32);
65 else
66 return wxUINT32_SWAP_ON_BE(i32);
cf447356
GL
67}
68
7b8bd818 69wxUint16 wxDataInputStream::Read16()
cf447356 70{
5a96d2f4 71 wxUint16 i16;
cf447356 72
5a96d2f4 73 m_input->Read(&i16, 2);
cf447356 74
5a96d2f4
GL
75 if (m_be_order)
76 return wxUINT16_SWAP_ON_LE(i16);
77 else
78 return wxUINT16_SWAP_ON_BE(i16);
cf447356
GL
79}
80
7b8bd818 81wxUint8 wxDataInputStream::Read8()
cf447356 82{
7b8bd818 83 wxUint8 buf;
cf447356 84
5a96d2f4 85 m_input->Read(&buf, 1);
7b8bd818 86 return (wxUint8)buf;
cf447356
GL
87}
88
5260b1c5
JS
89// Must be at global scope for VC++ 5
90extern "C" double ConvertFromIeeeExtended(const unsigned char *bytes);
91
3d4c6a21 92double wxDataInputStream::ReadDouble()
cf447356 93{
47d67540 94#if wxUSE_APPLE_IEEE
cf447356
GL
95 char buf[10];
96
fae05df5 97 m_input->Read(buf, 10);
cf447356
GL
98 return ConvertFromIeeeExtended((unsigned char *)buf);
99#else
100 return 0.0;
101#endif
102}
103
3d4c6a21 104wxString wxDataInputStream::ReadString()
eafc087e 105{
13111b2a 106 size_t len;
eafc087e 107
eafc087e 108 len = Read32();
eafc087e 109
39a16cb4
VS
110 if (len > 0)
111 {
2ae47e3f 112#if wxUSE_UNICODE
39a16cb4
VS
113 char *tmp = new char[len + 1];
114 m_input->Read(tmp, len);
115 tmp[len] = 0;
a99acbb0 116 wxString s(tmp, m_conv);
39a16cb4 117 delete[] tmp;
2ae47e3f 118#else
39a16cb4
VS
119 wxString s;
120 m_input->Read(s.GetWriteBuf(len), len);
121 s.UngetWriteBuf();
2ae47e3f 122#endif
39a16cb4
VS
123 return s;
124 }
38caaa61 125 else
39a16cb4 126 return wxEmptyString;
eafc087e 127}
13111b2a 128
fae05df5
GL
129wxDataInputStream& wxDataInputStream::operator>>(wxString& s)
130{
131 s = ReadString();
132 return *this;
133}
134
135wxDataInputStream& wxDataInputStream::operator>>(wxInt8& c)
136{
137 c = (wxInt8)Read8();
138 return *this;
139}
140
141wxDataInputStream& wxDataInputStream::operator>>(wxInt16& i)
142{
143 i = (wxInt16)Read16();
144 return *this;
145}
146
147wxDataInputStream& wxDataInputStream::operator>>(wxInt32& i)
148{
149 i = (wxInt32)Read32();
150 return *this;
151}
152
153wxDataInputStream& wxDataInputStream::operator>>(wxUint8& c)
154{
155 c = Read8();
156 return *this;
157}
158
159wxDataInputStream& wxDataInputStream::operator>>(wxUint16& i)
160{
161 i = Read16();
162 return *this;
163}
164
165wxDataInputStream& wxDataInputStream::operator>>(wxUint32& i)
166{
167 i = Read32();
168 return *this;
169}
170
41b0a113
RL
171wxDataInputStream& wxDataInputStream::operator>>(wxUint64& i)
172{
173 i = Read64();
174 return *this;
175}
176
fae05df5
GL
177wxDataInputStream& wxDataInputStream::operator>>(double& i)
178{
179 i = ReadDouble();
180 return *this;
181}
182
183wxDataInputStream& wxDataInputStream::operator>>(float& f)
184{
185 f = (float)ReadDouble();
186 return *this;
187}
eafc087e 188
f4ada568
GL
189// ---------------------------------------------------------------------------
190// wxDataOutputStream
191// ---------------------------------------------------------------------------
192
a99acbb0
VS
193#if wxUSE_UNICODE
194wxDataOutputStream::wxDataOutputStream(wxOutputStream& s, wxMBConv& conv)
195 : m_output(&s), m_be_order(FALSE), m_conv(conv)
196#else
3d4c6a21 197wxDataOutputStream::wxDataOutputStream(wxOutputStream& s)
7ff14117 198 : m_output(&s), m_be_order(FALSE)
a99acbb0 199#endif
3d4c6a21
GL
200{
201}
202
f0b07807
KB
203wxDataOutputStream::~wxDataOutputStream()
204{
205}
206
41b0a113
RL
207void wxDataOutputStream::Write64(wxUint64 i)
208{
209 wxUint64 i64;
210
211 if (m_be_order)
212 i64 = wxUINT64_SWAP_ON_LE(i);
213 else
214 i64 = wxUINT64_SWAP_ON_BE(i);
215 m_output->Write(&i64, 8);
216}
217
7b8bd818 218void wxDataOutputStream::Write32(wxUint32 i)
cf447356 219{
5a96d2f4 220 wxUint32 i32;
cf447356 221
5a96d2f4
GL
222 if (m_be_order)
223 i32 = wxUINT32_SWAP_ON_LE(i);
224 else
225 i32 = wxUINT32_SWAP_ON_BE(i);
226 m_output->Write(&i32, 4);
cf447356
GL
227}
228
7b8bd818 229void wxDataOutputStream::Write16(wxUint16 i)
cf447356 230{
5a96d2f4 231 wxUint16 i16;
cf447356 232
5a96d2f4
GL
233 if (m_be_order)
234 i16 = wxUINT16_SWAP_ON_LE(i);
235 else
236 i16 = wxUINT16_SWAP_ON_BE(i);
237
238 m_output->Write(&i16, 2);
cf447356
GL
239}
240
7b8bd818 241void wxDataOutputStream::Write8(wxUint8 i)
cf447356 242{
fae05df5 243 m_output->Write(&i, 1);
cf447356
GL
244}
245
3d4c6a21 246void wxDataOutputStream::WriteString(const wxString& string)
eafc087e 247{
a99acbb0
VS
248#if wxUSE_UNICODE
249 const wxWX2MBbuf buf = string.mb_str(m_conv);
250#else
c980c992 251 const wxWX2MBbuf buf = string.mb_str();
a99acbb0
VS
252#endif
253 size_t len = strlen(buf);
254 Write32(len);
255 if (len > 0)
256 m_output->Write(buf, len);
cf447356
GL
257}
258
5260b1c5
JS
259// Must be at global scope for VC++ 5
260extern "C" void ConvertToIeeeExtended(double num, unsigned char *bytes);
261
3d4c6a21 262void wxDataOutputStream::WriteDouble(double d)
cf447356 263{
cf447356
GL
264 char buf[10];
265
47d67540 266#if wxUSE_APPLE_IEEE
cf447356 267 ConvertToIeeeExtended(d, (unsigned char *)buf);
0e338ff9 268#else
338dd992
JJ
269#ifndef __VMS__
270# pragma warning "wxDataOutputStream::WriteDouble() not using IeeeExtended - will not work!"
271#endif
272 buf[0] = '\0';
0e338ff9 273#endif
fae05df5
GL
274 m_output->Write(buf, 10);
275}
276
277wxDataOutputStream& wxDataOutputStream::operator<<(const wxChar *string)
278{
279 Write32(wxStrlen(string));
280 m_output->Write((const char *)string, wxStrlen(string)*sizeof(wxChar));
281 return *this;
282}
283
38caaa61 284wxDataOutputStream& wxDataOutputStream::operator<<(const wxString& string)
fae05df5
GL
285{
286 WriteString(string);
287 return *this;
288}
289
290wxDataOutputStream& wxDataOutputStream::operator<<(wxInt8 c)
291{
292 Write8((wxUint8)c);
293 return *this;
294}
295
296wxDataOutputStream& wxDataOutputStream::operator<<(wxInt16 i)
297{
298 Write16((wxUint16)i);
299 return *this;
300}
301
302wxDataOutputStream& wxDataOutputStream::operator<<(wxInt32 i)
303{
304 Write32((wxUint32)i);
305 return *this;
306}
307
308wxDataOutputStream& wxDataOutputStream::operator<<(wxUint8 c)
309{
310 Write8(c);
311 return *this;
312}
313
314wxDataOutputStream& wxDataOutputStream::operator<<(wxUint16 i)
315{
316 Write16(i);
317 return *this;
318}
319
320wxDataOutputStream& wxDataOutputStream::operator<<(wxUint32 i)
321{
322 Write32(i);
323 return *this;
324}
325
41b0a113
RL
326wxDataOutputStream& wxDataOutputStream::operator<<(wxUint64 i)
327{
328 Write64(i);
329 return *this;
330}
331
fae05df5
GL
332wxDataOutputStream& wxDataOutputStream::operator<<(double f)
333{
334 WriteDouble(f);
335 return *this;
336}
337
338wxDataOutputStream& wxDataOutputStream::operator<<(float f)
339{
340 WriteDouble((double)f);
341 return *this;
cf447356 342}
ce4169a4
RR
343
344#endif
345 // wxUSE_STREAMS
13111b2a 346