]> git.saurik.com Git - wxWidgets.git/blob - src/common/datstrm.cpp
install wxUniv headers in make install and include wxUniv sources in make dist
[wxWidgets.git] / src / common / datstrm.cpp
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__
20 #pragma hdrstop
21 #endif
22
23 #if wxUSE_STREAMS
24
25 #include "wx/datstrm.h"
26
27 // ---------------------------------------------------------------------------
28 // wxDataInputStream
29 // ---------------------------------------------------------------------------
30
31 wxDataInputStream::wxDataInputStream(wxInputStream& s)
32 : m_input(&s), m_be_order(FALSE)
33 {
34 }
35
36 wxDataInputStream::~wxDataInputStream()
37 {
38 }
39
40 wxUint64 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
52 wxUint32 wxDataInputStream::Read32()
53 {
54 wxUint32 i32;
55
56 m_input->Read(&i32, 4);
57
58 if (m_be_order)
59 return wxUINT32_SWAP_ON_LE(i32);
60 else
61 return wxUINT32_SWAP_ON_BE(i32);
62 }
63
64 wxUint16 wxDataInputStream::Read16()
65 {
66 wxUint16 i16;
67
68 m_input->Read(&i16, 2);
69
70 if (m_be_order)
71 return wxUINT16_SWAP_ON_LE(i16);
72 else
73 return wxUINT16_SWAP_ON_BE(i16);
74 }
75
76 wxUint8 wxDataInputStream::Read8()
77 {
78 wxUint8 buf;
79
80 m_input->Read(&buf, 1);
81 return (wxUint8)buf;
82 }
83
84 // Must be at global scope for VC++ 5
85 extern "C" double ConvertFromIeeeExtended(const unsigned char *bytes);
86
87 double wxDataInputStream::ReadDouble()
88 {
89 #if wxUSE_APPLE_IEEE
90 char buf[10];
91
92 m_input->Read(buf, 10);
93 return ConvertFromIeeeExtended((unsigned char *)buf);
94 #else
95 return 0.0;
96 #endif
97 }
98
99 wxString wxDataInputStream::ReadString()
100 {
101 size_t len;
102
103 len = Read32();
104
105 if (len > 0)
106 {
107 #if wxUSE_UNICODE
108 char *tmp = new char[len + 1];
109 m_input->Read(tmp, len);
110 tmp[len] = 0;
111 wxString s(tmp);
112 delete[] tmp;
113 #else
114 wxString s;
115 m_input->Read(s.GetWriteBuf(len), len);
116 s.UngetWriteBuf();
117 #endif
118 return s;
119 }
120 else
121 return wxEmptyString;
122 }
123
124 wxDataInputStream& wxDataInputStream::operator>>(wxString& s)
125 {
126 s = ReadString();
127 return *this;
128 }
129
130 wxDataInputStream& wxDataInputStream::operator>>(wxInt8& c)
131 {
132 c = (wxInt8)Read8();
133 return *this;
134 }
135
136 wxDataInputStream& wxDataInputStream::operator>>(wxInt16& i)
137 {
138 i = (wxInt16)Read16();
139 return *this;
140 }
141
142 wxDataInputStream& wxDataInputStream::operator>>(wxInt32& i)
143 {
144 i = (wxInt32)Read32();
145 return *this;
146 }
147
148 wxDataInputStream& wxDataInputStream::operator>>(wxUint8& c)
149 {
150 c = Read8();
151 return *this;
152 }
153
154 wxDataInputStream& wxDataInputStream::operator>>(wxUint16& i)
155 {
156 i = Read16();
157 return *this;
158 }
159
160 wxDataInputStream& wxDataInputStream::operator>>(wxUint32& i)
161 {
162 i = Read32();
163 return *this;
164 }
165
166 wxDataInputStream& wxDataInputStream::operator>>(wxUint64& i)
167 {
168 i = Read64();
169 return *this;
170 }
171
172 wxDataInputStream& wxDataInputStream::operator>>(double& i)
173 {
174 i = ReadDouble();
175 return *this;
176 }
177
178 wxDataInputStream& wxDataInputStream::operator>>(float& f)
179 {
180 f = (float)ReadDouble();
181 return *this;
182 }
183
184 // ---------------------------------------------------------------------------
185 // wxDataOutputStream
186 // ---------------------------------------------------------------------------
187
188 wxDataOutputStream::wxDataOutputStream(wxOutputStream& s)
189 : m_output(&s), m_be_order(FALSE)
190 {
191 }
192
193 wxDataOutputStream::~wxDataOutputStream()
194 {
195 }
196
197 void 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
208 void wxDataOutputStream::Write32(wxUint32 i)
209 {
210 wxUint32 i32;
211
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);
217 }
218
219 void wxDataOutputStream::Write16(wxUint16 i)
220 {
221 wxUint16 i16;
222
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);
229 }
230
231 void wxDataOutputStream::Write8(wxUint8 i)
232 {
233 m_output->Write(&i, 1);
234 }
235
236 void wxDataOutputStream::WriteString(const wxString& string)
237 {
238 const wxWX2MBbuf buf = string.mb_str();
239 Write32(string.Len());
240 if (string.Len() > 0)
241 m_output->Write(buf, string.Len());
242 }
243
244 // Must be at global scope for VC++ 5
245 extern "C" void ConvertToIeeeExtended(double num, unsigned char *bytes);
246
247 void wxDataOutputStream::WriteDouble(double d)
248 {
249 char buf[10];
250
251 #if wxUSE_APPLE_IEEE
252 ConvertToIeeeExtended(d, (unsigned char *)buf);
253 #else
254 #ifndef __VMS__
255 # pragma warning "wxDataOutputStream::WriteDouble() not using IeeeExtended - will not work!"
256 #endif
257 buf[0] = '\0';
258 #endif
259 m_output->Write(buf, 10);
260 }
261
262 wxDataOutputStream& 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
269 wxDataOutputStream& wxDataOutputStream::operator<<(const wxString& string)
270 {
271 WriteString(string);
272 return *this;
273 }
274
275 wxDataOutputStream& wxDataOutputStream::operator<<(wxInt8 c)
276 {
277 Write8((wxUint8)c);
278 return *this;
279 }
280
281 wxDataOutputStream& wxDataOutputStream::operator<<(wxInt16 i)
282 {
283 Write16((wxUint16)i);
284 return *this;
285 }
286
287 wxDataOutputStream& wxDataOutputStream::operator<<(wxInt32 i)
288 {
289 Write32((wxUint32)i);
290 return *this;
291 }
292
293 wxDataOutputStream& wxDataOutputStream::operator<<(wxUint8 c)
294 {
295 Write8(c);
296 return *this;
297 }
298
299 wxDataOutputStream& wxDataOutputStream::operator<<(wxUint16 i)
300 {
301 Write16(i);
302 return *this;
303 }
304
305 wxDataOutputStream& wxDataOutputStream::operator<<(wxUint32 i)
306 {
307 Write32(i);
308 return *this;
309 }
310
311 wxDataOutputStream& wxDataOutputStream::operator<<(wxUint64 i)
312 {
313 Write64(i);
314 return *this;
315 }
316
317 wxDataOutputStream& wxDataOutputStream::operator<<(double f)
318 {
319 WriteDouble(f);
320 return *this;
321 }
322
323 wxDataOutputStream& wxDataOutputStream::operator<<(float f)
324 {
325 WriteDouble((double)f);
326 return *this;
327 }
328
329 #endif
330 // wxUSE_STREAMS
331