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