]>
git.saurik.com Git - wxWidgets.git/blob - src/common/txtstrm.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/txtstrm.cpp
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"
25 #include "wx/txtstrm.h"
29 // ----------------------------------------------------------------------------
31 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
42 wxTextInputStream::wxTextInputStream(wxInputStream
&s
,
45 : m_input(s
), m_separators(sep
), m_conv(conv
.Clone())
47 memset((void*)m_lastBytes
, 0, 10);
50 wxTextInputStream::wxTextInputStream(wxInputStream
&s
, const wxString
&sep
)
51 : m_input(s
), m_separators(sep
)
53 memset((void*)m_lastBytes
, 0, 10);
57 wxTextInputStream::~wxTextInputStream()
61 #endif // wxUSE_UNICODE
64 void wxTextInputStream::UngetLast()
67 while(m_lastBytes
[byteCount
]) // pseudo ANSI strlen (even for Unicode!)
69 m_input
.Ungetch(m_lastBytes
, byteCount
);
70 memset((void*)m_lastBytes
, 0, 10);
73 wxChar
wxTextInputStream::NextChar()
77 memset((void*)m_lastBytes
, 0, 10);
78 for(size_t inlen
= 0; inlen
< 9; inlen
++)
80 // actually read the next character
81 m_lastBytes
[inlen
] = m_input
.GetC();
83 if(m_input
.LastRead() <= 0)
86 if ( m_conv
->ToWChar(wbuf
, WXSIZEOF(wbuf
), m_lastBytes
, inlen
+ 1)
90 // there should be no encoding which requires more than nine bytes for one character...
93 m_lastBytes
[0] = m_input
.GetC();
95 if(m_input
.LastRead() <= 0)
98 return m_lastBytes
[0];
103 wxChar
wxTextInputStream::NextNonSeparators()
107 wxChar c
= NextChar();
108 if (c
== wxEOT
) return (wxChar
) 0;
110 if (c
!= wxT('\n') &&
112 !m_separators
.Contains(c
))
118 bool wxTextInputStream::EatEOL(const wxChar
&c
)
120 if (c
== wxT('\n')) return true; // eat on UNIX
122 if (c
== wxT('\r')) // eat on both Mac and DOS
124 wxChar c2
= NextChar();
125 if(c2
== wxEOT
) return true; // end of stream reached, had enough :-)
127 if (c2
!= wxT('\n')) UngetLast(); // Don't eat on Mac
134 wxUint32
wxTextInputStream::Read32(int base
)
136 wxASSERT_MSG( !base
|| (base
> 1 && base
<= 36), _T("invalid base") );
137 if(!m_input
) return 0;
139 wxString word
= ReadWord();
142 return wxStrtoul(word
.c_str(), 0, base
);
145 wxUint16
wxTextInputStream::Read16(int base
)
147 return (wxUint16
)Read32(base
);
150 wxUint8
wxTextInputStream::Read8(int base
)
152 return (wxUint8
)Read32(base
);
155 wxInt32
wxTextInputStream::Read32S(int base
)
157 wxASSERT_MSG( !base
|| (base
> 1 && base
<= 36), _T("invalid base") );
158 if(!m_input
) return 0;
160 wxString word
= ReadWord();
163 return wxStrtol(word
.c_str(), 0, base
);
166 wxInt16
wxTextInputStream::Read16S(int base
)
168 return (wxInt16
)Read32S(base
);
171 wxInt8
wxTextInputStream::Read8S(int base
)
173 return (wxInt8
)Read32S(base
);
176 double wxTextInputStream::ReadDouble()
178 if(!m_input
) return 0;
179 wxString word
= ReadWord();
182 return wxStrtod(word
.c_str(), 0);
185 #if WXWIN_COMPATIBILITY_2_6
187 wxString
wxTextInputStream::ReadString()
192 #endif // WXWIN_COMPATIBILITY_2_6
194 wxString
wxTextInputStream::ReadLine()
198 while ( !m_input
.Eof() )
200 wxChar c
= NextChar();
213 wxString
wxTextInputStream::ReadWord()
220 wxChar c
= NextNonSeparators();
226 while ( !m_input
.Eof() )
232 if (m_separators
.Contains(c
))
244 wxTextInputStream
& wxTextInputStream::operator>>(wxString
& word
)
250 wxTextInputStream
& wxTextInputStream::operator>>(char& c
)
253 if(m_input
.LastRead() <= 0) c
= 0;
261 #if wxUSE_UNICODE && wxWCHAR_T_IS_REAL_TYPE
263 wxTextInputStream
& wxTextInputStream::operator>>(wchar_t& wc
)
270 #endif // wxUSE_UNICODE
272 wxTextInputStream
& wxTextInputStream::operator>>(wxInt16
& i
)
274 i
= (wxInt16
)Read16();
278 wxTextInputStream
& wxTextInputStream::operator>>(wxInt32
& i
)
280 i
= (wxInt32
)Read32();
284 wxTextInputStream
& wxTextInputStream::operator>>(wxUint16
& i
)
290 wxTextInputStream
& wxTextInputStream::operator>>(wxUint32
& i
)
296 wxTextInputStream
& wxTextInputStream::operator>>(double& i
)
302 wxTextInputStream
& wxTextInputStream::operator>>(float& f
)
304 f
= (float)ReadDouble();
311 wxTextOutputStream::wxTextOutputStream(wxOutputStream
& s
,
313 const wxMBConv
& conv
)
314 : m_output(s
), m_conv(conv
.Clone())
316 wxTextOutputStream::wxTextOutputStream(wxOutputStream
& s
, wxEOL mode
)
321 if (m_mode
== wxEOL_NATIVE
)
323 #if defined(__WXMSW__) || defined(__WXPM__)
331 wxTextOutputStream::~wxTextOutputStream()
335 #endif // wxUSE_UNICODE
338 void wxTextOutputStream::SetMode(wxEOL mode
)
341 if (m_mode
== wxEOL_NATIVE
)
343 #if defined(__WXMSW__) || defined(__WXPM__)
351 void wxTextOutputStream::Write32(wxUint32 i
)
354 str
.Printf(wxT("%u"), i
);
359 void wxTextOutputStream::Write16(wxUint16 i
)
362 str
.Printf(wxT("%u"), (unsigned)i
);
367 void wxTextOutputStream::Write8(wxUint8 i
)
370 str
.Printf(wxT("%u"), (unsigned)i
);
375 void wxTextOutputStream::WriteDouble(double d
)
379 str
.Printf(wxT("%f"), d
);
383 void wxTextOutputStream::WriteString(const wxString
& string
)
385 size_t len
= string
.length();
390 for ( size_t i
= 0; i
< len
; i
++ )
392 const wxChar c
= string
[i
];
393 if ( c
== wxT('\n') )
406 wxFAIL_MSG( _T("unknown EOL mode in wxTextOutputStream") );
410 // don't treat '\n' specially
419 // FIXME-UTF8: use wxCharBufferWithLength if/when we have it
420 wxCharBuffer buffer
= m_conv
->cWC2MB(out
.wc_str(), out
.length(), &len
);
421 m_output
.Write(buffer
, len
);
423 m_output
.Write(out
.c_str(), out
.length() );
427 wxTextOutputStream
& wxTextOutputStream::PutChar(wxChar c
)
430 WriteString( wxString(&c
, *m_conv
, 1) );
432 WriteString( wxString(&c
, wxConvLocal
, 1) );
437 wxTextOutputStream
& wxTextOutputStream::operator<<(const wxString
& string
)
439 WriteString( string
);
443 wxTextOutputStream
& wxTextOutputStream::operator<<(char c
)
445 WriteString( wxString::FromAscii(c
) );
450 #if wxUSE_UNICODE && wxWCHAR_T_IS_REAL_TYPE
452 wxTextOutputStream
& wxTextOutputStream::operator<<(wchar_t wc
)
454 WriteString( wxString(&wc
, *m_conv
, 1) );
459 #endif // wxUSE_UNICODE
461 wxTextOutputStream
& wxTextOutputStream::operator<<(wxInt16 c
)
464 str
.Printf(wxT("%d"), (signed int)c
);
470 wxTextOutputStream
& wxTextOutputStream::operator<<(wxInt32 c
)
473 str
.Printf(wxT("%ld"), (signed long)c
);
479 wxTextOutputStream
& wxTextOutputStream::operator<<(wxUint16 c
)
482 str
.Printf(wxT("%u"), (unsigned int)c
);
488 wxTextOutputStream
& wxTextOutputStream::operator<<(wxUint32 c
)
491 str
.Printf(wxT("%lu"), (unsigned long)c
);
497 wxTextOutputStream
&wxTextOutputStream::operator<<(double f
)
503 wxTextOutputStream
& wxTextOutputStream::operator<<(float f
)
505 WriteDouble((double)f
);
509 wxTextOutputStream
&endl( wxTextOutputStream
&stream
)
511 return stream
.PutChar(wxT('\n'));