]>
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
)
46 wxTextInputStream::~wxTextInputStream()
50 wxChar
wxTextInputStream::NextNonWhiteSpace()
52 wxChar c
= (wxChar
) 0;
56 if (!m_input
) return (wxChar
) 0;
67 // this shouldn't happen
71 void wxTextInputStream::SkipIfEndOfLine( wxChar c
)
81 // eat on both Mac and DOS
83 wxChar c2
= m_input
->GetC();
94 m_input
->Ungetch( c2
);
100 m_input
->Ungetch( c
);
104 wxUint32
wxTextInputStream::Read32()
106 /* I only implemented a simple integer parser */
110 int c
= NextNonWhiteSpace();
111 if (!m_input
) return 0;
114 if (! (c
== _T('-') || c
== _T('+') || isdigit(c
)) )
136 i
= i
*10 + (c
- (int)_T('0'));
140 SkipIfEndOfLine( c
);
147 wxUint16
wxTextInputStream::Read16()
149 return (wxUint16
)Read32();
152 wxUint8
wxTextInputStream::Read8()
154 return (wxUint8
)Read32();
157 double wxTextInputStream::ReadDouble()
159 /* I only implemented a simple float parser */
163 int c
= NextNonWhiteSpace();
164 if (!m_input
) return 0.0;
167 if (! (c
== _T('.') || c
== _T('-') || c
== _T('+') || isdigit(c
)) )
190 f
= f
*10 + (c
- _T('0'));
196 double f_multiplicator
= (double) 0.1;
202 f
+= (c
-_T('0'))*f_multiplicator
;
203 f_multiplicator
/= 10;
209 double f_multiplicator
= 0.0;
216 case _T('-'): f_multiplicator
= 0.1; break;
217 case _T('+'): f_multiplicator
= 10.0; break;
220 e
= Read8(); // why only max 256 ?
223 f
*= f_multiplicator
;
226 SkipIfEndOfLine( c
);
238 wxString
wxTextInputStream::ReadString()
256 // eat on both Mac and DOS
258 wxChar c2
= m_input
->GetC();
269 m_input
->Ungetch( c2
);
280 wxTextInputStream
& wxTextInputStream::operator>>(wxString
& line
)
286 wxTextInputStream
& wxTextInputStream::operator>>(wxChar
& c
)
288 wxChar c1
= m_input
->GetC();
298 wxChar c2
= m_input
->GetC();
299 if (!m_input
) return *this;
304 m_input
->Ungetch( c2
);
315 wxTextInputStream
& wxTextInputStream::operator>>(wxInt16
& i
)
317 i
= (wxInt16
)Read16();
321 wxTextInputStream
& wxTextInputStream::operator>>(wxInt32
& i
)
323 i
= (wxInt32
)Read32();
327 wxTextInputStream
& wxTextInputStream::operator>>(wxUint16
& i
)
333 wxTextInputStream
& wxTextInputStream::operator>>(wxUint32
& i
)
339 wxTextInputStream
& wxTextInputStream::operator>>(double& i
)
345 wxTextInputStream
& wxTextInputStream::operator>>(float& f
)
347 f
= (float)ReadDouble();
351 wxTextOutputStream::wxTextOutputStream(wxOutputStream
& s
)
356 wxTextOutputStream::~wxTextOutputStream()
360 void wxTextOutputStream::Write32(wxUint32 i
)
363 str
.Printf(_T("%u"), i
);
368 void wxTextOutputStream::Write16(wxUint16 i
)
371 str
.Printf(_T("%u"), i
);
376 void wxTextOutputStream::Write8(wxUint8 i
)
379 str
.Printf(_T("%u"), i
);
384 void wxTextOutputStream::WriteDouble(double d
)
388 str
.Printf(_T("%f"), d
);
392 void wxTextOutputStream::WriteString(const wxString
& string
)
394 for (size_t i
= 0; i
< string
.Len(); i
++)
396 wxChar c
= string
[i
];
399 #if defined(__WINDOWS__)
401 m_output
->Write( (const void*)(&c
), sizeof(wxChar
) );
403 m_output
->Write( (const void*)(&c
), sizeof(wxChar
) );
404 #elif defined(__UNIX__)
406 m_output
->Write( (const void*)(&c
), sizeof(wxChar
) );
407 #elif defined(__WXMAC__)
409 m_output
->Write( (const void*)(&c
), sizeof(wxChar
) );
410 #elif defined(__OS2__)
412 m_output
->Write( (const void*)(&c
), sizeof(wxChar
) );
414 m_output
->Write( (const void*)(&c
), sizeof(wxChar
) );
416 #error "wxTextOutputStream: unsupported platform."
421 m_output
->Write( (const void*)(&c
), sizeof(wxChar
) );
426 wxTextOutputStream
& wxTextOutputStream::operator<<(const wxChar
*string
)
428 WriteString( wxString(string
) );
432 wxTextOutputStream
& wxTextOutputStream::operator<<(const wxString
& string
)
434 WriteString( string
);
438 wxTextOutputStream
& wxTextOutputStream::operator<<(wxChar c
)
440 WriteString( wxString(c
) );
444 wxTextOutputStream
& wxTextOutputStream::operator<<(wxInt16 c
)
446 Write16( (wxUint16
)c
);
450 wxTextOutputStream
& wxTextOutputStream::operator<<(wxInt32 c
)
452 Write32( (wxUint32
)c
);
456 wxTextOutputStream
& wxTextOutputStream::operator<<(wxUint16 c
)
462 wxTextOutputStream
& wxTextOutputStream::operator<<(wxUint32 c
)
468 wxTextOutputStream
&wxTextOutputStream::operator<<(double f
)
474 wxTextOutputStream
& wxTextOutputStream::operator<<(float f
)
476 WriteDouble((double)f
);
480 wxTextOutputStream
&endl( wxTextOutputStream
&stream
)
482 return stream
<< _T('\n');