]>
git.saurik.com Git - wxWidgets.git/blob - src/common/txtstrm.cpp
3cb2c5bb5fe88f2a166c5940b9ee69ec27fa38c4
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 wxChar
&sep
)
42 : m_input(&s
), m_string_separator(sep
)
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
== wxT('-') || c
== wxT('+') || isdigit(c
)) )
136 i
= i
*10 + (c
- (int)wxT('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
== wxT('.') || c
== wxT('-') || c
== wxT('+') || isdigit(c
)) )
190 f
= f
*10 + (c
- wxT('0'));
196 double f_multiplicator
= (double) 0.1;
202 f
+= (c
-wxT('0'))*f_multiplicator
;
203 f_multiplicator
/= 10;
209 double f_multiplicator
= 0.0;
216 case wxT('-'): f_multiplicator
= 0.1; break;
217 case wxT('+'): f_multiplicator
= 10.0; break;
220 e
= Read8(); // why only max 256 ?
223 f
*= f_multiplicator
;
226 SkipIfEndOfLine( c
);
238 wxString
wxTextInputStream::ReadString()
243 wxString
wxTextInputStream::ReadLine()
261 // eat on both Mac and DOS
263 wxChar c2
= m_input
->GetC();
274 m_input
->Ungetch( c2
);
285 wxString
wxTextInputStream::ReadWord()
290 if (m_string_separator
==wxT(' ')) c
=NextNonWhiteSpace();
291 else c
= m_input
->GetC();
297 if (c
== m_string_separator
)
308 // eat on both Mac and DOS
310 wxChar c2
= m_input
->GetC();
321 m_input
->Ungetch( c2
);
333 wxTextInputStream
& wxTextInputStream::operator>>(wxString
& word
)
339 wxTextInputStream
& wxTextInputStream::operator>>(wxChar
& c
)
341 wxChar c1
= m_input
->GetC();
351 wxChar c2
= m_input
->GetC();
352 if (!m_input
) return *this;
357 m_input
->Ungetch( c2
);
368 wxTextInputStream
& wxTextInputStream::operator>>(wxInt16
& i
)
370 i
= (wxInt16
)Read16();
374 wxTextInputStream
& wxTextInputStream::operator>>(wxInt32
& i
)
376 i
= (wxInt32
)Read32();
380 wxTextInputStream
& wxTextInputStream::operator>>(wxUint16
& i
)
386 wxTextInputStream
& wxTextInputStream::operator>>(wxUint32
& i
)
392 wxTextInputStream
& wxTextInputStream::operator>>(double& i
)
398 wxTextInputStream
& wxTextInputStream::operator>>(float& f
)
400 f
= (float)ReadDouble();
404 wxTextOutputStream::wxTextOutputStream(wxOutputStream
& s
)
409 wxTextOutputStream::~wxTextOutputStream()
413 void wxTextOutputStream::Write32(wxUint32 i
)
416 str
.Printf(wxT("%u"), i
);
421 void wxTextOutputStream::Write16(wxUint16 i
)
424 str
.Printf(wxT("%u"), i
);
429 void wxTextOutputStream::Write8(wxUint8 i
)
432 str
.Printf(wxT("%u"), i
);
437 void wxTextOutputStream::WriteDouble(double d
)
441 str
.Printf(wxT("%f"), d
);
445 void wxTextOutputStream::WriteString(const wxString
& string
)
447 for (size_t i
= 0; i
< string
.Len(); i
++)
449 wxChar c
= string
[i
];
452 #if defined(__WINDOWS__)
454 m_output
->Write( (const void*)(&c
), sizeof(wxChar
) );
456 m_output
->Write( (const void*)(&c
), sizeof(wxChar
) );
457 #elif defined(__UNIX__)
459 m_output
->Write( (const void*)(&c
), sizeof(wxChar
) );
460 #elif defined(__WXMAC__)
462 m_output
->Write( (const void*)(&c
), sizeof(wxChar
) );
463 #elif defined(__OS2__)
465 m_output
->Write( (const void*)(&c
), sizeof(wxChar
) );
467 m_output
->Write( (const void*)(&c
), sizeof(wxChar
) );
469 #error "wxTextOutputStream: unsupported platform."
474 m_output
->Write( (const void*)(&c
), sizeof(wxChar
) );
479 wxTextOutputStream
& wxTextOutputStream::operator<<(const wxChar
*string
)
481 WriteString( wxString(string
) );
485 wxTextOutputStream
& wxTextOutputStream::operator<<(const wxString
& string
)
487 WriteString( string
);
491 wxTextOutputStream
& wxTextOutputStream::operator<<(wxChar c
)
493 WriteString( wxString(c
) );
497 wxTextOutputStream
& wxTextOutputStream::operator<<(wxInt16 c
)
499 Write16( (wxUint16
)c
);
503 wxTextOutputStream
& wxTextOutputStream::operator<<(wxInt32 c
)
505 Write32( (wxUint32
)c
);
509 wxTextOutputStream
& wxTextOutputStream::operator<<(wxUint16 c
)
515 wxTextOutputStream
& wxTextOutputStream::operator<<(wxUint32 c
)
521 wxTextOutputStream
&wxTextOutputStream::operator<<(double f
)
527 wxTextOutputStream
& wxTextOutputStream::operator<<(float f
)
529 WriteDouble((double)f
);
533 wxTextOutputStream
&endl( wxTextOutputStream
&stream
)
535 return stream
<< wxT('\n');