]> git.saurik.com Git - wxWidgets.git/blob - src/common/txtstrm.cpp
wxCocoa: Added preliminary Blit support
[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 #ifdef __GNUG__
13 #pragma implementation "txtstrm.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #if wxUSE_STREAMS
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
42 wxTextInputStream::wxTextInputStream(wxInputStream &s, const wxString &sep, wxMBConv& conv)
43 : m_input(s), m_separators(sep), m_conv(conv)
44 {
45 memset((void*)m_lastBytes, 0, 10);
46 }
47 #else
48 wxTextInputStream::wxTextInputStream(wxInputStream &s, const wxString &sep)
49 : m_input(s), m_separators(sep)
50 {
51 memset((void*)m_lastBytes, 0, 10);
52 }
53 #endif
54
55 wxTextInputStream::~wxTextInputStream()
56 {
57 }
58
59 void wxTextInputStream::UngetLast()
60 {
61 size_t byteCount = 0;
62 while(m_lastBytes[byteCount]) // pseudo ANSI strlen (even for Unicode!)
63 byteCount++;
64 m_input.Ungetch(m_lastBytes, byteCount);
65 memset((void*)m_lastBytes, 0, 10);
66 }
67
68 wxChar wxTextInputStream::NextChar()
69 {
70 #if wxUSE_UNICODE
71 wxChar wbuf[2];
72 memset((void*)m_lastBytes, 0, 10);
73 for(size_t inlen = 0; inlen < 9; inlen++)
74 {
75 // actually read the next character
76 m_lastBytes[inlen] = m_input.GetC();
77
78 if(m_input.LastRead() <= 0)
79 return wxEOT;
80
81 int retlen = (int) m_conv.MB2WC(wbuf, m_lastBytes, 2); // returns -1 for failure
82 if(retlen >= 0) // res == 0 could happen for '\0' char
83 return wbuf[0];
84 }
85 // there should be no encoding which requires more than nine bytes for one character...
86 return wxEOT;
87 #else
88 m_lastBytes[0] = m_input.GetC();
89
90 if(m_input.LastRead() <= 0)
91 return wxEOT;
92
93 return m_lastBytes[0];
94 #endif
95
96 }
97
98 wxChar wxTextInputStream::NextNonSeparators()
99 {
100 wxChar c = (wxChar) 0;
101 for (;;)
102 {
103 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.IsEmpty())
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.IsEmpty())
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.IsEmpty())
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 wxTextInputStream& wxTextInputStream::operator>>(wxInt16& i)
257 {
258 i = (wxInt16)Read16();
259 return *this;
260 }
261
262 wxTextInputStream& wxTextInputStream::operator>>(wxInt32& i)
263 {
264 i = (wxInt32)Read32();
265 return *this;
266 }
267
268 wxTextInputStream& wxTextInputStream::operator>>(wxUint16& i)
269 {
270 i = Read16();
271 return *this;
272 }
273
274 wxTextInputStream& wxTextInputStream::operator>>(wxUint32& i)
275 {
276 i = Read32();
277 return *this;
278 }
279
280 wxTextInputStream& wxTextInputStream::operator>>(double& i)
281 {
282 i = ReadDouble();
283 return *this;
284 }
285
286 wxTextInputStream& wxTextInputStream::operator>>(float& f)
287 {
288 f = (float)ReadDouble();
289 return *this;
290 }
291
292
293
294 #if wxUSE_UNICODE
295 wxTextOutputStream::wxTextOutputStream(wxOutputStream& s, wxEOL mode, wxMBConv& conv)
296 : m_output(s), m_conv(conv)
297 #else
298 wxTextOutputStream::wxTextOutputStream(wxOutputStream& s, wxEOL mode)
299 : m_output(s)
300 #endif
301 {
302 m_mode = mode;
303 if (m_mode == wxEOL_NATIVE)
304 {
305 #if defined(__WXMSW__) || defined(__WXPM__)
306 m_mode = wxEOL_DOS;
307 #elif defined(__WXMAC__) && !defined(__DARWIN__)
308 m_mode = wxEOL_MAC;
309 #else
310 m_mode = wxEOL_UNIX;
311 #endif
312 }
313 }
314
315 wxTextOutputStream::~wxTextOutputStream()
316 {
317 }
318
319 void wxTextOutputStream::SetMode(wxEOL mode)
320 {
321 m_mode = mode;
322 if (m_mode == wxEOL_NATIVE)
323 {
324 #if defined(__WXMSW__) || defined(__WXPM__)
325 m_mode = wxEOL_DOS;
326 #elif defined(__WXMAC__) && !defined(__DARWIN__)
327 m_mode = wxEOL_MAC;
328 #else
329 m_mode = wxEOL_UNIX;
330 #endif
331 }
332 }
333
334 void wxTextOutputStream::Write32(wxUint32 i)
335 {
336 wxString str;
337 str.Printf(wxT("%u"), i);
338
339 WriteString(str);
340 }
341
342 void wxTextOutputStream::Write16(wxUint16 i)
343 {
344 wxString str;
345 str.Printf(wxT("%u"), i);
346
347 WriteString(str);
348 }
349
350 void wxTextOutputStream::Write8(wxUint8 i)
351 {
352 wxString str;
353 str.Printf(wxT("%u"), i);
354
355 WriteString(str);
356 }
357
358 void wxTextOutputStream::WriteDouble(double d)
359 {
360 wxString str;
361
362 str.Printf(wxT("%f"), d);
363 WriteString(str);
364 }
365
366 void wxTextOutputStream::WriteString(const wxString& string)
367 {
368 size_t len = string.length();
369
370 wxString out;
371 out.reserve(len);
372
373 for ( size_t i = 0; i < len; i++ )
374 {
375 const wxChar c = string[i];
376 if ( c == wxT('\n') )
377 {
378 switch ( m_mode )
379 {
380 case wxEOL_DOS:
381 out << _T("\r\n");
382 continue;
383
384 case wxEOL_MAC:
385 out << _T('\r');
386 continue;
387
388 default:
389 wxFAIL_MSG( _T("unknown EOL mode in wxTextOutputStream") );
390 // fall through
391
392 case wxEOL_UNIX:
393 // don't treat '\n' specially
394 ;
395 }
396 }
397
398 out << c;
399 }
400
401 // We must not write the trailing NULL here
402 #if wxUSE_UNICODE
403 wxCharBuffer buffer = m_conv.cWC2MB( out );
404 m_output.Write( (const char*) buffer, strlen( (const char*) buffer ) );
405 #else
406 m_output.Write(out.c_str(), out.length() );
407 #endif
408 }
409
410 wxTextOutputStream& wxTextOutputStream::operator<<(const wxChar *string)
411 {
412 WriteString( wxString(string) );
413 return *this;
414 }
415
416 wxTextOutputStream& wxTextOutputStream::operator<<(const wxString& string)
417 {
418 WriteString( string );
419 return *this;
420 }
421
422 wxTextOutputStream& wxTextOutputStream::operator<<(char c)
423 {
424 WriteString( wxString::FromAscii(c) );
425
426 return *this;
427 }
428
429 wxTextOutputStream& wxTextOutputStream::operator<<(wxInt16 c)
430 {
431 wxString str;
432 str.Printf(wxT("%d"), (signed int)c);
433 WriteString(str);
434
435 return *this;
436 }
437
438 wxTextOutputStream& wxTextOutputStream::operator<<(wxInt32 c)
439 {
440 wxString str;
441 str.Printf(wxT("%ld"), (signed long)c);
442 WriteString(str);
443
444 return *this;
445 }
446
447 wxTextOutputStream& wxTextOutputStream::operator<<(wxUint16 c)
448 {
449 wxString str;
450 str.Printf(wxT("%u"), (unsigned int)c);
451 WriteString(str);
452
453 return *this;
454 }
455
456 wxTextOutputStream& wxTextOutputStream::operator<<(wxUint32 c)
457 {
458 wxString str;
459 str.Printf(wxT("%lu"), (unsigned long)c);
460 WriteString(str);
461
462 return *this;
463 }
464
465 wxTextOutputStream &wxTextOutputStream::operator<<(double f)
466 {
467 WriteDouble(f);
468 return *this;
469 }
470
471 wxTextOutputStream& wxTextOutputStream::operator<<(float f)
472 {
473 WriteDouble((double)f);
474 return *this;
475 }
476
477 wxTextOutputStream &endl( wxTextOutputStream &stream )
478 {
479 return stream << wxT('\n');
480 }
481
482 #endif
483 // wxUSE_STREAMS