]>
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 // 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
, const wxString
&sep
, wxMBConv
& conv
)
39 : m_input(s
), m_separators(sep
), m_conv(conv
)
41 memset((void*)m_lastBytes
, 0, 10);
44 wxTextInputStream::wxTextInputStream(wxInputStream
&s
, const wxString
&sep
)
45 : m_input(s
), m_separators(sep
)
47 memset((void*)m_lastBytes
, 0, 10);
51 wxTextInputStream::~wxTextInputStream()
55 void wxTextInputStream::UngetLast()
58 while(m_lastBytes
[byteCount
]) // pseudo ANSI strlen (even for Unicode!)
60 m_input
.Ungetch(m_lastBytes
, byteCount
);
61 memset((void*)m_lastBytes
, 0, 10);
64 wxChar
wxTextInputStream::NextChar()
68 memset((void*)m_lastBytes
, 0, 10);
69 for(size_t inlen
= 0; inlen
< 9; inlen
++)
71 // actually read the next character
72 m_lastBytes
[inlen
] = m_input
.GetC();
74 if(m_input
.LastRead() <= 0)
77 int retlen
= (int) m_conv
.MB2WC(wbuf
, m_lastBytes
, 2); // returns -1 for failure
78 if(retlen
>= 0) // res == 0 could happen for '\0' char
81 // there should be no encoding which requires more than nine bytes for one character...
84 m_lastBytes
[0] = m_input
.GetC();
86 if(m_input
.LastRead() <= 0)
89 return m_lastBytes
[0];
94 wxChar
wxTextInputStream::NextNonSeparators()
98 wxChar c
= NextChar();
99 if (c
== wxEOT
) return (wxChar
) 0;
101 if (c
!= wxT('\n') &&
103 !m_separators
.Contains(c
))
109 bool wxTextInputStream::EatEOL(const wxChar
&c
)
111 if (c
== wxT('\n')) return true; // eat on UNIX
113 if (c
== wxT('\r')) // eat on both Mac and DOS
115 wxChar c2
= NextChar();
116 if(c2
== wxEOT
) return true; // end of stream reached, had enough :-)
118 if (c2
!= wxT('\n')) UngetLast(); // Don't eat on Mac
125 wxUint32
wxTextInputStream::Read32(int base
)
127 wxASSERT_MSG( !base
|| (base
> 1 && base
<= 36), _T("invalid base") );
128 if(!m_input
) return 0;
130 wxString word
= ReadWord();
133 return wxStrtoul(word
.c_str(), 0, base
);
136 wxUint16
wxTextInputStream::Read16(int base
)
138 return (wxUint16
)Read32(base
);
141 wxUint8
wxTextInputStream::Read8(int base
)
143 return (wxUint8
)Read32(base
);
146 wxInt32
wxTextInputStream::Read32S(int base
)
148 wxASSERT_MSG( !base
|| (base
> 1 && base
<= 36), _T("invalid base") );
149 if(!m_input
) return 0;
151 wxString word
= ReadWord();
154 return wxStrtol(word
.c_str(), 0, base
);
157 wxInt16
wxTextInputStream::Read16S(int base
)
159 return (wxInt16
)Read32S(base
);
162 wxInt8
wxTextInputStream::Read8S(int base
)
164 return (wxInt8
)Read32S(base
);
167 double wxTextInputStream::ReadDouble()
169 if(!m_input
) return 0;
170 wxString word
= ReadWord();
173 return wxStrtod(word
.c_str(), 0);
176 wxString
wxTextInputStream::ReadString()
181 wxString
wxTextInputStream::ReadLine()
185 while ( !m_input
.Eof() )
187 wxChar c
= NextChar();
203 wxString
wxTextInputStream::ReadWord()
210 wxChar c
= NextNonSeparators();
216 while ( !m_input
.Eof() )
222 if (m_separators
.Contains(c
))
234 wxTextInputStream
& wxTextInputStream::operator>>(wxString
& word
)
240 wxTextInputStream
& wxTextInputStream::operator>>(char& c
)
243 if(m_input
.LastRead() <= 0) c
= 0;
251 #if wxUSE_UNICODE && wxWCHAR_T_IS_REAL_TYPE
253 wxTextInputStream
& wxTextInputStream::operator>>(wchar_t& wc
)
260 #endif // wxUSE_UNICODE
262 wxTextInputStream
& wxTextInputStream::operator>>(wxInt16
& i
)
264 i
= (wxInt16
)Read16();
268 wxTextInputStream
& wxTextInputStream::operator>>(wxInt32
& i
)
270 i
= (wxInt32
)Read32();
274 wxTextInputStream
& wxTextInputStream::operator>>(wxUint16
& i
)
280 wxTextInputStream
& wxTextInputStream::operator>>(wxUint32
& i
)
286 wxTextInputStream
& wxTextInputStream::operator>>(double& i
)
292 wxTextInputStream
& wxTextInputStream::operator>>(float& f
)
294 f
= (float)ReadDouble();
301 wxTextOutputStream::wxTextOutputStream(wxOutputStream
& s
, wxEOL mode
, wxMBConv
& conv
)
302 : m_output(s
), m_conv(conv
)
304 wxTextOutputStream::wxTextOutputStream(wxOutputStream
& s
, wxEOL mode
)
309 if (m_mode
== wxEOL_NATIVE
)
311 #if defined(__WXMSW__) || defined(__WXPM__)
313 #elif defined(__WXMAC__) && !defined(__DARWIN__)
321 wxTextOutputStream::~wxTextOutputStream()
325 void wxTextOutputStream::SetMode(wxEOL mode
)
328 if (m_mode
== wxEOL_NATIVE
)
330 #if defined(__WXMSW__) || defined(__WXPM__)
332 #elif defined(__WXMAC__) && !defined(__DARWIN__)
340 void wxTextOutputStream::Write32(wxUint32 i
)
343 str
.Printf(wxT("%u"), i
);
348 void wxTextOutputStream::Write16(wxUint16 i
)
351 str
.Printf(wxT("%u"), (unsigned)i
);
356 void wxTextOutputStream::Write8(wxUint8 i
)
359 str
.Printf(wxT("%u"), (unsigned)i
);
364 void wxTextOutputStream::WriteDouble(double d
)
368 str
.Printf(wxT("%f"), d
);
372 void wxTextOutputStream::WriteString(const wxString
& string
)
374 size_t len
= string
.length();
379 for ( size_t i
= 0; i
< len
; i
++ )
381 const wxChar c
= string
[i
];
382 if ( c
== wxT('\n') )
395 wxFAIL_MSG( _T("unknown EOL mode in wxTextOutputStream") );
399 // don't treat '\n' specially
407 // We must not write the trailing NULL here
409 wxCharBuffer buffer
= m_conv
.cWC2MB( out
);
410 m_output
.Write( (const char*) buffer
, strlen( (const char*) buffer
) );
412 m_output
.Write(out
.c_str(), out
.length() );
416 wxTextOutputStream
& wxTextOutputStream::PutChar(wxChar c
)
419 WriteString( wxString(&c
, m_conv
, 1) );
421 WriteString( wxString(&c
, wxConvLocal
, 1) );
426 wxTextOutputStream
& wxTextOutputStream::operator<<(const wxChar
*string
)
428 WriteString( wxString(string
) );
432 wxTextOutputStream
& wxTextOutputStream::operator<<(const wxString
& string
)
434 WriteString( string
);
438 wxTextOutputStream
& wxTextOutputStream::operator<<(char c
)
440 WriteString( wxString::FromAscii(c
) );
445 #if wxUSE_UNICODE && wxWCHAR_T_IS_REAL_TYPE
447 wxTextOutputStream
& wxTextOutputStream::operator<<(wchar_t wc
)
449 WriteString( wxString(&wc
, m_conv
, 1) );
454 #endif // wxUSE_UNICODE
456 wxTextOutputStream
& wxTextOutputStream::operator<<(wxInt16 c
)
459 str
.Printf(wxT("%d"), (signed int)c
);
465 wxTextOutputStream
& wxTextOutputStream::operator<<(wxInt32 c
)
468 str
.Printf(wxT("%ld"), (signed long)c
);
474 wxTextOutputStream
& wxTextOutputStream::operator<<(wxUint16 c
)
477 str
.Printf(wxT("%u"), (unsigned int)c
);
483 wxTextOutputStream
& wxTextOutputStream::operator<<(wxUint32 c
)
486 str
.Printf(wxT("%lu"), (unsigned long)c
);
492 wxTextOutputStream
&wxTextOutputStream::operator<<(double f
)
498 wxTextOutputStream
& wxTextOutputStream::operator<<(float f
)
500 WriteDouble((double)f
);
504 wxTextOutputStream
&endl( wxTextOutputStream
&stream
)
506 return stream
.PutChar(wxT('\n'));