]>
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 |
fae05df5 GL |
9 | // Licence: wxWindows license |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifdef __GNUG__ | |
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 | ||
191549ed SB |
41 | wxTextInputStream::wxTextInputStream(wxInputStream &s, const wxString &sep) |
42 | : m_input(s), m_separators(sep) | |
fae05df5 GL |
43 | { |
44 | } | |
45 | ||
46 | wxTextInputStream::~wxTextInputStream() | |
47 | { | |
48 | } | |
49 | ||
191549ed | 50 | wxChar wxTextInputStream::NextNonSeparators() |
fae05df5 | 51 | { |
cd25b18c RR |
52 | wxChar c = (wxChar) 0; |
53 | for (;;) | |
54 | { | |
cd25b18c | 55 | if (!m_input) return (wxChar) 0; |
191549ed | 56 | c = m_input.GetC(); |
cd25b18c | 57 | |
191549ed | 58 | if (c != wxT('\n') && |
223d09f6 | 59 | c != wxT('\r') && |
191549ed SB |
60 | !m_separators.Contains(c)) |
61 | return c; | |
cd25b18c | 62 | } |
717b9bf2 | 63 | |
cd25b18c | 64 | } |
fae05df5 | 65 | |
191549ed | 66 | inline bool wxTextInputStream::EatEOL(const wxChar &c) |
cd25b18c | 67 | { |
191549ed SB |
68 | if (c == wxT('\n')) return TRUE; // eat on UNIX |
69 | ||
70 | if (c == wxT('\r')) // eat on both Mac and DOS | |
717b9bf2 | 71 | { |
191549ed SB |
72 | if (!m_input) return TRUE; |
73 | wxChar c2 = m_input.GetC(); | |
74 | ||
75 | if (c2 != wxT('\n')) m_input.Ungetch( c2 ); // Don't eat on Mac | |
76 | return TRUE; | |
cd25b18c | 77 | } |
717b9bf2 | 78 | |
191549ed SB |
79 | return FALSE; |
80 | } | |
81 | ||
82 | void wxTextInputStream::SkipIfEndOfLine( wxChar c ) | |
83 | { | |
84 | if (EatEOL(c)) return; | |
85 | else m_input.Ungetch( c ); // no line terminator | |
cd25b18c | 86 | } |
fae05df5 | 87 | |
cd25b18c RR |
88 | wxUint32 wxTextInputStream::Read32() |
89 | { | |
90 | /* I only implemented a simple integer parser */ | |
91 | int sign; | |
92 | wxInt32 i; | |
717b9bf2 | 93 | |
cd25b18c | 94 | if (!m_input) return 0; |
191549ed SB |
95 | int c = NextNonSeparators(); |
96 | if (c==(wxChar)0) return 0; | |
cd25b18c RR |
97 | |
98 | i = 0; | |
223d09f6 | 99 | if (! (c == wxT('-') || c == wxT('+') || isdigit(c)) ) |
cd25b18c | 100 | { |
191549ed | 101 | m_input.Ungetch(c); |
cd25b18c RR |
102 | return 0; |
103 | } | |
fae05df5 | 104 | |
223d09f6 | 105 | if (c == wxT('-')) |
cd25b18c RR |
106 | { |
107 | sign = -1; | |
191549ed | 108 | c = m_input.GetC(); |
717b9bf2 | 109 | } else |
223d09f6 | 110 | if (c == wxT('+')) |
cd25b18c RR |
111 | { |
112 | sign = 1; | |
191549ed | 113 | c = m_input.GetC(); |
717b9bf2 | 114 | } else |
cd25b18c RR |
115 | { |
116 | sign = 1; | |
117 | } | |
fae05df5 | 118 | |
717b9bf2 | 119 | while (isdigit(c)) |
cd25b18c | 120 | { |
223d09f6 | 121 | i = i*10 + (c - (int)wxT('0')); |
191549ed | 122 | c = m_input.GetC(); |
cd25b18c | 123 | } |
fae05df5 | 124 | |
cd25b18c | 125 | SkipIfEndOfLine( c ); |
fae05df5 | 126 | |
cd25b18c | 127 | i *= sign; |
fae05df5 | 128 | |
cd25b18c | 129 | return (wxUint32)i; |
fae05df5 GL |
130 | } |
131 | ||
132 | wxUint16 wxTextInputStream::Read16() | |
133 | { | |
cd25b18c | 134 | return (wxUint16)Read32(); |
fae05df5 GL |
135 | } |
136 | ||
137 | wxUint8 wxTextInputStream::Read8() | |
138 | { | |
cd25b18c | 139 | return (wxUint8)Read32(); |
fae05df5 GL |
140 | } |
141 | ||
142 | double wxTextInputStream::ReadDouble() | |
143 | { | |
cd25b18c RR |
144 | /* I only implemented a simple float parser */ |
145 | double f; | |
146 | int sign; | |
147 | ||
191549ed SB |
148 | if (!m_input) return 0; |
149 | int c = NextNonSeparators(); | |
150 | if (c==(wxChar)0) return 0; | |
cd25b18c RR |
151 | |
152 | f = 0.0; | |
78e848ca | 153 | if (! (c == wxT('.') || c == wxT(',') || c == wxT('-') || c == wxT('+') || isdigit(c)) ) |
cd25b18c | 154 | { |
191549ed | 155 | m_input.Ungetch(c); |
cd25b18c RR |
156 | return 0.0; |
157 | } | |
158 | ||
223d09f6 | 159 | if (c == wxT('-')) |
cd25b18c RR |
160 | { |
161 | sign = -1; | |
191549ed | 162 | c = m_input.GetC(); |
717b9bf2 | 163 | } else |
223d09f6 | 164 | if (c == wxT('+')) |
cd25b18c RR |
165 | { |
166 | sign = 1; | |
191549ed | 167 | c = m_input.GetC(); |
717b9bf2 DW |
168 | } |
169 | else | |
cd25b18c RR |
170 | { |
171 | sign = 1; | |
172 | } | |
173 | ||
717b9bf2 | 174 | while (isdigit(c)) |
cd25b18c | 175 | { |
223d09f6 | 176 | f = f*10 + (c - wxT('0')); |
191549ed | 177 | c = m_input.GetC(); |
fae05df5 GL |
178 | } |
179 | ||
78e848ca | 180 | if (c == wxT('.') || c == wxT(',')) |
cd25b18c RR |
181 | { |
182 | double f_multiplicator = (double) 0.1; | |
fae05df5 | 183 | |
191549ed | 184 | c = m_input.GetC(); |
fae05df5 | 185 | |
717b9bf2 | 186 | while (isdigit(c)) |
cd25b18c | 187 | { |
223d09f6 | 188 | f += (c-wxT('0'))*f_multiplicator; |
cd25b18c | 189 | f_multiplicator /= 10; |
191549ed | 190 | c = m_input.GetC(); |
cd25b18c | 191 | } |
fae05df5 | 192 | |
223d09f6 | 193 | if (c == wxT('e')) |
cd25b18c RR |
194 | { |
195 | double f_multiplicator = 0.0; | |
196 | int i, e; | |
fae05df5 | 197 | |
191549ed | 198 | c = m_input.GetC(); |
fae05df5 | 199 | |
717b9bf2 | 200 | switch (c) |
cd25b18c | 201 | { |
223d09f6 KB |
202 | case wxT('-'): f_multiplicator = 0.1; break; |
203 | case wxT('+'): f_multiplicator = 10.0; break; | |
cd25b18c | 204 | } |
fae05df5 | 205 | |
cd25b18c RR |
206 | e = Read8(); // why only max 256 ? |
207 | ||
208 | for (i=0;i<e;i++) | |
209 | f *= f_multiplicator; | |
717b9bf2 | 210 | } |
cd25b18c RR |
211 | else |
212 | SkipIfEndOfLine( c ); | |
213 | } | |
214 | else | |
215 | { | |
191549ed | 216 | m_input.Ungetch(c); |
cd25b18c RR |
217 | } |
218 | ||
219 | f *= sign; | |
220 | ||
221 | return f; | |
fae05df5 GL |
222 | } |
223 | ||
224 | wxString wxTextInputStream::ReadString() | |
9853d977 SB |
225 | { |
226 | return ReadLine(); | |
227 | } | |
228 | ||
229 | wxString wxTextInputStream::ReadLine() | |
fae05df5 | 230 | { |
cd25b18c RR |
231 | wxChar c; |
232 | wxString line; | |
233 | ||
717b9bf2 | 234 | for (;;) |
cd25b18c | 235 | { |
cd25b18c | 236 | if (!m_input) break; |
191549ed | 237 | c = m_input.GetC(); |
cd25b18c | 238 | |
191549ed | 239 | if (EatEOL(c)) break; |
cd25b18c RR |
240 | |
241 | line += c; | |
242 | } | |
717b9bf2 | 243 | |
cd25b18c | 244 | return line; |
fae05df5 | 245 | } |
717b9bf2 | 246 | |
9853d977 SB |
247 | wxString wxTextInputStream::ReadWord() |
248 | { | |
191549ed SB |
249 | if (!m_input) return ""; |
250 | ||
9853d977 | 251 | wxString word; |
191549ed SB |
252 | wxChar c=NextNonSeparators(); |
253 | if (c==(wxChar)0) return ""; | |
9853d977 SB |
254 | |
255 | for (;;) | |
256 | { | |
191549ed | 257 | if (m_separators.Contains(c)) break; |
9853d977 | 258 | |
191549ed | 259 | if (EatEOL(c)) break; |
9853d977 SB |
260 | |
261 | word += c; | |
191549ed SB |
262 | |
263 | if (!m_input) break; | |
264 | c = m_input.GetC(); | |
9853d977 SB |
265 | } |
266 | ||
267 | return word; | |
268 | } | |
269 | ||
270 | wxTextInputStream& wxTextInputStream::operator>>(wxString& word) | |
fae05df5 | 271 | { |
9853d977 | 272 | word = ReadWord(); |
fae05df5 GL |
273 | return *this; |
274 | } | |
275 | ||
940ddb19 | 276 | wxTextInputStream& wxTextInputStream::operator>>(wxChar& c) |
fae05df5 | 277 | { |
cd25b18c RR |
278 | if (!m_input) |
279 | { | |
280 | c = (wxChar) 0; | |
281 | return *this; | |
282 | } | |
717b9bf2 | 283 | |
191549ed | 284 | c = m_input.GetC(); |
717b9bf2 | 285 | |
191549ed | 286 | if (EatEOL(c)) c=wxT('\n'); |
cd25b18c | 287 | return *this; |
fae05df5 GL |
288 | } |
289 | ||
290 | wxTextInputStream& wxTextInputStream::operator>>(wxInt16& i) | |
291 | { | |
cd25b18c RR |
292 | i = (wxInt16)Read16(); |
293 | return *this; | |
fae05df5 GL |
294 | } |
295 | ||
296 | wxTextInputStream& wxTextInputStream::operator>>(wxInt32& i) | |
297 | { | |
cd25b18c RR |
298 | i = (wxInt32)Read32(); |
299 | return *this; | |
fae05df5 GL |
300 | } |
301 | ||
fae05df5 GL |
302 | wxTextInputStream& wxTextInputStream::operator>>(wxUint16& i) |
303 | { | |
cd25b18c RR |
304 | i = Read16(); |
305 | return *this; | |
fae05df5 GL |
306 | } |
307 | ||
308 | wxTextInputStream& wxTextInputStream::operator>>(wxUint32& i) | |
309 | { | |
cd25b18c RR |
310 | i = Read32(); |
311 | return *this; | |
fae05df5 GL |
312 | } |
313 | ||
314 | wxTextInputStream& wxTextInputStream::operator>>(double& i) | |
315 | { | |
cd25b18c RR |
316 | i = ReadDouble(); |
317 | return *this; | |
fae05df5 GL |
318 | } |
319 | ||
320 | wxTextInputStream& wxTextInputStream::operator>>(float& f) | |
321 | { | |
cd25b18c RR |
322 | f = (float)ReadDouble(); |
323 | return *this; | |
fae05df5 GL |
324 | } |
325 | ||
c7a9fa36 | 326 | wxTextOutputStream::wxTextOutputStream(wxOutputStream& s, wxEOL mode) |
191549ed | 327 | : m_output(s) |
fae05df5 | 328 | { |
c7a9fa36 RR |
329 | m_mode = mode; |
330 | if (m_mode == wxEOL_NATIVE) | |
331 | { | |
332 | #if defined(__WXMSW__) || defined(__WXPM__) | |
333 | m_mode = wxEOL_DOS; | |
334 | #elif defined(__WXMAC__) | |
335 | m_mode = wxEOL_MAC; | |
336 | #else | |
337 | m_mode = wxEOL_UNIX; | |
338 | #endif | |
339 | } | |
fae05df5 GL |
340 | } |
341 | ||
342 | wxTextOutputStream::~wxTextOutputStream() | |
343 | { | |
344 | } | |
345 | ||
cd0b1709 | 346 | void wxTextOutputStream::SetMode(wxEOL mode) |
c7a9fa36 RR |
347 | { |
348 | m_mode = mode; | |
349 | if (m_mode == wxEOL_NATIVE) | |
350 | { | |
351 | #if defined(__WXMSW__) || defined(__WXPM__) | |
352 | m_mode = wxEOL_DOS; | |
353 | #elif defined(__WXMAC__) | |
354 | m_mode = wxEOL_MAC; | |
355 | #else | |
356 | m_mode = wxEOL_UNIX; | |
357 | #endif | |
358 | } | |
359 | } | |
360 | ||
fae05df5 GL |
361 | void wxTextOutputStream::Write32(wxUint32 i) |
362 | { | |
cd25b18c | 363 | wxString str; |
223d09f6 | 364 | str.Printf(wxT("%u"), i); |
717b9bf2 | 365 | |
cd25b18c | 366 | WriteString(str); |
fae05df5 GL |
367 | } |
368 | ||
369 | void wxTextOutputStream::Write16(wxUint16 i) | |
370 | { | |
cd25b18c | 371 | wxString str; |
223d09f6 | 372 | str.Printf(wxT("%u"), i); |
717b9bf2 | 373 | |
cd25b18c | 374 | WriteString(str); |
fae05df5 GL |
375 | } |
376 | ||
377 | void wxTextOutputStream::Write8(wxUint8 i) | |
378 | { | |
cd25b18c | 379 | wxString str; |
223d09f6 | 380 | str.Printf(wxT("%u"), i); |
717b9bf2 | 381 | |
cd25b18c | 382 | WriteString(str); |
fae05df5 GL |
383 | } |
384 | ||
385 | void wxTextOutputStream::WriteDouble(double d) | |
386 | { | |
cd25b18c | 387 | wxString str; |
fae05df5 | 388 | |
223d09f6 | 389 | str.Printf(wxT("%f"), d); |
cd25b18c | 390 | WriteString(str); |
fae05df5 GL |
391 | } |
392 | ||
393 | void wxTextOutputStream::WriteString(const wxString& string) | |
394 | { | |
cd25b18c RR |
395 | for (size_t i = 0; i < string.Len(); i++) |
396 | { | |
397 | wxChar c = string[i]; | |
223d09f6 | 398 | if (c == wxT('\n')) |
cd25b18c | 399 | { |
c7a9fa36 RR |
400 | if (m_mode == wxEOL_DOS) |
401 | { | |
402 | c = wxT('\r'); | |
403 | m_output.Write( (const void*)(&c), sizeof(wxChar) ); | |
404 | c = wxT('\n'); | |
405 | m_output.Write( (const void*)(&c), sizeof(wxChar) ); | |
406 | } else | |
407 | if (m_mode == wxEOL_MAC) | |
408 | { | |
409 | c = wxT('\r'); | |
410 | m_output.Write( (const void*)(&c), sizeof(wxChar) ); | |
411 | } else | |
412 | { | |
413 | c = wxT('\n'); | |
414 | m_output.Write( (const void*)(&c), sizeof(wxChar) ); | |
415 | } | |
cd25b18c RR |
416 | } |
417 | else | |
418 | { | |
191549ed | 419 | m_output.Write( (const void*)(&c), sizeof(wxChar) ); |
cd25b18c RR |
420 | } |
421 | } | |
fae05df5 GL |
422 | } |
423 | ||
424 | wxTextOutputStream& wxTextOutputStream::operator<<(const wxChar *string) | |
425 | { | |
cd25b18c RR |
426 | WriteString( wxString(string) ); |
427 | return *this; | |
fae05df5 GL |
428 | } |
429 | ||
430 | wxTextOutputStream& wxTextOutputStream::operator<<(const wxString& string) | |
431 | { | |
cd25b18c RR |
432 | WriteString( string ); |
433 | return *this; | |
fae05df5 GL |
434 | } |
435 | ||
940ddb19 | 436 | wxTextOutputStream& wxTextOutputStream::operator<<(wxChar c) |
fae05df5 | 437 | { |
cd25b18c RR |
438 | WriteString( wxString(c) ); |
439 | return *this; | |
fae05df5 GL |
440 | } |
441 | ||
442 | wxTextOutputStream& wxTextOutputStream::operator<<(wxInt16 c) | |
443 | { | |
78e848ca RR |
444 | wxString str; |
445 | str.Printf(wxT("%d"), (signed int)c); | |
446 | WriteString(str); | |
447 | ||
cd25b18c | 448 | return *this; |
fae05df5 GL |
449 | } |
450 | ||
451 | wxTextOutputStream& wxTextOutputStream::operator<<(wxInt32 c) | |
452 | { | |
78e848ca RR |
453 | wxString str; |
454 | str.Printf(wxT("%ld"), (signed long)c); | |
455 | WriteString(str); | |
456 | ||
cd25b18c | 457 | return *this; |
fae05df5 GL |
458 | } |
459 | ||
fae05df5 GL |
460 | wxTextOutputStream& wxTextOutputStream::operator<<(wxUint16 c) |
461 | { | |
78e848ca RR |
462 | wxString str; |
463 | str.Printf(wxT("%u"), (unsigned int)c); | |
464 | WriteString(str); | |
465 | ||
cd25b18c | 466 | return *this; |
fae05df5 GL |
467 | } |
468 | ||
469 | wxTextOutputStream& wxTextOutputStream::operator<<(wxUint32 c) | |
470 | { | |
78e848ca RR |
471 | wxString str; |
472 | str.Printf(wxT("%lu"), (unsigned long)c); | |
473 | WriteString(str); | |
474 | ||
cd25b18c | 475 | return *this; |
fae05df5 GL |
476 | } |
477 | ||
478 | wxTextOutputStream &wxTextOutputStream::operator<<(double f) | |
479 | { | |
cd25b18c RR |
480 | WriteDouble(f); |
481 | return *this; | |
fae05df5 GL |
482 | } |
483 | ||
484 | wxTextOutputStream& wxTextOutputStream::operator<<(float f) | |
485 | { | |
cd25b18c RR |
486 | WriteDouble((double)f); |
487 | return *this; | |
fae05df5 GL |
488 | } |
489 | ||
ed58dbea RR |
490 | wxTextOutputStream &endl( wxTextOutputStream &stream ) |
491 | { | |
223d09f6 | 492 | return stream << wxT('\n'); |
ed58dbea RR |
493 | } |
494 | ||
fae05df5 GL |
495 | #endif |
496 | // wxUSE_STREAMS |