* Moved ReadLine()/WriteLine() to wxIn/OutputStream
[wxWidgets.git] / src / common / datstrm.cpp
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 #if wxUSE_STREAMS
24
25 #include "wx/datstrm.h"
26
27 // ---------------------------------------------------------------------------
28 // wxDataInputStream
29 // ---------------------------------------------------------------------------
30
31 wxDataInputStream::wxDataInputStream(wxInputStream& s)
32 : wxFilterInputStream(s)
33 {
34 }
35
36 wxDataInputStream::~wxDataInputStream()
37 {
38 }
39
40 wxUint32 wxDataInputStream::Read32()
41 {
42 char buf[4];
43
44 Read(buf, 4);
45
46 return (wxUint32)buf[0] |
47 ((wxUint32)buf[1] << 8) |
48 ((wxUint32)buf[2] << 16) |
49 ((wxUint32)buf[3] << 24);
50 }
51
52 wxUint16 wxDataInputStream::Read16()
53 {
54 char buf[2];
55
56 Read(buf, 2);
57
58 return (wxUint16)buf[0] |
59 ((wxUint16)buf[1] << 8);
60 }
61
62 wxUint8 wxDataInputStream::Read8()
63 {
64 wxUint8 buf;
65
66 Read((char *)&buf, 1);
67 return (wxUint8)buf;
68 }
69
70 // Must be at global scope for VC++ 5
71 extern "C" double ConvertFromIeeeExtended(const unsigned char *bytes);
72
73 double wxDataInputStream::ReadDouble()
74 {
75 #if wxUSE_APPLE_IEEE
76 char buf[10];
77
78 Read(buf, 10);
79 return ConvertFromIeeeExtended((unsigned char *)buf);
80 #else
81 return 0.0;
82 #endif
83 }
84
85 wxString wxDataInputStream::ReadString()
86 {
87 wxString wx_string;
88 char *string;
89 unsigned long len;
90
91 len = Read32();
92 string = new char[len+1];
93
94 Read(string, len);
95
96 string[len] = 0;
97 wx_string = string;
98 delete string;
99
100 return wx_string;
101 }
102
103 // ---------------------------------------------------------------------------
104 // wxDataOutputStream
105 // ---------------------------------------------------------------------------
106
107 wxDataOutputStream::wxDataOutputStream(wxOutputStream& s)
108 : wxFilterOutputStream(s)
109 {
110 }
111
112 wxDataOutputStream::~wxDataOutputStream()
113 {
114 }
115
116 void wxDataOutputStream::Write32(wxUint32 i)
117 {
118 char buf[4];
119
120 buf[0] = i & 0xff;
121 buf[1] = (i >> 8) & 0xff;
122 buf[2] = (i >> 16) & 0xff;
123 buf[3] = (i >> 24) & 0xff;
124 Write(buf, 4);
125 }
126
127 void wxDataOutputStream::Write16(wxUint16 i)
128 {
129 char buf[2];
130
131 buf[0] = i & 0xff;
132 buf[1] = (i >> 8) & 0xff;
133 Write(buf, 2);
134 }
135
136 void wxDataOutputStream::Write8(wxUint8 i)
137 {
138 Write(&i, 1);
139 }
140
141 void wxDataOutputStream::WriteString(const wxString& string)
142 {
143 Write32(string.Length());
144 Write((const wxChar *) string, string.Length()*sizeof(wxChar));
145 }
146
147 // Must be at global scope for VC++ 5
148 extern "C" void ConvertToIeeeExtended(double num, unsigned char *bytes);
149
150 void wxDataOutputStream::WriteDouble(double d)
151 {
152 char buf[10];
153
154 #if wxUSE_APPLE_IEEE
155 ConvertToIeeeExtended(d, (unsigned char *)buf);
156 #else
157 # pragma warning "wxDataOutputStream::WriteDouble() not using IeeeExtended - will not work!"
158 buf[0] = '\0';
159 #endif
160 Write(buf, 10);
161 }
162
163 #endif
164 // wxUSE_STREAMS
165