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