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