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