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