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