]> git.saurik.com Git - wxWidgets.git/blame - src/common/datstrm.cpp
Updated makefiles for VisualAge C++ V3.0
[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
631f1bfe 217# pragma warning "wxDataOutputStream::WriteDouble() not using IeeeExtended - will not work!"
0e338ff9
KB
218 buf[0] = '\0';
219#endif
fae05df5
GL
220 m_output->Write(buf, 10);
221}
222
223wxDataOutputStream& wxDataOutputStream::operator<<(const wxChar *string)
224{
225 Write32(wxStrlen(string));
226 m_output->Write((const char *)string, wxStrlen(string)*sizeof(wxChar));
227 return *this;
228}
229
230wxDataOutputStream& wxDataOutputStream::operator<<(wxString& string)
231{
232 WriteString(string);
233 return *this;
234}
235
236wxDataOutputStream& wxDataOutputStream::operator<<(wxInt8 c)
237{
238 Write8((wxUint8)c);
239 return *this;
240}
241
242wxDataOutputStream& wxDataOutputStream::operator<<(wxInt16 i)
243{
244 Write16((wxUint16)i);
245 return *this;
246}
247
248wxDataOutputStream& wxDataOutputStream::operator<<(wxInt32 i)
249{
250 Write32((wxUint32)i);
251 return *this;
252}
253
254wxDataOutputStream& wxDataOutputStream::operator<<(wxUint8 c)
255{
256 Write8(c);
257 return *this;
258}
259
260wxDataOutputStream& wxDataOutputStream::operator<<(wxUint16 i)
261{
262 Write16(i);
263 return *this;
264}
265
266wxDataOutputStream& wxDataOutputStream::operator<<(wxUint32 i)
267{
268 Write32(i);
269 return *this;
270}
271
272wxDataOutputStream& wxDataOutputStream::operator<<(double f)
273{
274 WriteDouble(f);
275 return *this;
276}
277
278wxDataOutputStream& wxDataOutputStream::operator<<(float f)
279{
280 WriteDouble((double)f);
281 return *this;
cf447356 282}
ce4169a4
RR
283
284#endif
285 // wxUSE_STREAMS
b6bff301 286