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