]> git.saurik.com Git - wxWidgets.git/blob - src/common/txtstrm.cpp
3cb2c5bb5fe88f2a166c5940b9ee69ec27fa38c4
[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, const wxChar &sep)
42 : m_input(&s), m_string_separator(sep)
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 != wxT('\n') &&
59 c != wxT('\r') &&
60 c != wxT('\t') &&
61 c != wxT(' '))
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 == wxT('\n'))
74 {
75 // eat on UNIX
76 return;
77 }
78
79 if (c == wxT('\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 == wxT('\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 == wxT('-') || c == wxT('+') || isdigit(c)) )
115 {
116 m_input->Ungetch(c);
117 return 0;
118 }
119
120 if (c == wxT('-'))
121 {
122 sign = -1;
123 c = m_input->GetC();
124 } else
125 if (c == wxT('+'))
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)wxT('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 == wxT('.') || c == wxT('-') || c == wxT('+') || isdigit(c)) )
168 {
169 m_input->Ungetch(c);
170 return 0.0;
171 }
172
173 if (c == wxT('-'))
174 {
175 sign = -1;
176 c = m_input->GetC();
177 } else
178 if (c == wxT('+'))
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 - wxT('0'));
191 c = m_input->GetC();
192 }
193
194 if (c == wxT('.'))
195 {
196 double f_multiplicator = (double) 0.1;
197
198 c = m_input->GetC();
199
200 while (isdigit(c))
201 {
202 f += (c-wxT('0'))*f_multiplicator;
203 f_multiplicator /= 10;
204 c = m_input->GetC();
205 }
206
207 if (c == wxT('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 wxT('-'): f_multiplicator = 0.1; break;
217 case wxT('+'): 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 return ReadLine();
241 }
242
243 wxString wxTextInputStream::ReadLine()
244 {
245 wxChar c;
246 wxString line;
247
248 for (;;)
249 {
250 c = m_input->GetC();
251 if (!m_input) break;
252
253 if (c == wxT('\n'))
254 {
255 // eat on UNIX
256 break;
257 }
258
259 if (c == wxT('\r'))
260 {
261 // eat on both Mac and DOS
262
263 wxChar c2 = m_input->GetC();
264 if (!m_input) break;
265
266 if (c2 == wxT('\n'))
267 {
268 // eat on DOS
269 break;
270 }
271 else
272 {
273 // Don't eat on Mac
274 m_input->Ungetch( c2 );
275 break;
276 }
277 }
278
279 line += c;
280 }
281
282 return line;
283 }
284
285 wxString wxTextInputStream::ReadWord()
286 {
287 wxChar c;
288 wxString word;
289
290 if (m_string_separator==wxT(' ')) c=NextNonWhiteSpace();
291 else c = m_input->GetC();
292
293 for (;;)
294 {
295 if (!m_input) break;
296
297 if (c == m_string_separator)
298 break;
299
300 if (c == wxT('\n'))
301 {
302 // eat on UNIX
303 break;
304 }
305
306 if (c == wxT('\r'))
307 {
308 // eat on both Mac and DOS
309
310 wxChar c2 = m_input->GetC();
311 if (!m_input) break;
312
313 if (c2 == wxT('\n'))
314 {
315 // eat on DOS
316 break;
317 }
318 else
319 {
320 // Don't eat on Mac
321 m_input->Ungetch( c2 );
322 break;
323 }
324 }
325
326 word += c;
327 c = m_input->GetC();
328 }
329
330 return word;
331 }
332
333 wxTextInputStream& wxTextInputStream::operator>>(wxString& word)
334 {
335 word = ReadWord();
336 return *this;
337 }
338
339 wxTextInputStream& wxTextInputStream::operator>>(wxChar& c)
340 {
341 wxChar c1 = m_input->GetC();
342 if (!m_input)
343 {
344 c = (wxChar) 0;
345 return *this;
346 }
347
348 if (c1 == wxT('\r'))
349 {
350 c = wxT('\n');
351 wxChar c2 = m_input->GetC();
352 if (!m_input) return *this;
353
354 if (c2 != wxT('\n'))
355 {
356 // we are on a Mac
357 m_input->Ungetch( c2 );
358 }
359 }
360 else
361 {
362 c = c1;
363 }
364
365 return *this;
366 }
367
368 wxTextInputStream& wxTextInputStream::operator>>(wxInt16& i)
369 {
370 i = (wxInt16)Read16();
371 return *this;
372 }
373
374 wxTextInputStream& wxTextInputStream::operator>>(wxInt32& i)
375 {
376 i = (wxInt32)Read32();
377 return *this;
378 }
379
380 wxTextInputStream& wxTextInputStream::operator>>(wxUint16& i)
381 {
382 i = Read16();
383 return *this;
384 }
385
386 wxTextInputStream& wxTextInputStream::operator>>(wxUint32& i)
387 {
388 i = Read32();
389 return *this;
390 }
391
392 wxTextInputStream& wxTextInputStream::operator>>(double& i)
393 {
394 i = ReadDouble();
395 return *this;
396 }
397
398 wxTextInputStream& wxTextInputStream::operator>>(float& f)
399 {
400 f = (float)ReadDouble();
401 return *this;
402 }
403
404 wxTextOutputStream::wxTextOutputStream(wxOutputStream& s)
405 : m_output(&s)
406 {
407 }
408
409 wxTextOutputStream::~wxTextOutputStream()
410 {
411 }
412
413 void wxTextOutputStream::Write32(wxUint32 i)
414 {
415 wxString str;
416 str.Printf(wxT("%u"), i);
417
418 WriteString(str);
419 }
420
421 void wxTextOutputStream::Write16(wxUint16 i)
422 {
423 wxString str;
424 str.Printf(wxT("%u"), i);
425
426 WriteString(str);
427 }
428
429 void wxTextOutputStream::Write8(wxUint8 i)
430 {
431 wxString str;
432 str.Printf(wxT("%u"), i);
433
434 WriteString(str);
435 }
436
437 void wxTextOutputStream::WriteDouble(double d)
438 {
439 wxString str;
440
441 str.Printf(wxT("%f"), d);
442 WriteString(str);
443 }
444
445 void wxTextOutputStream::WriteString(const wxString& string)
446 {
447 for (size_t i = 0; i < string.Len(); i++)
448 {
449 wxChar c = string[i];
450 if (c == wxT('\n'))
451 {
452 #if defined(__WINDOWS__)
453 c = wxT('\r');
454 m_output->Write( (const void*)(&c), sizeof(wxChar) );
455 c = wxT('\n');
456 m_output->Write( (const void*)(&c), sizeof(wxChar) );
457 #elif defined(__UNIX__)
458 c = wxT('\n');
459 m_output->Write( (const void*)(&c), sizeof(wxChar) );
460 #elif defined(__WXMAC__)
461 c = wxT('\r');
462 m_output->Write( (const void*)(&c), sizeof(wxChar) );
463 #elif defined(__OS2__)
464 c = wxT('\r');
465 m_output->Write( (const void*)(&c), sizeof(wxChar) );
466 c = wxT('\n');
467 m_output->Write( (const void*)(&c), sizeof(wxChar) );
468 #else
469 #error "wxTextOutputStream: unsupported platform."
470 #endif
471 }
472 else
473 {
474 m_output->Write( (const void*)(&c), sizeof(wxChar) );
475 }
476 }
477 }
478
479 wxTextOutputStream& wxTextOutputStream::operator<<(const wxChar *string)
480 {
481 WriteString( wxString(string) );
482 return *this;
483 }
484
485 wxTextOutputStream& wxTextOutputStream::operator<<(const wxString& string)
486 {
487 WriteString( string );
488 return *this;
489 }
490
491 wxTextOutputStream& wxTextOutputStream::operator<<(wxChar c)
492 {
493 WriteString( wxString(c) );
494 return *this;
495 }
496
497 wxTextOutputStream& wxTextOutputStream::operator<<(wxInt16 c)
498 {
499 Write16( (wxUint16)c );
500 return *this;
501 }
502
503 wxTextOutputStream& wxTextOutputStream::operator<<(wxInt32 c)
504 {
505 Write32( (wxUint32)c );
506 return *this;
507 }
508
509 wxTextOutputStream& wxTextOutputStream::operator<<(wxUint16 c)
510 {
511 Write16(c);
512 return *this;
513 }
514
515 wxTextOutputStream& wxTextOutputStream::operator<<(wxUint32 c)
516 {
517 Write32(c);
518 return *this;
519 }
520
521 wxTextOutputStream &wxTextOutputStream::operator<<(double f)
522 {
523 WriteDouble(f);
524 return *this;
525 }
526
527 wxTextOutputStream& wxTextOutputStream::operator<<(float f)
528 {
529 WriteDouble((double)f);
530 return *this;
531 }
532
533 wxTextOutputStream &endl( wxTextOutputStream &stream )
534 {
535 return stream << wxT('\n');
536 }
537
538 #endif
539 // wxUSE_STREAMS