]> git.saurik.com Git - wxWidgets.git/blob - src/common/txtstrm.cpp
cliet data crash fixed
[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
29 // ----------------------------------------------------------------------------
30 // constants
31 // ----------------------------------------------------------------------------
32
33 // Unix: "\n"
34 // Dos: "\r\n"
35 // Mac: "\r"
36
37 // ----------------------------------------------------------------------------
38 // wxTextInputStream
39 // ----------------------------------------------------------------------------
40
41 wxTextInputStream::wxTextInputStream(wxInputStream& s)
42 : m_input(&s)
43 {
44 }
45
46 wxTextInputStream::~wxTextInputStream()
47 {
48 }
49
50 wxChar wxTextInputStream::NextNonWhiteSpace()
51 {
52 wxChar c = (wxChar) 0;
53 for (;;)
54 {
55 c = m_input->GetC();
56 if (!m_input) return (wxChar) 0;
57
58 if (c != _T('\n') &&
59 c != _T('\r') &&
60 c != _T('\t') &&
61 c != _T(' '))
62 {
63 return c;
64 }
65 }
66
67 // this shouldn't happen
68 return (wxChar) 0;
69 }
70
71 void wxTextInputStream::SkipIfEndOfLine( wxChar c )
72 {
73 if (c == _T('\n'))
74 {
75 // eat on UNIX
76 return;
77 }
78
79 if (c == _T('\r'))
80 {
81 // eat on both Mac and DOS
82
83 wxChar c2 = m_input->GetC();
84 if (!m_input) return;
85
86 if (c2 == _T('\n'))
87 {
88 // eat on DOS
89 return;
90 }
91 else
92 {
93 // Don't eat on Mac
94 m_input->Ungetch( c2 );
95 }
96 }
97 else
98 {
99 // no line terminator
100 m_input->Ungetch( c );
101 }
102 }
103
104 wxUint32 wxTextInputStream::Read32()
105 {
106 /* I only implemented a simple integer parser */
107 int sign;
108 wxInt32 i;
109
110 int c = NextNonWhiteSpace();
111 if (!m_input) return 0;
112
113 i = 0;
114 if (! (c == _T('-') || c == _T('+') || isdigit(c)) )
115 {
116 m_input->Ungetch(c);
117 return 0;
118 }
119
120 if (c == _T('-'))
121 {
122 sign = -1;
123 c = m_input->GetC();
124 } else
125 if (c == _T('+'))
126 {
127 sign = 1;
128 c = m_input->GetC();
129 } else
130 {
131 sign = 1;
132 }
133
134 while (isdigit(c))
135 {
136 i = i*10 + (c - (int)_T('0'));
137 c = m_input->GetC();
138 }
139
140 SkipIfEndOfLine( c );
141
142 i *= sign;
143
144 return (wxUint32)i;
145 }
146
147 wxUint16 wxTextInputStream::Read16()
148 {
149 return (wxUint16)Read32();
150 }
151
152 wxUint8 wxTextInputStream::Read8()
153 {
154 return (wxUint8)Read32();
155 }
156
157 double wxTextInputStream::ReadDouble()
158 {
159 /* I only implemented a simple float parser */
160 double f;
161 int sign;
162
163 int c = NextNonWhiteSpace();
164 if (!m_input) return 0.0;
165
166 f = 0.0;
167 if (! (c == _T('.') || c == _T('-') || c == _T('+') || isdigit(c)) )
168 {
169 m_input->Ungetch(c);
170 return 0.0;
171 }
172
173 if (c == _T('-'))
174 {
175 sign = -1;
176 c = m_input->GetC();
177 } else
178 if (c == _T('+'))
179 {
180 sign = 1;
181 c = m_input->GetC();
182 }
183 else
184 {
185 sign = 1;
186 }
187
188 while (isdigit(c))
189 {
190 f = f*10 + (c - _T('0'));
191 c = m_input->GetC();
192 }
193
194 if (c == _T('.'))
195 {
196 double f_multiplicator = (double) 0.1;
197
198 c = m_input->GetC();
199
200 while (isdigit(c))
201 {
202 f += (c-_T('0'))*f_multiplicator;
203 f_multiplicator /= 10;
204 c = m_input->GetC();
205 }
206
207 if (c == _T('e'))
208 {
209 double f_multiplicator = 0.0;
210 int i, e;
211
212 c = m_input->GetC();
213
214 switch (c)
215 {
216 case _T('-'): f_multiplicator = 0.1; break;
217 case _T('+'): f_multiplicator = 10.0; break;
218 }
219
220 e = Read8(); // why only max 256 ?
221
222 for (i=0;i<e;i++)
223 f *= f_multiplicator;
224 }
225 else
226 SkipIfEndOfLine( c );
227 }
228 else
229 {
230 m_input->Ungetch(c);
231 }
232
233 f *= sign;
234
235 return f;
236 }
237
238 wxString wxTextInputStream::ReadString()
239 {
240 wxChar c;
241 wxString line;
242
243 for (;;)
244 {
245 c = m_input->GetC();
246 if (!m_input) break;
247
248 if (c == _T('\n'))
249 {
250 // eat on UNIX
251 break;
252 }
253
254 if (c == _T('\r'))
255 {
256 // eat on both Mac and DOS
257
258 wxChar c2 = m_input->GetC();
259 if (!m_input) break;
260
261 if (c2 == _T('\n'))
262 {
263 // eat on DOS
264 break;
265 }
266 else
267 {
268 // Don't eat on Mac
269 m_input->Ungetch( c2 );
270 break;
271 }
272 }
273
274 line += c;
275 }
276
277 return line;
278 }
279
280 wxTextInputStream& wxTextInputStream::operator>>(wxString& line)
281 {
282 line = ReadString();
283 return *this;
284 }
285
286 wxTextInputStream& wxTextInputStream::operator>>(wxChar& c)
287 {
288 wxChar c1 = m_input->GetC();
289 if (!m_input)
290 {
291 c = (wxChar) 0;
292 return *this;
293 }
294
295 if (c1 == _T('\r'))
296 {
297 c = _T('\n');
298 wxChar c2 = m_input->GetC();
299 if (!m_input) return *this;
300
301 if (c2 != _T('\n'))
302 {
303 // we are on a Mac
304 m_input->Ungetch( c2 );
305 }
306 }
307 else
308 {
309 c = c1;
310 }
311
312 return *this;
313 }
314
315 wxTextInputStream& wxTextInputStream::operator>>(wxInt16& i)
316 {
317 i = (wxInt16)Read16();
318 return *this;
319 }
320
321 wxTextInputStream& wxTextInputStream::operator>>(wxInt32& i)
322 {
323 i = (wxInt32)Read32();
324 return *this;
325 }
326
327 wxTextInputStream& wxTextInputStream::operator>>(wxUint16& i)
328 {
329 i = Read16();
330 return *this;
331 }
332
333 wxTextInputStream& wxTextInputStream::operator>>(wxUint32& i)
334 {
335 i = Read32();
336 return *this;
337 }
338
339 wxTextInputStream& wxTextInputStream::operator>>(double& i)
340 {
341 i = ReadDouble();
342 return *this;
343 }
344
345 wxTextInputStream& wxTextInputStream::operator>>(float& f)
346 {
347 f = (float)ReadDouble();
348 return *this;
349 }
350
351 wxTextOutputStream::wxTextOutputStream(wxOutputStream& s)
352 : m_output(&s)
353 {
354 }
355
356 wxTextOutputStream::~wxTextOutputStream()
357 {
358 }
359
360 void wxTextOutputStream::Write32(wxUint32 i)
361 {
362 wxString str;
363 str.Printf(_T("%u"), i);
364
365 WriteString(str);
366 }
367
368 void wxTextOutputStream::Write16(wxUint16 i)
369 {
370 wxString str;
371 str.Printf(_T("%u"), i);
372
373 WriteString(str);
374 }
375
376 void wxTextOutputStream::Write8(wxUint8 i)
377 {
378 wxString str;
379 str.Printf(_T("%u"), i);
380
381 WriteString(str);
382 }
383
384 void wxTextOutputStream::WriteDouble(double d)
385 {
386 wxString str;
387
388 str.Printf(_T("%f"), d);
389 WriteString(str);
390 }
391
392 void wxTextOutputStream::WriteString(const wxString& string)
393 {
394 for (size_t i = 0; i < string.Len(); i++)
395 {
396 wxChar c = string[i];
397 if (c == _T('\n'))
398 {
399 #if defined(__WINDOWS__)
400 c = _T('\r');
401 m_output->Write( (const void*)(&c), sizeof(wxChar) );
402 c = _T('\n');
403 m_output->Write( (const void*)(&c), sizeof(wxChar) );
404 #elif defined(__UNIX__)
405 c = _T('\n');
406 m_output->Write( (const void*)(&c), sizeof(wxChar) );
407 #elif defined(__WXMAC__)
408 c = _T('\r');
409 m_output->Write( (const void*)(&c), sizeof(wxChar) );
410 #elif defined(__OS2__)
411 c = _T('\r');
412 m_output->Write( (const void*)(&c), sizeof(wxChar) );
413 c = _T('\n');
414 m_output->Write( (const void*)(&c), sizeof(wxChar) );
415 #else
416 #error "wxTextOutputStream: unsupported platform."
417 #endif
418 }
419 else
420 {
421 m_output->Write( (const void*)(&c), sizeof(wxChar) );
422 }
423 }
424 }
425
426 wxTextOutputStream& wxTextOutputStream::operator<<(const wxChar *string)
427 {
428 WriteString( wxString(string) );
429 return *this;
430 }
431
432 wxTextOutputStream& wxTextOutputStream::operator<<(const wxString& string)
433 {
434 WriteString( string );
435 return *this;
436 }
437
438 wxTextOutputStream& wxTextOutputStream::operator<<(wxChar c)
439 {
440 WriteString( wxString(c) );
441 return *this;
442 }
443
444 wxTextOutputStream& wxTextOutputStream::operator<<(wxInt16 c)
445 {
446 Write16( (wxUint16)c );
447 return *this;
448 }
449
450 wxTextOutputStream& wxTextOutputStream::operator<<(wxInt32 c)
451 {
452 Write32( (wxUint32)c );
453 return *this;
454 }
455
456 wxTextOutputStream& wxTextOutputStream::operator<<(wxUint16 c)
457 {
458 Write16(c);
459 return *this;
460 }
461
462 wxTextOutputStream& wxTextOutputStream::operator<<(wxUint32 c)
463 {
464 Write32(c);
465 return *this;
466 }
467
468 wxTextOutputStream &wxTextOutputStream::operator<<(double f)
469 {
470 WriteDouble(f);
471 return *this;
472 }
473
474 wxTextOutputStream& wxTextOutputStream::operator<<(float f)
475 {
476 WriteDouble((double)f);
477 return *this;
478 }
479
480 #endif
481 // wxUSE_STREAMS