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