]> git.saurik.com Git - wxWidgets.git/blob - src/common/datstrm.cpp
Some more bug reports
[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 wxUint32 wxDataInputStream::Read32()
41 {
42 wxUint32 i32;
43
44 m_input->Read(&i32, 4);
45
46 if (m_be_order)
47 return wxUINT32_SWAP_ON_LE(i32);
48 else
49 return wxUINT32_SWAP_ON_BE(i32);
50 }
51
52 wxUint16 wxDataInputStream::Read16()
53 {
54 wxUint16 i16;
55
56 m_input->Read(&i16, 2);
57
58 if (m_be_order)
59 return wxUINT16_SWAP_ON_LE(i16);
60 else
61 return wxUINT16_SWAP_ON_BE(i16);
62 }
63
64 wxUint8 wxDataInputStream::Read8()
65 {
66 wxUint8 buf;
67
68 m_input->Read(&buf, 1);
69 return (wxUint8)buf;
70 }
71
72 // Must be at global scope for VC++ 5
73 extern "C" double ConvertFromIeeeExtended(const unsigned char *bytes);
74
75 double wxDataInputStream::ReadDouble()
76 {
77 #if wxUSE_APPLE_IEEE
78 char buf[10];
79
80 m_input->Read(buf, 10);
81 return ConvertFromIeeeExtended((unsigned char *)buf);
82 #else
83 return 0.0;
84 #endif
85 }
86
87 wxString wxDataInputStream::ReadString()
88 {
89 wxString s;
90 size_t len;
91
92 len = Read32();
93
94 m_input->Read(s.GetWriteBuf(len), len);
95 s.UngetWriteBuf();
96
97 return s;
98 }
99
100 wxDataInputStream& wxDataInputStream::operator>>(wxString& s)
101 {
102 s = ReadString();
103 return *this;
104 }
105
106 wxDataInputStream& wxDataInputStream::operator>>(wxInt8& c)
107 {
108 c = (wxInt8)Read8();
109 return *this;
110 }
111
112 wxDataInputStream& wxDataInputStream::operator>>(wxInt16& i)
113 {
114 i = (wxInt16)Read16();
115 return *this;
116 }
117
118 wxDataInputStream& wxDataInputStream::operator>>(wxInt32& i)
119 {
120 i = (wxInt32)Read32();
121 return *this;
122 }
123
124 wxDataInputStream& wxDataInputStream::operator>>(wxUint8& c)
125 {
126 c = Read8();
127 return *this;
128 }
129
130 wxDataInputStream& wxDataInputStream::operator>>(wxUint16& i)
131 {
132 i = Read16();
133 return *this;
134 }
135
136 wxDataInputStream& wxDataInputStream::operator>>(wxUint32& i)
137 {
138 i = Read32();
139 return *this;
140 }
141
142 wxDataInputStream& wxDataInputStream::operator>>(double& i)
143 {
144 i = ReadDouble();
145 return *this;
146 }
147
148 wxDataInputStream& wxDataInputStream::operator>>(float& f)
149 {
150 f = (float)ReadDouble();
151 return *this;
152 }
153
154 // ---------------------------------------------------------------------------
155 // wxDataOutputStream
156 // ---------------------------------------------------------------------------
157
158 wxDataOutputStream::wxDataOutputStream(wxOutputStream& s)
159 : m_output(&s), m_be_order(FALSE)
160 {
161 }
162
163 wxDataOutputStream::~wxDataOutputStream()
164 {
165 }
166
167 void wxDataOutputStream::Write32(wxUint32 i)
168 {
169 wxUint32 i32;
170
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);
176 }
177
178 void wxDataOutputStream::Write16(wxUint16 i)
179 {
180 wxUint16 i16;
181
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);
188 }
189
190 void wxDataOutputStream::Write8(wxUint8 i)
191 {
192 m_output->Write(&i, 1);
193 }
194
195 void wxDataOutputStream::WriteString(const wxString& string)
196 {
197 const wxWX2MBbuf buf = string.mb_str();
198 Write32(string.Length());
199 m_output->Write(buf, string.Len());
200 }
201
202 // Must be at global scope for VC++ 5
203 extern "C" void ConvertToIeeeExtended(double num, unsigned char *bytes);
204
205 void wxDataOutputStream::WriteDouble(double d)
206 {
207 char buf[10];
208
209 #if wxUSE_APPLE_IEEE
210 ConvertToIeeeExtended(d, (unsigned char *)buf);
211 #else
212 #ifndef __VMS__
213 # pragma warning "wxDataOutputStream::WriteDouble() not using IeeeExtended - will not work!"
214 #endif
215 buf[0] = '\0';
216 #endif
217 m_output->Write(buf, 10);
218 }
219
220 wxDataOutputStream& 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
227 wxDataOutputStream& wxDataOutputStream::operator<<(wxString& string)
228 {
229 WriteString(string);
230 return *this;
231 }
232
233 wxDataOutputStream& wxDataOutputStream::operator<<(wxInt8 c)
234 {
235 Write8((wxUint8)c);
236 return *this;
237 }
238
239 wxDataOutputStream& wxDataOutputStream::operator<<(wxInt16 i)
240 {
241 Write16((wxUint16)i);
242 return *this;
243 }
244
245 wxDataOutputStream& wxDataOutputStream::operator<<(wxInt32 i)
246 {
247 Write32((wxUint32)i);
248 return *this;
249 }
250
251 wxDataOutputStream& wxDataOutputStream::operator<<(wxUint8 c)
252 {
253 Write8(c);
254 return *this;
255 }
256
257 wxDataOutputStream& wxDataOutputStream::operator<<(wxUint16 i)
258 {
259 Write16(i);
260 return *this;
261 }
262
263 wxDataOutputStream& wxDataOutputStream::operator<<(wxUint32 i)
264 {
265 Write32(i);
266 return *this;
267 }
268
269 wxDataOutputStream& wxDataOutputStream::operator<<(double f)
270 {
271 WriteDouble(f);
272 return *this;
273 }
274
275 wxDataOutputStream& wxDataOutputStream::operator<<(float f)
276 {
277 WriteDouble((double)f);
278 return *this;
279 }
280
281 #endif
282 // wxUSE_STREAMS
283