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