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