]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/common/txtstrm.cpp
Don't use "Cancel" button in the about dialog of the listctrl sample.
[wxWidgets.git] / src / common / txtstrm.cpp
... / ...
CommitLineData
1///////////////////////////////////////////////////////////////////////////////
2// Name: src/common/txtstrm.cpp
3// Purpose: Text stream classes
4// Author: Guilhem Lavaux
5// Modified by:
6// Created: 28/06/98
7// RCS-ID: $Id$
8// Copyright: (c) Guilhem Lavaux
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
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"
22
23#ifndef WX_PRECOMP
24 #include "wx/crt.h"
25#endif
26
27#include <ctype.h>
28
29// ----------------------------------------------------------------------------
30// wxTextInputStream
31// ----------------------------------------------------------------------------
32
33#if wxUSE_UNICODE
34wxTextInputStream::wxTextInputStream(wxInputStream &s,
35 const wxString &sep,
36 const wxMBConv& conv)
37 : m_input(s), m_separators(sep), m_conv(conv.Clone())
38{
39 memset((void*)m_lastBytes, 0, 10);
40}
41#else
42wxTextInputStream::wxTextInputStream(wxInputStream &s, const wxString &sep)
43 : m_input(s), m_separators(sep)
44{
45 memset((void*)m_lastBytes, 0, 10);
46}
47#endif
48
49wxTextInputStream::~wxTextInputStream()
50{
51#if wxUSE_UNICODE
52 delete m_conv;
53#endif // wxUSE_UNICODE
54}
55
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);
70 for(size_t inlen = 0; inlen < 9; inlen++)
71 {
72 // actually read the next character
73 m_lastBytes[inlen] = m_input.GetC();
74
75 if(m_input.LastRead() <= 0)
76 return wxEOT;
77
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 }
102 }
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
107 return wxEOT;
108#else
109 m_lastBytes[0] = m_input.GetC();
110
111 if(m_input.LastRead() <= 0)
112 return wxEOT;
113
114 return m_lastBytes[0];
115#endif
116
117}
118
119wxChar wxTextInputStream::NextNonSeparators()
120{
121 for (;;)
122 {
123 wxChar c = NextChar();
124 if (c == wxEOT) return (wxChar) 0;
125
126 if (c != wxT('\n') &&
127 c != wxT('\r') &&
128 m_separators.Find(c) < 0)
129 return c;
130 }
131
132}
133
134bool wxTextInputStream::EatEOL(const wxChar &c)
135{
136 if (c == wxT('\n')) return true; // eat on UNIX
137
138 if (c == wxT('\r')) // eat on both Mac and DOS
139 {
140 wxChar c2 = NextChar();
141 if(c2 == wxEOT) return true; // end of stream reached, had enough :-)
142
143 if (c2 != wxT('\n')) UngetLast(); // Don't eat on Mac
144 return true;
145 }
146
147 return false;
148}
149
150wxUint32 wxTextInputStream::Read32(int base)
151{
152 wxASSERT_MSG( !base || (base > 1 && base <= 36), wxT("invalid base") );
153 if(!m_input) return 0;
154
155 wxString word = ReadWord();
156 if(word.empty())
157 return 0;
158 return wxStrtoul(word.c_str(), 0, base);
159}
160
161wxUint16 wxTextInputStream::Read16(int base)
162{
163 return (wxUint16)Read32(base);
164}
165
166wxUint8 wxTextInputStream::Read8(int base)
167{
168 return (wxUint8)Read32(base);
169}
170
171wxInt32 wxTextInputStream::Read32S(int base)
172{
173 wxASSERT_MSG( !base || (base > 1 && base <= 36), wxT("invalid base") );
174 if(!m_input) return 0;
175
176 wxString word = ReadWord();
177 if(word.empty())
178 return 0;
179 return wxStrtol(word.c_str(), 0, base);
180}
181
182wxInt16 wxTextInputStream::Read16S(int base)
183{
184 return (wxInt16)Read32S(base);
185}
186
187wxInt8 wxTextInputStream::Read8S(int base)
188{
189 return (wxInt8)Read32S(base);
190}
191
192double wxTextInputStream::ReadDouble()
193{
194 if(!m_input) return 0;
195 wxString word = ReadWord();
196 if(word.empty())
197 return 0;
198 return wxStrtod(word.c_str(), 0);
199}
200
201#if WXWIN_COMPATIBILITY_2_6
202
203wxString wxTextInputStream::ReadString()
204{
205 return ReadLine();
206}
207
208#endif // WXWIN_COMPATIBILITY_2_6
209
210wxString wxTextInputStream::ReadLine()
211{
212 wxString line;
213
214 while ( !m_input.Eof() )
215 {
216 wxChar c = NextChar();
217 if(c == wxEOT)
218 break;
219
220 if (EatEOL(c))
221 break;
222
223 line += c;
224 }
225
226 return line;
227}
228
229wxString wxTextInputStream::ReadWord()
230{
231 wxString word;
232
233 if ( !m_input )
234 return word;
235
236 wxChar c = NextNonSeparators();
237 if ( !c )
238 return word;
239
240 word += c;
241
242 while ( !m_input.Eof() )
243 {
244 c = NextChar();
245 if(c == wxEOT)
246 break;
247
248 if (m_separators.Find(c) >= 0)
249 break;
250
251 if (EatEOL(c))
252 break;
253
254 word += c;
255 }
256
257 return word;
258}
259
260wxTextInputStream& wxTextInputStream::operator>>(wxString& word)
261{
262 word = ReadWord();
263 return *this;
264}
265
266wxTextInputStream& wxTextInputStream::operator>>(char& c)
267{
268 c = m_input.GetC();
269 if(m_input.LastRead() <= 0) c = 0;
270
271 if (EatEOL(c))
272 c = '\n';
273
274 return *this;
275}
276
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
288wxTextInputStream& wxTextInputStream::operator>>(wxInt16& i)
289{
290 i = (wxInt16)Read16();
291 return *this;
292}
293
294wxTextInputStream& wxTextInputStream::operator>>(wxInt32& i)
295{
296 i = (wxInt32)Read32();
297 return *this;
298}
299
300wxTextInputStream& wxTextInputStream::operator>>(wxUint16& i)
301{
302 i = Read16();
303 return *this;
304}
305
306wxTextInputStream& wxTextInputStream::operator>>(wxUint32& i)
307{
308 i = Read32();
309 return *this;
310}
311
312wxTextInputStream& wxTextInputStream::operator>>(double& i)
313{
314 i = ReadDouble();
315 return *this;
316}
317
318wxTextInputStream& wxTextInputStream::operator>>(float& f)
319{
320 f = (float)ReadDouble();
321 return *this;
322}
323
324
325
326#if wxUSE_UNICODE
327wxTextOutputStream::wxTextOutputStream(wxOutputStream& s,
328 wxEOL mode,
329 const wxMBConv& conv)
330 : m_output(s), m_conv(conv.Clone())
331#else
332wxTextOutputStream::wxTextOutputStream(wxOutputStream& s, wxEOL mode)
333 : m_output(s)
334#endif
335{
336 m_mode = mode;
337 if (m_mode == wxEOL_NATIVE)
338 {
339#if defined(__WINDOWS__) || defined(__WXPM__)
340 m_mode = wxEOL_DOS;
341#else
342 m_mode = wxEOL_UNIX;
343#endif
344 }
345}
346
347wxTextOutputStream::~wxTextOutputStream()
348{
349#if wxUSE_UNICODE
350 delete m_conv;
351#endif // wxUSE_UNICODE
352}
353
354void wxTextOutputStream::SetMode(wxEOL mode)
355{
356 m_mode = mode;
357 if (m_mode == wxEOL_NATIVE)
358 {
359#if defined(__WINDOWS__) || defined(__WXPM__)
360 m_mode = wxEOL_DOS;
361#else
362 m_mode = wxEOL_UNIX;
363#endif
364 }
365}
366
367void wxTextOutputStream::Write32(wxUint32 i)
368{
369 wxString str;
370 str.Printf(wxT("%u"), i);
371
372 WriteString(str);
373}
374
375void wxTextOutputStream::Write16(wxUint16 i)
376{
377 wxString str;
378 str.Printf(wxT("%u"), (unsigned)i);
379
380 WriteString(str);
381}
382
383void wxTextOutputStream::Write8(wxUint8 i)
384{
385 wxString str;
386 str.Printf(wxT("%u"), (unsigned)i);
387
388 WriteString(str);
389}
390
391void wxTextOutputStream::WriteDouble(double d)
392{
393 wxString str;
394
395 str.Printf(wxT("%f"), d);
396 WriteString(str);
397}
398
399void wxTextOutputStream::WriteString(const wxString& string)
400{
401 size_t len = string.length();
402
403 wxString out;
404 out.reserve(len);
405
406 for ( size_t i = 0; i < len; i++ )
407 {
408 const wxChar c = string[i];
409 if ( c == wxT('\n') )
410 {
411 switch ( m_mode )
412 {
413 case wxEOL_DOS:
414 out << wxT("\r\n");
415 continue;
416
417 case wxEOL_MAC:
418 out << wxT('\r');
419 continue;
420
421 default:
422 wxFAIL_MSG( wxT("unknown EOL mode in wxTextOutputStream") );
423 // fall through
424
425 case wxEOL_UNIX:
426 // don't treat '\n' specially
427 ;
428 }
429 }
430
431 out << c;
432 }
433
434#if wxUSE_UNICODE
435 // FIXME-UTF8: use wxCharBufferWithLength if/when we have it
436 wxCharBuffer buffer = m_conv->cWC2MB(out.wc_str(), out.length(), &len);
437 m_output.Write(buffer, len);
438#else
439 m_output.Write(out.c_str(), out.length() );
440#endif
441}
442
443wxTextOutputStream& wxTextOutputStream::PutChar(wxChar c)
444{
445#if wxUSE_UNICODE
446 WriteString( wxString(&c, *m_conv, 1) );
447#else
448 WriteString( wxString(&c, wxConvLocal, 1) );
449#endif
450 return *this;
451}
452
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
466wxTextOutputStream& wxTextOutputStream::operator<<(const wxString& string)
467{
468 WriteString( string );
469 return *this;
470}
471
472wxTextOutputStream& wxTextOutputStream::operator<<(char c)
473{
474 WriteString( wxString::FromAscii(c) );
475
476 return *this;
477}
478
479#if wxUSE_UNICODE && wxWCHAR_T_IS_REAL_TYPE
480
481wxTextOutputStream& wxTextOutputStream::operator<<(wchar_t wc)
482{
483 WriteString( wxString(&wc, *m_conv, 1) );
484
485 return *this;
486}
487
488#endif // wxUSE_UNICODE
489
490wxTextOutputStream& wxTextOutputStream::operator<<(wxInt16 c)
491{
492 wxString str;
493 str.Printf(wxT("%d"), (signed int)c);
494 WriteString(str);
495
496 return *this;
497}
498
499wxTextOutputStream& wxTextOutputStream::operator<<(wxInt32 c)
500{
501 wxString str;
502 str.Printf(wxT("%ld"), (signed long)c);
503 WriteString(str);
504
505 return *this;
506}
507
508wxTextOutputStream& wxTextOutputStream::operator<<(wxUint16 c)
509{
510 wxString str;
511 str.Printf(wxT("%u"), (unsigned int)c);
512 WriteString(str);
513
514 return *this;
515}
516
517wxTextOutputStream& wxTextOutputStream::operator<<(wxUint32 c)
518{
519 wxString str;
520 str.Printf(wxT("%lu"), (unsigned long)c);
521 WriteString(str);
522
523 return *this;
524}
525
526wxTextOutputStream &wxTextOutputStream::operator<<(double f)
527{
528 WriteDouble(f);
529 return *this;
530}
531
532wxTextOutputStream& wxTextOutputStream::operator<<(float f)
533{
534 WriteDouble((double)f);
535 return *this;
536}
537
538wxTextOutputStream &endl( wxTextOutputStream &stream )
539{
540 return stream.PutChar(wxT('\n'));
541}
542
543#endif
544 // wxUSE_STREAMS