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