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