]>
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"
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 if ( m_conv
->ToWChar(wbuf
, WXSIZEOF(wbuf
), m_lastBytes
, inlen
+ 1)
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 #if WXWIN_COMPATIBILITY_2_6
183 wxString
wxTextInputStream::ReadString()
188 #endif // WXWIN_COMPATIBILITY_2_6
190 wxString
wxTextInputStream::ReadLine()
194 while ( !m_input
.Eof() )
196 wxChar c
= NextChar();
209 wxString
wxTextInputStream::ReadWord()
216 wxChar c
= NextNonSeparators();
222 while ( !m_input
.Eof() )
228 if (m_separators
.Contains(c
))
240 wxTextInputStream
& wxTextInputStream::operator>>(wxString
& word
)
246 wxTextInputStream
& wxTextInputStream::operator>>(char& c
)
249 if(m_input
.LastRead() <= 0) c
= 0;
257 #if wxUSE_UNICODE && wxWCHAR_T_IS_REAL_TYPE
259 wxTextInputStream
& wxTextInputStream::operator>>(wchar_t& wc
)
266 #endif // wxUSE_UNICODE
268 wxTextInputStream
& wxTextInputStream::operator>>(wxInt16
& i
)
270 i
= (wxInt16
)Read16();
274 wxTextInputStream
& wxTextInputStream::operator>>(wxInt32
& i
)
276 i
= (wxInt32
)Read32();
280 wxTextInputStream
& wxTextInputStream::operator>>(wxUint16
& i
)
286 wxTextInputStream
& wxTextInputStream::operator>>(wxUint32
& i
)
292 wxTextInputStream
& wxTextInputStream::operator>>(double& i
)
298 wxTextInputStream
& wxTextInputStream::operator>>(float& f
)
300 f
= (float)ReadDouble();
307 wxTextOutputStream::wxTextOutputStream(wxOutputStream
& s
,
309 const wxMBConv
& conv
)
310 : m_output(s
), m_conv(conv
.Clone())
312 wxTextOutputStream::wxTextOutputStream(wxOutputStream
& s
, wxEOL mode
)
317 if (m_mode
== wxEOL_NATIVE
)
319 #if defined(__WXMSW__) || defined(__WXPM__)
321 #elif defined(__WXMAC__) && !defined(__DARWIN__)
329 wxTextOutputStream::~wxTextOutputStream()
333 #endif // wxUSE_UNICODE
336 void wxTextOutputStream::SetMode(wxEOL mode
)
339 if (m_mode
== wxEOL_NATIVE
)
341 #if defined(__WXMSW__) || defined(__WXPM__)
343 #elif defined(__WXMAC__) && !defined(__DARWIN__)
351 void wxTextOutputStream::Write32(wxUint32 i
)
354 str
.Printf(wxT("%u"), i
);
359 void wxTextOutputStream::Write16(wxUint16 i
)
362 str
.Printf(wxT("%u"), (unsigned)i
);
367 void wxTextOutputStream::Write8(wxUint8 i
)
370 str
.Printf(wxT("%u"), (unsigned)i
);
375 void wxTextOutputStream::WriteDouble(double d
)
379 str
.Printf(wxT("%f"), d
);
383 void wxTextOutputStream::WriteString(const wxString
& string
)
385 size_t len
= string
.length();
390 for ( size_t i
= 0; i
< len
; i
++ )
392 const wxChar c
= string
[i
];
393 if ( c
== wxT('\n') )
406 wxFAIL_MSG( _T("unknown EOL mode in wxTextOutputStream") );
410 // don't treat '\n' specially
419 wxCharBuffer buffer
= m_conv
->cWC2MB(out
, out
.length(), &len
);
420 m_output
.Write(buffer
, len
);
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'));