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