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