]>
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 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)
156 int c
= NextNonSeparators();
157 if (c
==(wxChar
)0) return 0;
160 if (! (c
== wxT('.') || c
== wxT(',') || c
== wxT('-') || c
== wxT('+') || isdigit(c
)) )
183 f
= f
*10 + (c
- wxT('0'));
187 if (c
== wxT('.') || c
== wxT(','))
189 double f_multiplicator
= (double) 0.1;
195 f
+= (c
-wxT('0'))*f_multiplicator
;
196 f_multiplicator
/= 10;
202 double f_multiplicator
= 0.0;
209 case wxT('-'): f_multiplicator
= 0.1; break;
210 case wxT('+'): f_multiplicator
= 10.0; break;
213 e
= Read8(); // why only max 256 ?
216 f
*= f_multiplicator
;
219 SkipIfEndOfLine( c
);
230 wxString
wxTextInputStream::ReadString()
235 wxString
wxTextInputStream::ReadLine()
240 while ( !m_input
.Eof() )
256 wxString
wxTextInputStream::ReadWord()
263 wxChar c
= NextNonSeparators();
269 while ( !m_input
.Eof() )
276 if (m_separators
.Contains(c
))
288 wxTextInputStream
& wxTextInputStream::operator>>(wxString
& word
)
294 wxTextInputStream
& wxTextInputStream::operator>>(char& c
)
310 wxTextInputStream
& wxTextInputStream::operator>>(wxInt16
& i
)
312 i
= (wxInt16
)Read16();
316 wxTextInputStream
& wxTextInputStream::operator>>(wxInt32
& i
)
318 i
= (wxInt32
)Read32();
322 wxTextInputStream
& wxTextInputStream::operator>>(wxUint16
& i
)
328 wxTextInputStream
& wxTextInputStream::operator>>(wxUint32
& i
)
334 wxTextInputStream
& wxTextInputStream::operator>>(double& i
)
340 wxTextInputStream
& wxTextInputStream::operator>>(float& f
)
342 f
= (float)ReadDouble();
346 wxTextOutputStream::wxTextOutputStream(wxOutputStream
& s
, wxEOL mode
)
350 if (m_mode
== wxEOL_NATIVE
)
352 #if defined(__WXMSW__) || defined(__WXPM__)
354 #elif defined(__WXMAC__) && !defined(__DARWIN__)
362 wxTextOutputStream::~wxTextOutputStream()
366 void wxTextOutputStream::SetMode(wxEOL mode
)
369 if (m_mode
== wxEOL_NATIVE
)
371 #if defined(__WXMSW__) || defined(__WXPM__)
373 #elif defined(__WXMAC__) && !defined(__DARWIN__)
381 void wxTextOutputStream::Write32(wxUint32 i
)
384 str
.Printf(wxT("%u"), i
);
389 void wxTextOutputStream::Write16(wxUint16 i
)
392 str
.Printf(wxT("%u"), i
);
397 void wxTextOutputStream::Write8(wxUint8 i
)
400 str
.Printf(wxT("%u"), i
);
405 void wxTextOutputStream::WriteDouble(double d
)
409 str
.Printf(wxT("%f"), d
);
413 void wxTextOutputStream::WriteString(const wxString
& string
)
415 for (size_t i
= 0; i
< string
.Len(); i
++)
417 wxChar c
= string
[i
];
420 if (m_mode
== wxEOL_DOS
)
423 m_output
.Write( (const void*)(&c
), sizeof(wxChar
) );
425 m_output
.Write( (const void*)(&c
), sizeof(wxChar
) );
427 if (m_mode
== wxEOL_MAC
)
430 m_output
.Write( (const void*)(&c
), sizeof(wxChar
) );
434 m_output
.Write( (const void*)(&c
), sizeof(wxChar
) );
439 m_output
.Write( (const void*)(&c
), sizeof(wxChar
) );
444 wxTextOutputStream
& wxTextOutputStream::operator<<(const wxChar
*string
)
446 WriteString( wxString(string
) );
450 wxTextOutputStream
& wxTextOutputStream::operator<<(const wxString
& string
)
452 WriteString( string
);
456 wxTextOutputStream
& wxTextOutputStream::operator<<(char c
)
458 // these strange manipulations are needed in Unicode mode
463 WriteString( wxString(buf
) );
467 wxTextOutputStream
& wxTextOutputStream::operator<<(wxInt16 c
)
470 str
.Printf(wxT("%d"), (signed int)c
);
476 wxTextOutputStream
& wxTextOutputStream::operator<<(wxInt32 c
)
479 str
.Printf(wxT("%ld"), (signed long)c
);
485 wxTextOutputStream
& wxTextOutputStream::operator<<(wxUint16 c
)
488 str
.Printf(wxT("%u"), (unsigned int)c
);
494 wxTextOutputStream
& wxTextOutputStream::operator<<(wxUint32 c
)
497 str
.Printf(wxT("%lu"), (unsigned long)c
);
503 wxTextOutputStream
&wxTextOutputStream::operator<<(double f
)
509 wxTextOutputStream
& wxTextOutputStream::operator<<(float f
)
511 WriteDouble((double)f
);
515 wxTextOutputStream
&endl( wxTextOutputStream
&stream
)
517 return stream
<< wxT('\n');