]>
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$ | |
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__ | |
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) |
fae05df5 | 32 | : m_input(&s) |
cf447356 | 33 | { |
cf447356 GL |
34 | } |
35 | ||
3d4c6a21 | 36 | wxDataInputStream::~wxDataInputStream() |
cf447356 | 37 | { |
cf447356 GL |
38 | } |
39 | ||
7b8bd818 | 40 | wxUint32 wxDataInputStream::Read32() |
cf447356 GL |
41 | { |
42 | char buf[4]; | |
43 | ||
fae05df5 | 44 | m_input->Read(buf, 4); |
cf447356 | 45 | |
7b8bd818 GL |
46 | return (wxUint32)buf[0] | |
47 | ((wxUint32)buf[1] << 8) | | |
48 | ((wxUint32)buf[2] << 16) | | |
49 | ((wxUint32)buf[3] << 24); | |
cf447356 GL |
50 | } |
51 | ||
7b8bd818 | 52 | wxUint16 wxDataInputStream::Read16() |
cf447356 GL |
53 | { |
54 | char buf[2]; | |
55 | ||
fae05df5 | 56 | m_input->Read(buf, 2); |
cf447356 | 57 | |
7b8bd818 GL |
58 | return (wxUint16)buf[0] | |
59 | ((wxUint16)buf[1] << 8); | |
cf447356 GL |
60 | } |
61 | ||
7b8bd818 | 62 | wxUint8 wxDataInputStream::Read8() |
cf447356 | 63 | { |
7b8bd818 | 64 | wxUint8 buf; |
cf447356 | 65 | |
fae05df5 | 66 | m_input->Read((char *)&buf, 1); |
7b8bd818 | 67 | return (wxUint8)buf; |
cf447356 GL |
68 | } |
69 | ||
5260b1c5 JS |
70 | // Must be at global scope for VC++ 5 |
71 | extern "C" double ConvertFromIeeeExtended(const unsigned char *bytes); | |
72 | ||
3d4c6a21 | 73 | double wxDataInputStream::ReadDouble() |
cf447356 | 74 | { |
47d67540 | 75 | #if wxUSE_APPLE_IEEE |
cf447356 GL |
76 | char buf[10]; |
77 | ||
fae05df5 | 78 | m_input->Read(buf, 10); |
cf447356 GL |
79 | return ConvertFromIeeeExtended((unsigned char *)buf); |
80 | #else | |
81 | return 0.0; | |
82 | #endif | |
83 | } | |
84 | ||
3d4c6a21 | 85 | wxString wxDataInputStream::ReadString() |
eafc087e GL |
86 | { |
87 | wxString wx_string; | |
88 | char *string; | |
89 | unsigned long len; | |
90 | ||
eafc087e GL |
91 | len = Read32(); |
92 | string = new char[len+1]; | |
93 | ||
fae05df5 | 94 | m_input->Read(string, len); |
eafc087e GL |
95 | |
96 | string[len] = 0; | |
97 | wx_string = string; | |
98 | delete string; | |
99 | ||
a3622daa | 100 | return wx_string; |
eafc087e | 101 | } |
fae05df5 GL |
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 | } | |
eafc087e | 156 | |
f4ada568 GL |
157 | // --------------------------------------------------------------------------- |
158 | // wxDataOutputStream | |
159 | // --------------------------------------------------------------------------- | |
160 | ||
3d4c6a21 | 161 | wxDataOutputStream::wxDataOutputStream(wxOutputStream& s) |
fae05df5 | 162 | : m_output(&s) |
3d4c6a21 GL |
163 | { |
164 | } | |
165 | ||
f0b07807 KB |
166 | wxDataOutputStream::~wxDataOutputStream() |
167 | { | |
168 | } | |
169 | ||
7b8bd818 | 170 | void wxDataOutputStream::Write32(wxUint32 i) |
cf447356 GL |
171 | { |
172 | char buf[4]; | |
173 | ||
cf447356 GL |
174 | buf[0] = i & 0xff; |
175 | buf[1] = (i >> 8) & 0xff; | |
176 | buf[2] = (i >> 16) & 0xff; | |
177 | buf[3] = (i >> 24) & 0xff; | |
fae05df5 | 178 | m_output->Write(buf, 4); |
cf447356 GL |
179 | } |
180 | ||
7b8bd818 | 181 | void wxDataOutputStream::Write16(wxUint16 i) |
cf447356 GL |
182 | { |
183 | char buf[2]; | |
184 | ||
cf447356 GL |
185 | buf[0] = i & 0xff; |
186 | buf[1] = (i >> 8) & 0xff; | |
fae05df5 | 187 | m_output->Write(buf, 2); |
cf447356 GL |
188 | } |
189 | ||
7b8bd818 | 190 | void wxDataOutputStream::Write8(wxUint8 i) |
cf447356 | 191 | { |
fae05df5 | 192 | m_output->Write(&i, 1); |
cf447356 GL |
193 | } |
194 | ||
3d4c6a21 | 195 | void wxDataOutputStream::WriteString(const wxString& string) |
eafc087e | 196 | { |
c980c992 | 197 | const wxWX2MBbuf buf = string.mb_str(); |
debe6624 | 198 | Write32(string.Length()); |
c980c992 | 199 | m_output->Write(buf, string.Len()); |
cf447356 GL |
200 | } |
201 | ||
5260b1c5 JS |
202 | // Must be at global scope for VC++ 5 |
203 | extern "C" void ConvertToIeeeExtended(double num, unsigned char *bytes); | |
204 | ||
3d4c6a21 | 205 | void wxDataOutputStream::WriteDouble(double d) |
cf447356 | 206 | { |
cf447356 GL |
207 | char buf[10]; |
208 | ||
47d67540 | 209 | #if wxUSE_APPLE_IEEE |
cf447356 | 210 | ConvertToIeeeExtended(d, (unsigned char *)buf); |
0e338ff9 | 211 | #else |
631f1bfe | 212 | # pragma warning "wxDataOutputStream::WriteDouble() not using IeeeExtended - will not work!" |
0e338ff9 KB |
213 | buf[0] = '\0'; |
214 | #endif | |
fae05df5 GL |
215 | m_output->Write(buf, 10); |
216 | } | |
217 | ||
218 | wxDataOutputStream& wxDataOutputStream::operator<<(const wxChar *string) | |
219 | { | |
220 | Write32(wxStrlen(string)); | |
221 | m_output->Write((const char *)string, wxStrlen(string)*sizeof(wxChar)); | |
222 | return *this; | |
223 | } | |
224 | ||
225 | wxDataOutputStream& wxDataOutputStream::operator<<(wxString& string) | |
226 | { | |
227 | WriteString(string); | |
228 | return *this; | |
229 | } | |
230 | ||
231 | wxDataOutputStream& wxDataOutputStream::operator<<(wxInt8 c) | |
232 | { | |
233 | Write8((wxUint8)c); | |
234 | return *this; | |
235 | } | |
236 | ||
237 | wxDataOutputStream& wxDataOutputStream::operator<<(wxInt16 i) | |
238 | { | |
239 | Write16((wxUint16)i); | |
240 | return *this; | |
241 | } | |
242 | ||
243 | wxDataOutputStream& wxDataOutputStream::operator<<(wxInt32 i) | |
244 | { | |
245 | Write32((wxUint32)i); | |
246 | return *this; | |
247 | } | |
248 | ||
249 | wxDataOutputStream& wxDataOutputStream::operator<<(wxUint8 c) | |
250 | { | |
251 | Write8(c); | |
252 | return *this; | |
253 | } | |
254 | ||
255 | wxDataOutputStream& wxDataOutputStream::operator<<(wxUint16 i) | |
256 | { | |
257 | Write16(i); | |
258 | return *this; | |
259 | } | |
260 | ||
261 | wxDataOutputStream& wxDataOutputStream::operator<<(wxUint32 i) | |
262 | { | |
263 | Write32(i); | |
264 | return *this; | |
265 | } | |
266 | ||
267 | wxDataOutputStream& wxDataOutputStream::operator<<(double f) | |
268 | { | |
269 | WriteDouble(f); | |
270 | return *this; | |
271 | } | |
272 | ||
273 | wxDataOutputStream& wxDataOutputStream::operator<<(float f) | |
274 | { | |
275 | WriteDouble((double)f); | |
276 | return *this; | |
cf447356 | 277 | } |
ce4169a4 RR |
278 | |
279 | #endif | |
280 | // wxUSE_STREAMS | |
b6bff301 | 281 |