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