* Changed "true" to "TRUE" in some file: "true" doesn't exist in BC++ 5
[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 double wxDataStream::ReadDouble()
91 {
92 extern "C" double ConvertFromIeeeExtended(const unsigned char *bytes);
93
94 #if USE_APPLE_IEEE
95 char buf[10];
96
97 if (!m_istream)
98 return 0.0;
99
100 m_istream->read(buf, 10);
101 return ConvertFromIeeeExtended((unsigned char *)buf);
102 #else
103 return 0.0;
104 #endif
105 }
106
107 wxString wxDataStream::ReadLine()
108 {
109 char i_strg[255];
110
111 if (!m_istream)
112 return "";
113
114 m_istream->getline(i_strg, 255);
115 return i_strg;
116 }
117
118 void wxDataStream::Write32(unsigned long i)
119 {
120 char buf[4];
121
122 if (!m_ostream)
123 return;
124
125 buf[0] = i & 0xff;
126 buf[1] = (i >> 8) & 0xff;
127 buf[2] = (i >> 16) & 0xff;
128 buf[3] = (i >> 24) & 0xff;
129 m_ostream->write(buf, 4);
130 }
131
132 void wxDataStream::Write16(unsigned short i)
133 {
134 char buf[2];
135
136 if (!m_ostream)
137 return;
138
139 buf[0] = i & 0xff;
140 buf[1] = (i >> 8) & 0xff;
141 m_ostream->write(buf, 2);
142 }
143
144 void wxDataStream::Write8(unsigned char i)
145 {
146 if (!m_ostream)
147 return;
148
149 m_ostream->write(&i, 1);
150 }
151
152 void wxDataStream::WriteLine(const wxString& line)
153 {
154 wxString tmp_string = line + '\n';
155
156 if (!m_ostream)
157 return;
158
159 m_ostream->write((const char *) tmp_string, tmp_string.Length());
160 }
161
162 void wxDataStream::WriteDouble(double d)
163 {
164 extern "C" void ConvertToIeeeExtended(double num, unsigned char *bytes)
165 char buf[10];
166
167 if (!m_ostream)
168 return;
169
170 ConvertToIeeeExtended(d, (unsigned char *)buf);
171 m_ostream->write(buf, 10);
172 }