]>
Commit | Line | Data |
---|---|---|
5a96d2f4 | 1 | /////////////////////////////////////////////////////////////////////////////// |
40ff126a | 2 | // Name: src/common/txtstrm.cpp |
fae05df5 GL |
3 | // Purpose: Text stream classes |
4 | // Author: Guilhem Lavaux | |
5 | // Modified by: | |
6 | // Created: 28/06/98 | |
7 | // RCS-ID: $Id$ | |
717b9bf2 | 8 | // Copyright: (c) Guilhem Lavaux |
65571936 | 9 | // Licence: wxWindows licence |
fae05df5 GL |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
fae05df5 GL |
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 | ||
c22eea9c PC |
21 | #include "wx/txtstrm.h" |
22 | ||
0bf751e7 VS |
23 | #ifndef WX_PRECOMP |
24 | #include "wx/crt.h" | |
25 | #endif | |
26 | ||
c980c992 | 27 | #include <ctype.h> |
fae05df5 | 28 | |
cd25b18c RR |
29 | // ---------------------------------------------------------------------------- |
30 | // wxTextInputStream | |
31 | // ---------------------------------------------------------------------------- | |
32 | ||
2b5f62a0 | 33 | #if wxUSE_UNICODE |
830f8f11 VZ |
34 | wxTextInputStream::wxTextInputStream(wxInputStream &s, |
35 | const wxString &sep, | |
36 | const wxMBConv& conv) | |
d36c9347 | 37 | : m_input(s), m_separators(sep), m_conv(conv.Clone()) |
2b5f62a0 | 38 | { |
2348a842 | 39 | memset((void*)m_lastBytes, 0, 10); |
2b5f62a0 VZ |
40 | } |
41 | #else | |
191549ed SB |
42 | wxTextInputStream::wxTextInputStream(wxInputStream &s, const wxString &sep) |
43 | : m_input(s), m_separators(sep) | |
fae05df5 | 44 | { |
2348a842 | 45 | memset((void*)m_lastBytes, 0, 10); |
fae05df5 | 46 | } |
2b5f62a0 | 47 | #endif |
fae05df5 GL |
48 | |
49 | wxTextInputStream::~wxTextInputStream() | |
50 | { | |
d36c9347 VZ |
51 | #if wxUSE_UNICODE |
52 | delete m_conv; | |
53 | #endif // wxUSE_UNICODE | |
fae05df5 GL |
54 | } |
55 | ||
2348a842 VZ |
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); | |
cb719f2e | 70 | for(size_t inlen = 0; inlen < 9; inlen++) |
2348a842 VZ |
71 | { |
72 | // actually read the next character | |
73 | m_lastBytes[inlen] = m_input.GetC(); | |
74 | ||
cb719f2e | 75 | if(m_input.LastRead() <= 0) |
2348a842 | 76 | return wxEOT; |
cb719f2e | 77 | |
d7f73361 VZ |
78 | if ( m_conv->ToWChar(wbuf, WXSIZEOF(wbuf), m_lastBytes, inlen + 1) |
79 | != wxCONV_FAILED ) | |
2348a842 VZ |
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(); | |
cb719f2e WS |
86 | |
87 | if(m_input.LastRead() <= 0) | |
2348a842 | 88 | return wxEOT; |
cb719f2e | 89 | |
2348a842 VZ |
90 | return m_lastBytes[0]; |
91 | #endif | |
cb719f2e | 92 | |
2348a842 VZ |
93 | } |
94 | ||
191549ed | 95 | wxChar wxTextInputStream::NextNonSeparators() |
fae05df5 | 96 | { |
cd25b18c RR |
97 | for (;;) |
98 | { | |
999836aa | 99 | wxChar c = NextChar(); |
2348a842 | 100 | if (c == wxEOT) return (wxChar) 0; |
cd6ce4a9 VZ |
101 | |
102 | if (c != wxT('\n') && | |
103 | c != wxT('\r') && | |
c22eea9c | 104 | m_separators.Find(c) < 0) |
cd6ce4a9 | 105 | return c; |
cd25b18c | 106 | } |
717b9bf2 | 107 | |
cd25b18c | 108 | } |
fae05df5 | 109 | |
f6bcfd97 | 110 | bool wxTextInputStream::EatEOL(const wxChar &c) |
cd25b18c | 111 | { |
cb719f2e | 112 | if (c == wxT('\n')) return true; // eat on UNIX |
cd6ce4a9 | 113 | |
f6bcfd97 | 114 | if (c == wxT('\r')) // eat on both Mac and DOS |
717b9bf2 | 115 | { |
2348a842 | 116 | wxChar c2 = NextChar(); |
cb719f2e | 117 | if(c2 == wxEOT) return true; // end of stream reached, had enough :-) |
cd6ce4a9 | 118 | |
2348a842 | 119 | if (c2 != wxT('\n')) UngetLast(); // Don't eat on Mac |
cb719f2e | 120 | return true; |
cd25b18c | 121 | } |
717b9bf2 | 122 | |
cb719f2e | 123 | return false; |
191549ed SB |
124 | } |
125 | ||
2348a842 | 126 | wxUint32 wxTextInputStream::Read32(int base) |
191549ed | 127 | { |
2348a842 VZ |
128 | wxASSERT_MSG( !base || (base > 1 && base <= 36), _T("invalid base") ); |
129 | if(!m_input) return 0; | |
130 | ||
131 | wxString word = ReadWord(); | |
7448de8d | 132 | if(word.empty()) |
2348a842 VZ |
133 | return 0; |
134 | return wxStrtoul(word.c_str(), 0, base); | |
cd25b18c | 135 | } |
fae05df5 | 136 | |
2348a842 | 137 | wxUint16 wxTextInputStream::Read16(int base) |
cd25b18c | 138 | { |
2348a842 VZ |
139 | return (wxUint16)Read32(base); |
140 | } | |
cd6ce4a9 | 141 | |
2348a842 VZ |
142 | wxUint8 wxTextInputStream::Read8(int base) |
143 | { | |
144 | return (wxUint8)Read32(base); | |
145 | } | |
717b9bf2 | 146 | |
2348a842 VZ |
147 | wxInt32 wxTextInputStream::Read32S(int base) |
148 | { | |
149 | wxASSERT_MSG( !base || (base > 1 && base <= 36), _T("invalid base") ); | |
150 | if(!m_input) return 0; | |
cd25b18c | 151 | |
2348a842 | 152 | wxString word = ReadWord(); |
7448de8d | 153 | if(word.empty()) |
cd25b18c | 154 | return 0; |
2348a842 | 155 | return wxStrtol(word.c_str(), 0, base); |
fae05df5 GL |
156 | } |
157 | ||
2348a842 | 158 | wxInt16 wxTextInputStream::Read16S(int base) |
fae05df5 | 159 | { |
2348a842 | 160 | return (wxInt16)Read32S(base); |
fae05df5 GL |
161 | } |
162 | ||
2348a842 | 163 | wxInt8 wxTextInputStream::Read8S(int base) |
fae05df5 | 164 | { |
2348a842 | 165 | return (wxInt8)Read32S(base); |
fae05df5 GL |
166 | } |
167 | ||
168 | double wxTextInputStream::ReadDouble() | |
169 | { | |
2348a842 VZ |
170 | if(!m_input) return 0; |
171 | wxString word = ReadWord(); | |
7448de8d | 172 | if(word.empty()) |
f6bcfd97 | 173 | return 0; |
2348a842 | 174 | return wxStrtod(word.c_str(), 0); |
fae05df5 GL |
175 | } |
176 | ||
40ff126a WS |
177 | #if WXWIN_COMPATIBILITY_2_6 |
178 | ||
fae05df5 | 179 | wxString wxTextInputStream::ReadString() |
9853d977 | 180 | { |
cd6ce4a9 | 181 | return ReadLine(); |
9853d977 SB |
182 | } |
183 | ||
40ff126a WS |
184 | #endif // WXWIN_COMPATIBILITY_2_6 |
185 | ||
9853d977 | 186 | wxString wxTextInputStream::ReadLine() |
fae05df5 | 187 | { |
cd25b18c RR |
188 | wxString line; |
189 | ||
cd6ce4a9 | 190 | while ( !m_input.Eof() ) |
cd25b18c | 191 | { |
2348a842 VZ |
192 | wxChar c = NextChar(); |
193 | if(c == wxEOT) | |
194 | break; | |
cb719f2e | 195 | |
cd6ce4a9 VZ |
196 | if (EatEOL(c)) |
197 | break; | |
198 | ||
cd25b18c RR |
199 | line += c; |
200 | } | |
717b9bf2 | 201 | |
cd25b18c | 202 | return line; |
fae05df5 | 203 | } |
717b9bf2 | 204 | |
9853d977 SB |
205 | wxString wxTextInputStream::ReadWord() |
206 | { | |
9853d977 | 207 | wxString word; |
9853d977 | 208 | |
cd6ce4a9 VZ |
209 | if ( !m_input ) |
210 | return word; | |
211 | ||
212 | wxChar c = NextNonSeparators(); | |
213 | if ( !c ) | |
214 | return word; | |
215 | ||
f6bcfd97 | 216 | word += c; |
cb719f2e | 217 | |
cd6ce4a9 | 218 | while ( !m_input.Eof() ) |
9853d977 | 219 | { |
2348a842 VZ |
220 | c = NextChar(); |
221 | if(c == wxEOT) | |
f6bcfd97 | 222 | break; |
cb719f2e | 223 | |
c22eea9c | 224 | if (m_separators.Find(c) >= 0) |
cd6ce4a9 VZ |
225 | break; |
226 | ||
227 | if (EatEOL(c)) | |
228 | break; | |
229 | ||
9853d977 | 230 | word += c; |
9853d977 SB |
231 | } |
232 | ||
233 | return word; | |
234 | } | |
235 | ||
236 | wxTextInputStream& wxTextInputStream::operator>>(wxString& word) | |
fae05df5 | 237 | { |
cd6ce4a9 VZ |
238 | word = ReadWord(); |
239 | return *this; | |
fae05df5 GL |
240 | } |
241 | ||
f6bcfd97 | 242 | wxTextInputStream& wxTextInputStream::operator>>(char& c) |
fae05df5 | 243 | { |
191549ed | 244 | c = m_input.GetC(); |
2348a842 | 245 | if(m_input.LastRead() <= 0) c = 0; |
717b9bf2 | 246 | |
f6bcfd97 BP |
247 | if (EatEOL(c)) |
248 | c = '\n'; | |
249 | ||
cd25b18c | 250 | return *this; |
fae05df5 GL |
251 | } |
252 | ||
fcbe123e VZ |
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 | ||
fae05df5 GL |
264 | wxTextInputStream& wxTextInputStream::operator>>(wxInt16& i) |
265 | { | |
cd25b18c RR |
266 | i = (wxInt16)Read16(); |
267 | return *this; | |
fae05df5 GL |
268 | } |
269 | ||
270 | wxTextInputStream& wxTextInputStream::operator>>(wxInt32& i) | |
271 | { | |
cd25b18c RR |
272 | i = (wxInt32)Read32(); |
273 | return *this; | |
fae05df5 GL |
274 | } |
275 | ||
fae05df5 GL |
276 | wxTextInputStream& wxTextInputStream::operator>>(wxUint16& i) |
277 | { | |
cd25b18c RR |
278 | i = Read16(); |
279 | return *this; | |
fae05df5 GL |
280 | } |
281 | ||
282 | wxTextInputStream& wxTextInputStream::operator>>(wxUint32& i) | |
283 | { | |
cd25b18c RR |
284 | i = Read32(); |
285 | return *this; | |
fae05df5 GL |
286 | } |
287 | ||
288 | wxTextInputStream& wxTextInputStream::operator>>(double& i) | |
289 | { | |
cd25b18c RR |
290 | i = ReadDouble(); |
291 | return *this; | |
fae05df5 GL |
292 | } |
293 | ||
294 | wxTextInputStream& wxTextInputStream::operator>>(float& f) | |
295 | { | |
cd25b18c RR |
296 | f = (float)ReadDouble(); |
297 | return *this; | |
fae05df5 GL |
298 | } |
299 | ||
2b5f62a0 VZ |
300 | |
301 | ||
302 | #if wxUSE_UNICODE | |
830f8f11 VZ |
303 | wxTextOutputStream::wxTextOutputStream(wxOutputStream& s, |
304 | wxEOL mode, | |
305 | const wxMBConv& conv) | |
d36c9347 | 306 | : m_output(s), m_conv(conv.Clone()) |
2b5f62a0 | 307 | #else |
c7a9fa36 | 308 | wxTextOutputStream::wxTextOutputStream(wxOutputStream& s, wxEOL mode) |
191549ed | 309 | : m_output(s) |
2b5f62a0 | 310 | #endif |
fae05df5 | 311 | { |
c7a9fa36 RR |
312 | m_mode = mode; |
313 | if (m_mode == wxEOL_NATIVE) | |
314 | { | |
315 | #if defined(__WXMSW__) || defined(__WXPM__) | |
316 | m_mode = wxEOL_DOS; | |
c7a9fa36 RR |
317 | #else |
318 | m_mode = wxEOL_UNIX; | |
319 | #endif | |
320 | } | |
fae05df5 GL |
321 | } |
322 | ||
323 | wxTextOutputStream::~wxTextOutputStream() | |
324 | { | |
d36c9347 VZ |
325 | #if wxUSE_UNICODE |
326 | delete m_conv; | |
327 | #endif // wxUSE_UNICODE | |
fae05df5 GL |
328 | } |
329 | ||
cd0b1709 | 330 | void wxTextOutputStream::SetMode(wxEOL mode) |
c7a9fa36 RR |
331 | { |
332 | m_mode = mode; | |
333 | if (m_mode == wxEOL_NATIVE) | |
334 | { | |
335 | #if defined(__WXMSW__) || defined(__WXPM__) | |
336 | m_mode = wxEOL_DOS; | |
c7a9fa36 RR |
337 | #else |
338 | m_mode = wxEOL_UNIX; | |
339 | #endif | |
340 | } | |
341 | } | |
342 | ||
fae05df5 GL |
343 | void wxTextOutputStream::Write32(wxUint32 i) |
344 | { | |
cd25b18c | 345 | wxString str; |
223d09f6 | 346 | str.Printf(wxT("%u"), i); |
717b9bf2 | 347 | |
cd25b18c | 348 | WriteString(str); |
fae05df5 GL |
349 | } |
350 | ||
351 | void wxTextOutputStream::Write16(wxUint16 i) | |
352 | { | |
cd25b18c | 353 | wxString str; |
17a1ebd1 | 354 | str.Printf(wxT("%u"), (unsigned)i); |
717b9bf2 | 355 | |
cd25b18c | 356 | WriteString(str); |
fae05df5 GL |
357 | } |
358 | ||
359 | void wxTextOutputStream::Write8(wxUint8 i) | |
360 | { | |
cd25b18c | 361 | wxString str; |
17a1ebd1 | 362 | str.Printf(wxT("%u"), (unsigned)i); |
717b9bf2 | 363 | |
cd25b18c | 364 | WriteString(str); |
fae05df5 GL |
365 | } |
366 | ||
367 | void wxTextOutputStream::WriteDouble(double d) | |
368 | { | |
cd25b18c | 369 | wxString str; |
fae05df5 | 370 | |
223d09f6 | 371 | str.Printf(wxT("%f"), d); |
cd25b18c | 372 | WriteString(str); |
fae05df5 GL |
373 | } |
374 | ||
375 | void wxTextOutputStream::WriteString(const wxString& string) | |
376 | { | |
20ea6894 VZ |
377 | size_t len = string.length(); |
378 | ||
379 | wxString out; | |
380 | out.reserve(len); | |
381 | ||
382 | for ( size_t i = 0; i < len; i++ ) | |
cd25b18c | 383 | { |
20ea6894 VZ |
384 | const wxChar c = string[i]; |
385 | if ( c == wxT('\n') ) | |
cd25b18c | 386 | { |
20ea6894 | 387 | switch ( m_mode ) |
cd6ce4a9 | 388 | { |
20ea6894 VZ |
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 | ; | |
c7a9fa36 | 404 | } |
cd25b18c | 405 | } |
20ea6894 VZ |
406 | |
407 | out << c; | |
7448de8d | 408 | } |
20ea6894 | 409 | |
2b5f62a0 | 410 | #if wxUSE_UNICODE |
86501081 VS |
411 | // FIXME-UTF8: use wxCharBufferWithLength if/when we have it |
412 | wxCharBuffer buffer = m_conv->cWC2MB(out.wc_str(), out.length(), &len); | |
bfaee57e | 413 | m_output.Write(buffer, len); |
2b5f62a0 VZ |
414 | #else |
415 | m_output.Write(out.c_str(), out.length() ); | |
416 | #endif | |
fae05df5 GL |
417 | } |
418 | ||
ba854691 RN |
419 | wxTextOutputStream& wxTextOutputStream::PutChar(wxChar c) |
420 | { | |
421 | #if wxUSE_UNICODE | |
d36c9347 | 422 | WriteString( wxString(&c, *m_conv, 1) ); |
ba854691 RN |
423 | #else |
424 | WriteString( wxString(&c, wxConvLocal, 1) ); | |
425 | #endif | |
426 | return *this; | |
427 | } | |
428 | ||
fae05df5 GL |
429 | wxTextOutputStream& wxTextOutputStream::operator<<(const wxString& string) |
430 | { | |
cd25b18c RR |
431 | WriteString( string ); |
432 | return *this; | |
fae05df5 GL |
433 | } |
434 | ||
f6bcfd97 | 435 | wxTextOutputStream& wxTextOutputStream::operator<<(char c) |
fae05df5 | 436 | { |
2b5f62a0 | 437 | WriteString( wxString::FromAscii(c) ); |
cb719f2e | 438 | |
cd25b18c | 439 | return *this; |
fae05df5 GL |
440 | } |
441 | ||
e4940feb | 442 | #if wxUSE_UNICODE && wxWCHAR_T_IS_REAL_TYPE |
3ca1bf5a VZ |
443 | |
444 | wxTextOutputStream& wxTextOutputStream::operator<<(wchar_t wc) | |
445 | { | |
d36c9347 | 446 | WriteString( wxString(&wc, *m_conv, 1) ); |
65a1bb98 VZ |
447 | |
448 | return *this; | |
3ca1bf5a VZ |
449 | } |
450 | ||
e4940feb | 451 | #endif // wxUSE_UNICODE |
3ca1bf5a | 452 | |
fae05df5 GL |
453 | wxTextOutputStream& wxTextOutputStream::operator<<(wxInt16 c) |
454 | { | |
78e848ca RR |
455 | wxString str; |
456 | str.Printf(wxT("%d"), (signed int)c); | |
457 | WriteString(str); | |
cd6ce4a9 | 458 | |
cd25b18c | 459 | return *this; |
fae05df5 GL |
460 | } |
461 | ||
462 | wxTextOutputStream& wxTextOutputStream::operator<<(wxInt32 c) | |
463 | { | |
78e848ca RR |
464 | wxString str; |
465 | str.Printf(wxT("%ld"), (signed long)c); | |
466 | WriteString(str); | |
cd6ce4a9 | 467 | |
cd25b18c | 468 | return *this; |
fae05df5 GL |
469 | } |
470 | ||
fae05df5 GL |
471 | wxTextOutputStream& wxTextOutputStream::operator<<(wxUint16 c) |
472 | { | |
78e848ca RR |
473 | wxString str; |
474 | str.Printf(wxT("%u"), (unsigned int)c); | |
475 | WriteString(str); | |
cd6ce4a9 | 476 | |
cd25b18c | 477 | return *this; |
fae05df5 GL |
478 | } |
479 | ||
480 | wxTextOutputStream& wxTextOutputStream::operator<<(wxUint32 c) | |
481 | { | |
78e848ca RR |
482 | wxString str; |
483 | str.Printf(wxT("%lu"), (unsigned long)c); | |
484 | WriteString(str); | |
485 | ||
cd25b18c | 486 | return *this; |
fae05df5 GL |
487 | } |
488 | ||
489 | wxTextOutputStream &wxTextOutputStream::operator<<(double f) | |
490 | { | |
cd25b18c RR |
491 | WriteDouble(f); |
492 | return *this; | |
fae05df5 GL |
493 | } |
494 | ||
495 | wxTextOutputStream& wxTextOutputStream::operator<<(float f) | |
496 | { | |
cd25b18c RR |
497 | WriteDouble((double)f); |
498 | return *this; | |
fae05df5 GL |
499 | } |
500 | ||
ed58dbea RR |
501 | wxTextOutputStream &endl( wxTextOutputStream &stream ) |
502 | { | |
ba854691 | 503 | return stream.PutChar(wxT('\n')); |
ed58dbea RR |
504 | } |
505 | ||
fae05df5 GL |
506 | #endif |
507 | // wxUSE_STREAMS |