]> git.saurik.com Git - wxWidgets.git/blob - src/common/datstrm.cpp
d83f968795578e4d6d070a71fe207ef8d7ddd325
[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 if (LastError() != wxStream_NOERROR)
94 break;
95
96 switch (c) {
97 case '\n':
98 end_line = TRUE;
99 break;
100 case '\r':
101 last_endl = '\r';
102 break;
103 default:
104 if (last_endl == '\r') {
105 end_line = TRUE;
106 InputStreamBuffer()->WriteBack(c);
107 break;
108 }
109 line += c;
110 break;
111 }
112 }
113 return line;
114 }
115
116 wxString wxDataInputStream::ReadString()
117 {
118 wxString wx_string;
119 char *string;
120 unsigned long len;
121
122 len = Read32();
123 string = new char[len+1];
124
125 Read(string, len);
126
127 string[len] = 0;
128 wx_string = string;
129 delete string;
130
131 return wx_string;
132 }
133
134 // ---------------------------------------------------------------------------
135 // wxDataOutputStream
136 // ---------------------------------------------------------------------------
137
138 wxDataOutputStream::wxDataOutputStream(wxOutputStream& s)
139 : wxFilterOutputStream(s)
140 {
141 }
142
143 wxDataOutputStream::~wxDataOutputStream()
144 {
145 }
146
147 void wxDataOutputStream::Write32(unsigned long i)
148 {
149 char buf[4];
150
151 buf[0] = i & 0xff;
152 buf[1] = (i >> 8) & 0xff;
153 buf[2] = (i >> 16) & 0xff;
154 buf[3] = (i >> 24) & 0xff;
155 Write(buf, 4);
156 }
157
158 void wxDataOutputStream::Write16(unsigned short i)
159 {
160 char buf[2];
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 Write(&i, 1);
170 }
171
172 void wxDataOutputStream::WriteLine(const wxString& line)
173 {
174 #ifdef __WXMSW__
175 wxString tmp_string = line + _T("\r\n");
176 #else
177 wxString tmp_string = line + _T('\n');
178 #endif
179
180 Write((const wxChar *) tmp_string, tmp_string.Length()*sizeof(wxChar));
181 }
182
183 void wxDataOutputStream::WriteString(const wxString& string)
184 {
185 Write32(string.Length());
186 Write((const wxChar *) string, string.Length()*sizeof(wxChar));
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 wxUSE_APPLE_IEEE
197 ConvertToIeeeExtended(d, (unsigned char *)buf);
198 #else
199 # pragma warning "wxDataOutputStream::WriteDouble() not using IeeeExtended - will not work!"
200 buf[0] = '\0';
201 #endif
202 Write(buf, 10);
203 }
204
205 #endif
206 // wxUSE_STREAMS
207