]>
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 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
21 #include "wx/txtstrm.h"
25 // ----------------------------------------------------------------------------
27 // ----------------------------------------------------------------------------
33 // ----------------------------------------------------------------------------
35 // ----------------------------------------------------------------------------
38 wxTextInputStream::wxTextInputStream(wxInputStream
&s
,
41 : m_input(s
), m_separators(sep
), m_conv(conv
.Clone())
43 memset((void*)m_lastBytes
, 0, 10);
46 wxTextInputStream::wxTextInputStream(wxInputStream
&s
, const wxString
&sep
)
47 : m_input(s
), m_separators(sep
)
49 memset((void*)m_lastBytes
, 0, 10);
53 wxTextInputStream::~wxTextInputStream()
57 #endif // wxUSE_UNICODE
60 void wxTextInputStream::UngetLast()
63 while(m_lastBytes
[byteCount
]) // pseudo ANSI strlen (even for Unicode!)
65 m_input
.Ungetch(m_lastBytes
, byteCount
);
66 memset((void*)m_lastBytes
, 0, 10);
69 wxChar
wxTextInputStream::NextChar()
73 memset((void*)m_lastBytes
, 0, 10);
74 for(size_t inlen
= 0; inlen
< 9; inlen
++)
76 // actually read the next character
77 m_lastBytes
[inlen
] = m_input
.GetC();
79 if(m_input
.LastRead() <= 0)
82 if ( m_conv
->ToWChar(wbuf
, WXSIZEOF(wbuf
), m_lastBytes
, inlen
+ 1)
86 // there should be no encoding which requires more than nine bytes for one character...
89 m_lastBytes
[0] = m_input
.GetC();
91 if(m_input
.LastRead() <= 0)
94 return m_lastBytes
[0];
99 wxChar
wxTextInputStream::NextNonSeparators()
103 wxChar c
= NextChar();
104 if (c
== wxEOT
) return (wxChar
) 0;
106 if (c
!= wxT('\n') &&
108 !m_separators
.Contains(c
))
114 bool wxTextInputStream::EatEOL(const wxChar
&c
)
116 if (c
== wxT('\n')) return true; // eat on UNIX
118 if (c
== wxT('\r')) // eat on both Mac and DOS
120 wxChar c2
= NextChar();
121 if(c2
== wxEOT
) return true; // end of stream reached, had enough :-)
123 if (c2
!= wxT('\n')) UngetLast(); // Don't eat on Mac
130 wxUint32
wxTextInputStream::Read32(int base
)
132 wxASSERT_MSG( !base
|| (base
> 1 && base
<= 36), _T("invalid base") );
133 if(!m_input
) return 0;
135 wxString word
= ReadWord();
138 return wxStrtoul(word
.c_str(), 0, base
);
141 wxUint16
wxTextInputStream::Read16(int base
)
143 return (wxUint16
)Read32(base
);
146 wxUint8
wxTextInputStream::Read8(int base
)
148 return (wxUint8
)Read32(base
);
151 wxInt32
wxTextInputStream::Read32S(int base
)
153 wxASSERT_MSG( !base
|| (base
> 1 && base
<= 36), _T("invalid base") );
154 if(!m_input
) return 0;
156 wxString word
= ReadWord();
159 return wxStrtol(word
.c_str(), 0, base
);
162 wxInt16
wxTextInputStream::Read16S(int base
)
164 return (wxInt16
)Read32S(base
);
167 wxInt8
wxTextInputStream::Read8S(int base
)
169 return (wxInt8
)Read32S(base
);
172 double wxTextInputStream::ReadDouble()
174 if(!m_input
) return 0;
175 wxString word
= ReadWord();
178 return wxStrtod(word
.c_str(), 0);
181 wxString
wxTextInputStream::ReadString()
186 wxString
wxTextInputStream::ReadLine()
190 while ( !m_input
.Eof() )
192 wxChar c
= NextChar();
208 wxString
wxTextInputStream::ReadWord()
215 wxChar c
= NextNonSeparators();
221 while ( !m_input
.Eof() )
227 if (m_separators
.Contains(c
))
239 wxTextInputStream
& wxTextInputStream::operator>>(wxString
& word
)
245 wxTextInputStream
& wxTextInputStream::operator>>(char& c
)
248 if(m_input
.LastRead() <= 0) c
= 0;
256 #if wxUSE_UNICODE && wxWCHAR_T_IS_REAL_TYPE
258 wxTextInputStream
& wxTextInputStream::operator>>(wchar_t& wc
)
265 #endif // wxUSE_UNICODE
267 wxTextInputStream
& wxTextInputStream::operator>>(wxInt16
& i
)
269 i
= (wxInt16
)Read16();
273 wxTextInputStream
& wxTextInputStream::operator>>(wxInt32
& i
)
275 i
= (wxInt32
)Read32();
279 wxTextInputStream
& wxTextInputStream::operator>>(wxUint16
& i
)
285 wxTextInputStream
& wxTextInputStream::operator>>(wxUint32
& i
)
291 wxTextInputStream
& wxTextInputStream::operator>>(double& i
)
297 wxTextInputStream
& wxTextInputStream::operator>>(float& f
)
299 f
= (float)ReadDouble();
306 wxTextOutputStream::wxTextOutputStream(wxOutputStream
& s
,
308 const wxMBConv
& conv
)
309 : m_output(s
), m_conv(conv
.Clone())
311 wxTextOutputStream::wxTextOutputStream(wxOutputStream
& s
, wxEOL mode
)
316 if (m_mode
== wxEOL_NATIVE
)
318 #if defined(__WXMSW__) || defined(__WXPM__)
320 #elif defined(__WXMAC__) && !defined(__DARWIN__)
328 wxTextOutputStream::~wxTextOutputStream()
332 #endif // wxUSE_UNICODE
335 void wxTextOutputStream::SetMode(wxEOL mode
)
338 if (m_mode
== wxEOL_NATIVE
)
340 #if defined(__WXMSW__) || defined(__WXPM__)
342 #elif defined(__WXMAC__) && !defined(__DARWIN__)
350 void wxTextOutputStream::Write32(wxUint32 i
)
353 str
.Printf(wxT("%u"), i
);
358 void wxTextOutputStream::Write16(wxUint16 i
)
361 str
.Printf(wxT("%u"), (unsigned)i
);
366 void wxTextOutputStream::Write8(wxUint8 i
)
369 str
.Printf(wxT("%u"), (unsigned)i
);
374 void wxTextOutputStream::WriteDouble(double d
)
378 str
.Printf(wxT("%f"), d
);
382 void wxTextOutputStream::WriteString(const wxString
& string
)
384 size_t len
= string
.length();
389 for ( size_t i
= 0; i
< len
; i
++ )
391 const wxChar c
= string
[i
];
392 if ( c
== wxT('\n') )
405 wxFAIL_MSG( _T("unknown EOL mode in wxTextOutputStream") );
409 // don't treat '\n' specially
418 wxCharBuffer buffer
= m_conv
->cWC2MB(out
, out
.length(), &len
);
419 m_output
.Write(buffer
, len
);
421 m_output
.Write(out
.c_str(), out
.length() );
425 wxTextOutputStream
& wxTextOutputStream::PutChar(wxChar c
)
428 WriteString( wxString(&c
, *m_conv
, 1) );
430 WriteString( wxString(&c
, wxConvLocal
, 1) );
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<<(char c
)
449 WriteString( wxString::FromAscii(c
) );
454 #if wxUSE_UNICODE && wxWCHAR_T_IS_REAL_TYPE
456 wxTextOutputStream
& wxTextOutputStream::operator<<(wchar_t wc
)
458 WriteString( wxString(&wc
, *m_conv
, 1) );
463 #endif // wxUSE_UNICODE
465 wxTextOutputStream
& wxTextOutputStream::operator<<(wxInt16 c
)
468 str
.Printf(wxT("%d"), (signed int)c
);
474 wxTextOutputStream
& wxTextOutputStream::operator<<(wxInt32 c
)
477 str
.Printf(wxT("%ld"), (signed long)c
);
483 wxTextOutputStream
& wxTextOutputStream::operator<<(wxUint16 c
)
486 str
.Printf(wxT("%u"), (unsigned int)c
);
492 wxTextOutputStream
& wxTextOutputStream::operator<<(wxUint32 c
)
495 str
.Printf(wxT("%lu"), (unsigned long)c
);
501 wxTextOutputStream
&wxTextOutputStream::operator<<(double f
)
507 wxTextOutputStream
& wxTextOutputStream::operator<<(float f
)
509 WriteDouble((double)f
);
513 wxTextOutputStream
&endl( wxTextOutputStream
&stream
)
515 return stream
.PutChar(wxT('\n'));