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