]>
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"
29 // ----------------------------------------------------------------------------
31 // ----------------------------------------------------------------------------
34 wxTextInputStream::wxTextInputStream(wxInputStream
&s
,
37 : m_input(s
), m_separators(sep
), m_conv(conv
.Clone())
39 memset((void*)m_lastBytes
, 0, 10);
42 wxTextInputStream::wxTextInputStream(wxInputStream
&s
, const wxString
&sep
)
43 : m_input(s
), m_separators(sep
)
45 memset((void*)m_lastBytes
, 0, 10);
49 wxTextInputStream::~wxTextInputStream()
53 #endif // wxUSE_UNICODE
56 void wxTextInputStream::UngetLast()
59 while(m_lastBytes
[byteCount
]) // pseudo ANSI strlen (even for Unicode!)
61 m_input
.Ungetch(m_lastBytes
, byteCount
);
62 memset((void*)m_lastBytes
, 0, 10);
65 wxChar
wxTextInputStream::NextChar()
69 memset((void*)m_lastBytes
, 0, 10);
70 for(size_t inlen
= 0; inlen
< 9; inlen
++)
72 // actually read the next character
73 m_lastBytes
[inlen
] = m_input
.GetC();
75 if(m_input
.LastRead() <= 0)
78 if ( m_conv
->ToWChar(wbuf
, WXSIZEOF(wbuf
), m_lastBytes
, inlen
+ 1)
82 // there should be no encoding which requires more than nine bytes for one character...
85 m_lastBytes
[0] = m_input
.GetC();
87 if(m_input
.LastRead() <= 0)
90 return m_lastBytes
[0];
95 wxChar
wxTextInputStream::NextNonSeparators()
99 wxChar c
= NextChar();
100 if (c
== wxEOT
) return (wxChar
) 0;
102 if (c
!= wxT('\n') &&
104 m_separators
.Find(c
) < 0)
110 bool wxTextInputStream::EatEOL(const wxChar
&c
)
112 if (c
== wxT('\n')) return true; // eat on UNIX
114 if (c
== wxT('\r')) // eat on both Mac and DOS
116 wxChar c2
= NextChar();
117 if(c2
== wxEOT
) return true; // end of stream reached, had enough :-)
119 if (c2
!= wxT('\n')) UngetLast(); // Don't eat on Mac
126 wxUint32
wxTextInputStream::Read32(int base
)
128 wxASSERT_MSG( !base
|| (base
> 1 && base
<= 36), _T("invalid base") );
129 if(!m_input
) return 0;
131 wxString word
= ReadWord();
134 return wxStrtoul(word
.c_str(), 0, base
);
137 wxUint16
wxTextInputStream::Read16(int base
)
139 return (wxUint16
)Read32(base
);
142 wxUint8
wxTextInputStream::Read8(int base
)
144 return (wxUint8
)Read32(base
);
147 wxInt32
wxTextInputStream::Read32S(int base
)
149 wxASSERT_MSG( !base
|| (base
> 1 && base
<= 36), _T("invalid base") );
150 if(!m_input
) return 0;
152 wxString word
= ReadWord();
155 return wxStrtol(word
.c_str(), 0, base
);
158 wxInt16
wxTextInputStream::Read16S(int base
)
160 return (wxInt16
)Read32S(base
);
163 wxInt8
wxTextInputStream::Read8S(int base
)
165 return (wxInt8
)Read32S(base
);
168 double wxTextInputStream::ReadDouble()
170 if(!m_input
) return 0;
171 wxString word
= ReadWord();
174 return wxStrtod(word
.c_str(), 0);
177 #if WXWIN_COMPATIBILITY_2_6
179 wxString
wxTextInputStream::ReadString()
184 #endif // WXWIN_COMPATIBILITY_2_6
186 wxString
wxTextInputStream::ReadLine()
190 while ( !m_input
.Eof() )
192 wxChar c
= NextChar();
205 wxString
wxTextInputStream::ReadWord()
212 wxChar c
= NextNonSeparators();
218 while ( !m_input
.Eof() )
224 if (m_separators
.Find(c
) >= 0)
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
.Clone())
308 wxTextOutputStream::wxTextOutputStream(wxOutputStream
& s
, wxEOL mode
)
313 if (m_mode
== wxEOL_NATIVE
)
315 #if defined(__WXMSW__) || defined(__WXPM__)
323 wxTextOutputStream::~wxTextOutputStream()
327 #endif // wxUSE_UNICODE
330 void wxTextOutputStream::SetMode(wxEOL mode
)
333 if (m_mode
== wxEOL_NATIVE
)
335 #if defined(__WXMSW__) || defined(__WXPM__)
343 void wxTextOutputStream::Write32(wxUint32 i
)
346 str
.Printf(wxT("%u"), i
);
351 void wxTextOutputStream::Write16(wxUint16 i
)
354 str
.Printf(wxT("%u"), (unsigned)i
);
359 void wxTextOutputStream::Write8(wxUint8 i
)
362 str
.Printf(wxT("%u"), (unsigned)i
);
367 void wxTextOutputStream::WriteDouble(double d
)
371 str
.Printf(wxT("%f"), d
);
375 void wxTextOutputStream::WriteString(const wxString
& string
)
377 size_t len
= string
.length();
382 for ( size_t i
= 0; i
< len
; i
++ )
384 const wxChar c
= string
[i
];
385 if ( c
== wxT('\n') )
398 wxFAIL_MSG( _T("unknown EOL mode in wxTextOutputStream") );
402 // don't treat '\n' specially
411 // FIXME-UTF8: use wxCharBufferWithLength if/when we have it
412 wxCharBuffer buffer
= m_conv
->cWC2MB(out
.wc_str(), out
.length(), &len
);
413 m_output
.Write(buffer
, len
);
415 m_output
.Write(out
.c_str(), out
.length() );
419 wxTextOutputStream
& wxTextOutputStream::PutChar(wxChar c
)
422 WriteString( wxString(&c
, *m_conv
, 1) );
424 WriteString( wxString(&c
, wxConvLocal
, 1) );
429 void wxTextOutputStream::Flush()
432 const size_t len
= m_conv
->FromWChar(NULL
, 0, L
"", 1);
433 if ( len
> m_conv
->GetMBNulLen() )
435 wxCharBuffer
buf(len
);
436 m_conv
->FromWChar(buf
.data(), len
, L
"", 1);
437 m_output
.Write(buf
, len
- m_conv
->GetMBNulLen());
439 #endif // wxUSE_UNICODE
442 wxTextOutputStream
& wxTextOutputStream::operator<<(const wxString
& string
)
444 WriteString( string
);
448 wxTextOutputStream
& wxTextOutputStream::operator<<(char c
)
450 WriteString( wxString::FromAscii(c
) );
455 #if wxUSE_UNICODE && wxWCHAR_T_IS_REAL_TYPE
457 wxTextOutputStream
& wxTextOutputStream::operator<<(wchar_t wc
)
459 WriteString( wxString(&wc
, *m_conv
, 1) );
464 #endif // wxUSE_UNICODE
466 wxTextOutputStream
& wxTextOutputStream::operator<<(wxInt16 c
)
469 str
.Printf(wxT("%d"), (signed int)c
);
475 wxTextOutputStream
& wxTextOutputStream::operator<<(wxInt32 c
)
478 str
.Printf(wxT("%ld"), (signed long)c
);
484 wxTextOutputStream
& wxTextOutputStream::operator<<(wxUint16 c
)
487 str
.Printf(wxT("%u"), (unsigned int)c
);
493 wxTextOutputStream
& wxTextOutputStream::operator<<(wxUint32 c
)
496 str
.Printf(wxT("%lu"), (unsigned long)c
);
502 wxTextOutputStream
&wxTextOutputStream::operator<<(double f
)
508 wxTextOutputStream
& wxTextOutputStream::operator<<(float f
)
510 WriteDouble((double)f
);
514 wxTextOutputStream
&endl( wxTextOutputStream
&stream
)
516 return stream
.PutChar(wxT('\n'));