]>
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; | |
223d09f6 | 153 | if (! (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 | ||
223d09f6 | 180 | if (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 | ||
326 | wxTextOutputStream::wxTextOutputStream(wxOutputStream& s) | |
191549ed | 327 | : m_output(s) |
fae05df5 GL |
328 | { |
329 | } | |
330 | ||
331 | wxTextOutputStream::~wxTextOutputStream() | |
332 | { | |
333 | } | |
334 | ||
335 | void wxTextOutputStream::Write32(wxUint32 i) | |
336 | { | |
cd25b18c | 337 | wxString str; |
223d09f6 | 338 | str.Printf(wxT("%u"), i); |
717b9bf2 | 339 | |
cd25b18c | 340 | WriteString(str); |
fae05df5 GL |
341 | } |
342 | ||
343 | void wxTextOutputStream::Write16(wxUint16 i) | |
344 | { | |
cd25b18c | 345 | wxString str; |
223d09f6 | 346 | str.Printf(wxT("%u"), i); |
717b9bf2 | 347 | |
cd25b18c | 348 | WriteString(str); |
fae05df5 GL |
349 | } |
350 | ||
351 | void wxTextOutputStream::Write8(wxUint8 i) | |
352 | { | |
cd25b18c | 353 | wxString str; |
223d09f6 | 354 | str.Printf(wxT("%u"), i); |
717b9bf2 | 355 | |
cd25b18c | 356 | WriteString(str); |
fae05df5 GL |
357 | } |
358 | ||
359 | void wxTextOutputStream::WriteDouble(double d) | |
360 | { | |
cd25b18c | 361 | wxString str; |
fae05df5 | 362 | |
223d09f6 | 363 | str.Printf(wxT("%f"), d); |
cd25b18c | 364 | WriteString(str); |
fae05df5 GL |
365 | } |
366 | ||
367 | void wxTextOutputStream::WriteString(const wxString& string) | |
368 | { | |
cd25b18c RR |
369 | for (size_t i = 0; i < string.Len(); i++) |
370 | { | |
371 | wxChar c = string[i]; | |
223d09f6 | 372 | if (c == wxT('\n')) |
cd25b18c RR |
373 | { |
374 | #if defined(__WINDOWS__) | |
223d09f6 | 375 | c = wxT('\r'); |
191549ed | 376 | m_output.Write( (const void*)(&c), sizeof(wxChar) ); |
223d09f6 | 377 | c = wxT('\n'); |
191549ed | 378 | m_output.Write( (const void*)(&c), sizeof(wxChar) ); |
cd25b18c | 379 | #elif defined(__UNIX__) |
223d09f6 | 380 | c = wxT('\n'); |
191549ed | 381 | m_output.Write( (const void*)(&c), sizeof(wxChar) ); |
cd25b18c | 382 | #elif defined(__WXMAC__) |
223d09f6 | 383 | c = wxT('\r'); |
191549ed | 384 | m_output.Write( (const void*)(&c), sizeof(wxChar) ); |
717b9bf2 | 385 | #elif defined(__OS2__) |
223d09f6 | 386 | c = wxT('\r'); |
191549ed | 387 | m_output.Write( (const void*)(&c), sizeof(wxChar) ); |
223d09f6 | 388 | c = wxT('\n'); |
191549ed | 389 | m_output.Write( (const void*)(&c), sizeof(wxChar) ); |
fae05df5 | 390 | #else |
cd25b18c | 391 | #error "wxTextOutputStream: unsupported platform." |
fae05df5 | 392 | #endif |
cd25b18c RR |
393 | } |
394 | else | |
395 | { | |
191549ed | 396 | m_output.Write( (const void*)(&c), sizeof(wxChar) ); |
cd25b18c RR |
397 | } |
398 | } | |
fae05df5 GL |
399 | } |
400 | ||
401 | wxTextOutputStream& wxTextOutputStream::operator<<(const wxChar *string) | |
402 | { | |
cd25b18c RR |
403 | WriteString( wxString(string) ); |
404 | return *this; | |
fae05df5 GL |
405 | } |
406 | ||
407 | wxTextOutputStream& wxTextOutputStream::operator<<(const wxString& string) | |
408 | { | |
cd25b18c RR |
409 | WriteString( string ); |
410 | return *this; | |
fae05df5 GL |
411 | } |
412 | ||
940ddb19 | 413 | wxTextOutputStream& wxTextOutputStream::operator<<(wxChar c) |
fae05df5 | 414 | { |
cd25b18c RR |
415 | WriteString( wxString(c) ); |
416 | return *this; | |
fae05df5 GL |
417 | } |
418 | ||
419 | wxTextOutputStream& wxTextOutputStream::operator<<(wxInt16 c) | |
420 | { | |
cd25b18c RR |
421 | Write16( (wxUint16)c ); |
422 | return *this; | |
fae05df5 GL |
423 | } |
424 | ||
425 | wxTextOutputStream& wxTextOutputStream::operator<<(wxInt32 c) | |
426 | { | |
cd25b18c RR |
427 | Write32( (wxUint32)c ); |
428 | return *this; | |
fae05df5 GL |
429 | } |
430 | ||
fae05df5 GL |
431 | wxTextOutputStream& wxTextOutputStream::operator<<(wxUint16 c) |
432 | { | |
cd25b18c RR |
433 | Write16(c); |
434 | return *this; | |
fae05df5 GL |
435 | } |
436 | ||
437 | wxTextOutputStream& wxTextOutputStream::operator<<(wxUint32 c) | |
438 | { | |
cd25b18c RR |
439 | Write32(c); |
440 | return *this; | |
fae05df5 GL |
441 | } |
442 | ||
443 | wxTextOutputStream &wxTextOutputStream::operator<<(double f) | |
444 | { | |
cd25b18c RR |
445 | WriteDouble(f); |
446 | return *this; | |
fae05df5 GL |
447 | } |
448 | ||
449 | wxTextOutputStream& wxTextOutputStream::operator<<(float f) | |
450 | { | |
cd25b18c RR |
451 | WriteDouble((double)f); |
452 | return *this; | |
fae05df5 GL |
453 | } |
454 | ||
ed58dbea RR |
455 | wxTextOutputStream &endl( wxTextOutputStream &stream ) |
456 | { | |
223d09f6 | 457 | return stream << wxT('\n'); |
ed58dbea RR |
458 | } |
459 | ||
fae05df5 GL |
460 | #endif |
461 | // wxUSE_STREAMS |