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