]>
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 licence
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 // ----------------------------------------------------------------------------
42 wxTextInputStream::wxTextInputStream(wxInputStream
&s
, const wxString
&sep
, wxMBConv
& conv
)
43 : m_input(s
), m_separators(sep
), m_conv(conv
)
47 wxTextInputStream::wxTextInputStream(wxInputStream
&s
, const wxString
&sep
)
48 : m_input(s
), m_separators(sep
)
53 wxTextInputStream::~wxTextInputStream()
57 wxChar
wxTextInputStream::NextNonSeparators()
59 wxChar c
= (wxChar
) 0;
62 if (!m_input
) return (wxChar
) 0;
67 !m_separators
.Contains(c
))
73 bool wxTextInputStream::EatEOL(const wxChar
&c
)
75 if (c
== wxT('\n')) return TRUE
; // eat on UNIX
77 if (c
== wxT('\r')) // eat on both Mac and DOS
79 if (!m_input
) return TRUE
;
80 wxChar c2
= m_input
.GetC();
82 if (c2
!= wxT('\n')) m_input
.Ungetch( c2
); // Don't eat on Mac
89 void wxTextInputStream::SkipIfEndOfLine( wxChar c
)
91 if (EatEOL(c
)) return;
92 else m_input
.Ungetch( c
); // no line terminator
95 wxUint32
wxTextInputStream::Read32()
97 /* I only implemented a simple integer parser */
98 // VZ: what about using strtol()?? (TODO)
103 if (!m_input
) return 0;
104 int c
= NextNonSeparators();
105 if (c
==(wxChar
)0) return 0;
108 if (! (c
== wxT('-') || c
== wxT('+') || isdigit(c
)) )
130 i
= i
*10 + (c
- (int)wxT('0'));
134 SkipIfEndOfLine( c
);
141 wxUint16
wxTextInputStream::Read16()
143 return (wxUint16
)Read32();
146 wxUint8
wxTextInputStream::Read8()
148 return (wxUint8
)Read32();
151 double wxTextInputStream::ReadDouble()
153 /* I only implemented a simple float parser
154 * VZ: what about using strtod()?? (TODO)
163 int c
= NextNonSeparators();
164 if (c
==(wxChar
)0) return 0;
167 if (! (c
== wxT('.') || c
== wxT(',') || c
== wxT('-') || c
== wxT('+') || isdigit(c
)) )
190 f
= f
*10 + (c
- wxT('0'));
194 if (c
== wxT('.') || c
== wxT(','))
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
);
237 wxString
wxTextInputStream::ReadString()
242 wxString
wxTextInputStream::ReadLine()
246 while ( !m_input
.Eof() )
249 // FIXME: this is only works for single byte encodings
250 // How-to read a single char in an unkown encoding???
252 buf
[0] = m_input
.GetC();
256 m_conv
.MB2WC( wbuf
, buf
, 2 );
259 char c
= m_input
.GetC();
274 wxString
wxTextInputStream::ReadWord()
281 wxChar c
= NextNonSeparators();
287 while ( !m_input
.Eof() )
294 if (m_separators
.Contains(c
))
306 wxTextInputStream
& wxTextInputStream::operator>>(wxString
& word
)
312 wxTextInputStream
& wxTextInputStream::operator>>(char& c
)
328 wxTextInputStream
& wxTextInputStream::operator>>(wxInt16
& i
)
330 i
= (wxInt16
)Read16();
334 wxTextInputStream
& wxTextInputStream::operator>>(wxInt32
& i
)
336 i
= (wxInt32
)Read32();
340 wxTextInputStream
& wxTextInputStream::operator>>(wxUint16
& i
)
346 wxTextInputStream
& wxTextInputStream::operator>>(wxUint32
& i
)
352 wxTextInputStream
& wxTextInputStream::operator>>(double& i
)
358 wxTextInputStream
& wxTextInputStream::operator>>(float& f
)
360 f
= (float)ReadDouble();
367 wxTextOutputStream::wxTextOutputStream(wxOutputStream
& s
, wxEOL mode
, wxMBConv
& conv
)
368 : m_output(s
), m_conv(conv
)
370 wxTextOutputStream::wxTextOutputStream(wxOutputStream
& s
, wxEOL mode
)
375 if (m_mode
== wxEOL_NATIVE
)
377 #if defined(__WXMSW__) || defined(__WXPM__)
379 #elif defined(__WXMAC__) && !defined(__DARWIN__)
387 wxTextOutputStream::~wxTextOutputStream()
391 void wxTextOutputStream::SetMode(wxEOL mode
)
394 if (m_mode
== wxEOL_NATIVE
)
396 #if defined(__WXMSW__) || defined(__WXPM__)
398 #elif defined(__WXMAC__) && !defined(__DARWIN__)
406 void wxTextOutputStream::Write32(wxUint32 i
)
409 str
.Printf(wxT("%u"), i
);
414 void wxTextOutputStream::Write16(wxUint16 i
)
417 str
.Printf(wxT("%u"), i
);
422 void wxTextOutputStream::Write8(wxUint8 i
)
425 str
.Printf(wxT("%u"), i
);
430 void wxTextOutputStream::WriteDouble(double d
)
434 str
.Printf(wxT("%f"), d
);
438 void wxTextOutputStream::WriteString(const wxString
& string
)
440 size_t len
= string
.length();
445 for ( size_t i
= 0; i
< len
; i
++ )
447 const wxChar c
= string
[i
];
448 if ( c
== wxT('\n') )
461 wxFAIL_MSG( _T("unknown EOL mode in wxTextOutputStream") );
465 // don't treat '\n' specially
473 // We must not write the trailing NULL here
475 wxCharBuffer buffer
= m_conv
.cWC2MB( out
);
476 m_output
.Write( (const char*) buffer
, strlen( (const char*) buffer
) );
478 m_output
.Write(out
.c_str(), out
.length() );
482 wxTextOutputStream
& wxTextOutputStream::operator<<(const wxChar
*string
)
484 WriteString( wxString(string
) );
488 wxTextOutputStream
& wxTextOutputStream::operator<<(const wxString
& string
)
490 WriteString( string
);
494 wxTextOutputStream
& wxTextOutputStream::operator<<(char c
)
496 WriteString( wxString::FromAscii(c
) );
501 wxTextOutputStream
& wxTextOutputStream::operator<<(wxInt16 c
)
504 str
.Printf(wxT("%d"), (signed int)c
);
510 wxTextOutputStream
& wxTextOutputStream::operator<<(wxInt32 c
)
513 str
.Printf(wxT("%ld"), (signed long)c
);
519 wxTextOutputStream
& wxTextOutputStream::operator<<(wxUint16 c
)
522 str
.Printf(wxT("%u"), (unsigned int)c
);
528 wxTextOutputStream
& wxTextOutputStream::operator<<(wxUint32 c
)
531 str
.Printf(wxT("%lu"), (unsigned long)c
);
537 wxTextOutputStream
&wxTextOutputStream::operator<<(double f
)
543 wxTextOutputStream
& wxTextOutputStream::operator<<(float f
)
545 WriteDouble((double)f
);
549 wxTextOutputStream
&endl( wxTextOutputStream
&stream
)
551 return stream
<< wxT('\n');