]>
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 | ||
255 | wxTextInputStream& wxTextInputStream::operator>>(wxInt16& i) | |
256 | { | |
cd25b18c RR |
257 | i = (wxInt16)Read16(); |
258 | return *this; | |
fae05df5 GL |
259 | } |
260 | ||
261 | wxTextInputStream& wxTextInputStream::operator>>(wxInt32& i) | |
262 | { | |
cd25b18c RR |
263 | i = (wxInt32)Read32(); |
264 | return *this; | |
fae05df5 GL |
265 | } |
266 | ||
fae05df5 GL |
267 | wxTextInputStream& wxTextInputStream::operator>>(wxUint16& i) |
268 | { | |
cd25b18c RR |
269 | i = Read16(); |
270 | return *this; | |
fae05df5 GL |
271 | } |
272 | ||
273 | wxTextInputStream& wxTextInputStream::operator>>(wxUint32& i) | |
274 | { | |
cd25b18c RR |
275 | i = Read32(); |
276 | return *this; | |
fae05df5 GL |
277 | } |
278 | ||
279 | wxTextInputStream& wxTextInputStream::operator>>(double& i) | |
280 | { | |
cd25b18c RR |
281 | i = ReadDouble(); |
282 | return *this; | |
fae05df5 GL |
283 | } |
284 | ||
285 | wxTextInputStream& wxTextInputStream::operator>>(float& f) | |
286 | { | |
cd25b18c RR |
287 | f = (float)ReadDouble(); |
288 | return *this; | |
fae05df5 GL |
289 | } |
290 | ||
2b5f62a0 VZ |
291 | |
292 | ||
293 | #if wxUSE_UNICODE | |
294 | wxTextOutputStream::wxTextOutputStream(wxOutputStream& s, wxEOL mode, wxMBConv& conv) | |
295 | : m_output(s), m_conv(conv) | |
296 | #else | |
c7a9fa36 | 297 | wxTextOutputStream::wxTextOutputStream(wxOutputStream& s, wxEOL mode) |
191549ed | 298 | : m_output(s) |
2b5f62a0 | 299 | #endif |
fae05df5 | 300 | { |
c7a9fa36 RR |
301 | m_mode = mode; |
302 | if (m_mode == wxEOL_NATIVE) | |
303 | { | |
304 | #if defined(__WXMSW__) || defined(__WXPM__) | |
305 | m_mode = wxEOL_DOS; | |
f3ff3813 | 306 | #elif defined(__WXMAC__) && !defined(__DARWIN__) |
c7a9fa36 RR |
307 | m_mode = wxEOL_MAC; |
308 | #else | |
309 | m_mode = wxEOL_UNIX; | |
310 | #endif | |
311 | } | |
fae05df5 GL |
312 | } |
313 | ||
314 | wxTextOutputStream::~wxTextOutputStream() | |
315 | { | |
316 | } | |
317 | ||
cd0b1709 | 318 | void wxTextOutputStream::SetMode(wxEOL mode) |
c7a9fa36 RR |
319 | { |
320 | m_mode = mode; | |
321 | if (m_mode == wxEOL_NATIVE) | |
322 | { | |
323 | #if defined(__WXMSW__) || defined(__WXPM__) | |
324 | m_mode = wxEOL_DOS; | |
f3ff3813 | 325 | #elif defined(__WXMAC__) && !defined(__DARWIN__) |
c7a9fa36 RR |
326 | m_mode = wxEOL_MAC; |
327 | #else | |
328 | m_mode = wxEOL_UNIX; | |
329 | #endif | |
330 | } | |
331 | } | |
332 | ||
fae05df5 GL |
333 | void wxTextOutputStream::Write32(wxUint32 i) |
334 | { | |
cd25b18c | 335 | wxString str; |
223d09f6 | 336 | str.Printf(wxT("%u"), i); |
717b9bf2 | 337 | |
cd25b18c | 338 | WriteString(str); |
fae05df5 GL |
339 | } |
340 | ||
341 | void wxTextOutputStream::Write16(wxUint16 i) | |
342 | { | |
cd25b18c | 343 | wxString str; |
223d09f6 | 344 | str.Printf(wxT("%u"), i); |
717b9bf2 | 345 | |
cd25b18c | 346 | WriteString(str); |
fae05df5 GL |
347 | } |
348 | ||
349 | void wxTextOutputStream::Write8(wxUint8 i) | |
350 | { | |
cd25b18c | 351 | wxString str; |
223d09f6 | 352 | str.Printf(wxT("%u"), i); |
717b9bf2 | 353 | |
cd25b18c | 354 | WriteString(str); |
fae05df5 GL |
355 | } |
356 | ||
357 | void wxTextOutputStream::WriteDouble(double d) | |
358 | { | |
cd25b18c | 359 | wxString str; |
fae05df5 | 360 | |
223d09f6 | 361 | str.Printf(wxT("%f"), d); |
cd25b18c | 362 | WriteString(str); |
fae05df5 GL |
363 | } |
364 | ||
365 | void wxTextOutputStream::WriteString(const wxString& string) | |
366 | { | |
20ea6894 VZ |
367 | size_t len = string.length(); |
368 | ||
369 | wxString out; | |
370 | out.reserve(len); | |
371 | ||
372 | for ( size_t i = 0; i < len; i++ ) | |
cd25b18c | 373 | { |
20ea6894 VZ |
374 | const wxChar c = string[i]; |
375 | if ( c == wxT('\n') ) | |
cd25b18c | 376 | { |
20ea6894 | 377 | switch ( m_mode ) |
cd6ce4a9 | 378 | { |
20ea6894 VZ |
379 | case wxEOL_DOS: |
380 | out << _T("\r\n"); | |
381 | continue; | |
382 | ||
383 | case wxEOL_MAC: | |
384 | out << _T('\r'); | |
385 | continue; | |
386 | ||
387 | default: | |
388 | wxFAIL_MSG( _T("unknown EOL mode in wxTextOutputStream") ); | |
389 | // fall through | |
390 | ||
391 | case wxEOL_UNIX: | |
392 | // don't treat '\n' specially | |
393 | ; | |
c7a9fa36 | 394 | } |
cd25b18c | 395 | } |
20ea6894 VZ |
396 | |
397 | out << c; | |
cd25b18c | 398 | } |
20ea6894 | 399 | |
2b5f62a0 VZ |
400 | // We must not write the trailing NULL here |
401 | #if wxUSE_UNICODE | |
402 | wxCharBuffer buffer = m_conv.cWC2MB( out ); | |
403 | m_output.Write( (const char*) buffer, strlen( (const char*) buffer ) ); | |
404 | #else | |
405 | m_output.Write(out.c_str(), out.length() ); | |
406 | #endif | |
fae05df5 GL |
407 | } |
408 | ||
409 | wxTextOutputStream& wxTextOutputStream::operator<<(const wxChar *string) | |
410 | { | |
cd25b18c RR |
411 | WriteString( wxString(string) ); |
412 | return *this; | |
fae05df5 GL |
413 | } |
414 | ||
415 | wxTextOutputStream& wxTextOutputStream::operator<<(const wxString& string) | |
416 | { | |
cd25b18c RR |
417 | WriteString( string ); |
418 | return *this; | |
fae05df5 GL |
419 | } |
420 | ||
f6bcfd97 | 421 | wxTextOutputStream& wxTextOutputStream::operator<<(char c) |
fae05df5 | 422 | { |
2b5f62a0 | 423 | WriteString( wxString::FromAscii(c) ); |
cb719f2e | 424 | |
cd25b18c | 425 | return *this; |
fae05df5 GL |
426 | } |
427 | ||
e4940feb | 428 | #if wxUSE_UNICODE && wxWCHAR_T_IS_REAL_TYPE |
3ca1bf5a VZ |
429 | |
430 | wxTextOutputStream& wxTextOutputStream::operator<<(wchar_t wc) | |
431 | { | |
432 | WriteString( wxString(&wc, m_conv, 1) ); | |
65a1bb98 VZ |
433 | |
434 | return *this; | |
3ca1bf5a VZ |
435 | } |
436 | ||
e4940feb | 437 | #endif // wxUSE_UNICODE |
3ca1bf5a | 438 | |
fae05df5 GL |
439 | wxTextOutputStream& wxTextOutputStream::operator<<(wxInt16 c) |
440 | { | |
78e848ca RR |
441 | wxString str; |
442 | str.Printf(wxT("%d"), (signed int)c); | |
443 | WriteString(str); | |
cd6ce4a9 | 444 | |
cd25b18c | 445 | return *this; |
fae05df5 GL |
446 | } |
447 | ||
448 | wxTextOutputStream& wxTextOutputStream::operator<<(wxInt32 c) | |
449 | { | |
78e848ca RR |
450 | wxString str; |
451 | str.Printf(wxT("%ld"), (signed long)c); | |
452 | WriteString(str); | |
cd6ce4a9 | 453 | |
cd25b18c | 454 | return *this; |
fae05df5 GL |
455 | } |
456 | ||
fae05df5 GL |
457 | wxTextOutputStream& wxTextOutputStream::operator<<(wxUint16 c) |
458 | { | |
78e848ca RR |
459 | wxString str; |
460 | str.Printf(wxT("%u"), (unsigned int)c); | |
461 | WriteString(str); | |
cd6ce4a9 | 462 | |
cd25b18c | 463 | return *this; |
fae05df5 GL |
464 | } |
465 | ||
466 | wxTextOutputStream& wxTextOutputStream::operator<<(wxUint32 c) | |
467 | { | |
78e848ca RR |
468 | wxString str; |
469 | str.Printf(wxT("%lu"), (unsigned long)c); | |
470 | WriteString(str); | |
471 | ||
cd25b18c | 472 | return *this; |
fae05df5 GL |
473 | } |
474 | ||
475 | wxTextOutputStream &wxTextOutputStream::operator<<(double f) | |
476 | { | |
cd25b18c RR |
477 | WriteDouble(f); |
478 | return *this; | |
fae05df5 GL |
479 | } |
480 | ||
481 | wxTextOutputStream& wxTextOutputStream::operator<<(float f) | |
482 | { | |
cd25b18c RR |
483 | WriteDouble((double)f); |
484 | return *this; | |
fae05df5 GL |
485 | } |
486 | ||
ed58dbea RR |
487 | wxTextOutputStream &endl( wxTextOutputStream &stream ) |
488 | { | |
223d09f6 | 489 | return stream << wxT('\n'); |
ed58dbea RR |
490 | } |
491 | ||
fae05df5 GL |
492 | #endif |
493 | // wxUSE_STREAMS |