Some #include and #if wxUSE_XX things
[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 : wxFilterInputStream(s)
33 {
34 }
35
36 wxDataInputStream::~wxDataInputStream()
37 {
38 }
39
40 unsigned long wxDataInputStream::Read32()
41 {
42 char buf[4];
43
44 Read(buf, 4);
45
46 return (unsigned long)buf[0] |
47 ((unsigned long)buf[1] << 8) |
48 ((unsigned long)buf[2] << 16) |
49 ((unsigned long)buf[3] << 24);
50 }
51
52 unsigned short wxDataInputStream::Read16()
53 {
54 char buf[2];
55
56 Read(buf, 2);
57
58 return (unsigned short)buf[0] |
59 ((unsigned short)buf[1] << 8);
60 }
61
62 unsigned char wxDataInputStream::Read8()
63 {
64 char buf;
65
66 Read(&buf, 1);
67 return (unsigned char)buf;
68 }
69
70 // Must be at global scope for VC++ 5
71 extern "C" double ConvertFromIeeeExtended(const unsigned char *bytes);
72
73 double wxDataInputStream::ReadDouble()
74 {
75 #if wxUSE_APPLE_IEEE
76 char buf[10];
77
78 Read(buf, 10);
79 return ConvertFromIeeeExtended((unsigned char *)buf);
80 #else
81 return 0.0;
82 #endif
83 }
84
85 wxString wxDataInputStream::ReadLine()
86 {
87 char c, last_endl = 0;
88 bool end_line = FALSE;
89 wxString line;
90
91 while (!end_line) {
92 c = GetC();
93 switch (c) {
94 case '\n':
95 end_line = TRUE;
96 break;
97 case '\r':
98 last_endl = '\r';
99 break;
100 default:
101 if (last_endl == '\r') {
102 end_line = TRUE;
103 InputStreamBuffer()->WriteBack(c);
104 break;
105 }
106 line += c;
107 break;
108 }
109 }
110 return line;
111 }
112
113 wxString wxDataInputStream::ReadString()
114 {
115 wxString wx_string;
116 char *string;
117 unsigned long len;
118
119 len = Read32();
120 string = new char[len+1];
121
122 Read(string, len);
123
124 string[len] = 0;
125 wx_string = string;
126 delete string;
127
128 return wx_string;
129 }
130
131 // ---------------------------------------------------------------------------
132 // wxDataOutputStream
133 // ---------------------------------------------------------------------------
134
135 wxDataOutputStream::wxDataOutputStream(wxOutputStream& s)
136 : wxFilterOutputStream(s)
137 {
138 }
139
140 wxDataOutputStream::~wxDataOutputStream()
141 {
142 }
143
144 void wxDataOutputStream::Write32(unsigned long i)
145 {
146 char buf[4];
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 buf[0] = i & 0xff;
160 buf[1] = (i >> 8) & 0xff;
161 Write(buf, 2);
162 }
163
164 void wxDataOutputStream::Write8(unsigned char i)
165 {
166 Write(&i, 1);
167 }
168
169 void wxDataOutputStream::WriteLine(const wxString& line)
170 {
171 #ifdef __WXMSW__
172 wxString tmp_string = line + _T("\r\n");
173 #else
174 wxString tmp_string = line + _T('\n');
175 #endif
176
177 Write((const wxChar *) tmp_string, tmp_string.Length()*sizeof(wxChar));
178 }
179
180 void wxDataOutputStream::WriteString(const wxString& string)
181 {
182 Write32(string.Length());
183 Write((const wxChar *) string, string.Length()*sizeof(wxChar));
184 }
185
186 // Must be at global scope for VC++ 5
187 extern "C" void ConvertToIeeeExtended(double num, unsigned char *bytes);
188
189 void wxDataOutputStream::WriteDouble(double d)
190 {
191 char buf[10];
192
193 #if wxUSE_APPLE_IEEE
194 ConvertToIeeeExtended(d, (unsigned char *)buf);
195 #else
196 # pragma warning "wxDataOutputStream::WriteDouble() not using IeeeExtended - will not work!"
197 buf[0] = '\0';
198 #endif
199 Write(buf, 10);
200 }
201
202 #endif
203 // wxUSE_STREAMS
204