]>
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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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
)
45 memset((void*)m_lastBytes
, 0, 10);
48 wxTextInputStream::wxTextInputStream(wxInputStream
&s
, const wxString
&sep
)
49 : m_input(s
), m_separators(sep
)
51 memset((void*)m_lastBytes
, 0, 10);
55 wxTextInputStream::~wxTextInputStream()
59 void wxTextInputStream::UngetLast()
62 while(m_lastBytes
[byteCount
]) // pseudo ANSI strlen (even for Unicode!)
64 m_input
.Ungetch(m_lastBytes
, byteCount
);
65 memset((void*)m_lastBytes
, 0, 10);
68 wxChar
wxTextInputStream::NextChar()
72 memset((void*)m_lastBytes
, 0, 10);
73 for(size_t inlen
= 0; inlen
< 9; inlen
++)
75 // actually read the next character
76 m_lastBytes
[inlen
] = m_input
.GetC();
78 if(m_input
.LastRead() <= 0)
81 int retlen
= (int) m_conv
.MB2WC(wbuf
, m_lastBytes
, 2); // returns -1 for failure
82 if(retlen
>= 0) // res == 0 could happen for '\0' char
85 // there should be no encoding which requires more than nine bytes for one character...
88 m_lastBytes
[0] = m_input
.GetC();
90 if(m_input
.LastRead() <= 0)
93 return m_lastBytes
[0];
98 wxChar
wxTextInputStream::NextNonSeparators()
102 wxChar c
= NextChar();
103 if (c
== wxEOT
) return (wxChar
) 0;
105 if (c
!= wxT('\n') &&
107 !m_separators
.Contains(c
))
113 bool wxTextInputStream::EatEOL(const wxChar
&c
)
115 if (c
== wxT('\n')) return TRUE
; // eat on UNIX
117 if (c
== wxT('\r')) // eat on both Mac and DOS
119 wxChar c2
= NextChar();
120 if(c2
== wxEOT
) return TRUE
; // end of stream reached, had enough :-)
122 if (c2
!= wxT('\n')) UngetLast(); // Don't eat on Mac
129 wxUint32
wxTextInputStream::Read32(int base
)
131 wxASSERT_MSG( !base
|| (base
> 1 && base
<= 36), _T("invalid base") );
132 if(!m_input
) return 0;
134 wxString word
= ReadWord();
137 return wxStrtoul(word
.c_str(), 0, base
);
140 wxUint16
wxTextInputStream::Read16(int base
)
142 return (wxUint16
)Read32(base
);
145 wxUint8
wxTextInputStream::Read8(int base
)
147 return (wxUint8
)Read32(base
);
150 wxInt32
wxTextInputStream::Read32S(int base
)
152 wxASSERT_MSG( !base
|| (base
> 1 && base
<= 36), _T("invalid base") );
153 if(!m_input
) return 0;
155 wxString word
= ReadWord();
158 return wxStrtol(word
.c_str(), 0, base
);
161 wxInt16
wxTextInputStream::Read16S(int base
)
163 return (wxInt16
)Read32S(base
);
166 wxInt8
wxTextInputStream::Read8S(int base
)
168 return (wxInt8
)Read32S(base
);
171 double wxTextInputStream::ReadDouble()
173 if(!m_input
) return 0;
174 wxString word
= ReadWord();
177 return wxStrtod(word
.c_str(), 0);
180 wxString
wxTextInputStream::ReadString()
185 wxString
wxTextInputStream::ReadLine()
189 while ( !m_input
.Eof() )
191 wxChar c
= NextChar();
207 wxString
wxTextInputStream::ReadWord()
214 wxChar c
= NextNonSeparators();
220 while ( !m_input
.Eof() )
226 if (m_separators
.Contains(c
))
238 wxTextInputStream
& wxTextInputStream::operator>>(wxString
& word
)
244 wxTextInputStream
& wxTextInputStream::operator>>(char& c
)
247 if(m_input
.LastRead() <= 0) c
= 0;
255 wxTextInputStream
& wxTextInputStream::operator>>(wxInt16
& i
)
257 i
= (wxInt16
)Read16();
261 wxTextInputStream
& wxTextInputStream::operator>>(wxInt32
& i
)
263 i
= (wxInt32
)Read32();
267 wxTextInputStream
& wxTextInputStream::operator>>(wxUint16
& i
)
273 wxTextInputStream
& wxTextInputStream::operator>>(wxUint32
& i
)
279 wxTextInputStream
& wxTextInputStream::operator>>(double& i
)
285 wxTextInputStream
& wxTextInputStream::operator>>(float& f
)
287 f
= (float)ReadDouble();
294 wxTextOutputStream::wxTextOutputStream(wxOutputStream
& s
, wxEOL mode
, wxMBConv
& conv
)
295 : m_output(s
), m_conv(conv
)
297 wxTextOutputStream::wxTextOutputStream(wxOutputStream
& s
, wxEOL mode
)
302 if (m_mode
== wxEOL_NATIVE
)
304 #if defined(__WXMSW__) || defined(__WXPM__)
306 #elif defined(__WXMAC__) && !defined(__DARWIN__)
314 wxTextOutputStream::~wxTextOutputStream()
318 void wxTextOutputStream::SetMode(wxEOL mode
)
321 if (m_mode
== wxEOL_NATIVE
)
323 #if defined(__WXMSW__) || defined(__WXPM__)
325 #elif defined(__WXMAC__) && !defined(__DARWIN__)
333 void wxTextOutputStream::Write32(wxUint32 i
)
336 str
.Printf(wxT("%u"), i
);
341 void wxTextOutputStream::Write16(wxUint16 i
)
344 str
.Printf(wxT("%u"), i
);
349 void wxTextOutputStream::Write8(wxUint8 i
)
352 str
.Printf(wxT("%u"), i
);
357 void wxTextOutputStream::WriteDouble(double d
)
361 str
.Printf(wxT("%f"), d
);
365 void wxTextOutputStream::WriteString(const wxString
& string
)
367 size_t len
= string
.length();
372 for ( size_t i
= 0; i
< len
; i
++ )
374 const wxChar c
= string
[i
];
375 if ( c
== wxT('\n') )
388 wxFAIL_MSG( _T("unknown EOL mode in wxTextOutputStream") );
392 // don't treat '\n' specially
400 // We must not write the trailing NULL here
402 wxCharBuffer buffer
= m_conv
.cWC2MB( out
);
403 m_output
.Write( (const char*) buffer
, strlen( (const char*) buffer
) );
405 m_output
.Write(out
.c_str(), out
.length() );
409 wxTextOutputStream
& wxTextOutputStream::operator<<(const wxChar
*string
)
411 WriteString( wxString(string
) );
415 wxTextOutputStream
& wxTextOutputStream::operator<<(const wxString
& string
)
417 WriteString( string
);
421 wxTextOutputStream
& wxTextOutputStream::operator<<(char c
)
423 WriteString( wxString::FromAscii(c
) );
428 #if wxUSE_UNICODE && wxWCHAR_T_IS_REAL_TYPE
430 wxTextOutputStream
& wxTextOutputStream::operator<<(wchar_t wc
)
432 WriteString( wxString(&wc
, m_conv
, 1) );
437 #endif // wxUSE_UNICODE
439 wxTextOutputStream
& wxTextOutputStream::operator<<(wxInt16 c
)
442 str
.Printf(wxT("%d"), (signed int)c
);
448 wxTextOutputStream
& wxTextOutputStream::operator<<(wxInt32 c
)
451 str
.Printf(wxT("%ld"), (signed long)c
);
457 wxTextOutputStream
& wxTextOutputStream::operator<<(wxUint16 c
)
460 str
.Printf(wxT("%u"), (unsigned int)c
);
466 wxTextOutputStream
& wxTextOutputStream::operator<<(wxUint32 c
)
469 str
.Printf(wxT("%lu"), (unsigned long)c
);
475 wxTextOutputStream
&wxTextOutputStream::operator<<(double f
)
481 wxTextOutputStream
& wxTextOutputStream::operator<<(float f
)
483 WriteDouble((double)f
);
487 wxTextOutputStream
&endl( wxTextOutputStream
&stream
)
489 return stream
<< wxT('\n');