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