]> git.saurik.com Git - wxWidgets.git/blob - src/common/datstrm.cpp
Whole lotta stuff for wxPlotWindow,
[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 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;
101 m_input->Read(s.GetWriteBuf(len), len);
102 s.UngetWriteBuf();
103 #endif
104
105 return s;
106 }
107
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 }
161
162 // ---------------------------------------------------------------------------
163 // wxDataOutputStream
164 // ---------------------------------------------------------------------------
165
166 wxDataOutputStream::wxDataOutputStream(wxOutputStream& s)
167 : m_output(&s), m_be_order(FALSE)
168 {
169 }
170
171 wxDataOutputStream::~wxDataOutputStream()
172 {
173 }
174
175 void wxDataOutputStream::Write32(wxUint32 i)
176 {
177 wxUint32 i32;
178
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);
184 }
185
186 void wxDataOutputStream::Write16(wxUint16 i)
187 {
188 wxUint16 i16;
189
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);
196 }
197
198 void wxDataOutputStream::Write8(wxUint8 i)
199 {
200 m_output->Write(&i, 1);
201 }
202
203 void wxDataOutputStream::WriteString(const wxString& string)
204 {
205 const wxWX2MBbuf buf = string.mb_str();
206 Write32(string.Length());
207 m_output->Write(buf, string.Len());
208 }
209
210 // Must be at global scope for VC++ 5
211 extern "C" void ConvertToIeeeExtended(double num, unsigned char *bytes);
212
213 void wxDataOutputStream::WriteDouble(double d)
214 {
215 char buf[10];
216
217 #if wxUSE_APPLE_IEEE
218 ConvertToIeeeExtended(d, (unsigned char *)buf);
219 #else
220 #ifndef __VMS__
221 # pragma warning "wxDataOutputStream::WriteDouble() not using IeeeExtended - will not work!"
222 #endif
223 buf[0] = '\0';
224 #endif
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;
287 }
288
289 #endif
290 // wxUSE_STREAMS
291