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