]> git.saurik.com Git - wxWidgets.git/blame - src/common/txtstrm.cpp
#warning is a compilation error, not warning
[wxWidgets.git] / src / common / txtstrm.cpp
CommitLineData
5a96d2f4 1///////////////////////////////////////////////////////////////////////////////
fae05df5
GL
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"
c980c992 26#include <ctype.h>
fae05df5 27
cd25b18c
RR
28
29// ----------------------------------------------------------------------------
30// constants
31// ----------------------------------------------------------------------------
32
33// Unix: "\n"
34// Dos: "\r\n"
35// Mac: "\r"
36
37// ----------------------------------------------------------------------------
38// wxTextInputStream
39// ----------------------------------------------------------------------------
40
fae05df5
GL
41wxTextInputStream::wxTextInputStream(wxInputStream& s)
42 : m_input(&s)
43{
44}
45
46wxTextInputStream::~wxTextInputStream()
47{
48}
49
cd25b18c 50wxChar wxTextInputStream::NextNonWhiteSpace()
fae05df5 51{
cd25b18c
RR
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}
fae05df5 70
cd25b18c
RR
71void 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}
fae05df5 103
cd25b18c
RR
104wxUint32 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 }
fae05df5 119
cd25b18c
RR
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 }
fae05df5 133
cd25b18c
RR
134 while (isdigit(c))
135 {
136 i = i*10 + (c - (int)_T('0'));
137 c = m_input->GetC();
138 }
fae05df5 139
cd25b18c 140 SkipIfEndOfLine( c );
fae05df5 141
cd25b18c 142 i *= sign;
fae05df5 143
cd25b18c 144 return (wxUint32)i;
fae05df5
GL
145}
146
147wxUint16 wxTextInputStream::Read16()
148{
cd25b18c 149 return (wxUint16)Read32();
fae05df5
GL
150}
151
152wxUint8 wxTextInputStream::Read8()
153{
cd25b18c 154 return (wxUint8)Read32();
fae05df5
GL
155}
156
157double wxTextInputStream::ReadDouble()
158{
cd25b18c
RR
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();
fae05df5
GL
192 }
193
cd25b18c
RR
194 if (c == _T('.'))
195 {
196 double f_multiplicator = (double) 0.1;
fae05df5 197
cd25b18c 198 c = m_input->GetC();
fae05df5 199
cd25b18c
RR
200 while (isdigit(c))
201 {
202 f += (c-_T('0'))*f_multiplicator;
203 f_multiplicator /= 10;
204 c = m_input->GetC();
205 }
fae05df5 206
cd25b18c
RR
207 if (c == _T('e'))
208 {
209 double f_multiplicator = 0.0;
210 int i, e;
fae05df5 211
cd25b18c 212 c = m_input->GetC();
fae05df5 213
cd25b18c
RR
214 switch (c)
215 {
216 case _T('-'): f_multiplicator = 0.1; break;
217 case _T('+'): f_multiplicator = 10.0; break;
218 }
fae05df5 219
cd25b18c
RR
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;
fae05df5
GL
236}
237
238wxString wxTextInputStream::ReadString()
239{
cd25b18c
RR
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;
fae05df5
GL
278}
279
280wxTextInputStream& wxTextInputStream::operator>>(wxString& line)
281{
282 line = ReadString();
283 return *this;
284}
285
940ddb19 286wxTextInputStream& wxTextInputStream::operator>>(wxChar& c)
fae05df5 287{
cd25b18c
RR
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;
fae05df5
GL
313}
314
315wxTextInputStream& wxTextInputStream::operator>>(wxInt16& i)
316{
cd25b18c
RR
317 i = (wxInt16)Read16();
318 return *this;
fae05df5
GL
319}
320
321wxTextInputStream& wxTextInputStream::operator>>(wxInt32& i)
322{
cd25b18c
RR
323 i = (wxInt32)Read32();
324 return *this;
fae05df5
GL
325}
326
fae05df5
GL
327wxTextInputStream& wxTextInputStream::operator>>(wxUint16& i)
328{
cd25b18c
RR
329 i = Read16();
330 return *this;
fae05df5
GL
331}
332
333wxTextInputStream& wxTextInputStream::operator>>(wxUint32& i)
334{
cd25b18c
RR
335 i = Read32();
336 return *this;
fae05df5
GL
337}
338
339wxTextInputStream& wxTextInputStream::operator>>(double& i)
340{
cd25b18c
RR
341 i = ReadDouble();
342 return *this;
fae05df5
GL
343}
344
345wxTextInputStream& wxTextInputStream::operator>>(float& f)
346{
cd25b18c
RR
347 f = (float)ReadDouble();
348 return *this;
fae05df5
GL
349}
350
351wxTextOutputStream::wxTextOutputStream(wxOutputStream& s)
352 : m_output(&s)
353{
354}
355
356wxTextOutputStream::~wxTextOutputStream()
357{
358}
359
360void wxTextOutputStream::Write32(wxUint32 i)
361{
cd25b18c
RR
362 wxString str;
363 str.Printf(_T("%u"), i);
364
365 WriteString(str);
fae05df5
GL
366}
367
368void wxTextOutputStream::Write16(wxUint16 i)
369{
cd25b18c
RR
370 wxString str;
371 str.Printf(_T("%u"), i);
372
373 WriteString(str);
fae05df5
GL
374}
375
376void wxTextOutputStream::Write8(wxUint8 i)
377{
cd25b18c
RR
378 wxString str;
379 str.Printf(_T("%u"), i);
380
381 WriteString(str);
fae05df5
GL
382}
383
384void wxTextOutputStream::WriteDouble(double d)
385{
cd25b18c 386 wxString str;
fae05df5 387
cd25b18c
RR
388 str.Printf(_T("%f"), d);
389 WriteString(str);
fae05df5
GL
390}
391
392void wxTextOutputStream::WriteString(const wxString& string)
393{
cd25b18c
RR
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) );
fae05df5 410#else
cd25b18c 411 #error "wxTextOutputStream: unsupported platform."
fae05df5 412#endif
cd25b18c
RR
413 }
414 else
415 {
416 m_output->Write( (const void*)(&c), sizeof(wxChar) );
417 }
418 }
fae05df5
GL
419}
420
421wxTextOutputStream& wxTextOutputStream::operator<<(const wxChar *string)
422{
cd25b18c
RR
423 WriteString( wxString(string) );
424 return *this;
fae05df5
GL
425}
426
427wxTextOutputStream& wxTextOutputStream::operator<<(const wxString& string)
428{
cd25b18c
RR
429 WriteString( string );
430 return *this;
fae05df5
GL
431}
432
940ddb19 433wxTextOutputStream& wxTextOutputStream::operator<<(wxChar c)
fae05df5 434{
cd25b18c
RR
435 WriteString( wxString(c) );
436 return *this;
fae05df5
GL
437}
438
439wxTextOutputStream& wxTextOutputStream::operator<<(wxInt16 c)
440{
cd25b18c
RR
441 Write16( (wxUint16)c );
442 return *this;
fae05df5
GL
443}
444
445wxTextOutputStream& wxTextOutputStream::operator<<(wxInt32 c)
446{
cd25b18c
RR
447 Write32( (wxUint32)c );
448 return *this;
fae05df5
GL
449}
450
fae05df5
GL
451wxTextOutputStream& wxTextOutputStream::operator<<(wxUint16 c)
452{
cd25b18c
RR
453 Write16(c);
454 return *this;
fae05df5
GL
455}
456
457wxTextOutputStream& wxTextOutputStream::operator<<(wxUint32 c)
458{
cd25b18c
RR
459 Write32(c);
460 return *this;
fae05df5
GL
461}
462
463wxTextOutputStream &wxTextOutputStream::operator<<(double f)
464{
cd25b18c
RR
465 WriteDouble(f);
466 return *this;
fae05df5
GL
467}
468
469wxTextOutputStream& wxTextOutputStream::operator<<(float f)
470{
cd25b18c
RR
471 WriteDouble((double)f);
472 return *this;
fae05df5
GL
473}
474
475#endif
476 // wxUSE_STREAMS