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