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