]>
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
,
41 : m_input(s
), m_separators(sep
), m_conv(conv
.Clone())
43 memset((void*)m_lastBytes
, 0, 10);
46 wxTextInputStream::wxTextInputStream(wxInputStream
&s
, const wxString
&sep
)
47 : m_input(s
), m_separators(sep
)
49 memset((void*)m_lastBytes
, 0, 10);
53 wxTextInputStream::~wxTextInputStream()
57 #endif // wxUSE_UNICODE
60 void wxTextInputStream::UngetLast()
63 while(m_lastBytes
[byteCount
]) // pseudo ANSI strlen (even for Unicode!)
65 m_input
.Ungetch(m_lastBytes
, byteCount
);
66 memset((void*)m_lastBytes
, 0, 10);
69 wxChar
wxTextInputStream::NextChar()
73 memset((void*)m_lastBytes
, 0, 10);
74 for(size_t inlen
= 0; inlen
< 9; inlen
++)
76 // actually read the next character
77 m_lastBytes
[inlen
] = m_input
.GetC();
79 if(m_input
.LastRead() <= 0)
82 int retlen
= (int) m_conv
->MB2WC(wbuf
, m_lastBytes
, 2); // returns -1 for failure
83 if(retlen
>= 0) // res == 0 could happen for '\0' char
86 // there should be no encoding which requires more than nine bytes for one character...
89 m_lastBytes
[0] = m_input
.GetC();
91 if(m_input
.LastRead() <= 0)
94 return m_lastBytes
[0];
99 wxChar
wxTextInputStream::NextNonSeparators()
103 wxChar c
= NextChar();
104 if (c
== wxEOT
) return (wxChar
) 0;
106 if (c
!= wxT('\n') &&
108 !m_separators
.Contains(c
))
114 bool wxTextInputStream::EatEOL(const wxChar
&c
)
116 if (c
== wxT('\n')) return true; // eat on UNIX
118 if (c
== wxT('\r')) // eat on both Mac and DOS
120 wxChar c2
= NextChar();
121 if(c2
== wxEOT
) return true; // end of stream reached, had enough :-)
123 if (c2
!= wxT('\n')) UngetLast(); // Don't eat on Mac
130 wxUint32
wxTextInputStream::Read32(int base
)
132 wxASSERT_MSG( !base
|| (base
> 1 && base
<= 36), _T("invalid base") );
133 if(!m_input
) return 0;
135 wxString word
= ReadWord();
138 return wxStrtoul(word
.c_str(), 0, base
);
141 wxUint16
wxTextInputStream::Read16(int base
)
143 return (wxUint16
)Read32(base
);
146 wxUint8
wxTextInputStream::Read8(int base
)
148 return (wxUint8
)Read32(base
);
151 wxInt32
wxTextInputStream::Read32S(int base
)
153 wxASSERT_MSG( !base
|| (base
> 1 && base
<= 36), _T("invalid base") );
154 if(!m_input
) return 0;
156 wxString word
= ReadWord();
159 return wxStrtol(word
.c_str(), 0, base
);
162 wxInt16
wxTextInputStream::Read16S(int base
)
164 return (wxInt16
)Read32S(base
);
167 wxInt8
wxTextInputStream::Read8S(int base
)
169 return (wxInt8
)Read32S(base
);
172 double wxTextInputStream::ReadDouble()
174 if(!m_input
) return 0;
175 wxString word
= ReadWord();
178 return wxStrtod(word
.c_str(), 0);
181 wxString
wxTextInputStream::ReadString()
186 wxString
wxTextInputStream::ReadLine()
190 while ( !m_input
.Eof() )
192 wxChar c
= NextChar();
208 wxString
wxTextInputStream::ReadWord()
215 wxChar c
= NextNonSeparators();
221 while ( !m_input
.Eof() )
227 if (m_separators
.Contains(c
))
239 wxTextInputStream
& wxTextInputStream::operator>>(wxString
& word
)
245 wxTextInputStream
& wxTextInputStream::operator>>(char& c
)
248 if(m_input
.LastRead() <= 0) c
= 0;
256 #if wxUSE_UNICODE && wxWCHAR_T_IS_REAL_TYPE
258 wxTextInputStream
& wxTextInputStream::operator>>(wchar_t& wc
)
265 #endif // wxUSE_UNICODE
267 wxTextInputStream
& wxTextInputStream::operator>>(wxInt16
& i
)
269 i
= (wxInt16
)Read16();
273 wxTextInputStream
& wxTextInputStream::operator>>(wxInt32
& i
)
275 i
= (wxInt32
)Read32();
279 wxTextInputStream
& wxTextInputStream::operator>>(wxUint16
& i
)
285 wxTextInputStream
& wxTextInputStream::operator>>(wxUint32
& i
)
291 wxTextInputStream
& wxTextInputStream::operator>>(double& i
)
297 wxTextInputStream
& wxTextInputStream::operator>>(float& f
)
299 f
= (float)ReadDouble();
306 wxTextOutputStream::wxTextOutputStream(wxOutputStream
& s
,
308 const wxMBConv
& conv
)
309 : m_output(s
), m_conv(conv
.Clone())
311 wxTextOutputStream::wxTextOutputStream(wxOutputStream
& s
, wxEOL mode
)
316 if (m_mode
== wxEOL_NATIVE
)
318 #if defined(__WXMSW__) || defined(__WXPM__)
320 #elif defined(__WXMAC__) && !defined(__DARWIN__)
328 wxTextOutputStream::~wxTextOutputStream()
332 #endif // wxUSE_UNICODE
335 void wxTextOutputStream::SetMode(wxEOL mode
)
338 if (m_mode
== wxEOL_NATIVE
)
340 #if defined(__WXMSW__) || defined(__WXPM__)
342 #elif defined(__WXMAC__) && !defined(__DARWIN__)
350 void wxTextOutputStream::Write32(wxUint32 i
)
353 str
.Printf(wxT("%u"), i
);
358 void wxTextOutputStream::Write16(wxUint16 i
)
361 str
.Printf(wxT("%u"), (unsigned)i
);
366 void wxTextOutputStream::Write8(wxUint8 i
)
369 str
.Printf(wxT("%u"), (unsigned)i
);
374 void wxTextOutputStream::WriteDouble(double d
)
378 str
.Printf(wxT("%f"), d
);
382 void wxTextOutputStream::WriteString(const wxString
& string
)
384 size_t len
= string
.length();
389 for ( size_t i
= 0; i
< len
; i
++ )
391 const wxChar c
= string
[i
];
392 if ( c
== wxT('\n') )
405 wxFAIL_MSG( _T("unknown EOL mode in wxTextOutputStream") );
409 // don't treat '\n' specially
417 // We must not write the trailing NULL here
419 wxCharBuffer buffer
= m_conv
->cWC2MB( out
);
420 m_output
.Write( (const char*) buffer
, strlen( (const char*) buffer
) );
422 m_output
.Write(out
.c_str(), out
.length() );
426 wxTextOutputStream
& wxTextOutputStream::PutChar(wxChar c
)
429 WriteString( wxString(&c
, *m_conv
, 1) );
431 WriteString( wxString(&c
, wxConvLocal
, 1) );
436 wxTextOutputStream
& wxTextOutputStream::operator<<(const wxChar
*string
)
438 WriteString( wxString(string
) );
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'));