]> git.saurik.com Git - wxWidgets.git/blame - src/common/txtstrm.cpp
font inheritance fixes (patch 983251)
[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);
73 for(size_t inlen = 0; inlen < 9; inlen++)
74 {
75 // actually read the next character
76 m_lastBytes[inlen] = m_input.GetC();
77
78 if(m_input.LastRead() <= 0)
79 return wxEOT;
80
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();
89
90 if(m_input.LastRead() <= 0)
91 return wxEOT;
92
93 return m_lastBytes[0];
94#endif
95
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{
f6bcfd97 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
VZ
119 wxChar c2 = NextChar();
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
f6bcfd97 123 return TRUE;
cd25b18c 124 }
717b9bf2 125
f6bcfd97 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;
f6bcfd97 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
BP
218 word += c;
219
cd6ce4a9 220 while ( !m_input.Eof() )
9853d977 221 {
2348a842
VZ
222 c = NextChar();
223 if(c == wxEOT)
f6bcfd97
BP
224 break;
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
255wxTextInputStream& wxTextInputStream::operator>>(wxInt16& i)
256{
cd25b18c
RR
257 i = (wxInt16)Read16();
258 return *this;
fae05df5
GL
259}
260
261wxTextInputStream& wxTextInputStream::operator>>(wxInt32& i)
262{
cd25b18c
RR
263 i = (wxInt32)Read32();
264 return *this;
fae05df5
GL
265}
266
fae05df5
GL
267wxTextInputStream& wxTextInputStream::operator>>(wxUint16& i)
268{
cd25b18c
RR
269 i = Read16();
270 return *this;
fae05df5
GL
271}
272
273wxTextInputStream& wxTextInputStream::operator>>(wxUint32& i)
274{
cd25b18c
RR
275 i = Read32();
276 return *this;
fae05df5
GL
277}
278
279wxTextInputStream& wxTextInputStream::operator>>(double& i)
280{
cd25b18c
RR
281 i = ReadDouble();
282 return *this;
fae05df5
GL
283}
284
285wxTextInputStream& wxTextInputStream::operator>>(float& f)
286{
cd25b18c
RR
287 f = (float)ReadDouble();
288 return *this;
fae05df5
GL
289}
290
2b5f62a0
VZ
291
292
293#if wxUSE_UNICODE
294wxTextOutputStream::wxTextOutputStream(wxOutputStream& s, wxEOL mode, wxMBConv& conv)
295 : m_output(s), m_conv(conv)
296#else
c7a9fa36 297wxTextOutputStream::wxTextOutputStream(wxOutputStream& s, wxEOL mode)
191549ed 298 : m_output(s)
2b5f62a0 299#endif
fae05df5 300{
c7a9fa36
RR
301 m_mode = mode;
302 if (m_mode == wxEOL_NATIVE)
303 {
304#if defined(__WXMSW__) || defined(__WXPM__)
305 m_mode = wxEOL_DOS;
f3ff3813 306#elif defined(__WXMAC__) && !defined(__DARWIN__)
c7a9fa36
RR
307 m_mode = wxEOL_MAC;
308#else
309 m_mode = wxEOL_UNIX;
310#endif
311 }
fae05df5
GL
312}
313
314wxTextOutputStream::~wxTextOutputStream()
315{
316}
317
cd0b1709 318void wxTextOutputStream::SetMode(wxEOL mode)
c7a9fa36
RR
319{
320 m_mode = mode;
321 if (m_mode == wxEOL_NATIVE)
322 {
323#if defined(__WXMSW__) || defined(__WXPM__)
324 m_mode = wxEOL_DOS;
f3ff3813 325#elif defined(__WXMAC__) && !defined(__DARWIN__)
c7a9fa36
RR
326 m_mode = wxEOL_MAC;
327#else
328 m_mode = wxEOL_UNIX;
329#endif
330 }
331}
332
fae05df5
GL
333void wxTextOutputStream::Write32(wxUint32 i)
334{
cd25b18c 335 wxString str;
223d09f6 336 str.Printf(wxT("%u"), i);
717b9bf2 337
cd25b18c 338 WriteString(str);
fae05df5
GL
339}
340
341void wxTextOutputStream::Write16(wxUint16 i)
342{
cd25b18c 343 wxString str;
223d09f6 344 str.Printf(wxT("%u"), i);
717b9bf2 345
cd25b18c 346 WriteString(str);
fae05df5
GL
347}
348
349void wxTextOutputStream::Write8(wxUint8 i)
350{
cd25b18c 351 wxString str;
223d09f6 352 str.Printf(wxT("%u"), i);
717b9bf2 353
cd25b18c 354 WriteString(str);
fae05df5
GL
355}
356
357void wxTextOutputStream::WriteDouble(double d)
358{
cd25b18c 359 wxString str;
fae05df5 360
223d09f6 361 str.Printf(wxT("%f"), d);
cd25b18c 362 WriteString(str);
fae05df5
GL
363}
364
365void wxTextOutputStream::WriteString(const wxString& string)
366{
20ea6894
VZ
367 size_t len = string.length();
368
369 wxString out;
370 out.reserve(len);
371
372 for ( size_t i = 0; i < len; i++ )
cd25b18c 373 {
20ea6894
VZ
374 const wxChar c = string[i];
375 if ( c == wxT('\n') )
cd25b18c 376 {
20ea6894 377 switch ( m_mode )
cd6ce4a9 378 {
20ea6894
VZ
379 case wxEOL_DOS:
380 out << _T("\r\n");
381 continue;
382
383 case wxEOL_MAC:
384 out << _T('\r');
385 continue;
386
387 default:
388 wxFAIL_MSG( _T("unknown EOL mode in wxTextOutputStream") );
389 // fall through
390
391 case wxEOL_UNIX:
392 // don't treat '\n' specially
393 ;
c7a9fa36 394 }
cd25b18c 395 }
20ea6894
VZ
396
397 out << c;
cd25b18c 398 }
20ea6894 399
2b5f62a0
VZ
400 // We must not write the trailing NULL here
401#if wxUSE_UNICODE
402 wxCharBuffer buffer = m_conv.cWC2MB( out );
403 m_output.Write( (const char*) buffer, strlen( (const char*) buffer ) );
404#else
405 m_output.Write(out.c_str(), out.length() );
406#endif
fae05df5
GL
407}
408
409wxTextOutputStream& wxTextOutputStream::operator<<(const wxChar *string)
410{
cd25b18c
RR
411 WriteString( wxString(string) );
412 return *this;
fae05df5
GL
413}
414
415wxTextOutputStream& wxTextOutputStream::operator<<(const wxString& string)
416{
cd25b18c
RR
417 WriteString( string );
418 return *this;
fae05df5
GL
419}
420
f6bcfd97 421wxTextOutputStream& wxTextOutputStream::operator<<(char c)
fae05df5 422{
2b5f62a0
VZ
423 WriteString( wxString::FromAscii(c) );
424
cd25b18c 425 return *this;
fae05df5
GL
426}
427
428wxTextOutputStream& wxTextOutputStream::operator<<(wxInt16 c)
429{
78e848ca
RR
430 wxString str;
431 str.Printf(wxT("%d"), (signed int)c);
432 WriteString(str);
cd6ce4a9 433
cd25b18c 434 return *this;
fae05df5
GL
435}
436
437wxTextOutputStream& wxTextOutputStream::operator<<(wxInt32 c)
438{
78e848ca
RR
439 wxString str;
440 str.Printf(wxT("%ld"), (signed long)c);
441 WriteString(str);
cd6ce4a9 442
cd25b18c 443 return *this;
fae05df5
GL
444}
445
fae05df5
GL
446wxTextOutputStream& wxTextOutputStream::operator<<(wxUint16 c)
447{
78e848ca
RR
448 wxString str;
449 str.Printf(wxT("%u"), (unsigned int)c);
450 WriteString(str);
cd6ce4a9 451
cd25b18c 452 return *this;
fae05df5
GL
453}
454
455wxTextOutputStream& wxTextOutputStream::operator<<(wxUint32 c)
456{
78e848ca
RR
457 wxString str;
458 str.Printf(wxT("%lu"), (unsigned long)c);
459 WriteString(str);
460
cd25b18c 461 return *this;
fae05df5
GL
462}
463
464wxTextOutputStream &wxTextOutputStream::operator<<(double f)
465{
cd25b18c
RR
466 WriteDouble(f);
467 return *this;
fae05df5
GL
468}
469
470wxTextOutputStream& wxTextOutputStream::operator<<(float f)
471{
cd25b18c
RR
472 WriteDouble((double)f);
473 return *this;
fae05df5
GL
474}
475
ed58dbea
RR
476wxTextOutputStream &endl( wxTextOutputStream &stream )
477{
223d09f6 478 return stream << wxT('\n');
ed58dbea
RR
479}
480
fae05df5
GL
481#endif
482 // wxUSE_STREAMS