]> git.saurik.com Git - wxWidgets.git/blame - src/common/txtstrm.cpp
in case of an faulty event this might not get initialized
[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
65571936 9// Licence: wxWindows licence
fae05df5
GL
10/////////////////////////////////////////////////////////////////////////////
11
fae05df5
GL
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
16 #pragma hdrstop
17#endif
18
19#if wxUSE_STREAMS
20
21#include "wx/txtstrm.h"
c980c992 22#include <ctype.h>
fae05df5 23
cd25b18c
RR
24
25// ----------------------------------------------------------------------------
26// constants
27// ----------------------------------------------------------------------------
28
29// Unix: "\n"
30// Dos: "\r\n"
31// Mac: "\r"
32
33// ----------------------------------------------------------------------------
34// wxTextInputStream
35// ----------------------------------------------------------------------------
36
2b5f62a0 37#if wxUSE_UNICODE
830f8f11
VZ
38wxTextInputStream::wxTextInputStream(wxInputStream &s,
39 const wxString &sep,
40 const wxMBConv& conv)
d36c9347 41 : m_input(s), m_separators(sep), m_conv(conv.Clone())
2b5f62a0 42{
2348a842 43 memset((void*)m_lastBytes, 0, 10);
2b5f62a0
VZ
44}
45#else
191549ed
SB
46wxTextInputStream::wxTextInputStream(wxInputStream &s, const wxString &sep)
47 : m_input(s), m_separators(sep)
fae05df5 48{
2348a842 49 memset((void*)m_lastBytes, 0, 10);
fae05df5 50}
2b5f62a0 51#endif
fae05df5
GL
52
53wxTextInputStream::~wxTextInputStream()
54{
d36c9347
VZ
55#if wxUSE_UNICODE
56 delete m_conv;
57#endif // wxUSE_UNICODE
fae05df5
GL
58}
59
2348a842
VZ
60void wxTextInputStream::UngetLast()
61{
62 size_t byteCount = 0;
63 while(m_lastBytes[byteCount]) // pseudo ANSI strlen (even for Unicode!)
64 byteCount++;
65 m_input.Ungetch(m_lastBytes, byteCount);
66 memset((void*)m_lastBytes, 0, 10);
67}
68
69wxChar wxTextInputStream::NextChar()
70{
71#if wxUSE_UNICODE
72 wxChar wbuf[2];
73 memset((void*)m_lastBytes, 0, 10);
cb719f2e 74 for(size_t inlen = 0; inlen < 9; inlen++)
2348a842
VZ
75 {
76 // actually read the next character
77 m_lastBytes[inlen] = m_input.GetC();
78
cb719f2e 79 if(m_input.LastRead() <= 0)
2348a842 80 return wxEOT;
cb719f2e 81
d7f73361
VZ
82 if ( m_conv->ToWChar(wbuf, WXSIZEOF(wbuf), m_lastBytes, inlen + 1)
83 != wxCONV_FAILED )
2348a842
VZ
84 return wbuf[0];
85 }
86 // there should be no encoding which requires more than nine bytes for one character...
87 return wxEOT;
88#else
89 m_lastBytes[0] = m_input.GetC();
cb719f2e
WS
90
91 if(m_input.LastRead() <= 0)
2348a842 92 return wxEOT;
cb719f2e 93
2348a842
VZ
94 return m_lastBytes[0];
95#endif
cb719f2e 96
2348a842
VZ
97}
98
191549ed 99wxChar wxTextInputStream::NextNonSeparators()
fae05df5 100{
cd25b18c
RR
101 for (;;)
102 {
999836aa 103 wxChar c = NextChar();
2348a842 104 if (c == wxEOT) return (wxChar) 0;
cd6ce4a9
VZ
105
106 if (c != wxT('\n') &&
107 c != wxT('\r') &&
108 !m_separators.Contains(c))
109 return c;
cd25b18c 110 }
717b9bf2 111
cd25b18c 112}
fae05df5 113
f6bcfd97 114bool wxTextInputStream::EatEOL(const wxChar &c)
cd25b18c 115{
cb719f2e 116 if (c == wxT('\n')) return true; // eat on UNIX
cd6ce4a9 117
f6bcfd97 118 if (c == wxT('\r')) // eat on both Mac and DOS
717b9bf2 119 {
2348a842 120 wxChar c2 = NextChar();
cb719f2e 121 if(c2 == wxEOT) return true; // end of stream reached, had enough :-)
cd6ce4a9 122
2348a842 123 if (c2 != wxT('\n')) UngetLast(); // Don't eat on Mac
cb719f2e 124 return true;
cd25b18c 125 }
717b9bf2 126
cb719f2e 127 return false;
191549ed
SB
128}
129
2348a842 130wxUint32 wxTextInputStream::Read32(int base)
191549ed 131{
2348a842
VZ
132 wxASSERT_MSG( !base || (base > 1 && base <= 36), _T("invalid base") );
133 if(!m_input) return 0;
134
135 wxString word = ReadWord();
7448de8d 136 if(word.empty())
2348a842
VZ
137 return 0;
138 return wxStrtoul(word.c_str(), 0, base);
cd25b18c 139}
fae05df5 140
2348a842 141wxUint16 wxTextInputStream::Read16(int base)
cd25b18c 142{
2348a842
VZ
143 return (wxUint16)Read32(base);
144}
cd6ce4a9 145
2348a842
VZ
146wxUint8 wxTextInputStream::Read8(int base)
147{
148 return (wxUint8)Read32(base);
149}
717b9bf2 150
2348a842
VZ
151wxInt32 wxTextInputStream::Read32S(int base)
152{
153 wxASSERT_MSG( !base || (base > 1 && base <= 36), _T("invalid base") );
154 if(!m_input) return 0;
cd25b18c 155
2348a842 156 wxString word = ReadWord();
7448de8d 157 if(word.empty())
cd25b18c 158 return 0;
2348a842 159 return wxStrtol(word.c_str(), 0, base);
fae05df5
GL
160}
161
2348a842 162wxInt16 wxTextInputStream::Read16S(int base)
fae05df5 163{
2348a842 164 return (wxInt16)Read32S(base);
fae05df5
GL
165}
166
2348a842 167wxInt8 wxTextInputStream::Read8S(int base)
fae05df5 168{
2348a842 169 return (wxInt8)Read32S(base);
fae05df5
GL
170}
171
172double wxTextInputStream::ReadDouble()
173{
2348a842
VZ
174 if(!m_input) return 0;
175 wxString word = ReadWord();
7448de8d 176 if(word.empty())
f6bcfd97 177 return 0;
2348a842 178 return wxStrtod(word.c_str(), 0);
fae05df5
GL
179}
180
181wxString wxTextInputStream::ReadString()
9853d977 182{
cd6ce4a9 183 return ReadLine();
9853d977
SB
184}
185
186wxString wxTextInputStream::ReadLine()
fae05df5 187{
cd25b18c
RR
188 wxString line;
189
cd6ce4a9 190 while ( !m_input.Eof() )
cd25b18c 191 {
2348a842
VZ
192 wxChar c = NextChar();
193 if(c == wxEOT)
194 break;
cb719f2e 195
cd6ce4a9
VZ
196 if ( !m_input )
197 break;
198
199 if (EatEOL(c))
200 break;
201
cd25b18c
RR
202 line += c;
203 }
717b9bf2 204
cd25b18c 205 return line;
fae05df5 206}
717b9bf2 207
9853d977
SB
208wxString wxTextInputStream::ReadWord()
209{
9853d977 210 wxString word;
9853d977 211
cd6ce4a9
VZ
212 if ( !m_input )
213 return word;
214
215 wxChar c = NextNonSeparators();
216 if ( !c )
217 return word;
218
f6bcfd97 219 word += c;
cb719f2e 220
cd6ce4a9 221 while ( !m_input.Eof() )
9853d977 222 {
2348a842
VZ
223 c = NextChar();
224 if(c == wxEOT)
f6bcfd97 225 break;
cb719f2e 226
cd6ce4a9
VZ
227 if (m_separators.Contains(c))
228 break;
229
230 if (EatEOL(c))
231 break;
232
9853d977 233 word += c;
9853d977
SB
234 }
235
236 return word;
237}
238
239wxTextInputStream& wxTextInputStream::operator>>(wxString& word)
fae05df5 240{
cd6ce4a9
VZ
241 word = ReadWord();
242 return *this;
fae05df5
GL
243}
244
f6bcfd97 245wxTextInputStream& wxTextInputStream::operator>>(char& c)
fae05df5 246{
191549ed 247 c = m_input.GetC();
2348a842 248 if(m_input.LastRead() <= 0) c = 0;
717b9bf2 249
f6bcfd97
BP
250 if (EatEOL(c))
251 c = '\n';
252
cd25b18c 253 return *this;
fae05df5
GL
254}
255
fcbe123e
VZ
256#if wxUSE_UNICODE && wxWCHAR_T_IS_REAL_TYPE
257
258wxTextInputStream& wxTextInputStream::operator>>(wchar_t& wc)
259{
260 wc = GetChar();
261
262 return *this;
263}
264
265#endif // wxUSE_UNICODE
266
fae05df5
GL
267wxTextInputStream& wxTextInputStream::operator>>(wxInt16& i)
268{
cd25b18c
RR
269 i = (wxInt16)Read16();
270 return *this;
fae05df5
GL
271}
272
273wxTextInputStream& wxTextInputStream::operator>>(wxInt32& i)
274{
cd25b18c
RR
275 i = (wxInt32)Read32();
276 return *this;
fae05df5
GL
277}
278
fae05df5
GL
279wxTextInputStream& wxTextInputStream::operator>>(wxUint16& i)
280{
cd25b18c
RR
281 i = Read16();
282 return *this;
fae05df5
GL
283}
284
285wxTextInputStream& wxTextInputStream::operator>>(wxUint32& i)
286{
cd25b18c
RR
287 i = Read32();
288 return *this;
fae05df5
GL
289}
290
291wxTextInputStream& wxTextInputStream::operator>>(double& i)
292{
cd25b18c
RR
293 i = ReadDouble();
294 return *this;
fae05df5
GL
295}
296
297wxTextInputStream& wxTextInputStream::operator>>(float& f)
298{
cd25b18c
RR
299 f = (float)ReadDouble();
300 return *this;
fae05df5
GL
301}
302
2b5f62a0
VZ
303
304
305#if wxUSE_UNICODE
830f8f11
VZ
306wxTextOutputStream::wxTextOutputStream(wxOutputStream& s,
307 wxEOL mode,
308 const wxMBConv& conv)
d36c9347 309 : m_output(s), m_conv(conv.Clone())
2b5f62a0 310#else
c7a9fa36 311wxTextOutputStream::wxTextOutputStream(wxOutputStream& s, wxEOL mode)
191549ed 312 : m_output(s)
2b5f62a0 313#endif
fae05df5 314{
c7a9fa36
RR
315 m_mode = mode;
316 if (m_mode == wxEOL_NATIVE)
317 {
318#if defined(__WXMSW__) || defined(__WXPM__)
319 m_mode = wxEOL_DOS;
f3ff3813 320#elif defined(__WXMAC__) && !defined(__DARWIN__)
c7a9fa36
RR
321 m_mode = wxEOL_MAC;
322#else
323 m_mode = wxEOL_UNIX;
324#endif
325 }
fae05df5
GL
326}
327
328wxTextOutputStream::~wxTextOutputStream()
329{
d36c9347
VZ
330#if wxUSE_UNICODE
331 delete m_conv;
332#endif // wxUSE_UNICODE
fae05df5
GL
333}
334
cd0b1709 335void wxTextOutputStream::SetMode(wxEOL mode)
c7a9fa36
RR
336{
337 m_mode = mode;
338 if (m_mode == wxEOL_NATIVE)
339 {
340#if defined(__WXMSW__) || defined(__WXPM__)
341 m_mode = wxEOL_DOS;
f3ff3813 342#elif defined(__WXMAC__) && !defined(__DARWIN__)
c7a9fa36
RR
343 m_mode = wxEOL_MAC;
344#else
345 m_mode = wxEOL_UNIX;
346#endif
347 }
348}
349
fae05df5
GL
350void wxTextOutputStream::Write32(wxUint32 i)
351{
cd25b18c 352 wxString str;
223d09f6 353 str.Printf(wxT("%u"), i);
717b9bf2 354
cd25b18c 355 WriteString(str);
fae05df5
GL
356}
357
358void wxTextOutputStream::Write16(wxUint16 i)
359{
cd25b18c 360 wxString str;
17a1ebd1 361 str.Printf(wxT("%u"), (unsigned)i);
717b9bf2 362
cd25b18c 363 WriteString(str);
fae05df5
GL
364}
365
366void wxTextOutputStream::Write8(wxUint8 i)
367{
cd25b18c 368 wxString str;
17a1ebd1 369 str.Printf(wxT("%u"), (unsigned)i);
717b9bf2 370
cd25b18c 371 WriteString(str);
fae05df5
GL
372}
373
374void wxTextOutputStream::WriteDouble(double d)
375{
cd25b18c 376 wxString str;
fae05df5 377
223d09f6 378 str.Printf(wxT("%f"), d);
cd25b18c 379 WriteString(str);
fae05df5
GL
380}
381
382void wxTextOutputStream::WriteString(const wxString& string)
383{
20ea6894
VZ
384 size_t len = string.length();
385
386 wxString out;
387 out.reserve(len);
388
389 for ( size_t i = 0; i < len; i++ )
cd25b18c 390 {
20ea6894
VZ
391 const wxChar c = string[i];
392 if ( c == wxT('\n') )
cd25b18c 393 {
20ea6894 394 switch ( m_mode )
cd6ce4a9 395 {
20ea6894
VZ
396 case wxEOL_DOS:
397 out << _T("\r\n");
398 continue;
399
400 case wxEOL_MAC:
401 out << _T('\r');
402 continue;
403
404 default:
405 wxFAIL_MSG( _T("unknown EOL mode in wxTextOutputStream") );
406 // fall through
407
408 case wxEOL_UNIX:
409 // don't treat '\n' specially
410 ;
c7a9fa36 411 }
cd25b18c 412 }
20ea6894
VZ
413
414 out << c;
7448de8d 415 }
20ea6894 416
2b5f62a0 417#if wxUSE_UNICODE
3793402c 418 wxCharBuffer buffer = m_conv->cWC2MB(out, out.length(), &len);
bfaee57e 419 m_output.Write(buffer, len);
2b5f62a0
VZ
420#else
421 m_output.Write(out.c_str(), out.length() );
422#endif
fae05df5
GL
423}
424
ba854691
RN
425wxTextOutputStream& wxTextOutputStream::PutChar(wxChar c)
426{
427#if wxUSE_UNICODE
d36c9347 428 WriteString( wxString(&c, *m_conv, 1) );
ba854691
RN
429#else
430 WriteString( wxString(&c, wxConvLocal, 1) );
431#endif
432 return *this;
433}
434
fae05df5
GL
435wxTextOutputStream& wxTextOutputStream::operator<<(const wxChar *string)
436{
cd25b18c
RR
437 WriteString( wxString(string) );
438 return *this;
fae05df5
GL
439}
440
441wxTextOutputStream& wxTextOutputStream::operator<<(const wxString& string)
442{
cd25b18c
RR
443 WriteString( string );
444 return *this;
fae05df5
GL
445}
446
f6bcfd97 447wxTextOutputStream& wxTextOutputStream::operator<<(char c)
fae05df5 448{
2b5f62a0 449 WriteString( wxString::FromAscii(c) );
cb719f2e 450
cd25b18c 451 return *this;
fae05df5
GL
452}
453
e4940feb 454#if wxUSE_UNICODE && wxWCHAR_T_IS_REAL_TYPE
3ca1bf5a
VZ
455
456wxTextOutputStream& wxTextOutputStream::operator<<(wchar_t wc)
457{
d36c9347 458 WriteString( wxString(&wc, *m_conv, 1) );
65a1bb98
VZ
459
460 return *this;
3ca1bf5a
VZ
461}
462
e4940feb 463#endif // wxUSE_UNICODE
3ca1bf5a 464
fae05df5
GL
465wxTextOutputStream& wxTextOutputStream::operator<<(wxInt16 c)
466{
78e848ca
RR
467 wxString str;
468 str.Printf(wxT("%d"), (signed int)c);
469 WriteString(str);
cd6ce4a9 470
cd25b18c 471 return *this;
fae05df5
GL
472}
473
474wxTextOutputStream& wxTextOutputStream::operator<<(wxInt32 c)
475{
78e848ca
RR
476 wxString str;
477 str.Printf(wxT("%ld"), (signed long)c);
478 WriteString(str);
cd6ce4a9 479
cd25b18c 480 return *this;
fae05df5
GL
481}
482
fae05df5
GL
483wxTextOutputStream& wxTextOutputStream::operator<<(wxUint16 c)
484{
78e848ca
RR
485 wxString str;
486 str.Printf(wxT("%u"), (unsigned int)c);
487 WriteString(str);
cd6ce4a9 488
cd25b18c 489 return *this;
fae05df5
GL
490}
491
492wxTextOutputStream& wxTextOutputStream::operator<<(wxUint32 c)
493{
78e848ca
RR
494 wxString str;
495 str.Printf(wxT("%lu"), (unsigned long)c);
496 WriteString(str);
497
cd25b18c 498 return *this;
fae05df5
GL
499}
500
501wxTextOutputStream &wxTextOutputStream::operator<<(double f)
502{
cd25b18c
RR
503 WriteDouble(f);
504 return *this;
fae05df5
GL
505}
506
507wxTextOutputStream& wxTextOutputStream::operator<<(float f)
508{
cd25b18c
RR
509 WriteDouble((double)f);
510 return *this;
fae05df5
GL
511}
512
ed58dbea
RR
513wxTextOutputStream &endl( wxTextOutputStream &stream )
514{
ba854691 515 return stream.PutChar(wxT('\n'));
ed58dbea
RR
516}
517
fae05df5
GL
518#endif
519 // wxUSE_STREAMS