]> git.saurik.com Git - wxWidgets.git/blob - src/common/txtstrm.cpp
added wxMBConv::Clone() to be able to copy conversion objects polymorphically
[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 int retlen = (int) m_conv->MB2WC(wbuf, m_lastBytes, 2); // returns -1 for failure
83 if(retlen >= 0) // res == 0 could happen for '\0' char
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 // We must not write the trailing NULL here
418 #if wxUSE_UNICODE
419 wxCharBuffer buffer = m_conv->cWC2MB( out );
420 m_output.Write( (const char*) buffer, strlen( (const char*) buffer ) );
421 #else
422 m_output.Write(out.c_str(), out.length() );
423 #endif
424 }
425
426 wxTextOutputStream& wxTextOutputStream::PutChar(wxChar c)
427 {
428 #if wxUSE_UNICODE
429 WriteString( wxString(&c, *m_conv, 1) );
430 #else
431 WriteString( wxString(&c, wxConvLocal, 1) );
432 #endif
433 return *this;
434 }
435
436 wxTextOutputStream& wxTextOutputStream::operator<<(const wxChar *string)
437 {
438 WriteString( wxString(string) );
439 return *this;
440 }
441
442 wxTextOutputStream& wxTextOutputStream::operator<<(const wxString& string)
443 {
444 WriteString( string );
445 return *this;
446 }
447
448 wxTextOutputStream& wxTextOutputStream::operator<<(char c)
449 {
450 WriteString( wxString::FromAscii(c) );
451
452 return *this;
453 }
454
455 #if wxUSE_UNICODE && wxWCHAR_T_IS_REAL_TYPE
456
457 wxTextOutputStream& wxTextOutputStream::operator<<(wchar_t wc)
458 {
459 WriteString( wxString(&wc, *m_conv, 1) );
460
461 return *this;
462 }
463
464 #endif // wxUSE_UNICODE
465
466 wxTextOutputStream& wxTextOutputStream::operator<<(wxInt16 c)
467 {
468 wxString str;
469 str.Printf(wxT("%d"), (signed int)c);
470 WriteString(str);
471
472 return *this;
473 }
474
475 wxTextOutputStream& wxTextOutputStream::operator<<(wxInt32 c)
476 {
477 wxString str;
478 str.Printf(wxT("%ld"), (signed long)c);
479 WriteString(str);
480
481 return *this;
482 }
483
484 wxTextOutputStream& wxTextOutputStream::operator<<(wxUint16 c)
485 {
486 wxString str;
487 str.Printf(wxT("%u"), (unsigned int)c);
488 WriteString(str);
489
490 return *this;
491 }
492
493 wxTextOutputStream& wxTextOutputStream::operator<<(wxUint32 c)
494 {
495 wxString str;
496 str.Printf(wxT("%lu"), (unsigned long)c);
497 WriteString(str);
498
499 return *this;
500 }
501
502 wxTextOutputStream &wxTextOutputStream::operator<<(double f)
503 {
504 WriteDouble(f);
505 return *this;
506 }
507
508 wxTextOutputStream& wxTextOutputStream::operator<<(float f)
509 {
510 WriteDouble((double)f);
511 return *this;
512 }
513
514 wxTextOutputStream &endl( wxTextOutputStream &stream )
515 {
516 return stream.PutChar(wxT('\n'));
517 }
518
519 #endif
520 // wxUSE_STREAMS