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