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