]>
Commit | Line | Data |
---|---|---|
5a96d2f4 | 1 | /////////////////////////////////////////////////////////////////////////////// |
fae05df5 GL |
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$ | |
717b9bf2 | 8 | // Copyright: (c) Guilhem Lavaux |
65571936 | 9 | // Licence: wxWindows licence |
fae05df5 GL |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
14f355c2 | 12 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
fae05df5 GL |
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" | |
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 VZ |
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 | { | |
2348a842 | 45 | memset((void*)m_lastBytes, 0, 10); |
2b5f62a0 VZ |
46 | } |
47 | #else | |
191549ed SB |
48 | wxTextInputStream::wxTextInputStream(wxInputStream &s, const wxString &sep) |
49 | : m_input(s), m_separators(sep) | |
fae05df5 | 50 | { |
2348a842 | 51 | memset((void*)m_lastBytes, 0, 10); |
fae05df5 | 52 | } |
2b5f62a0 | 53 | #endif |
fae05df5 GL |
54 | |
55 | wxTextInputStream::~wxTextInputStream() | |
56 | { | |
57 | } | |
58 | ||
2348a842 VZ |
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); | |
cb719f2e | 73 | for(size_t inlen = 0; inlen < 9; inlen++) |
2348a842 VZ |
74 | { |
75 | // actually read the next character | |
76 | m_lastBytes[inlen] = m_input.GetC(); | |
77 | ||
cb719f2e | 78 | if(m_input.LastRead() <= 0) |
2348a842 | 79 | return wxEOT; |
cb719f2e | 80 | |
2348a842 VZ |
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(); | |
cb719f2e WS |
89 | |
90 | if(m_input.LastRead() <= 0) | |
2348a842 | 91 | return wxEOT; |
cb719f2e | 92 | |
2348a842 VZ |
93 | return m_lastBytes[0]; |
94 | #endif | |
cb719f2e | 95 | |
2348a842 VZ |
96 | } |
97 | ||
191549ed | 98 | wxChar wxTextInputStream::NextNonSeparators() |
fae05df5 | 99 | { |
cd25b18c RR |
100 | for (;;) |
101 | { | |
999836aa | 102 | wxChar c = NextChar(); |
2348a842 | 103 | if (c == wxEOT) return (wxChar) 0; |
cd6ce4a9 VZ |
104 | |
105 | if (c != wxT('\n') && | |
106 | c != wxT('\r') && | |
107 | !m_separators.Contains(c)) | |
108 | return c; | |
cd25b18c | 109 | } |
717b9bf2 | 110 | |
cd25b18c | 111 | } |
fae05df5 | 112 | |
f6bcfd97 | 113 | bool wxTextInputStream::EatEOL(const wxChar &c) |
cd25b18c | 114 | { |
cb719f2e | 115 | if (c == wxT('\n')) return true; // eat on UNIX |
cd6ce4a9 | 116 | |
f6bcfd97 | 117 | if (c == wxT('\r')) // eat on both Mac and DOS |
717b9bf2 | 118 | { |
2348a842 | 119 | wxChar c2 = NextChar(); |
cb719f2e | 120 | if(c2 == wxEOT) return true; // end of stream reached, had enough :-) |
cd6ce4a9 | 121 | |
2348a842 | 122 | if (c2 != wxT('\n')) UngetLast(); // Don't eat on Mac |
cb719f2e | 123 | return true; |
cd25b18c | 124 | } |
717b9bf2 | 125 | |
cb719f2e | 126 | return false; |
191549ed SB |
127 | } |
128 | ||
2348a842 | 129 | wxUint32 wxTextInputStream::Read32(int base) |
191549ed | 130 | { |
2348a842 VZ |
131 | wxASSERT_MSG( !base || (base > 1 && base <= 36), _T("invalid base") ); |
132 | if(!m_input) return 0; | |
133 | ||
134 | wxString word = ReadWord(); | |
135 | if(word.IsEmpty()) | |
136 | return 0; | |
137 | return wxStrtoul(word.c_str(), 0, base); | |
cd25b18c | 138 | } |
fae05df5 | 139 | |
2348a842 | 140 | wxUint16 wxTextInputStream::Read16(int base) |
cd25b18c | 141 | { |
2348a842 VZ |
142 | return (wxUint16)Read32(base); |
143 | } | |
cd6ce4a9 | 144 | |
2348a842 VZ |
145 | wxUint8 wxTextInputStream::Read8(int base) |
146 | { | |
147 | return (wxUint8)Read32(base); | |
148 | } | |
717b9bf2 | 149 | |
2348a842 VZ |
150 | wxInt32 wxTextInputStream::Read32S(int base) |
151 | { | |
152 | wxASSERT_MSG( !base || (base > 1 && base <= 36), _T("invalid base") ); | |
153 | if(!m_input) return 0; | |
cd25b18c | 154 | |
2348a842 VZ |
155 | wxString word = ReadWord(); |
156 | if(word.IsEmpty()) | |
cd25b18c | 157 | return 0; |
2348a842 | 158 | return wxStrtol(word.c_str(), 0, base); |
fae05df5 GL |
159 | } |
160 | ||
2348a842 | 161 | wxInt16 wxTextInputStream::Read16S(int base) |
fae05df5 | 162 | { |
2348a842 | 163 | return (wxInt16)Read32S(base); |
fae05df5 GL |
164 | } |
165 | ||
2348a842 | 166 | wxInt8 wxTextInputStream::Read8S(int base) |
fae05df5 | 167 | { |
2348a842 | 168 | return (wxInt8)Read32S(base); |
fae05df5 GL |
169 | } |
170 | ||
171 | double wxTextInputStream::ReadDouble() | |
172 | { | |
2348a842 VZ |
173 | if(!m_input) return 0; |
174 | wxString word = ReadWord(); | |
175 | if(word.IsEmpty()) | |
f6bcfd97 | 176 | return 0; |
2348a842 | 177 | return wxStrtod(word.c_str(), 0); |
fae05df5 GL |
178 | } |
179 | ||
180 | wxString wxTextInputStream::ReadString() | |
9853d977 | 181 | { |
cd6ce4a9 | 182 | return ReadLine(); |
9853d977 SB |
183 | } |
184 | ||
185 | wxString wxTextInputStream::ReadLine() | |
fae05df5 | 186 | { |
cd25b18c RR |
187 | wxString line; |
188 | ||
cd6ce4a9 | 189 | while ( !m_input.Eof() ) |
cd25b18c | 190 | { |
2348a842 VZ |
191 | wxChar c = NextChar(); |
192 | if(c == wxEOT) | |
193 | break; | |
cb719f2e | 194 | |
cd6ce4a9 VZ |
195 | if ( !m_input ) |
196 | break; | |
197 | ||
198 | if (EatEOL(c)) | |
199 | break; | |
200 | ||
cd25b18c RR |
201 | line += c; |
202 | } | |
717b9bf2 | 203 | |
cd25b18c | 204 | return line; |
fae05df5 | 205 | } |
717b9bf2 | 206 | |
9853d977 SB |
207 | wxString wxTextInputStream::ReadWord() |
208 | { | |
9853d977 | 209 | wxString word; |
9853d977 | 210 | |
cd6ce4a9 VZ |
211 | if ( !m_input ) |
212 | return word; | |
213 | ||
214 | wxChar c = NextNonSeparators(); | |
215 | if ( !c ) | |
216 | return word; | |
217 | ||
f6bcfd97 | 218 | word += c; |
cb719f2e | 219 | |
cd6ce4a9 | 220 | while ( !m_input.Eof() ) |
9853d977 | 221 | { |
2348a842 VZ |
222 | c = NextChar(); |
223 | if(c == wxEOT) | |
f6bcfd97 | 224 | break; |
cb719f2e | 225 | |
cd6ce4a9 VZ |
226 | if (m_separators.Contains(c)) |
227 | break; | |
228 | ||
229 | if (EatEOL(c)) | |
230 | break; | |
231 | ||
9853d977 | 232 | word += c; |
9853d977 SB |
233 | } |
234 | ||
235 | return word; | |
236 | } | |
237 | ||
238 | wxTextInputStream& wxTextInputStream::operator>>(wxString& word) | |
fae05df5 | 239 | { |
cd6ce4a9 VZ |
240 | word = ReadWord(); |
241 | return *this; | |
fae05df5 GL |
242 | } |
243 | ||
f6bcfd97 | 244 | wxTextInputStream& wxTextInputStream::operator>>(char& c) |
fae05df5 | 245 | { |
191549ed | 246 | c = m_input.GetC(); |
2348a842 | 247 | if(m_input.LastRead() <= 0) c = 0; |
717b9bf2 | 248 | |
f6bcfd97 BP |
249 | if (EatEOL(c)) |
250 | c = '\n'; | |
251 | ||
cd25b18c | 252 | return *this; |
fae05df5 GL |
253 | } |
254 | ||
fcbe123e VZ |
255 | #if wxUSE_UNICODE && wxWCHAR_T_IS_REAL_TYPE |
256 | ||
257 | wxTextInputStream& wxTextInputStream::operator>>(wchar_t& wc) | |
258 | { | |
259 | wc = GetChar(); | |
260 | ||
261 | return *this; | |
262 | } | |
263 | ||
264 | #endif // wxUSE_UNICODE | |
265 | ||
fae05df5 GL |
266 | wxTextInputStream& wxTextInputStream::operator>>(wxInt16& i) |
267 | { | |
cd25b18c RR |
268 | i = (wxInt16)Read16(); |
269 | return *this; | |
fae05df5 GL |
270 | } |
271 | ||
272 | wxTextInputStream& wxTextInputStream::operator>>(wxInt32& i) | |
273 | { | |
cd25b18c RR |
274 | i = (wxInt32)Read32(); |
275 | return *this; | |
fae05df5 GL |
276 | } |
277 | ||
fae05df5 GL |
278 | wxTextInputStream& wxTextInputStream::operator>>(wxUint16& i) |
279 | { | |
cd25b18c RR |
280 | i = Read16(); |
281 | return *this; | |
fae05df5 GL |
282 | } |
283 | ||
284 | wxTextInputStream& wxTextInputStream::operator>>(wxUint32& i) | |
285 | { | |
cd25b18c RR |
286 | i = Read32(); |
287 | return *this; | |
fae05df5 GL |
288 | } |
289 | ||
290 | wxTextInputStream& wxTextInputStream::operator>>(double& i) | |
291 | { | |
cd25b18c RR |
292 | i = ReadDouble(); |
293 | return *this; | |
fae05df5 GL |
294 | } |
295 | ||
296 | wxTextInputStream& wxTextInputStream::operator>>(float& f) | |
297 | { | |
cd25b18c RR |
298 | f = (float)ReadDouble(); |
299 | return *this; | |
fae05df5 GL |
300 | } |
301 | ||
2b5f62a0 VZ |
302 | |
303 | ||
304 | #if wxUSE_UNICODE | |
305 | wxTextOutputStream::wxTextOutputStream(wxOutputStream& s, wxEOL mode, wxMBConv& conv) | |
306 | : m_output(s), m_conv(conv) | |
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; | |
f3ff3813 | 317 | #elif defined(__WXMAC__) && !defined(__DARWIN__) |
c7a9fa36 RR |
318 | m_mode = wxEOL_MAC; |
319 | #else | |
320 | m_mode = wxEOL_UNIX; | |
321 | #endif | |
322 | } | |
fae05df5 GL |
323 | } |
324 | ||
325 | wxTextOutputStream::~wxTextOutputStream() | |
326 | { | |
327 | } | |
328 | ||
cd0b1709 | 329 | void wxTextOutputStream::SetMode(wxEOL mode) |
c7a9fa36 RR |
330 | { |
331 | m_mode = mode; | |
332 | if (m_mode == wxEOL_NATIVE) | |
333 | { | |
334 | #if defined(__WXMSW__) || defined(__WXPM__) | |
335 | m_mode = wxEOL_DOS; | |
f3ff3813 | 336 | #elif defined(__WXMAC__) && !defined(__DARWIN__) |
c7a9fa36 RR |
337 | m_mode = wxEOL_MAC; |
338 | #else | |
339 | m_mode = wxEOL_UNIX; | |
340 | #endif | |
341 | } | |
342 | } | |
343 | ||
fae05df5 GL |
344 | void wxTextOutputStream::Write32(wxUint32 i) |
345 | { | |
cd25b18c | 346 | wxString str; |
223d09f6 | 347 | str.Printf(wxT("%u"), i); |
717b9bf2 | 348 | |
cd25b18c | 349 | WriteString(str); |
fae05df5 GL |
350 | } |
351 | ||
352 | void wxTextOutputStream::Write16(wxUint16 i) | |
353 | { | |
cd25b18c | 354 | wxString str; |
223d09f6 | 355 | str.Printf(wxT("%u"), i); |
717b9bf2 | 356 | |
cd25b18c | 357 | WriteString(str); |
fae05df5 GL |
358 | } |
359 | ||
360 | void wxTextOutputStream::Write8(wxUint8 i) | |
361 | { | |
cd25b18c | 362 | wxString str; |
223d09f6 | 363 | str.Printf(wxT("%u"), i); |
717b9bf2 | 364 | |
cd25b18c | 365 | WriteString(str); |
fae05df5 GL |
366 | } |
367 | ||
368 | void wxTextOutputStream::WriteDouble(double d) | |
369 | { | |
cd25b18c | 370 | wxString str; |
fae05df5 | 371 | |
223d09f6 | 372 | str.Printf(wxT("%f"), d); |
cd25b18c | 373 | WriteString(str); |
fae05df5 GL |
374 | } |
375 | ||
376 | void wxTextOutputStream::WriteString(const wxString& string) | |
377 | { | |
20ea6894 VZ |
378 | size_t len = string.length(); |
379 | ||
380 | wxString out; | |
381 | out.reserve(len); | |
382 | ||
383 | for ( size_t i = 0; i < len; i++ ) | |
cd25b18c | 384 | { |
20ea6894 VZ |
385 | const wxChar c = string[i]; |
386 | if ( c == wxT('\n') ) | |
cd25b18c | 387 | { |
20ea6894 | 388 | switch ( m_mode ) |
cd6ce4a9 | 389 | { |
20ea6894 VZ |
390 | case wxEOL_DOS: |
391 | out << _T("\r\n"); | |
392 | continue; | |
393 | ||
394 | case wxEOL_MAC: | |
395 | out << _T('\r'); | |
396 | continue; | |
397 | ||
398 | default: | |
399 | wxFAIL_MSG( _T("unknown EOL mode in wxTextOutputStream") ); | |
400 | // fall through | |
401 | ||
402 | case wxEOL_UNIX: | |
403 | // don't treat '\n' specially | |
404 | ; | |
c7a9fa36 | 405 | } |
cd25b18c | 406 | } |
20ea6894 VZ |
407 | |
408 | out << c; | |
cd25b18c | 409 | } |
20ea6894 | 410 | |
2b5f62a0 VZ |
411 | // We must not write the trailing NULL here |
412 | #if wxUSE_UNICODE | |
413 | wxCharBuffer buffer = m_conv.cWC2MB( out ); | |
414 | m_output.Write( (const char*) buffer, strlen( (const char*) buffer ) ); | |
415 | #else | |
416 | m_output.Write(out.c_str(), out.length() ); | |
417 | #endif | |
fae05df5 GL |
418 | } |
419 | ||
420 | wxTextOutputStream& wxTextOutputStream::operator<<(const wxChar *string) | |
421 | { | |
cd25b18c RR |
422 | WriteString( wxString(string) ); |
423 | return *this; | |
fae05df5 GL |
424 | } |
425 | ||
426 | wxTextOutputStream& wxTextOutputStream::operator<<(const wxString& string) | |
427 | { | |
cd25b18c RR |
428 | WriteString( string ); |
429 | return *this; | |
fae05df5 GL |
430 | } |
431 | ||
f6bcfd97 | 432 | wxTextOutputStream& wxTextOutputStream::operator<<(char c) |
fae05df5 | 433 | { |
2b5f62a0 | 434 | WriteString( wxString::FromAscii(c) ); |
cb719f2e | 435 | |
cd25b18c | 436 | return *this; |
fae05df5 GL |
437 | } |
438 | ||
e4940feb | 439 | #if wxUSE_UNICODE && wxWCHAR_T_IS_REAL_TYPE |
3ca1bf5a VZ |
440 | |
441 | wxTextOutputStream& wxTextOutputStream::operator<<(wchar_t wc) | |
442 | { | |
443 | WriteString( wxString(&wc, m_conv, 1) ); | |
65a1bb98 VZ |
444 | |
445 | return *this; | |
3ca1bf5a VZ |
446 | } |
447 | ||
e4940feb | 448 | #endif // wxUSE_UNICODE |
3ca1bf5a | 449 | |
fae05df5 GL |
450 | wxTextOutputStream& wxTextOutputStream::operator<<(wxInt16 c) |
451 | { | |
78e848ca RR |
452 | wxString str; |
453 | str.Printf(wxT("%d"), (signed int)c); | |
454 | WriteString(str); | |
cd6ce4a9 | 455 | |
cd25b18c | 456 | return *this; |
fae05df5 GL |
457 | } |
458 | ||
459 | wxTextOutputStream& wxTextOutputStream::operator<<(wxInt32 c) | |
460 | { | |
78e848ca RR |
461 | wxString str; |
462 | str.Printf(wxT("%ld"), (signed long)c); | |
463 | WriteString(str); | |
cd6ce4a9 | 464 | |
cd25b18c | 465 | return *this; |
fae05df5 GL |
466 | } |
467 | ||
fae05df5 GL |
468 | wxTextOutputStream& wxTextOutputStream::operator<<(wxUint16 c) |
469 | { | |
78e848ca RR |
470 | wxString str; |
471 | str.Printf(wxT("%u"), (unsigned int)c); | |
472 | WriteString(str); | |
cd6ce4a9 | 473 | |
cd25b18c | 474 | return *this; |
fae05df5 GL |
475 | } |
476 | ||
477 | wxTextOutputStream& wxTextOutputStream::operator<<(wxUint32 c) | |
478 | { | |
78e848ca RR |
479 | wxString str; |
480 | str.Printf(wxT("%lu"), (unsigned long)c); | |
481 | WriteString(str); | |
482 | ||
cd25b18c | 483 | return *this; |
fae05df5 GL |
484 | } |
485 | ||
486 | wxTextOutputStream &wxTextOutputStream::operator<<(double f) | |
487 | { | |
cd25b18c RR |
488 | WriteDouble(f); |
489 | return *this; | |
fae05df5 GL |
490 | } |
491 | ||
492 | wxTextOutputStream& wxTextOutputStream::operator<<(float f) | |
493 | { | |
cd25b18c RR |
494 | WriteDouble((double)f); |
495 | return *this; | |
fae05df5 GL |
496 | } |
497 | ||
ed58dbea RR |
498 | wxTextOutputStream &endl( wxTextOutputStream &stream ) |
499 | { | |
876f6cee | 500 | //RN: Normally a single char on builds without a real |
ed5f2bd8 RN |
501 | //wchar_t will call the version that corresponds to the |
502 | //numeric value of wchar_t itself (wxUint16 if 2 bytes etc.), | |
503 | //which is not what we want (will print a 10 numeric value, | |
504 | //not a newline). Thus, we need to send it a string in that | |
505 | // case instead so that it correctly prints the newline. | |
876f6cee | 506 | #if !wxUSE_UNICODE || wxWCHAR_T_IS_REAL_TYPE |
223d09f6 | 507 | return stream << wxT('\n'); |
876f6cee RN |
508 | #else |
509 | return stream << wxT("\n"); | |
510 | #endif | |
ed58dbea RR |
511 | } |
512 | ||
fae05df5 GL |
513 | #endif |
514 | // wxUSE_STREAMS |