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