]>
git.saurik.com Git - wxWidgets.git/blob - src/common/txtstrm.cpp
4f863093a6e6a97ad59c6b0639f7e13aefc5bb34
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
))
66 inline bool wxTextInputStream::EatEOL(const wxChar
&c
)
68 if (c
== wxT('\n')) return TRUE
; // eat on UNIX
70 if (c
== wxT('\r')) // eat on both Mac and DOS
72 if (!m_input
) return TRUE
;
73 wxChar c2
= m_input
.GetC();
75 if (c2
!= wxT('\n')) m_input
.Ungetch( c2
); // Don't eat on Mac
82 void wxTextInputStream::SkipIfEndOfLine( wxChar c
)
84 if (EatEOL(c
)) return;
85 else m_input
.Ungetch( c
); // no line terminator
88 wxUint32
wxTextInputStream::Read32()
90 /* I only implemented a simple integer parser */
94 if (!m_input
) return 0;
95 int c
= NextNonSeparators();
96 if (c
==(wxChar
)0) return 0;
99 if (! (c
== wxT('-') || c
== wxT('+') || isdigit(c
)) )
121 i
= i
*10 + (c
- (int)wxT('0'));
125 SkipIfEndOfLine( c
);
132 wxUint16
wxTextInputStream::Read16()
134 return (wxUint16
)Read32();
137 wxUint8
wxTextInputStream::Read8()
139 return (wxUint8
)Read32();
142 double wxTextInputStream::ReadDouble()
144 /* I only implemented a simple float parser */
148 if (!m_input
) return 0;
149 int c
= NextNonSeparators();
150 if (c
==(wxChar
)0) return 0;
153 if (! (c
== wxT('.') || c
== wxT(',') || c
== wxT('-') || c
== wxT('+') || isdigit(c
)) )
176 f
= f
*10 + (c
- wxT('0'));
180 if (c
== wxT('.') || c
== wxT(','))
182 double f_multiplicator
= (double) 0.1;
188 f
+= (c
-wxT('0'))*f_multiplicator
;
189 f_multiplicator
/= 10;
195 double f_multiplicator
= 0.0;
202 case wxT('-'): f_multiplicator
= 0.1; break;
203 case wxT('+'): f_multiplicator
= 10.0; break;
206 e
= Read8(); // why only max 256 ?
209 f
*= f_multiplicator
;
212 SkipIfEndOfLine( c
);
224 wxString
wxTextInputStream::ReadString()
229 wxString
wxTextInputStream::ReadLine()
239 if (EatEOL(c
)) break;
247 wxString
wxTextInputStream::ReadWord()
249 if (!m_input
) return "";
252 wxChar c
=NextNonSeparators();
253 if (c
==(wxChar
)0) return "";
257 if (m_separators
.Contains(c
)) break;
259 if (EatEOL(c
)) break;
270 wxTextInputStream
& wxTextInputStream::operator>>(wxString
& word
)
276 wxTextInputStream
& wxTextInputStream::operator>>(wxChar
& c
)
286 if (EatEOL(c
)) c
=wxT('\n');
290 wxTextInputStream
& wxTextInputStream::operator>>(wxInt16
& i
)
292 i
= (wxInt16
)Read16();
296 wxTextInputStream
& wxTextInputStream::operator>>(wxInt32
& i
)
298 i
= (wxInt32
)Read32();
302 wxTextInputStream
& wxTextInputStream::operator>>(wxUint16
& i
)
308 wxTextInputStream
& wxTextInputStream::operator>>(wxUint32
& i
)
314 wxTextInputStream
& wxTextInputStream::operator>>(double& i
)
320 wxTextInputStream
& wxTextInputStream::operator>>(float& f
)
322 f
= (float)ReadDouble();
326 wxTextOutputStream::wxTextOutputStream(wxOutputStream
& s
)
331 wxTextOutputStream::~wxTextOutputStream()
335 void wxTextOutputStream::Write32(wxUint32 i
)
338 str
.Printf(wxT("%u"), i
);
343 void wxTextOutputStream::Write16(wxUint16 i
)
346 str
.Printf(wxT("%u"), i
);
351 void wxTextOutputStream::Write8(wxUint8 i
)
354 str
.Printf(wxT("%u"), i
);
359 void wxTextOutputStream::WriteDouble(double d
)
363 str
.Printf(wxT("%f"), d
);
367 void wxTextOutputStream::WriteString(const wxString
& string
)
369 for (size_t i
= 0; i
< string
.Len(); i
++)
371 wxChar c
= string
[i
];
374 #if defined(__WINDOWS__)
376 m_output
.Write( (const void*)(&c
), sizeof(wxChar
) );
378 m_output
.Write( (const void*)(&c
), sizeof(wxChar
) );
379 #elif defined(__UNIX__)
381 m_output
.Write( (const void*)(&c
), sizeof(wxChar
) );
382 #elif defined(__WXMAC__)
384 m_output
.Write( (const void*)(&c
), sizeof(wxChar
) );
385 #elif defined(__OS2__)
387 m_output
.Write( (const void*)(&c
), sizeof(wxChar
) );
389 m_output
.Write( (const void*)(&c
), sizeof(wxChar
) );
391 #error "wxTextOutputStream: unsupported platform."
396 m_output
.Write( (const void*)(&c
), sizeof(wxChar
) );
401 wxTextOutputStream
& wxTextOutputStream::operator<<(const wxChar
*string
)
403 WriteString( wxString(string
) );
407 wxTextOutputStream
& wxTextOutputStream::operator<<(const wxString
& string
)
409 WriteString( string
);
413 wxTextOutputStream
& wxTextOutputStream::operator<<(wxChar c
)
415 WriteString( wxString(c
) );
419 wxTextOutputStream
& wxTextOutputStream::operator<<(wxInt16 c
)
422 str
.Printf(wxT("%d"), (signed int)c
);
428 wxTextOutputStream
& wxTextOutputStream::operator<<(wxInt32 c
)
431 str
.Printf(wxT("%ld"), (signed long)c
);
437 wxTextOutputStream
& wxTextOutputStream::operator<<(wxUint16 c
)
440 str
.Printf(wxT("%u"), (unsigned int)c
);
446 wxTextOutputStream
& wxTextOutputStream::operator<<(wxUint32 c
)
449 str
.Printf(wxT("%lu"), (unsigned long)c
);
455 wxTextOutputStream
&wxTextOutputStream::operator<<(double f
)
461 wxTextOutputStream
& wxTextOutputStream::operator<<(float f
)
463 WriteDouble((double)f
);
467 wxTextOutputStream
&endl( wxTextOutputStream
&stream
)
469 return stream
<< wxT('\n');