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