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