Various changes for 16-bit compilation
[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 void wxDataStream::Write32(unsigned long i)
120 {
121 char buf[4];
122
123 if (!m_ostream)
124 return;
125
126 buf[0] = i & 0xff;
127 buf[1] = (i >> 8) & 0xff;
128 buf[2] = (i >> 16) & 0xff;
129 buf[3] = (i >> 24) & 0xff;
130 m_ostream->write(buf, 4);
131 }
132
133 void wxDataStream::Write16(unsigned short i)
134 {
135 char buf[2];
136
137 if (!m_ostream)
138 return;
139
140 buf[0] = i & 0xff;
141 buf[1] = (i >> 8) & 0xff;
142 m_ostream->write(buf, 2);
143 }
144
145 void wxDataStream::Write8(unsigned char i)
146 {
147 if (!m_ostream)
148 return;
149
150 m_ostream->write(&i, 1);
151 }
152
153 void wxDataStream::WriteLine(const wxString& line)
154 {
155 wxString tmp_string = line + '\n';
156
157 if (!m_ostream)
158 return;
159
160 m_ostream->write((const char *) tmp_string, tmp_string.Length());
161 }
162
163 // Must be at global scope for VC++ 5
164 extern "C" void ConvertToIeeeExtended(double num, unsigned char *bytes);
165
166 void wxDataStream::WriteDouble(double d)
167 {
168 char buf[10];
169
170 if (!m_ostream)
171 return;
172
173 ConvertToIeeeExtended(d, (unsigned char *)buf);
174 m_ostream->write(buf, 10);
175 }