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