]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/common/txtstrm.cpp
add XRC handler for wxSearchCtrl (extended patch 1751516)
[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#ifndef WX_PRECOMP
22 #include "wx/crt.h"
23#endif
24
25#include "wx/txtstrm.h"
26#include <ctype.h>
27
28
29// ----------------------------------------------------------------------------
30// constants
31// ----------------------------------------------------------------------------
32
33// Unix: "\n"
34// Dos: "\r\n"
35// Mac: "\r"
36
37// ----------------------------------------------------------------------------
38// wxTextInputStream
39// ----------------------------------------------------------------------------
40
41#if wxUSE_UNICODE
42wxTextInputStream::wxTextInputStream(wxInputStream &s,
43 const wxString &sep,
44 const wxMBConv& conv)
45 : m_input(s), m_separators(sep), m_conv(conv.Clone())
46{
47 memset((void*)m_lastBytes, 0, 10);
48}
49#else
50wxTextInputStream::wxTextInputStream(wxInputStream &s, const wxString &sep)
51 : m_input(s), m_separators(sep)
52{
53 memset((void*)m_lastBytes, 0, 10);
54}
55#endif
56
57wxTextInputStream::~wxTextInputStream()
58{
59#if wxUSE_UNICODE
60 delete m_conv;
61#endif // wxUSE_UNICODE
62}
63
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);
78 for(size_t inlen = 0; inlen < 9; inlen++)
79 {
80 // actually read the next character
81 m_lastBytes[inlen] = m_input.GetC();
82
83 if(m_input.LastRead() <= 0)
84 return wxEOT;
85
86 if ( m_conv->ToWChar(wbuf, WXSIZEOF(wbuf), m_lastBytes, inlen + 1)
87 != wxCONV_FAILED )
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();
94
95 if(m_input.LastRead() <= 0)
96 return wxEOT;
97
98 return m_lastBytes[0];
99#endif
100
101}
102
103wxChar wxTextInputStream::NextNonSeparators()
104{
105 for (;;)
106 {
107 wxChar c = NextChar();
108 if (c == wxEOT) return (wxChar) 0;
109
110 if (c != wxT('\n') &&
111 c != wxT('\r') &&
112 !m_separators.Contains(c))
113 return c;
114 }
115
116}
117
118bool wxTextInputStream::EatEOL(const wxChar &c)
119{
120 if (c == wxT('\n')) return true; // eat on UNIX
121
122 if (c == wxT('\r')) // eat on both Mac and DOS
123 {
124 wxChar c2 = NextChar();
125 if(c2 == wxEOT) return true; // end of stream reached, had enough :-)
126
127 if (c2 != wxT('\n')) UngetLast(); // Don't eat on Mac
128 return true;
129 }
130
131 return false;
132}
133
134wxUint32 wxTextInputStream::Read32(int base)
135{
136 wxASSERT_MSG( !base || (base > 1 && base <= 36), _T("invalid base") );
137 if(!m_input) return 0;
138
139 wxString word = ReadWord();
140 if(word.empty())
141 return 0;
142 return wxStrtoul(word.c_str(), 0, base);
143}
144
145wxUint16 wxTextInputStream::Read16(int base)
146{
147 return (wxUint16)Read32(base);
148}
149
150wxUint8 wxTextInputStream::Read8(int base)
151{
152 return (wxUint8)Read32(base);
153}
154
155wxInt32 wxTextInputStream::Read32S(int base)
156{
157 wxASSERT_MSG( !base || (base > 1 && base <= 36), _T("invalid base") );
158 if(!m_input) return 0;
159
160 wxString word = ReadWord();
161 if(word.empty())
162 return 0;
163 return wxStrtol(word.c_str(), 0, base);
164}
165
166wxInt16 wxTextInputStream::Read16S(int base)
167{
168 return (wxInt16)Read32S(base);
169}
170
171wxInt8 wxTextInputStream::Read8S(int base)
172{
173 return (wxInt8)Read32S(base);
174}
175
176double wxTextInputStream::ReadDouble()
177{
178 if(!m_input) return 0;
179 wxString word = ReadWord();
180 if(word.empty())
181 return 0;
182 return wxStrtod(word.c_str(), 0);
183}
184
185#if WXWIN_COMPATIBILITY_2_6
186
187wxString wxTextInputStream::ReadString()
188{
189 return ReadLine();
190}
191
192#endif // WXWIN_COMPATIBILITY_2_6
193
194wxString wxTextInputStream::ReadLine()
195{
196 wxString line;
197
198 while ( !m_input.Eof() )
199 {
200 wxChar c = NextChar();
201 if(c == wxEOT)
202 break;
203
204 if (EatEOL(c))
205 break;
206
207 line += c;
208 }
209
210 return line;
211}
212
213wxString wxTextInputStream::ReadWord()
214{
215 wxString word;
216
217 if ( !m_input )
218 return word;
219
220 wxChar c = NextNonSeparators();
221 if ( !c )
222 return word;
223
224 word += c;
225
226 while ( !m_input.Eof() )
227 {
228 c = NextChar();
229 if(c == wxEOT)
230 break;
231
232 if (m_separators.Contains(c))
233 break;
234
235 if (EatEOL(c))
236 break;
237
238 word += c;
239 }
240
241 return word;
242}
243
244wxTextInputStream& wxTextInputStream::operator>>(wxString& word)
245{
246 word = ReadWord();
247 return *this;
248}
249
250wxTextInputStream& wxTextInputStream::operator>>(char& c)
251{
252 c = m_input.GetC();
253 if(m_input.LastRead() <= 0) c = 0;
254
255 if (EatEOL(c))
256 c = '\n';
257
258 return *this;
259}
260
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
272wxTextInputStream& wxTextInputStream::operator>>(wxInt16& i)
273{
274 i = (wxInt16)Read16();
275 return *this;
276}
277
278wxTextInputStream& wxTextInputStream::operator>>(wxInt32& i)
279{
280 i = (wxInt32)Read32();
281 return *this;
282}
283
284wxTextInputStream& wxTextInputStream::operator>>(wxUint16& i)
285{
286 i = Read16();
287 return *this;
288}
289
290wxTextInputStream& wxTextInputStream::operator>>(wxUint32& i)
291{
292 i = Read32();
293 return *this;
294}
295
296wxTextInputStream& wxTextInputStream::operator>>(double& i)
297{
298 i = ReadDouble();
299 return *this;
300}
301
302wxTextInputStream& wxTextInputStream::operator>>(float& f)
303{
304 f = (float)ReadDouble();
305 return *this;
306}
307
308
309
310#if wxUSE_UNICODE
311wxTextOutputStream::wxTextOutputStream(wxOutputStream& s,
312 wxEOL mode,
313 const wxMBConv& conv)
314 : m_output(s), m_conv(conv.Clone())
315#else
316wxTextOutputStream::wxTextOutputStream(wxOutputStream& s, wxEOL mode)
317 : m_output(s)
318#endif
319{
320 m_mode = mode;
321 if (m_mode == wxEOL_NATIVE)
322 {
323#if defined(__WXMSW__) || defined(__WXPM__)
324 m_mode = wxEOL_DOS;
325#elif defined(__WXMAC__) && !defined(__DARWIN__)
326 m_mode = wxEOL_MAC;
327#else
328 m_mode = wxEOL_UNIX;
329#endif
330 }
331}
332
333wxTextOutputStream::~wxTextOutputStream()
334{
335#if wxUSE_UNICODE
336 delete m_conv;
337#endif // wxUSE_UNICODE
338}
339
340void wxTextOutputStream::SetMode(wxEOL mode)
341{
342 m_mode = mode;
343 if (m_mode == wxEOL_NATIVE)
344 {
345#if defined(__WXMSW__) || defined(__WXPM__)
346 m_mode = wxEOL_DOS;
347#elif defined(__WXMAC__) && !defined(__DARWIN__)
348 m_mode = wxEOL_MAC;
349#else
350 m_mode = wxEOL_UNIX;
351#endif
352 }
353}
354
355void wxTextOutputStream::Write32(wxUint32 i)
356{
357 wxString str;
358 str.Printf(wxT("%u"), i);
359
360 WriteString(str);
361}
362
363void wxTextOutputStream::Write16(wxUint16 i)
364{
365 wxString str;
366 str.Printf(wxT("%u"), (unsigned)i);
367
368 WriteString(str);
369}
370
371void wxTextOutputStream::Write8(wxUint8 i)
372{
373 wxString str;
374 str.Printf(wxT("%u"), (unsigned)i);
375
376 WriteString(str);
377}
378
379void wxTextOutputStream::WriteDouble(double d)
380{
381 wxString str;
382
383 str.Printf(wxT("%f"), d);
384 WriteString(str);
385}
386
387void wxTextOutputStream::WriteString(const wxString& string)
388{
389 size_t len = string.length();
390
391 wxString out;
392 out.reserve(len);
393
394 for ( size_t i = 0; i < len; i++ )
395 {
396 const wxChar c = string[i];
397 if ( c == wxT('\n') )
398 {
399 switch ( m_mode )
400 {
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 ;
416 }
417 }
418
419 out << c;
420 }
421
422#if wxUSE_UNICODE
423 // FIXME-UTF8: use wxCharBufferWithLength if/when we have it
424 wxCharBuffer buffer = m_conv->cWC2MB(out.wc_str(), out.length(), &len);
425 m_output.Write(buffer, len);
426#else
427 m_output.Write(out.c_str(), out.length() );
428#endif
429}
430
431wxTextOutputStream& wxTextOutputStream::PutChar(wxChar c)
432{
433#if wxUSE_UNICODE
434 WriteString( wxString(&c, *m_conv, 1) );
435#else
436 WriteString( wxString(&c, wxConvLocal, 1) );
437#endif
438 return *this;
439}
440
441wxTextOutputStream& wxTextOutputStream::operator<<(const wxString& string)
442{
443 WriteString( string );
444 return *this;
445}
446
447wxTextOutputStream& wxTextOutputStream::operator<<(char c)
448{
449 WriteString( wxString::FromAscii(c) );
450
451 return *this;
452}
453
454#if wxUSE_UNICODE && wxWCHAR_T_IS_REAL_TYPE
455
456wxTextOutputStream& wxTextOutputStream::operator<<(wchar_t wc)
457{
458 WriteString( wxString(&wc, *m_conv, 1) );
459
460 return *this;
461}
462
463#endif // wxUSE_UNICODE
464
465wxTextOutputStream& wxTextOutputStream::operator<<(wxInt16 c)
466{
467 wxString str;
468 str.Printf(wxT("%d"), (signed int)c);
469 WriteString(str);
470
471 return *this;
472}
473
474wxTextOutputStream& wxTextOutputStream::operator<<(wxInt32 c)
475{
476 wxString str;
477 str.Printf(wxT("%ld"), (signed long)c);
478 WriteString(str);
479
480 return *this;
481}
482
483wxTextOutputStream& wxTextOutputStream::operator<<(wxUint16 c)
484{
485 wxString str;
486 str.Printf(wxT("%u"), (unsigned int)c);
487 WriteString(str);
488
489 return *this;
490}
491
492wxTextOutputStream& wxTextOutputStream::operator<<(wxUint32 c)
493{
494 wxString str;
495 str.Printf(wxT("%lu"), (unsigned long)c);
496 WriteString(str);
497
498 return *this;
499}
500
501wxTextOutputStream &wxTextOutputStream::operator<<(double f)
502{
503 WriteDouble(f);
504 return *this;
505}
506
507wxTextOutputStream& wxTextOutputStream::operator<<(float f)
508{
509 WriteDouble((double)f);
510 return *this;
511}
512
513wxTextOutputStream &endl( wxTextOutputStream &stream )
514{
515 return stream.PutChar(wxT('\n'));
516}
517
518#endif
519 // wxUSE_STREAMS