]> git.saurik.com Git - wxWidgets.git/blame - src/common/txtstrm.cpp
i18n files are installed as part of wxBase (and should be ideally part of separate...
[wxWidgets.git] / src / common / txtstrm.cpp
CommitLineData
5a96d2f4 1///////////////////////////////////////////////////////////////////////////////
40ff126a 2// Name: src/common/txtstrm.cpp
fae05df5
GL
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
40ff126a
WS
181#if WXWIN_COMPATIBILITY_2_6
182
fae05df5 183wxString wxTextInputStream::ReadString()
9853d977 184{
cd6ce4a9 185 return ReadLine();
9853d977
SB
186}
187
40ff126a
WS
188#endif // WXWIN_COMPATIBILITY_2_6
189
9853d977 190wxString wxTextInputStream::ReadLine()
fae05df5 191{
cd25b18c
RR
192 wxString line;
193
cd6ce4a9 194 while ( !m_input.Eof() )
cd25b18c 195 {
2348a842
VZ
196 wxChar c = NextChar();
197 if(c == wxEOT)
198 break;
cb719f2e 199
cd6ce4a9
VZ
200 if ( !m_input )
201 break;
202
203 if (EatEOL(c))
204 break;
205
cd25b18c
RR
206 line += c;
207 }
717b9bf2 208
cd25b18c 209 return line;
fae05df5 210}
717b9bf2 211
9853d977
SB
212wxString wxTextInputStream::ReadWord()
213{
9853d977 214 wxString word;
9853d977 215
cd6ce4a9
VZ
216 if ( !m_input )
217 return word;
218
219 wxChar c = NextNonSeparators();
220 if ( !c )
221 return word;
222
f6bcfd97 223 word += c;
cb719f2e 224
cd6ce4a9 225 while ( !m_input.Eof() )
9853d977 226 {
2348a842
VZ
227 c = NextChar();
228 if(c == wxEOT)
f6bcfd97 229 break;
cb719f2e 230
cd6ce4a9
VZ
231 if (m_separators.Contains(c))
232 break;
233
234 if (EatEOL(c))
235 break;
236
9853d977 237 word += c;
9853d977
SB
238 }
239
240 return word;
241}
242
243wxTextInputStream& wxTextInputStream::operator>>(wxString& word)
fae05df5 244{
cd6ce4a9
VZ
245 word = ReadWord();
246 return *this;
fae05df5
GL
247}
248
f6bcfd97 249wxTextInputStream& wxTextInputStream::operator>>(char& c)
fae05df5 250{
191549ed 251 c = m_input.GetC();
2348a842 252 if(m_input.LastRead() <= 0) c = 0;
717b9bf2 253
f6bcfd97
BP
254 if (EatEOL(c))
255 c = '\n';
256
cd25b18c 257 return *this;
fae05df5
GL
258}
259
fcbe123e
VZ
260#if wxUSE_UNICODE && wxWCHAR_T_IS_REAL_TYPE
261
262wxTextInputStream& wxTextInputStream::operator>>(wchar_t& wc)
263{
264 wc = GetChar();
265
266 return *this;
267}
268
269#endif // wxUSE_UNICODE
270
fae05df5
GL
271wxTextInputStream& wxTextInputStream::operator>>(wxInt16& i)
272{
cd25b18c
RR
273 i = (wxInt16)Read16();
274 return *this;
fae05df5
GL
275}
276
277wxTextInputStream& wxTextInputStream::operator>>(wxInt32& i)
278{
cd25b18c
RR
279 i = (wxInt32)Read32();
280 return *this;
fae05df5
GL
281}
282
fae05df5
GL
283wxTextInputStream& wxTextInputStream::operator>>(wxUint16& i)
284{
cd25b18c
RR
285 i = Read16();
286 return *this;
fae05df5
GL
287}
288
289wxTextInputStream& wxTextInputStream::operator>>(wxUint32& i)
290{
cd25b18c
RR
291 i = Read32();
292 return *this;
fae05df5
GL
293}
294
295wxTextInputStream& wxTextInputStream::operator>>(double& i)
296{
cd25b18c
RR
297 i = ReadDouble();
298 return *this;
fae05df5
GL
299}
300
301wxTextInputStream& wxTextInputStream::operator>>(float& f)
302{
cd25b18c
RR
303 f = (float)ReadDouble();
304 return *this;
fae05df5
GL
305}
306
2b5f62a0
VZ
307
308
309#if wxUSE_UNICODE
830f8f11
VZ
310wxTextOutputStream::wxTextOutputStream(wxOutputStream& s,
311 wxEOL mode,
312 const wxMBConv& conv)
d36c9347 313 : m_output(s), m_conv(conv.Clone())
2b5f62a0 314#else
c7a9fa36 315wxTextOutputStream::wxTextOutputStream(wxOutputStream& s, wxEOL mode)
191549ed 316 : m_output(s)
2b5f62a0 317#endif
fae05df5 318{
c7a9fa36
RR
319 m_mode = mode;
320 if (m_mode == wxEOL_NATIVE)
321 {
322#if defined(__WXMSW__) || defined(__WXPM__)
323 m_mode = wxEOL_DOS;
f3ff3813 324#elif defined(__WXMAC__) && !defined(__DARWIN__)
c7a9fa36
RR
325 m_mode = wxEOL_MAC;
326#else
327 m_mode = wxEOL_UNIX;
328#endif
329 }
fae05df5
GL
330}
331
332wxTextOutputStream::~wxTextOutputStream()
333{
d36c9347
VZ
334#if wxUSE_UNICODE
335 delete m_conv;
336#endif // wxUSE_UNICODE
fae05df5
GL
337}
338
cd0b1709 339void wxTextOutputStream::SetMode(wxEOL mode)
c7a9fa36
RR
340{
341 m_mode = mode;
342 if (m_mode == wxEOL_NATIVE)
343 {
344#if defined(__WXMSW__) || defined(__WXPM__)
345 m_mode = wxEOL_DOS;
f3ff3813 346#elif defined(__WXMAC__) && !defined(__DARWIN__)
c7a9fa36
RR
347 m_mode = wxEOL_MAC;
348#else
349 m_mode = wxEOL_UNIX;
350#endif
351 }
352}
353
fae05df5
GL
354void wxTextOutputStream::Write32(wxUint32 i)
355{
cd25b18c 356 wxString str;
223d09f6 357 str.Printf(wxT("%u"), i);
717b9bf2 358
cd25b18c 359 WriteString(str);
fae05df5
GL
360}
361
362void wxTextOutputStream::Write16(wxUint16 i)
363{
cd25b18c 364 wxString str;
17a1ebd1 365 str.Printf(wxT("%u"), (unsigned)i);
717b9bf2 366
cd25b18c 367 WriteString(str);
fae05df5
GL
368}
369
370void wxTextOutputStream::Write8(wxUint8 i)
371{
cd25b18c 372 wxString str;
17a1ebd1 373 str.Printf(wxT("%u"), (unsigned)i);
717b9bf2 374
cd25b18c 375 WriteString(str);
fae05df5
GL
376}
377
378void wxTextOutputStream::WriteDouble(double d)
379{
cd25b18c 380 wxString str;
fae05df5 381
223d09f6 382 str.Printf(wxT("%f"), d);
cd25b18c 383 WriteString(str);
fae05df5
GL
384}
385
386void wxTextOutputStream::WriteString(const wxString& string)
387{
20ea6894
VZ
388 size_t len = string.length();
389
390 wxString out;
391 out.reserve(len);
392
393 for ( size_t i = 0; i < len; i++ )
cd25b18c 394 {
20ea6894
VZ
395 const wxChar c = string[i];
396 if ( c == wxT('\n') )
cd25b18c 397 {
20ea6894 398 switch ( m_mode )
cd6ce4a9 399 {
20ea6894
VZ
400 case wxEOL_DOS:
401 out << _T("\r\n");
402 continue;
403
404 case wxEOL_MAC:
405 out << _T('\r');
406 continue;
407
408 default:
409 wxFAIL_MSG( _T("unknown EOL mode in wxTextOutputStream") );
410 // fall through
411
412 case wxEOL_UNIX:
413 // don't treat '\n' specially
414 ;
c7a9fa36 415 }
cd25b18c 416 }
20ea6894
VZ
417
418 out << c;
7448de8d 419 }
20ea6894 420
2b5f62a0 421#if wxUSE_UNICODE
3793402c 422 wxCharBuffer buffer = m_conv->cWC2MB(out, out.length(), &len);
bfaee57e 423 m_output.Write(buffer, len);
2b5f62a0
VZ
424#else
425 m_output.Write(out.c_str(), out.length() );
426#endif
fae05df5
GL
427}
428
ba854691
RN
429wxTextOutputStream& wxTextOutputStream::PutChar(wxChar c)
430{
431#if wxUSE_UNICODE
d36c9347 432 WriteString( wxString(&c, *m_conv, 1) );
ba854691
RN
433#else
434 WriteString( wxString(&c, wxConvLocal, 1) );
435#endif
436 return *this;
437}
438
fae05df5
GL
439wxTextOutputStream& wxTextOutputStream::operator<<(const wxChar *string)
440{
cd25b18c
RR
441 WriteString( wxString(string) );
442 return *this;
fae05df5
GL
443}
444
445wxTextOutputStream& wxTextOutputStream::operator<<(const wxString& string)
446{
cd25b18c
RR
447 WriteString( string );
448 return *this;
fae05df5
GL
449}
450
f6bcfd97 451wxTextOutputStream& wxTextOutputStream::operator<<(char c)
fae05df5 452{
2b5f62a0 453 WriteString( wxString::FromAscii(c) );
cb719f2e 454
cd25b18c 455 return *this;
fae05df5
GL
456}
457
e4940feb 458#if wxUSE_UNICODE && wxWCHAR_T_IS_REAL_TYPE
3ca1bf5a
VZ
459
460wxTextOutputStream& wxTextOutputStream::operator<<(wchar_t wc)
461{
d36c9347 462 WriteString( wxString(&wc, *m_conv, 1) );
65a1bb98
VZ
463
464 return *this;
3ca1bf5a
VZ
465}
466
e4940feb 467#endif // wxUSE_UNICODE
3ca1bf5a 468
fae05df5
GL
469wxTextOutputStream& wxTextOutputStream::operator<<(wxInt16 c)
470{
78e848ca
RR
471 wxString str;
472 str.Printf(wxT("%d"), (signed int)c);
473 WriteString(str);
cd6ce4a9 474
cd25b18c 475 return *this;
fae05df5
GL
476}
477
478wxTextOutputStream& wxTextOutputStream::operator<<(wxInt32 c)
479{
78e848ca
RR
480 wxString str;
481 str.Printf(wxT("%ld"), (signed long)c);
482 WriteString(str);
cd6ce4a9 483
cd25b18c 484 return *this;
fae05df5
GL
485}
486
fae05df5
GL
487wxTextOutputStream& wxTextOutputStream::operator<<(wxUint16 c)
488{
78e848ca
RR
489 wxString str;
490 str.Printf(wxT("%u"), (unsigned int)c);
491 WriteString(str);
cd6ce4a9 492
cd25b18c 493 return *this;
fae05df5
GL
494}
495
496wxTextOutputStream& wxTextOutputStream::operator<<(wxUint32 c)
497{
78e848ca
RR
498 wxString str;
499 str.Printf(wxT("%lu"), (unsigned long)c);
500 WriteString(str);
501
cd25b18c 502 return *this;
fae05df5
GL
503}
504
505wxTextOutputStream &wxTextOutputStream::operator<<(double f)
506{
cd25b18c
RR
507 WriteDouble(f);
508 return *this;
fae05df5
GL
509}
510
511wxTextOutputStream& wxTextOutputStream::operator<<(float f)
512{
cd25b18c
RR
513 WriteDouble((double)f);
514 return *this;
fae05df5
GL
515}
516
ed58dbea
RR
517wxTextOutputStream &endl( wxTextOutputStream &stream )
518{
ba854691 519 return stream.PutChar(wxT('\n'));
ed58dbea
RR
520}
521
fae05df5
GL
522#endif
523 // wxUSE_STREAMS