]> git.saurik.com Git - wxWidgets.git/blame_incremental - 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
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$
8// Copyright: (c) Guilhem Lavaux
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__
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
32wxDataInputStream::wxDataInputStream(wxInputStream& s, wxMBConv& conv)
33 : m_input(&s), m_be_order(FALSE), m_conv(conv)
34#else
35wxDataInputStream::wxDataInputStream(wxInputStream& s)
36 : m_input(&s), m_be_order(FALSE)
37#endif
38{
39}
40
41wxDataInputStream::~wxDataInputStream()
42{
43}
44
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
57wxUint32 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
69wxUint16 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
81wxUint8 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
90extern "C" double ConvertFromIeeeExtended(const unsigned char *bytes);
91
92double 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
104wxString wxDataInputStream::ReadString()
105{
106 size_t len;
107
108 len = Read32();
109
110 if (len > 0)
111 {
112#if wxUSE_UNICODE
113 char *tmp = new char[len + 1];
114 m_input->Read(tmp, len);
115 tmp[len] = 0;
116 wxString s(tmp, m_conv);
117 delete[] tmp;
118#else
119 wxString s;
120 m_input->Read(s.GetWriteBuf(len), len);
121 s.UngetWriteBuf();
122#endif
123 return s;
124 }
125 else
126 return wxEmptyString;
127}
128
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
171wxDataInputStream& wxDataInputStream::operator>>(wxUint64& i)
172{
173 i = Read64();
174 return *this;
175}
176
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}
188
189// ---------------------------------------------------------------------------
190// wxDataOutputStream
191// ---------------------------------------------------------------------------
192
193#if wxUSE_UNICODE
194wxDataOutputStream::wxDataOutputStream(wxOutputStream& s, wxMBConv& conv)
195 : m_output(&s), m_be_order(FALSE), m_conv(conv)
196#else
197wxDataOutputStream::wxDataOutputStream(wxOutputStream& s)
198 : m_output(&s), m_be_order(FALSE)
199#endif
200{
201}
202
203wxDataOutputStream::~wxDataOutputStream()
204{
205}
206
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
218void wxDataOutputStream::Write32(wxUint32 i)
219{
220 wxUint32 i32;
221
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);
227}
228
229void wxDataOutputStream::Write16(wxUint16 i)
230{
231 wxUint16 i16;
232
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);
239}
240
241void wxDataOutputStream::Write8(wxUint8 i)
242{
243 m_output->Write(&i, 1);
244}
245
246void wxDataOutputStream::WriteString(const wxString& string)
247{
248#if wxUSE_UNICODE
249 const wxWX2MBbuf buf = string.mb_str(m_conv);
250#else
251 const wxWX2MBbuf buf = string.mb_str();
252#endif
253 size_t len = strlen(buf);
254 Write32(len);
255 if (len > 0)
256 m_output->Write(buf, len);
257}
258
259// Must be at global scope for VC++ 5
260extern "C" void ConvertToIeeeExtended(double num, unsigned char *bytes);
261
262void wxDataOutputStream::WriteDouble(double d)
263{
264 char buf[10];
265
266#if wxUSE_APPLE_IEEE
267 ConvertToIeeeExtended(d, (unsigned char *)buf);
268#else
269#ifndef __VMS__
270# pragma warning "wxDataOutputStream::WriteDouble() not using IeeeExtended - will not work!"
271#endif
272 buf[0] = '\0';
273#endif
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
284wxDataOutputStream& wxDataOutputStream::operator<<(const wxString& string)
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
326wxDataOutputStream& wxDataOutputStream::operator<<(wxUint64 i)
327{
328 Write64(i);
329 return *this;
330}
331
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;
342}
343
344#endif
345 // wxUSE_STREAMS
346