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