Added wxStream but I haven't tested them.
[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 #ifndef WX_PRECOMP
24 #include "wx/defs.h"
25 #endif
26
27 #include "wx/datstrm.h"
28
29 wxDataInputStream::wxDataInputStream(wxInputStream& s)
30 : wxFilterInputStream(s)
31 {
32 }
33
34 wxDataInputStream::~wxDataInputStream()
35 {
36 }
37
38 unsigned long wxDataInputStream::Read32()
39 {
40 char buf[4];
41
42 if (!m_istream)
43 return 0;
44
45 Read(buf, 4);
46
47 return (unsigned long)buf[0] |
48 ((unsigned long)buf[1] << 8) |
49 ((unsigned long)buf[2] << 16) |
50 ((unsigned long)buf[3] << 24);
51 }
52
53 unsigned short wxDataInputStream::Read16()
54 {
55 char buf[2];
56
57 if (!m_istream)
58 return 0;
59
60 Read(buf, 2);
61
62 return (unsigned short)buf[0] |
63 ((unsigned short)buf[1] << 8);
64 }
65
66 unsigned char wxDataInputStream::Read8()
67 {
68 char buf;
69
70 if (!m_istream)
71 return 0;
72
73 Read(&buf, 1);
74 return (unsigned char)buf;
75 }
76
77 // Must be at global scope for VC++ 5
78 extern "C" double ConvertFromIeeeExtended(const unsigned char *bytes);
79
80 double wxDataInputStream::ReadDouble()
81 {
82 #if USE_APPLE_IEEE
83 char buf[10];
84
85 if (!m_istream)
86 return 0.0;
87
88 Read(buf, 10);
89 return ConvertFromIeeeExtended((unsigned char *)buf);
90 #else
91 return 0.0;
92 #endif
93 }
94
95 wxString wxDataInputStream::ReadLine()
96 {
97 char i_strg[255];
98
99 if (!m_istream)
100 return "";
101
102 // TODO: Implement ReadLine
103 return i_strg;
104 }
105
106 wxString wxDataInputStream::ReadString()
107 {
108 wxString wx_string;
109 char *string;
110 unsigned long len;
111
112 if (!m_istream)
113 return "";
114
115 len = Read32();
116 string = new char[len+1];
117
118 Read(string, len);
119
120 string[len] = 0;
121 wx_string = string;
122 delete string;
123
124 return wx_string;
125 }
126
127 wxDataOutputStream::wxDataOutputStream(wxOutputStream& s)
128 : wxFilterOutputStream(s)
129 {
130 }
131
132 void wxDataOutputStream::Write32(unsigned long i)
133 {
134 char buf[4];
135
136 if (!m_ostream)
137 return;
138
139 buf[0] = i & 0xff;
140 buf[1] = (i >> 8) & 0xff;
141 buf[2] = (i >> 16) & 0xff;
142 buf[3] = (i >> 24) & 0xff;
143 Write(buf, 4);
144 }
145
146 void wxDataOutputStream::Write16(unsigned short i)
147 {
148 char buf[2];
149
150 if (!m_ostream)
151 return;
152
153 buf[0] = i & 0xff;
154 buf[1] = (i >> 8) & 0xff;
155 Write(buf, 2);
156 }
157
158 void wxDataOutputStream::Write8(unsigned char i)
159 {
160 if (!m_ostream)
161 return;
162
163 Write(&i, 1);
164 }
165
166 void wxDataOutputStream::WriteLine(const wxString& line)
167 {
168 #ifdef __WXMSW__
169 wxString tmp_string = line + "\r\n";
170 #else
171 wxString tmp_string = line + '\n';
172 #endif
173
174 if (!m_ostream)
175 return;
176
177 Write((const char *) tmp_string, tmp_string.Length());
178 }
179
180 void wxDataOutputStream::WriteString(const wxString& string)
181 {
182 if (!m_ostream)
183 return;
184
185 Write32(string.Length());
186 Write((const char *) string, string.Length());
187 }
188
189 // Must be at global scope for VC++ 5
190 extern "C" void ConvertToIeeeExtended(double num, unsigned char *bytes);
191
192 void wxDataOutputStream::WriteDouble(double d)
193 {
194 char buf[10];
195
196 if (!m_ostream)
197 return;
198
199 #if USE_APPLE_IEEE
200 ConvertToIeeeExtended(d, (unsigned char *)buf);
201 #else
202 # pragma warning "wxDataStream::WriteDouble() not using IeeeExtended - will not work!"
203 buf[0] = '\0';
204 #endif
205 Write(buf, 10);
206 }