]>
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"
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 #if WXWIN_COMPATIBILITY_2_6
183 wxString
wxTextInputStream::ReadString()
188 #endif // WXWIN_COMPATIBILITY_2_6
190 wxString
wxTextInputStream::ReadLine()
194 while ( !m_input
.Eof() )
196 wxChar c
= NextChar();
212 wxString
wxTextInputStream::ReadWord()
219 wxChar c
= NextNonSeparators();
225 while ( !m_input
.Eof() )
231 if (m_separators
.Contains(c
))
243 wxTextInputStream
& wxTextInputStream::operator>>(wxString
& word
)
249 wxTextInputStream
& wxTextInputStream::operator>>(char& c
)
252 if(m_input
.LastRead() <= 0) c
= 0;
260 #if wxUSE_UNICODE && wxWCHAR_T_IS_REAL_TYPE
262 wxTextInputStream
& wxTextInputStream::operator>>(wchar_t& wc
)
269 #endif // wxUSE_UNICODE
271 wxTextInputStream
& wxTextInputStream::operator>>(wxInt16
& i
)
273 i
= (wxInt16
)Read16();
277 wxTextInputStream
& wxTextInputStream::operator>>(wxInt32
& i
)
279 i
= (wxInt32
)Read32();
283 wxTextInputStream
& wxTextInputStream::operator>>(wxUint16
& i
)
289 wxTextInputStream
& wxTextInputStream::operator>>(wxUint32
& i
)
295 wxTextInputStream
& wxTextInputStream::operator>>(double& i
)
301 wxTextInputStream
& wxTextInputStream::operator>>(float& f
)
303 f
= (float)ReadDouble();
310 wxTextOutputStream::wxTextOutputStream(wxOutputStream
& s
,
312 const wxMBConv
& conv
)
313 : m_output(s
), m_conv(conv
.Clone())
315 wxTextOutputStream::wxTextOutputStream(wxOutputStream
& s
, wxEOL mode
)
320 if (m_mode
== wxEOL_NATIVE
)
322 #if defined(__WXMSW__) || defined(__WXPM__)
324 #elif defined(__WXMAC__) && !defined(__DARWIN__)
332 wxTextOutputStream::~wxTextOutputStream()
336 #endif // wxUSE_UNICODE
339 void wxTextOutputStream::SetMode(wxEOL mode
)
342 if (m_mode
== wxEOL_NATIVE
)
344 #if defined(__WXMSW__) || defined(__WXPM__)
346 #elif defined(__WXMAC__) && !defined(__DARWIN__)
354 void wxTextOutputStream::Write32(wxUint32 i
)
357 str
.Printf(wxT("%u"), i
);
362 void wxTextOutputStream::Write16(wxUint16 i
)
365 str
.Printf(wxT("%u"), (unsigned)i
);
370 void wxTextOutputStream::Write8(wxUint8 i
)
373 str
.Printf(wxT("%u"), (unsigned)i
);
378 void wxTextOutputStream::WriteDouble(double d
)
382 str
.Printf(wxT("%f"), d
);
386 void wxTextOutputStream::WriteString(const wxString
& string
)
388 size_t len
= string
.length();
393 for ( size_t i
= 0; i
< len
; i
++ )
395 const wxChar c
= string
[i
];
396 if ( c
== wxT('\n') )
409 wxFAIL_MSG( _T("unknown EOL mode in wxTextOutputStream") );
413 // don't treat '\n' specially
422 wxCharBuffer buffer
= m_conv
->cWC2MB(out
, out
.length(), &len
);
423 m_output
.Write(buffer
, len
);
425 m_output
.Write(out
.c_str(), out
.length() );
429 wxTextOutputStream
& wxTextOutputStream::PutChar(wxChar c
)
432 WriteString( wxString(&c
, *m_conv
, 1) );
434 WriteString( wxString(&c
, wxConvLocal
, 1) );
439 wxTextOutputStream
& wxTextOutputStream::operator<<(const wxChar
*string
)
441 WriteString( wxString(string
) );
445 wxTextOutputStream
& wxTextOutputStream::operator<<(const wxString
& string
)
447 WriteString( string
);
451 wxTextOutputStream
& wxTextOutputStream::operator<<(char c
)
453 WriteString( wxString::FromAscii(c
) );
458 #if wxUSE_UNICODE && wxWCHAR_T_IS_REAL_TYPE
460 wxTextOutputStream
& wxTextOutputStream::operator<<(wchar_t wc
)
462 WriteString( wxString(&wc
, *m_conv
, 1) );
467 #endif // wxUSE_UNICODE
469 wxTextOutputStream
& wxTextOutputStream::operator<<(wxInt16 c
)
472 str
.Printf(wxT("%d"), (signed int)c
);
478 wxTextOutputStream
& wxTextOutputStream::operator<<(wxInt32 c
)
481 str
.Printf(wxT("%ld"), (signed long)c
);
487 wxTextOutputStream
& wxTextOutputStream::operator<<(wxUint16 c
)
490 str
.Printf(wxT("%u"), (unsigned int)c
);
496 wxTextOutputStream
& wxTextOutputStream::operator<<(wxUint32 c
)
499 str
.Printf(wxT("%lu"), (unsigned long)c
);
505 wxTextOutputStream
&wxTextOutputStream::operator<<(double f
)
511 wxTextOutputStream
& wxTextOutputStream::operator<<(float f
)
513 WriteDouble((double)f
);
517 wxTextOutputStream
&endl( wxTextOutputStream
&stream
)
519 return stream
.PutChar(wxT('\n'));