]>
git.saurik.com Git - wxWidgets.git/blob - src/common/txtstrm.cpp
884c4b84c002fec379fc1e10fe8cfbd98211d521
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
)
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 void wxTextInputStream::UngetLast()
60 while(m_lastBytes
[byteCount
]) // pseudo ANSI strlen (even for Unicode!)
62 m_input
.Ungetch(m_lastBytes
, byteCount
);
63 memset((void*)m_lastBytes
, 0, 10);
66 wxChar
wxTextInputStream::NextChar()
70 memset((void*)m_lastBytes
, 0, 10);
71 for(size_t inlen
= 0; inlen
< 9; inlen
++)
73 // actually read the next character
74 m_lastBytes
[inlen
] = m_input
.GetC();
76 if(m_input
.LastRead() <= 0)
79 int retlen
= (int) m_conv
.MB2WC(wbuf
, m_lastBytes
, 2); // returns -1 for failure
80 if(retlen
>= 0) // res == 0 could happen for '\0' char
83 // there should be no encoding which requires more than nine bytes for one character...
86 m_lastBytes
[0] = m_input
.GetC();
88 if(m_input
.LastRead() <= 0)
91 return m_lastBytes
[0];
96 wxChar
wxTextInputStream::NextNonSeparators()
100 wxChar c
= NextChar();
101 if (c
== wxEOT
) return (wxChar
) 0;
103 if (c
!= wxT('\n') &&
105 !m_separators
.Contains(c
))
111 bool wxTextInputStream::EatEOL(const wxChar
&c
)
113 if (c
== wxT('\n')) return true; // eat on UNIX
115 if (c
== wxT('\r')) // eat on both Mac and DOS
117 wxChar c2
= NextChar();
118 if(c2
== wxEOT
) return true; // end of stream reached, had enough :-)
120 if (c2
!= wxT('\n')) UngetLast(); // Don't eat on Mac
127 wxUint32
wxTextInputStream::Read32(int base
)
129 wxASSERT_MSG( !base
|| (base
> 1 && base
<= 36), _T("invalid base") );
130 if(!m_input
) return 0;
132 wxString word
= ReadWord();
135 return wxStrtoul(word
.c_str(), 0, base
);
138 wxUint16
wxTextInputStream::Read16(int base
)
140 return (wxUint16
)Read32(base
);
143 wxUint8
wxTextInputStream::Read8(int base
)
145 return (wxUint8
)Read32(base
);
148 wxInt32
wxTextInputStream::Read32S(int base
)
150 wxASSERT_MSG( !base
|| (base
> 1 && base
<= 36), _T("invalid base") );
151 if(!m_input
) return 0;
153 wxString word
= ReadWord();
156 return wxStrtol(word
.c_str(), 0, base
);
159 wxInt16
wxTextInputStream::Read16S(int base
)
161 return (wxInt16
)Read32S(base
);
164 wxInt8
wxTextInputStream::Read8S(int base
)
166 return (wxInt8
)Read32S(base
);
169 double wxTextInputStream::ReadDouble()
171 if(!m_input
) return 0;
172 wxString word
= ReadWord();
175 return wxStrtod(word
.c_str(), 0);
178 wxString
wxTextInputStream::ReadString()
183 wxString
wxTextInputStream::ReadLine()
187 while ( !m_input
.Eof() )
189 wxChar c
= NextChar();
205 wxString
wxTextInputStream::ReadWord()
212 wxChar c
= NextNonSeparators();
218 while ( !m_input
.Eof() )
224 if (m_separators
.Contains(c
))
236 wxTextInputStream
& wxTextInputStream::operator>>(wxString
& word
)
242 wxTextInputStream
& wxTextInputStream::operator>>(char& c
)
245 if(m_input
.LastRead() <= 0) c
= 0;
253 #if wxUSE_UNICODE && wxWCHAR_T_IS_REAL_TYPE
255 wxTextInputStream
& wxTextInputStream::operator>>(wchar_t& wc
)
262 #endif // wxUSE_UNICODE
264 wxTextInputStream
& wxTextInputStream::operator>>(wxInt16
& i
)
266 i
= (wxInt16
)Read16();
270 wxTextInputStream
& wxTextInputStream::operator>>(wxInt32
& i
)
272 i
= (wxInt32
)Read32();
276 wxTextInputStream
& wxTextInputStream::operator>>(wxUint16
& i
)
282 wxTextInputStream
& wxTextInputStream::operator>>(wxUint32
& i
)
288 wxTextInputStream
& wxTextInputStream::operator>>(double& i
)
294 wxTextInputStream
& wxTextInputStream::operator>>(float& f
)
296 f
= (float)ReadDouble();
303 wxTextOutputStream::wxTextOutputStream(wxOutputStream
& s
,
305 const wxMBConv
& conv
)
306 : m_output(s
), m_conv(conv
)
308 wxTextOutputStream::wxTextOutputStream(wxOutputStream
& s
, wxEOL mode
)
313 if (m_mode
== wxEOL_NATIVE
)
315 #if defined(__WXMSW__) || defined(__WXPM__)
317 #elif defined(__WXMAC__) && !defined(__DARWIN__)
325 wxTextOutputStream::~wxTextOutputStream()
329 void wxTextOutputStream::SetMode(wxEOL mode
)
332 if (m_mode
== wxEOL_NATIVE
)
334 #if defined(__WXMSW__) || defined(__WXPM__)
336 #elif defined(__WXMAC__) && !defined(__DARWIN__)
344 void wxTextOutputStream::Write32(wxUint32 i
)
347 str
.Printf(wxT("%u"), i
);
352 void wxTextOutputStream::Write16(wxUint16 i
)
355 str
.Printf(wxT("%u"), (unsigned)i
);
360 void wxTextOutputStream::Write8(wxUint8 i
)
363 str
.Printf(wxT("%u"), (unsigned)i
);
368 void wxTextOutputStream::WriteDouble(double d
)
372 str
.Printf(wxT("%f"), d
);
376 void wxTextOutputStream::WriteString(const wxString
& string
)
378 size_t len
= string
.length();
383 for ( size_t i
= 0; i
< len
; i
++ )
385 const wxChar c
= string
[i
];
386 if ( c
== wxT('\n') )
399 wxFAIL_MSG( _T("unknown EOL mode in wxTextOutputStream") );
403 // don't treat '\n' specially
411 // We must not write the trailing NULL here
413 wxCharBuffer buffer
= m_conv
.cWC2MB( out
);
414 m_output
.Write( (const char*) buffer
, strlen( (const char*) buffer
) );
416 m_output
.Write(out
.c_str(), out
.length() );
420 wxTextOutputStream
& wxTextOutputStream::PutChar(wxChar c
)
423 WriteString( wxString(&c
, m_conv
, 1) );
425 WriteString( wxString(&c
, wxConvLocal
, 1) );
430 wxTextOutputStream
& wxTextOutputStream::operator<<(const wxChar
*string
)
432 WriteString( wxString(string
) );
436 wxTextOutputStream
& wxTextOutputStream::operator<<(const wxString
& string
)
438 WriteString( string
);
442 wxTextOutputStream
& wxTextOutputStream::operator<<(char c
)
444 WriteString( wxString::FromAscii(c
) );
449 #if wxUSE_UNICODE && wxWCHAR_T_IS_REAL_TYPE
451 wxTextOutputStream
& wxTextOutputStream::operator<<(wchar_t wc
)
453 WriteString( wxString(&wc
, m_conv
, 1) );
458 #endif // wxUSE_UNICODE
460 wxTextOutputStream
& wxTextOutputStream::operator<<(wxInt16 c
)
463 str
.Printf(wxT("%d"), (signed int)c
);
469 wxTextOutputStream
& wxTextOutputStream::operator<<(wxInt32 c
)
472 str
.Printf(wxT("%ld"), (signed long)c
);
478 wxTextOutputStream
& wxTextOutputStream::operator<<(wxUint16 c
)
481 str
.Printf(wxT("%u"), (unsigned int)c
);
487 wxTextOutputStream
& wxTextOutputStream::operator<<(wxUint32 c
)
490 str
.Printf(wxT("%lu"), (unsigned long)c
);
496 wxTextOutputStream
&wxTextOutputStream::operator<<(double f
)
502 wxTextOutputStream
& wxTextOutputStream::operator<<(float f
)
504 WriteDouble((double)f
);
508 wxTextOutputStream
&endl( wxTextOutputStream
&stream
)
510 return stream
.PutChar(wxT('\n'));