]>
git.saurik.com Git - wxWidgets.git/blob - src/common/txtstrm.cpp
1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: Text stream classes
4 // Author: Guilhem Lavaux
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "txtstrm.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
25 #include "wx/txtstrm.h"
29 // ----------------------------------------------------------------------------
31 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
41 wxTextInputStream::wxTextInputStream(wxInputStream
&s
, const wxString
&sep
)
42 : m_input(s
), m_separators(sep
)
46 wxTextInputStream::~wxTextInputStream()
50 wxChar
wxTextInputStream::NextNonSeparators()
52 wxChar c
= (wxChar
) 0;
55 if (!m_input
) return (wxChar
) 0;
60 !m_separators
.Contains(c
))
64 // this shouldn't happen
68 inline bool wxTextInputStream::EatEOL(const wxChar
&c
)
70 if (c
== wxT('\n')) return TRUE
; // eat on UNIX
72 if (c
== wxT('\r')) // eat on both Mac and DOS
74 if (!m_input
) return TRUE
;
75 wxChar c2
= m_input
.GetC();
77 if (c2
!= wxT('\n')) m_input
.Ungetch( c2
); // Don't eat on Mac
84 void wxTextInputStream::SkipIfEndOfLine( wxChar c
)
86 if (EatEOL(c
)) return;
87 else m_input
.Ungetch( c
); // no line terminator
90 wxUint32
wxTextInputStream::Read32()
92 /* I only implemented a simple integer parser */
96 if (!m_input
) return 0;
97 int c
= NextNonSeparators();
98 if (c
==(wxChar
)0) return 0;
101 if (! (c
== wxT('-') || c
== wxT('+') || isdigit(c
)) )
123 i
= i
*10 + (c
- (int)wxT('0'));
127 SkipIfEndOfLine( c
);
134 wxUint16
wxTextInputStream::Read16()
136 return (wxUint16
)Read32();
139 wxUint8
wxTextInputStream::Read8()
141 return (wxUint8
)Read32();
144 double wxTextInputStream::ReadDouble()
146 /* I only implemented a simple float parser */
150 if (!m_input
) return 0;
151 int c
= NextNonSeparators();
152 if (c
==(wxChar
)0) return 0;
155 if (! (c
== wxT('.') || c
== wxT('-') || c
== wxT('+') || isdigit(c
)) )
178 f
= f
*10 + (c
- wxT('0'));
184 double f_multiplicator
= (double) 0.1;
190 f
+= (c
-wxT('0'))*f_multiplicator
;
191 f_multiplicator
/= 10;
197 double f_multiplicator
= 0.0;
204 case wxT('-'): f_multiplicator
= 0.1; break;
205 case wxT('+'): f_multiplicator
= 10.0; break;
208 e
= Read8(); // why only max 256 ?
211 f
*= f_multiplicator
;
214 SkipIfEndOfLine( c
);
226 wxString
wxTextInputStream::ReadString()
231 wxString
wxTextInputStream::ReadLine()
241 if (EatEOL(c
)) break;
249 wxString
wxTextInputStream::ReadWord()
251 if (!m_input
) return "";
254 wxChar c
=NextNonSeparators();
255 if (c
==(wxChar
)0) return "";
259 if (m_separators
.Contains(c
)) break;
261 if (EatEOL(c
)) break;
272 wxTextInputStream
& wxTextInputStream::operator>>(wxString
& word
)
278 wxTextInputStream
& wxTextInputStream::operator>>(wxChar
& c
)
288 if (EatEOL(c
)) c
=wxT('\n');
292 wxTextInputStream
& wxTextInputStream::operator>>(wxInt16
& i
)
294 i
= (wxInt16
)Read16();
298 wxTextInputStream
& wxTextInputStream::operator>>(wxInt32
& i
)
300 i
= (wxInt32
)Read32();
304 wxTextInputStream
& wxTextInputStream::operator>>(wxUint16
& i
)
310 wxTextInputStream
& wxTextInputStream::operator>>(wxUint32
& i
)
316 wxTextInputStream
& wxTextInputStream::operator>>(double& i
)
322 wxTextInputStream
& wxTextInputStream::operator>>(float& f
)
324 f
= (float)ReadDouble();
328 wxTextOutputStream::wxTextOutputStream(wxOutputStream
& s
)
333 wxTextOutputStream::~wxTextOutputStream()
337 void wxTextOutputStream::Write32(wxUint32 i
)
340 str
.Printf(wxT("%u"), i
);
345 void wxTextOutputStream::Write16(wxUint16 i
)
348 str
.Printf(wxT("%u"), i
);
353 void wxTextOutputStream::Write8(wxUint8 i
)
356 str
.Printf(wxT("%u"), i
);
361 void wxTextOutputStream::WriteDouble(double d
)
365 str
.Printf(wxT("%f"), d
);
369 void wxTextOutputStream::WriteString(const wxString
& string
)
371 for (size_t i
= 0; i
< string
.Len(); i
++)
373 wxChar c
= string
[i
];
376 #if defined(__WINDOWS__)
378 m_output
.Write( (const void*)(&c
), sizeof(wxChar
) );
380 m_output
.Write( (const void*)(&c
), sizeof(wxChar
) );
381 #elif defined(__UNIX__)
383 m_output
.Write( (const void*)(&c
), sizeof(wxChar
) );
384 #elif defined(__WXMAC__)
386 m_output
.Write( (const void*)(&c
), sizeof(wxChar
) );
387 #elif defined(__OS2__)
389 m_output
.Write( (const void*)(&c
), sizeof(wxChar
) );
391 m_output
.Write( (const void*)(&c
), sizeof(wxChar
) );
393 #error "wxTextOutputStream: unsupported platform."
398 m_output
.Write( (const void*)(&c
), sizeof(wxChar
) );
403 wxTextOutputStream
& wxTextOutputStream::operator<<(const wxChar
*string
)
405 WriteString( wxString(string
) );
409 wxTextOutputStream
& wxTextOutputStream::operator<<(const wxString
& string
)
411 WriteString( string
);
415 wxTextOutputStream
& wxTextOutputStream::operator<<(wxChar c
)
417 WriteString( wxString(c
) );
421 wxTextOutputStream
& wxTextOutputStream::operator<<(wxInt16 c
)
423 Write16( (wxUint16
)c
);
427 wxTextOutputStream
& wxTextOutputStream::operator<<(wxInt32 c
)
429 Write32( (wxUint32
)c
);
433 wxTextOutputStream
& wxTextOutputStream::operator<<(wxUint16 c
)
439 wxTextOutputStream
& wxTextOutputStream::operator<<(wxUint32 c
)
445 wxTextOutputStream
&wxTextOutputStream::operator<<(double f
)
451 wxTextOutputStream
& wxTextOutputStream::operator<<(float f
)
453 WriteDouble((double)f
);
457 wxTextOutputStream
&endl( wxTextOutputStream
&stream
)
459 return stream
<< wxT('\n');