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