]> git.saurik.com Git - wxWidgets.git/blob - src/common/txtstrm.cpp
* wxMemoryStreams updates
[wxWidgets.git] / src / common / txtstrm.cpp
1 /////////////////////////////////////////////////////////////////////////////////
2 // Name: txtstrm.cpp
3 // Purpose: Text 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 "txtstrm.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/txtstrm.h"
26 #include <ctype.h>
27
28 wxTextInputStream::wxTextInputStream(wxInputStream& s)
29 : m_input(&s)
30 {
31 }
32
33 wxTextInputStream::~wxTextInputStream()
34 {
35 }
36
37 wxUint32 wxTextInputStream::Read32()
38 {
39 /* I only implemented a simple integer parser */
40 int c;
41 int sign;
42 wxInt32 i;
43
44 while (isspace( c = m_input->GetC() ) )
45 /* Do nothing */ ;
46
47 i = 0;
48 if (! (c == '-' || isdigit(c)) ) {
49 m_input->Ungetch(c);
50 return 0;
51 }
52
53 if (c == '-') {
54 sign = -1;
55 c = m_input->GetC();
56 } else if (c == '+') {
57 sign = 1;
58 c = m_input->GetC();
59 } else {
60 sign = 1;
61 }
62
63 while (isdigit(c)) {
64 i = i*10 + (c - (int)'0');
65 c = m_input->GetC();
66 }
67
68 if (c != '\n' && c != '\r')
69 m_input->Ungetch(c);
70
71 i *= sign;
72
73 return (wxUint32)i;
74 }
75
76 wxUint16 wxTextInputStream::Read16()
77 {
78 return (wxUint16)Read32();
79 }
80
81 wxUint8 wxTextInputStream::Read8()
82 {
83 return (wxUint8)Read32();
84 }
85
86 double wxTextInputStream::ReadDouble()
87 {
88 /* I only implemented a simple float parser */
89 double f;
90 int c, sign;
91
92 while (isspace( c = m_input->GetC() ) || c == '\n' || c == '\r')
93 /* Do nothing */ ;
94
95 f = 0.0;
96 if (! (c == '-' || isdigit(c)) ) {
97 m_input->Ungetch(c);
98 return 0.0;
99 }
100
101 if (c == '-') {
102 sign = -1;
103 c = m_input->GetC();
104 } else if (c == '+') {
105 sign = 1;
106 c = m_input->GetC();
107 } else {
108 sign = 1;
109 }
110
111 while (isdigit(c)) {
112 f = f*10 + (c - '0');
113 c = m_input->GetC();
114 }
115
116 if (c == '.') {
117 double f_multiplicator = (double) 0.1;
118
119 c = m_input->GetC();
120
121 while (isdigit(c)) {
122 f += (c-'0')*f_multiplicator;
123 f_multiplicator /= 10;
124 c = m_input->GetC();
125 }
126
127 if (c == 'e') {
128 double f_multiplicator = 0.0;
129 int i, e;
130
131 c = m_input->GetC();
132
133 switch(c) {
134 case '-':
135 f_multiplicator = 0.1;
136 break;
137 case '+':
138 f_multiplicator = 10.0;
139 break;
140 }
141
142 e = Read8();
143
144 for (i=0;i<e;i++)
145 f *= f_multiplicator;
146 } else if (c != '\n' && c != '\r')
147 m_input->Ungetch(c);
148
149 }
150
151 f *= sign;
152
153 return f;
154 }
155
156 wxString wxTextInputStream::ReadString()
157 {
158 char c, last_endl = 0;
159 bool end_line = FALSE;
160 wxString line;
161
162 while (!end_line) {
163 c = m_input->GetC();
164 if (m_input->LastError() != wxStream_NOERROR)
165 break;
166
167 switch (c) {
168 case '\n':
169 end_line = TRUE;
170 break;
171 case '\r':
172 last_endl = '\r';
173 break;
174 default:
175 if (last_endl == '\r') {
176 end_line = TRUE;
177 m_input->Ungetch(c);
178 break;
179 }
180 line += c;
181 break;
182 }
183 }
184 return line;
185 }
186
187 wxTextInputStream& wxTextInputStream::operator>>(wxString& line)
188 {
189 line = ReadString();
190 return *this;
191 }
192
193 wxTextInputStream& wxTextInputStream::operator>>(wxChar& c)
194 {
195 // TODO
196 /*
197 m_input->Read(&c, sizeof(wxChar));
198 */
199 return *this;
200 }
201
202 wxTextInputStream& wxTextInputStream::operator>>(wxInt16& i)
203 {
204 i = (wxInt16)Read16();
205 return *this;
206 }
207
208 wxTextInputStream& wxTextInputStream::operator>>(wxInt32& i)
209 {
210 i = (wxInt32)Read32();
211 return *this;
212 }
213
214 wxTextInputStream& wxTextInputStream::operator>>(wxUint16& i)
215 {
216 i = Read16();
217 return *this;
218 }
219
220 wxTextInputStream& wxTextInputStream::operator>>(wxUint32& i)
221 {
222 i = Read32();
223 return *this;
224 }
225
226 wxTextInputStream& wxTextInputStream::operator>>(double& i)
227 {
228 i = ReadDouble();
229 return *this;
230 }
231
232 wxTextInputStream& wxTextInputStream::operator>>(float& f)
233 {
234 f = (float)ReadDouble();
235 return *this;
236 }
237
238 wxTextOutputStream::wxTextOutputStream(wxOutputStream& s)
239 : m_output(&s)
240 {
241 }
242
243 wxTextOutputStream::~wxTextOutputStream()
244 {
245 }
246
247 void wxTextOutputStream::Write32(wxUint32 i)
248 {
249 wxString str;
250
251 str.Printf(_T("%u"), i);
252 WriteString(str);
253 }
254
255 void wxTextOutputStream::Write16(wxUint16 i)
256 {
257 wxString str;
258
259 str.Printf(_T("%u"), i);
260 WriteString(str);
261 }
262
263 void wxTextOutputStream::Write8(wxUint8 i)
264 {
265 wxString str;
266
267 str.Printf(_T("%u"), i);
268 WriteString(str);
269 }
270
271 void wxTextOutputStream::WriteDouble(double d)
272 {
273 wxString str;
274
275 str.Printf(_T("%f"), d);
276 WriteString(str);
277 }
278
279 void wxTextOutputStream::WriteString(const wxString& string)
280 {
281 #if wxUSE_UNICODE
282 const wxWX2MBbuf buf = string.mb_str();
283 m_output->Write(buf, string.Len());
284 #else
285 m_output->Write(string, string.Len());
286 #endif
287 }
288
289 wxTextOutputStream& wxTextOutputStream::operator<<(const wxChar *string)
290 {
291 WriteString(wxString(string));
292 return *this;
293 }
294
295 wxTextOutputStream& wxTextOutputStream::operator<<(const wxString& string)
296 {
297 WriteString(string);
298 return *this;
299 }
300
301 wxTextOutputStream& wxTextOutputStream::operator<<(wxChar c)
302 {
303 wxString tmp_str;
304 tmp_str.Printf(_T("%c"), c);
305 WriteString(tmp_str);
306 return *this;
307 }
308
309 wxTextOutputStream& wxTextOutputStream::operator<<(wxInt16 c)
310 {
311 Write16((wxUint16)c);
312 return *this;
313 }
314
315 wxTextOutputStream& wxTextOutputStream::operator<<(wxInt32 c)
316 {
317 Write32((wxUint32)c);
318 return *this;
319 }
320
321 wxTextOutputStream& wxTextOutputStream::operator<<(wxUint16 c)
322 {
323 Write16(c);
324 return *this;
325 }
326
327 wxTextOutputStream& wxTextOutputStream::operator<<(wxUint32 c)
328 {
329 Write32(c);
330 return *this;
331 }
332
333 wxTextOutputStream &wxTextOutputStream::operator<<(double f)
334 {
335 WriteDouble(f);
336 return *this;
337 }
338
339 wxTextOutputStream& wxTextOutputStream::operator<<(float f)
340 {
341 WriteDouble((double)f);
342 return *this;
343 }
344
345 #endif
346 // wxUSE_STREAMS