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