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