]>
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 if (!m_input
.CanRead())
83 // actually read the next character
84 m_lastBytes
[inlen
] = m_input
.GetC();
86 if(m_input
.LastRead() <= 0)
89 if ( m_conv
->ToWChar(wbuf
, WXSIZEOF(wbuf
), m_lastBytes
, inlen
+ 1)
93 // there should be no encoding which requires more than nine bytes for one character...
96 if (!m_input
.CanRead())
99 m_lastBytes
[0] = m_input
.GetC();
101 if(m_input
.LastRead() <= 0)
104 return m_lastBytes
[0];
109 wxChar
wxTextInputStream::NextNonSeparators()
113 wxChar c
= NextChar();
114 if (c
== wxEOT
) return (wxChar
) 0;
116 if (c
!= wxT('\n') &&
118 !m_separators
.Contains(c
))
124 bool wxTextInputStream::EatEOL(const wxChar
&c
)
126 if (c
== wxT('\n')) return true; // eat on UNIX
128 if (c
== wxT('\r')) // eat on both Mac and DOS
130 wxChar c2
= NextChar();
131 if(c2
== wxEOT
) return true; // end of stream reached, had enough :-)
133 if (c2
!= wxT('\n')) UngetLast(); // Don't eat on Mac
140 wxUint32
wxTextInputStream::Read32(int base
)
142 wxASSERT_MSG( !base
|| (base
> 1 && base
<= 36), _T("invalid base") );
143 if(!m_input
) return 0;
145 wxString word
= ReadWord();
148 return wxStrtoul(word
.c_str(), 0, base
);
151 wxUint16
wxTextInputStream::Read16(int base
)
153 return (wxUint16
)Read32(base
);
156 wxUint8
wxTextInputStream::Read8(int base
)
158 return (wxUint8
)Read32(base
);
161 wxInt32
wxTextInputStream::Read32S(int base
)
163 wxASSERT_MSG( !base
|| (base
> 1 && base
<= 36), _T("invalid base") );
164 if(!m_input
) return 0;
166 wxString word
= ReadWord();
169 return wxStrtol(word
.c_str(), 0, base
);
172 wxInt16
wxTextInputStream::Read16S(int base
)
174 return (wxInt16
)Read32S(base
);
177 wxInt8
wxTextInputStream::Read8S(int base
)
179 return (wxInt8
)Read32S(base
);
182 double wxTextInputStream::ReadDouble()
184 if(!m_input
) return 0;
185 wxString word
= ReadWord();
188 return wxStrtod(word
.c_str(), 0);
191 #if WXWIN_COMPATIBILITY_2_6
193 wxString
wxTextInputStream::ReadString()
198 #endif // WXWIN_COMPATIBILITY_2_6
200 wxString
wxTextInputStream::ReadLine()
204 while ( !m_input
.Eof() )
206 wxChar c
= NextChar();
219 wxString
wxTextInputStream::ReadWord()
226 wxChar c
= NextNonSeparators();
232 while ( !m_input
.Eof() )
238 if (m_separators
.Contains(c
))
250 wxTextInputStream
& wxTextInputStream::operator>>(wxString
& word
)
256 wxTextInputStream
& wxTextInputStream::operator>>(char& c
)
259 if(m_input
.LastRead() <= 0) c
= 0;
267 #if wxUSE_UNICODE && wxWCHAR_T_IS_REAL_TYPE
269 wxTextInputStream
& wxTextInputStream::operator>>(wchar_t& wc
)
276 #endif // wxUSE_UNICODE
278 wxTextInputStream
& wxTextInputStream::operator>>(wxInt16
& i
)
280 i
= (wxInt16
)Read16();
284 wxTextInputStream
& wxTextInputStream::operator>>(wxInt32
& i
)
286 i
= (wxInt32
)Read32();
290 wxTextInputStream
& wxTextInputStream::operator>>(wxUint16
& i
)
296 wxTextInputStream
& wxTextInputStream::operator>>(wxUint32
& i
)
302 wxTextInputStream
& wxTextInputStream::operator>>(double& i
)
308 wxTextInputStream
& wxTextInputStream::operator>>(float& f
)
310 f
= (float)ReadDouble();
317 wxTextOutputStream::wxTextOutputStream(wxOutputStream
& s
,
319 const wxMBConv
& conv
)
320 : m_output(s
), m_conv(conv
.Clone())
322 wxTextOutputStream::wxTextOutputStream(wxOutputStream
& s
, wxEOL mode
)
327 if (m_mode
== wxEOL_NATIVE
)
329 #if defined(__WXMSW__) || defined(__WXPM__)
331 #elif defined(__WXMAC__) && !defined(__DARWIN__)
339 wxTextOutputStream::~wxTextOutputStream()
343 #endif // wxUSE_UNICODE
346 void wxTextOutputStream::SetMode(wxEOL mode
)
349 if (m_mode
== wxEOL_NATIVE
)
351 #if defined(__WXMSW__) || defined(__WXPM__)
353 #elif defined(__WXMAC__) && !defined(__DARWIN__)
361 void wxTextOutputStream::Write32(wxUint32 i
)
364 str
.Printf(wxT("%u"), i
);
369 void wxTextOutputStream::Write16(wxUint16 i
)
372 str
.Printf(wxT("%u"), (unsigned)i
);
377 void wxTextOutputStream::Write8(wxUint8 i
)
380 str
.Printf(wxT("%u"), (unsigned)i
);
385 void wxTextOutputStream::WriteDouble(double d
)
389 str
.Printf(wxT("%f"), d
);
393 void wxTextOutputStream::WriteString(const wxString
& string
)
395 size_t len
= string
.length();
400 for ( size_t i
= 0; i
< len
; i
++ )
402 const wxChar c
= string
[i
];
403 if ( c
== wxT('\n') )
416 wxFAIL_MSG( _T("unknown EOL mode in wxTextOutputStream") );
420 // don't treat '\n' specially
429 // FIXME-UTF8: use wxCharBufferWithLength if/when we have it
430 wxCharBuffer buffer
= m_conv
->cWC2MB(out
.wc_str(), out
.length(), &len
);
431 m_output
.Write(buffer
, len
);
433 m_output
.Write(out
.c_str(), out
.length() );
437 wxTextOutputStream
& wxTextOutputStream::PutChar(wxChar c
)
440 WriteString( wxString(&c
, *m_conv
, 1) );
442 WriteString( wxString(&c
, wxConvLocal
, 1) );
447 wxTextOutputStream
& wxTextOutputStream::operator<<(const wxString
& string
)
449 WriteString( string
);
453 wxTextOutputStream
& wxTextOutputStream::operator<<(char c
)
455 WriteString( wxString::FromAscii(c
) );
460 #if wxUSE_UNICODE && wxWCHAR_T_IS_REAL_TYPE
462 wxTextOutputStream
& wxTextOutputStream::operator<<(wchar_t wc
)
464 WriteString( wxString(&wc
, *m_conv
, 1) );
469 #endif // wxUSE_UNICODE
471 wxTextOutputStream
& wxTextOutputStream::operator<<(wxInt16 c
)
474 str
.Printf(wxT("%d"), (signed int)c
);
480 wxTextOutputStream
& wxTextOutputStream::operator<<(wxInt32 c
)
483 str
.Printf(wxT("%ld"), (signed long)c
);
489 wxTextOutputStream
& wxTextOutputStream::operator<<(wxUint16 c
)
492 str
.Printf(wxT("%u"), (unsigned int)c
);
498 wxTextOutputStream
& wxTextOutputStream::operator<<(wxUint32 c
)
501 str
.Printf(wxT("%lu"), (unsigned long)c
);
507 wxTextOutputStream
&wxTextOutputStream::operator<<(double f
)
513 wxTextOutputStream
& wxTextOutputStream::operator<<(float f
)
515 WriteDouble((double)f
);
519 wxTextOutputStream
&endl( wxTextOutputStream
&stream
)
521 return stream
.PutChar(wxT('\n'));