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