]>
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 switch ( m_conv
->ToWChar(wbuf
, WXSIZEOF(wbuf
), m_lastBytes
, inlen
+ 1) )
81 // this is a bug in converter object as it should either fail
82 // or decode non-empty string to something non-empty
83 wxFAIL_MSG("ToWChar() can't return 0 for non-empty input");
87 // the buffer probably doesn't contain enough bytes to decode
88 // as a complete character, try with more bytes
92 // if we couldn't decode a single character during the last
93 // loop iteration we shouldn't be able to decode 2 or more of
94 // them with an extra single byte, something fishy is going on
95 wxFAIL_MSG("unexpected decoding result");
96 // fall through nevertheless and return at least something
99 // we finally decoded a character
104 // there should be no encoding which requires more than nine bytes for one
105 // character so something must be wrong with our conversion but we have no
106 // way to signal it from here
109 m_lastBytes
[0] = m_input
.GetC();
111 if(m_input
.LastRead() <= 0)
114 return m_lastBytes
[0];
119 wxChar
wxTextInputStream::NextNonSeparators()
123 wxChar c
= NextChar();
124 if (c
== wxEOT
) return (wxChar
) 0;
126 if (c
!= wxT('\n') &&
128 m_separators
.Find(c
) < 0)
134 bool wxTextInputStream::EatEOL(const wxChar
&c
)
136 if (c
== wxT('\n')) return true; // eat on UNIX
138 if (c
== wxT('\r')) // eat on both Mac and DOS
140 wxChar c2
= NextChar();
141 if(c2
== wxEOT
) return true; // end of stream reached, had enough :-)
143 if (c2
!= wxT('\n')) UngetLast(); // Don't eat on Mac
150 wxUint32
wxTextInputStream::Read32(int base
)
152 wxASSERT_MSG( !base
|| (base
> 1 && base
<= 36), wxT("invalid base") );
153 if(!m_input
) return 0;
155 wxString word
= ReadWord();
158 return wxStrtoul(word
.c_str(), 0, base
);
161 wxUint16
wxTextInputStream::Read16(int base
)
163 return (wxUint16
)Read32(base
);
166 wxUint8
wxTextInputStream::Read8(int base
)
168 return (wxUint8
)Read32(base
);
171 wxInt32
wxTextInputStream::Read32S(int base
)
173 wxASSERT_MSG( !base
|| (base
> 1 && base
<= 36), wxT("invalid base") );
174 if(!m_input
) return 0;
176 wxString word
= ReadWord();
179 return wxStrtol(word
.c_str(), 0, base
);
182 wxInt16
wxTextInputStream::Read16S(int base
)
184 return (wxInt16
)Read32S(base
);
187 wxInt8
wxTextInputStream::Read8S(int base
)
189 return (wxInt8
)Read32S(base
);
192 double wxTextInputStream::ReadDouble()
194 if(!m_input
) return 0;
195 wxString word
= ReadWord();
198 return wxStrtod(word
.c_str(), 0);
201 #if WXWIN_COMPATIBILITY_2_6
203 wxString
wxTextInputStream::ReadString()
208 #endif // WXWIN_COMPATIBILITY_2_6
210 wxString
wxTextInputStream::ReadLine()
214 while ( !m_input
.Eof() )
216 wxChar c
= NextChar();
229 wxString
wxTextInputStream::ReadWord()
236 wxChar c
= NextNonSeparators();
242 while ( !m_input
.Eof() )
248 if (m_separators
.Find(c
) >= 0)
260 wxTextInputStream
& wxTextInputStream::operator>>(wxString
& word
)
266 wxTextInputStream
& wxTextInputStream::operator>>(char& c
)
269 if(m_input
.LastRead() <= 0) c
= 0;
277 #if wxUSE_UNICODE && wxWCHAR_T_IS_REAL_TYPE
279 wxTextInputStream
& wxTextInputStream::operator>>(wchar_t& wc
)
286 #endif // wxUSE_UNICODE
288 wxTextInputStream
& wxTextInputStream::operator>>(wxInt16
& i
)
290 i
= (wxInt16
)Read16();
294 wxTextInputStream
& wxTextInputStream::operator>>(wxInt32
& i
)
296 i
= (wxInt32
)Read32();
300 wxTextInputStream
& wxTextInputStream::operator>>(wxUint16
& i
)
306 wxTextInputStream
& wxTextInputStream::operator>>(wxUint32
& i
)
312 wxTextInputStream
& wxTextInputStream::operator>>(double& i
)
318 wxTextInputStream
& wxTextInputStream::operator>>(float& f
)
320 f
= (float)ReadDouble();
327 wxTextOutputStream::wxTextOutputStream(wxOutputStream
& s
,
329 const wxMBConv
& conv
)
330 : m_output(s
), m_conv(conv
.Clone())
332 wxTextOutputStream::wxTextOutputStream(wxOutputStream
& s
, wxEOL mode
)
337 if (m_mode
== wxEOL_NATIVE
)
339 #if defined(__WINDOWS__) || defined(__WXPM__)
347 wxTextOutputStream::~wxTextOutputStream()
351 #endif // wxUSE_UNICODE
354 void wxTextOutputStream::SetMode(wxEOL mode
)
357 if (m_mode
== wxEOL_NATIVE
)
359 #if defined(__WINDOWS__) || defined(__WXPM__)
367 void wxTextOutputStream::Write32(wxUint32 i
)
370 str
.Printf(wxT("%u"), i
);
375 void wxTextOutputStream::Write16(wxUint16 i
)
378 str
.Printf(wxT("%u"), (unsigned)i
);
383 void wxTextOutputStream::Write8(wxUint8 i
)
386 str
.Printf(wxT("%u"), (unsigned)i
);
391 void wxTextOutputStream::WriteDouble(double d
)
395 str
.Printf(wxT("%f"), d
);
399 void wxTextOutputStream::WriteString(const wxString
& string
)
401 size_t len
= string
.length();
406 for ( size_t i
= 0; i
< len
; i
++ )
408 const wxChar c
= string
[i
];
409 if ( c
== wxT('\n') )
422 wxFAIL_MSG( wxT("unknown EOL mode in wxTextOutputStream") );
426 // don't treat '\n' specially
435 // FIXME-UTF8: use wxCharBufferWithLength if/when we have it
436 wxCharBuffer buffer
= m_conv
->cWC2MB(out
.wc_str(), out
.length(), &len
);
437 m_output
.Write(buffer
, len
);
439 m_output
.Write(out
.c_str(), out
.length() );
443 wxTextOutputStream
& wxTextOutputStream::PutChar(wxChar c
)
446 WriteString( wxString(&c
, *m_conv
, 1) );
448 WriteString( wxString(&c
, wxConvLocal
, 1) );
453 void wxTextOutputStream::Flush()
456 const size_t len
= m_conv
->FromWChar(NULL
, 0, L
"", 1);
457 if ( len
> m_conv
->GetMBNulLen() )
459 wxCharBuffer
buf(len
);
460 m_conv
->FromWChar(buf
.data(), len
, L
"", 1);
461 m_output
.Write(buf
, len
- m_conv
->GetMBNulLen());
463 #endif // wxUSE_UNICODE
466 wxTextOutputStream
& wxTextOutputStream::operator<<(const wxString
& string
)
468 WriteString( string
);
472 wxTextOutputStream
& wxTextOutputStream::operator<<(char c
)
474 WriteString( wxString::FromAscii(c
) );
479 #if wxUSE_UNICODE && wxWCHAR_T_IS_REAL_TYPE
481 wxTextOutputStream
& wxTextOutputStream::operator<<(wchar_t wc
)
483 WriteString( wxString(&wc
, *m_conv
, 1) );
488 #endif // wxUSE_UNICODE
490 wxTextOutputStream
& wxTextOutputStream::operator<<(wxInt16 c
)
493 str
.Printf(wxT("%d"), (signed int)c
);
499 wxTextOutputStream
& wxTextOutputStream::operator<<(wxInt32 c
)
502 str
.Printf(wxT("%ld"), (signed long)c
);
508 wxTextOutputStream
& wxTextOutputStream::operator<<(wxUint16 c
)
511 str
.Printf(wxT("%u"), (unsigned int)c
);
517 wxTextOutputStream
& wxTextOutputStream::operator<<(wxUint32 c
)
520 str
.Printf(wxT("%lu"), (unsigned long)c
);
526 wxTextOutputStream
&wxTextOutputStream::operator<<(double f
)
532 wxTextOutputStream
& wxTextOutputStream::operator<<(float f
)
534 WriteDouble((double)f
);
538 wxTextOutputStream
&endl( wxTextOutputStream
&stream
)
540 return stream
.PutChar(wxT('\n'));