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