]>
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
))
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 */
91 // VZ: what about using strtol()?? (TODO)
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 */
147 // VZ: what about using strtod()?? (TODO)
151 if (!m_input
) return 0;
152 int c
= NextNonSeparators();
153 if (c
==(wxChar
)0) return 0;
156 if (! (c
== wxT('.') || c
== wxT(',') || c
== wxT('-') || c
== wxT('+') || isdigit(c
)) )
179 f
= f
*10 + (c
- wxT('0'));
183 if (c
== wxT('.') || c
== wxT(','))
185 double f_multiplicator
= (double) 0.1;
191 f
+= (c
-wxT('0'))*f_multiplicator
;
192 f_multiplicator
/= 10;
198 double f_multiplicator
= 0.0;
205 case wxT('-'): f_multiplicator
= 0.1; break;
206 case wxT('+'): f_multiplicator
= 10.0; break;
209 e
= Read8(); // why only max 256 ?
212 f
*= f_multiplicator
;
215 SkipIfEndOfLine( c
);
227 wxString
wxTextInputStream::ReadString()
232 wxString
wxTextInputStream::ReadLine()
237 while ( !m_input
.Eof() )
252 wxString
wxTextInputStream::ReadWord()
259 wxChar c
= NextNonSeparators();
263 while ( !m_input
.Eof() )
265 if (m_separators
.Contains(c
))
281 wxTextInputStream
& wxTextInputStream::operator>>(wxString
& word
)
287 wxTextInputStream
& wxTextInputStream::operator>>(wxChar
& c
)
297 if (EatEOL(c
)) c
=wxT('\n');
301 wxTextInputStream
& wxTextInputStream::operator>>(wxInt16
& i
)
303 i
= (wxInt16
)Read16();
307 wxTextInputStream
& wxTextInputStream::operator>>(wxInt32
& i
)
309 i
= (wxInt32
)Read32();
313 wxTextInputStream
& wxTextInputStream::operator>>(wxUint16
& i
)
319 wxTextInputStream
& wxTextInputStream::operator>>(wxUint32
& i
)
325 wxTextInputStream
& wxTextInputStream::operator>>(double& i
)
331 wxTextInputStream
& wxTextInputStream::operator>>(float& f
)
333 f
= (float)ReadDouble();
337 wxTextOutputStream::wxTextOutputStream(wxOutputStream
& s
, wxEOL mode
)
341 if (m_mode
== wxEOL_NATIVE
)
343 #if defined(__WXMSW__) || defined(__WXPM__)
345 #elif defined(__WXMAC__)
353 wxTextOutputStream::~wxTextOutputStream()
357 void wxTextOutputStream::SetMode(wxEOL mode
)
360 if (m_mode
== wxEOL_NATIVE
)
362 #if defined(__WXMSW__) || defined(__WXPM__)
364 #elif defined(__WXMAC__)
372 void wxTextOutputStream::Write32(wxUint32 i
)
375 str
.Printf(wxT("%u"), i
);
380 void wxTextOutputStream::Write16(wxUint16 i
)
383 str
.Printf(wxT("%u"), i
);
388 void wxTextOutputStream::Write8(wxUint8 i
)
391 str
.Printf(wxT("%u"), i
);
396 void wxTextOutputStream::WriteDouble(double d
)
400 str
.Printf(wxT("%f"), d
);
404 void wxTextOutputStream::WriteString(const wxString
& string
)
406 for (size_t i
= 0; i
< string
.Len(); i
++)
408 wxChar c
= string
[i
];
411 if (m_mode
== wxEOL_DOS
)
414 m_output
.Write( (const void*)(&c
), sizeof(wxChar
) );
416 m_output
.Write( (const void*)(&c
), sizeof(wxChar
) );
418 if (m_mode
== wxEOL_MAC
)
421 m_output
.Write( (const void*)(&c
), sizeof(wxChar
) );
425 m_output
.Write( (const void*)(&c
), sizeof(wxChar
) );
430 m_output
.Write( (const void*)(&c
), sizeof(wxChar
) );
435 wxTextOutputStream
& wxTextOutputStream::operator<<(const wxChar
*string
)
437 WriteString( wxString(string
) );
441 wxTextOutputStream
& wxTextOutputStream::operator<<(const wxString
& string
)
443 WriteString( string
);
447 wxTextOutputStream
& wxTextOutputStream::operator<<(wxChar c
)
449 WriteString( wxString(c
) );
453 wxTextOutputStream
& wxTextOutputStream::operator<<(wxInt16 c
)
456 str
.Printf(wxT("%d"), (signed int)c
);
462 wxTextOutputStream
& wxTextOutputStream::operator<<(wxInt32 c
)
465 str
.Printf(wxT("%ld"), (signed long)c
);
471 wxTextOutputStream
& wxTextOutputStream::operator<<(wxUint16 c
)
474 str
.Printf(wxT("%u"), (unsigned int)c
);
480 wxTextOutputStream
& wxTextOutputStream::operator<<(wxUint32 c
)
483 str
.Printf(wxT("%lu"), (unsigned long)c
);
489 wxTextOutputStream
&wxTextOutputStream::operator<<(double f
)
495 wxTextOutputStream
& wxTextOutputStream::operator<<(float f
)
497 WriteDouble((double)f
);
501 wxTextOutputStream
&endl( wxTextOutputStream
&stream
)
503 return stream
<< wxT('\n');