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