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