]>
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"
25 #include "wx/txtstrm.h"
29 // ----------------------------------------------------------------------------
31 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
42 wxTextInputStream::wxTextInputStream(wxInputStream
&s
,
45 : m_input(s
), m_separators(sep
), m_conv(conv
.Clone())
47 memset((void*)m_lastBytes
, 0, 10);
50 wxTextInputStream::wxTextInputStream(wxInputStream
&s
, const wxString
&sep
)
51 : m_input(s
), m_separators(sep
)
53 memset((void*)m_lastBytes
, 0, 10);
57 wxTextInputStream::~wxTextInputStream()
61 #endif // wxUSE_UNICODE
64 void wxTextInputStream::UngetLast()
67 while(m_lastBytes
[byteCount
]) // pseudo ANSI strlen (even for Unicode!)
69 m_input
.Ungetch(m_lastBytes
, byteCount
);
70 memset((void*)m_lastBytes
, 0, 10);
73 wxChar
wxTextInputStream::NextChar()
77 memset((void*)m_lastBytes
, 0, 10);
78 for(size_t inlen
= 0; inlen
< 9; inlen
++)
80 // actually read the next character
81 m_lastBytes
[inlen
] = m_input
.GetC();
83 if(m_input
.LastRead() <= 0)
86 if ( m_conv
->ToWChar(wbuf
, WXSIZEOF(wbuf
), m_lastBytes
, inlen
+ 1)
90 // there should be no encoding which requires more than nine bytes for one character...
93 m_lastBytes
[0] = m_input
.GetC();
95 if(m_input
.LastRead() <= 0)
98 return m_lastBytes
[0];
103 wxChar
wxTextInputStream::NextNonSeparators()
107 wxChar c
= NextChar();
108 if (c
== wxEOT
) return (wxChar
) 0;
110 if (c
!= wxT('\n') &&
112 !m_separators
.Contains(c
))
118 bool wxTextInputStream::EatEOL(const wxChar
&c
)
120 if (c
== wxT('\n')) return true; // eat on UNIX
122 if (c
== wxT('\r')) // eat on both Mac and DOS
124 wxChar c2
= NextChar();
125 if(c2
== wxEOT
) return true; // end of stream reached, had enough :-)
127 if (c2
!= wxT('\n')) UngetLast(); // Don't eat on Mac
134 wxUint32
wxTextInputStream::Read32(int base
)
136 wxASSERT_MSG( !base
|| (base
> 1 && base
<= 36), _T("invalid base") );
137 if(!m_input
) return 0;
139 wxString word
= ReadWord();
142 return wxStrtoul(word
.c_str(), 0, base
);
145 wxUint16
wxTextInputStream::Read16(int base
)
147 return (wxUint16
)Read32(base
);
150 wxUint8
wxTextInputStream::Read8(int base
)
152 return (wxUint8
)Read32(base
);
155 wxInt32
wxTextInputStream::Read32S(int base
)
157 wxASSERT_MSG( !base
|| (base
> 1 && base
<= 36), _T("invalid base") );
158 if(!m_input
) return 0;
160 wxString word
= ReadWord();
163 return wxStrtol(word
.c_str(), 0, base
);
166 wxInt16
wxTextInputStream::Read16S(int base
)
168 return (wxInt16
)Read32S(base
);
171 wxInt8
wxTextInputStream::Read8S(int base
)
173 return (wxInt8
)Read32S(base
);
176 double wxTextInputStream::ReadDouble()
178 if(!m_input
) return 0;
179 wxString word
= ReadWord();
182 return wxStrtod(word
.c_str(), 0);
185 #if WXWIN_COMPATIBILITY_2_6
187 wxString
wxTextInputStream::ReadString()
192 #endif // WXWIN_COMPATIBILITY_2_6
194 wxString
wxTextInputStream::ReadLine()
198 while ( !m_input
.Eof() )
200 wxChar c
= NextChar();
213 wxString
wxTextInputStream::ReadWord()
220 wxChar c
= NextNonSeparators();
226 while ( !m_input
.Eof() )
232 if (m_separators
.Contains(c
))
244 wxTextInputStream
& wxTextInputStream::operator>>(wxString
& word
)
250 wxTextInputStream
& wxTextInputStream::operator>>(char& c
)
253 if(m_input
.LastRead() <= 0) c
= 0;
261 #if wxUSE_UNICODE && wxWCHAR_T_IS_REAL_TYPE
263 wxTextInputStream
& wxTextInputStream::operator>>(wchar_t& wc
)
270 #endif // wxUSE_UNICODE
272 wxTextInputStream
& wxTextInputStream::operator>>(wxInt16
& i
)
274 i
= (wxInt16
)Read16();
278 wxTextInputStream
& wxTextInputStream::operator>>(wxInt32
& i
)
280 i
= (wxInt32
)Read32();
284 wxTextInputStream
& wxTextInputStream::operator>>(wxUint16
& i
)
290 wxTextInputStream
& wxTextInputStream::operator>>(wxUint32
& i
)
296 wxTextInputStream
& wxTextInputStream::operator>>(double& i
)
302 wxTextInputStream
& wxTextInputStream::operator>>(float& f
)
304 f
= (float)ReadDouble();
311 wxTextOutputStream::wxTextOutputStream(wxOutputStream
& s
,
313 const wxMBConv
& conv
)
314 : m_output(s
), m_conv(conv
.Clone())
316 wxTextOutputStream::wxTextOutputStream(wxOutputStream
& s
, wxEOL mode
)
321 if (m_mode
== wxEOL_NATIVE
)
323 #if defined(__WXMSW__) || defined(__WXPM__)
325 #elif defined(__WXMAC__) && !defined(__DARWIN__)
333 wxTextOutputStream::~wxTextOutputStream()
337 #endif // wxUSE_UNICODE
340 void wxTextOutputStream::SetMode(wxEOL mode
)
343 if (m_mode
== wxEOL_NATIVE
)
345 #if defined(__WXMSW__) || defined(__WXPM__)
347 #elif defined(__WXMAC__) && !defined(__DARWIN__)
355 void wxTextOutputStream::Write32(wxUint32 i
)
358 str
.Printf(wxT("%u"), i
);
363 void wxTextOutputStream::Write16(wxUint16 i
)
366 str
.Printf(wxT("%u"), (unsigned)i
);
371 void wxTextOutputStream::Write8(wxUint8 i
)
374 str
.Printf(wxT("%u"), (unsigned)i
);
379 void wxTextOutputStream::WriteDouble(double d
)
383 str
.Printf(wxT("%f"), d
);
387 void wxTextOutputStream::WriteString(const wxString
& string
)
389 size_t len
= string
.length();
394 for ( size_t i
= 0; i
< len
; i
++ )
396 const wxChar c
= string
[i
];
397 if ( c
== wxT('\n') )
410 wxFAIL_MSG( _T("unknown EOL mode in wxTextOutputStream") );
414 // don't treat '\n' specially
423 // FIXME-UTF8: use wxCharBufferWithLength if/when we have it
424 wxCharBuffer buffer
= m_conv
->cWC2MB(out
.wc_str(), out
.length(), &len
);
425 m_output
.Write(buffer
, len
);
427 m_output
.Write(out
.c_str(), out
.length() );
431 wxTextOutputStream
& wxTextOutputStream::PutChar(wxChar c
)
434 WriteString( wxString(&c
, *m_conv
, 1) );
436 WriteString( wxString(&c
, wxConvLocal
, 1) );
441 wxTextOutputStream
& wxTextOutputStream::operator<<(const wxString
& string
)
443 WriteString( string
);
447 wxTextOutputStream
& wxTextOutputStream::operator<<(char c
)
449 WriteString( wxString::FromAscii(c
) );
454 #if wxUSE_UNICODE && wxWCHAR_T_IS_REAL_TYPE
456 wxTextOutputStream
& wxTextOutputStream::operator<<(wchar_t wc
)
458 WriteString( wxString(&wc
, *m_conv
, 1) );
463 #endif // wxUSE_UNICODE
465 wxTextOutputStream
& wxTextOutputStream::operator<<(wxInt16 c
)
468 str
.Printf(wxT("%d"), (signed int)c
);
474 wxTextOutputStream
& wxTextOutputStream::operator<<(wxInt32 c
)
477 str
.Printf(wxT("%ld"), (signed long)c
);
483 wxTextOutputStream
& wxTextOutputStream::operator<<(wxUint16 c
)
486 str
.Printf(wxT("%u"), (unsigned int)c
);
492 wxTextOutputStream
& wxTextOutputStream::operator<<(wxUint32 c
)
495 str
.Printf(wxT("%lu"), (unsigned long)c
);
501 wxTextOutputStream
&wxTextOutputStream::operator<<(double f
)
507 wxTextOutputStream
& wxTextOutputStream::operator<<(float f
)
509 WriteDouble((double)f
);
513 wxTextOutputStream
&endl( wxTextOutputStream
&stream
)
515 return stream
.PutChar(wxT('\n'));