]>
git.saurik.com Git - wxWidgets.git/blob - src/common/txtstrm.cpp
ede8a1c5ea49f9b21d2835125962a3d30abe09f1
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 #if wxUSE_UNICODE && wxWCHAR_T_IS_REAL_TYPE
257 wxTextInputStream
& wxTextInputStream::operator>>(wchar_t& wc
)
264 #endif // wxUSE_UNICODE
266 wxTextInputStream
& wxTextInputStream::operator>>(wxInt16
& i
)
268 i
= (wxInt16
)Read16();
272 wxTextInputStream
& wxTextInputStream::operator>>(wxInt32
& i
)
274 i
= (wxInt32
)Read32();
278 wxTextInputStream
& wxTextInputStream::operator>>(wxUint16
& i
)
284 wxTextInputStream
& wxTextInputStream::operator>>(wxUint32
& i
)
290 wxTextInputStream
& wxTextInputStream::operator>>(double& i
)
296 wxTextInputStream
& wxTextInputStream::operator>>(float& f
)
298 f
= (float)ReadDouble();
305 wxTextOutputStream::wxTextOutputStream(wxOutputStream
& s
, wxEOL mode
, 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"), i
);
360 void wxTextOutputStream::Write8(wxUint8 i
)
363 str
.Printf(wxT("%u"), 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'));