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