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