]> git.saurik.com Git - wxWidgets.git/blame - src/common/datstrm.cpp
latest sources from M
[wxWidgets.git] / src / common / datstrm.cpp
CommitLineData
cf447356
GL
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
29wxDataStream::wxDataStream(istream& s)
30{
31 m_istream = &s;
32 m_ostream = NULL;
33}
34
35wxDataStream::wxDataStream(ostream& s)
36{
37 m_istream = NULL;
38 m_ostream = &s;
39}
40
41wxDataStream::wxDataStream(iostream& s)
42{
43 m_istream = &s;
44 m_ostream = &s;
45}
46
47wxDataStream::~wxDataStream()
48{
49}
50
51unsigned 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
66unsigned 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
79unsigned 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
5260b1c5
JS
90// Must be at global scope for VC++ 5
91extern "C" double ConvertFromIeeeExtended(const unsigned char *bytes);
92
cf447356
GL
93double wxDataStream::ReadDouble()
94{
cf447356
GL
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
108wxString 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
eafc087e
GL
119wxString 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
cf447356
GL
140void 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
154void 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
166void wxDataStream::Write8(unsigned char i)
167{
168 if (!m_ostream)
169 return;
170
171 m_ostream->write(&i, 1);
172}
173
174void wxDataStream::WriteLine(const wxString& line)
175{
2049ba38 176#ifdef __WXMSW__
eafc087e
GL
177 wxString tmp_string = line + "\r\n";
178#else
cf447356 179 wxString tmp_string = line + '\n';
eafc087e
GL
180#endif
181
182 if (!m_ostream)
183 return;
cf447356 184
eafc087e
GL
185 m_ostream->write((const char *) tmp_string, tmp_string.Length());
186}
187
188void wxDataStream::WriteString(const wxString& string)
189{
cf447356
GL
190 if (!m_ostream)
191 return;
192
debe6624
JS
193 Write32(string.Length());
194 m_ostream->write((const char *) string, string.Length());
cf447356
GL
195}
196
5260b1c5
JS
197// Must be at global scope for VC++ 5
198extern "C" void ConvertToIeeeExtended(double num, unsigned char *bytes);
199
cf447356
GL
200void wxDataStream::WriteDouble(double d)
201{
cf447356
GL
202 char buf[10];
203
204 if (!m_ostream)
205 return;
206
0e338ff9 207#if USE_APPLE_IEEE
cf447356 208 ConvertToIeeeExtended(d, (unsigned char *)buf);
0e338ff9
KB
209#else
210# pragma warning "wxDataStream::WriteDouble() not using IeeeExtended - will not work!"
211 buf[0] = '\0';
212#endif
cf447356
GL
213 m_ostream->write(buf, 10);
214}