]> git.saurik.com Git - wxWidgets.git/blob - src/common/txtstrm.cpp
only use Mac-specific menu item under Mac
[wxWidgets.git] / src / common / txtstrm.cpp
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
34 wxTextInputStream::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
42 wxTextInputStream::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
49 wxTextInputStream::~wxTextInputStream()
50 {
51 #if wxUSE_UNICODE
52 delete m_conv;
53 #endif // wxUSE_UNICODE
54 }
55
56 void 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
65 wxChar 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 if ( m_conv->ToWChar(wbuf, WXSIZEOF(wbuf), m_lastBytes, inlen + 1)
79 != wxCONV_FAILED )
80 return wbuf[0];
81 }
82 // there should be no encoding which requires more than nine bytes for one character...
83 return wxEOT;
84 #else
85 m_lastBytes[0] = m_input.GetC();
86
87 if(m_input.LastRead() <= 0)
88 return wxEOT;
89
90 return m_lastBytes[0];
91 #endif
92
93 }
94
95 wxChar wxTextInputStream::NextNonSeparators()
96 {
97 for (;;)
98 {
99 wxChar c = NextChar();
100 if (c == wxEOT) return (wxChar) 0;
101
102 if (c != wxT('\n') &&
103 c != wxT('\r') &&
104 m_separators.Find(c) < 0)
105 return c;
106 }
107
108 }
109
110 bool wxTextInputStream::EatEOL(const wxChar &c)
111 {
112 if (c == wxT('\n')) return true; // eat on UNIX
113
114 if (c == wxT('\r')) // eat on both Mac and DOS
115 {
116 wxChar c2 = NextChar();
117 if(c2 == wxEOT) return true; // end of stream reached, had enough :-)
118
119 if (c2 != wxT('\n')) UngetLast(); // Don't eat on Mac
120 return true;
121 }
122
123 return false;
124 }
125
126 wxUint32 wxTextInputStream::Read32(int base)
127 {
128 wxASSERT_MSG( !base || (base > 1 && base <= 36), _T("invalid base") );
129 if(!m_input) return 0;
130
131 wxString word = ReadWord();
132 if(word.empty())
133 return 0;
134 return wxStrtoul(word.c_str(), 0, base);
135 }
136
137 wxUint16 wxTextInputStream::Read16(int base)
138 {
139 return (wxUint16)Read32(base);
140 }
141
142 wxUint8 wxTextInputStream::Read8(int base)
143 {
144 return (wxUint8)Read32(base);
145 }
146
147 wxInt32 wxTextInputStream::Read32S(int base)
148 {
149 wxASSERT_MSG( !base || (base > 1 && base <= 36), _T("invalid base") );
150 if(!m_input) return 0;
151
152 wxString word = ReadWord();
153 if(word.empty())
154 return 0;
155 return wxStrtol(word.c_str(), 0, base);
156 }
157
158 wxInt16 wxTextInputStream::Read16S(int base)
159 {
160 return (wxInt16)Read32S(base);
161 }
162
163 wxInt8 wxTextInputStream::Read8S(int base)
164 {
165 return (wxInt8)Read32S(base);
166 }
167
168 double wxTextInputStream::ReadDouble()
169 {
170 if(!m_input) return 0;
171 wxString word = ReadWord();
172 if(word.empty())
173 return 0;
174 return wxStrtod(word.c_str(), 0);
175 }
176
177 #if WXWIN_COMPATIBILITY_2_6
178
179 wxString wxTextInputStream::ReadString()
180 {
181 return ReadLine();
182 }
183
184 #endif // WXWIN_COMPATIBILITY_2_6
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 (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.Find(c) >= 0)
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.Clone())
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 #else
318 m_mode = wxEOL_UNIX;
319 #endif
320 }
321 }
322
323 wxTextOutputStream::~wxTextOutputStream()
324 {
325 #if wxUSE_UNICODE
326 delete m_conv;
327 #endif // wxUSE_UNICODE
328 }
329
330 void wxTextOutputStream::SetMode(wxEOL mode)
331 {
332 m_mode = mode;
333 if (m_mode == wxEOL_NATIVE)
334 {
335 #if defined(__WXMSW__) || defined(__WXPM__)
336 m_mode = wxEOL_DOS;
337 #else
338 m_mode = wxEOL_UNIX;
339 #endif
340 }
341 }
342
343 void wxTextOutputStream::Write32(wxUint32 i)
344 {
345 wxString str;
346 str.Printf(wxT("%u"), i);
347
348 WriteString(str);
349 }
350
351 void wxTextOutputStream::Write16(wxUint16 i)
352 {
353 wxString str;
354 str.Printf(wxT("%u"), (unsigned)i);
355
356 WriteString(str);
357 }
358
359 void wxTextOutputStream::Write8(wxUint8 i)
360 {
361 wxString str;
362 str.Printf(wxT("%u"), (unsigned)i);
363
364 WriteString(str);
365 }
366
367 void wxTextOutputStream::WriteDouble(double d)
368 {
369 wxString str;
370
371 str.Printf(wxT("%f"), d);
372 WriteString(str);
373 }
374
375 void wxTextOutputStream::WriteString(const wxString& string)
376 {
377 size_t len = string.length();
378
379 wxString out;
380 out.reserve(len);
381
382 for ( size_t i = 0; i < len; i++ )
383 {
384 const wxChar c = string[i];
385 if ( c == wxT('\n') )
386 {
387 switch ( m_mode )
388 {
389 case wxEOL_DOS:
390 out << _T("\r\n");
391 continue;
392
393 case wxEOL_MAC:
394 out << _T('\r');
395 continue;
396
397 default:
398 wxFAIL_MSG( _T("unknown EOL mode in wxTextOutputStream") );
399 // fall through
400
401 case wxEOL_UNIX:
402 // don't treat '\n' specially
403 ;
404 }
405 }
406
407 out << c;
408 }
409
410 #if wxUSE_UNICODE
411 // FIXME-UTF8: use wxCharBufferWithLength if/when we have it
412 wxCharBuffer buffer = m_conv->cWC2MB(out.wc_str(), out.length(), &len);
413 m_output.Write(buffer, len);
414 #else
415 m_output.Write(out.c_str(), out.length() );
416 #endif
417 }
418
419 wxTextOutputStream& wxTextOutputStream::PutChar(wxChar c)
420 {
421 #if wxUSE_UNICODE
422 WriteString( wxString(&c, *m_conv, 1) );
423 #else
424 WriteString( wxString(&c, wxConvLocal, 1) );
425 #endif
426 return *this;
427 }
428
429 void wxTextOutputStream::Flush()
430 {
431 #if wxUSE_UNICODE
432 const size_t len = m_conv->FromWChar(NULL, 0, L"", 1);
433 if ( len > m_conv->GetMBNulLen() )
434 {
435 wxCharBuffer buf(len);
436 m_conv->FromWChar(buf.data(), len, L"", 1);
437 m_output.Write(buf, len - m_conv->GetMBNulLen());
438 }
439 #endif // wxUSE_UNICODE
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