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