]>
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 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "txtstrm.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
25 #include "wx/txtstrm.h"
29 // ----------------------------------------------------------------------------
31 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
42 wxTextInputStream::wxTextInputStream(wxInputStream
&s
, const wxString
&sep
, wxMBConv
& conv
)
43 : m_input(s
), m_separators(sep
), m_conv(conv
)
45 memset((void*)m_lastBytes
, 0, 10);
48 wxTextInputStream::wxTextInputStream(wxInputStream
&s
, const wxString
&sep
)
49 : m_input(s
), m_separators(sep
)
51 memset((void*)m_lastBytes
, 0, 10);
55 wxTextInputStream::~wxTextInputStream()
59 void wxTextInputStream::UngetLast()
62 while(m_lastBytes
[byteCount
]) // pseudo ANSI strlen (even for Unicode!)
64 m_input
.Ungetch(m_lastBytes
, byteCount
);
65 memset((void*)m_lastBytes
, 0, 10);
68 wxChar
wxTextInputStream::NextChar()
72 memset((void*)m_lastBytes
, 0, 10);
73 for(size_t inlen
= 0; inlen
< 9; inlen
++)
75 // actually read the next character
76 m_lastBytes
[inlen
] = m_input
.GetC();
78 if(m_input
.LastRead() <= 0)
81 int retlen
= (int) m_conv
.MB2WC(wbuf
, m_lastBytes
, 2); // returns -1 for failure
82 if(retlen
>= 0) // res == 0 could happen for '\0' char
85 // there should be no encoding which requires more than nine bytes for one character...
88 m_lastBytes
[0] = m_input
.GetC();
90 if(m_input
.LastRead() <= 0)
93 return m_lastBytes
[0];
98 wxChar
wxTextInputStream::NextNonSeparators()
100 wxChar c
= (wxChar
) 0;
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 wxTextInputStream
& wxTextInputStream::operator>>(wxInt16
& i
)
258 i
= (wxInt16
)Read16();
262 wxTextInputStream
& wxTextInputStream::operator>>(wxInt32
& i
)
264 i
= (wxInt32
)Read32();
268 wxTextInputStream
& wxTextInputStream::operator>>(wxUint16
& i
)
274 wxTextInputStream
& wxTextInputStream::operator>>(wxUint32
& i
)
280 wxTextInputStream
& wxTextInputStream::operator>>(double& i
)
286 wxTextInputStream
& wxTextInputStream::operator>>(float& f
)
288 f
= (float)ReadDouble();
295 wxTextOutputStream::wxTextOutputStream(wxOutputStream
& s
, wxEOL mode
, wxMBConv
& conv
)
296 : m_output(s
), m_conv(conv
)
298 wxTextOutputStream::wxTextOutputStream(wxOutputStream
& s
, wxEOL mode
)
303 if (m_mode
== wxEOL_NATIVE
)
305 #if defined(__WXMSW__) || defined(__WXPM__)
307 #elif defined(__WXMAC__) && !defined(__DARWIN__)
315 wxTextOutputStream::~wxTextOutputStream()
319 void wxTextOutputStream::SetMode(wxEOL mode
)
322 if (m_mode
== wxEOL_NATIVE
)
324 #if defined(__WXMSW__) || defined(__WXPM__)
326 #elif defined(__WXMAC__) && !defined(__DARWIN__)
334 void wxTextOutputStream::Write32(wxUint32 i
)
337 str
.Printf(wxT("%u"), i
);
342 void wxTextOutputStream::Write16(wxUint16 i
)
345 str
.Printf(wxT("%u"), i
);
350 void wxTextOutputStream::Write8(wxUint8 i
)
353 str
.Printf(wxT("%u"), i
);
358 void wxTextOutputStream::WriteDouble(double d
)
362 str
.Printf(wxT("%f"), d
);
366 void wxTextOutputStream::WriteString(const wxString
& string
)
368 size_t len
= string
.length();
373 for ( size_t i
= 0; i
< len
; i
++ )
375 const wxChar c
= string
[i
];
376 if ( c
== wxT('\n') )
389 wxFAIL_MSG( _T("unknown EOL mode in wxTextOutputStream") );
393 // don't treat '\n' specially
401 // We must not write the trailing NULL here
403 wxCharBuffer buffer
= m_conv
.cWC2MB( out
);
404 m_output
.Write( (const char*) buffer
, strlen( (const char*) buffer
) );
406 m_output
.Write(out
.c_str(), out
.length() );
410 wxTextOutputStream
& wxTextOutputStream::operator<<(const wxChar
*string
)
412 WriteString( wxString(string
) );
416 wxTextOutputStream
& wxTextOutputStream::operator<<(const wxString
& string
)
418 WriteString( string
);
422 wxTextOutputStream
& wxTextOutputStream::operator<<(char c
)
424 WriteString( wxString::FromAscii(c
) );
429 wxTextOutputStream
& wxTextOutputStream::operator<<(wxInt16 c
)
432 str
.Printf(wxT("%d"), (signed int)c
);
438 wxTextOutputStream
& wxTextOutputStream::operator<<(wxInt32 c
)
441 str
.Printf(wxT("%ld"), (signed long)c
);
447 wxTextOutputStream
& wxTextOutputStream::operator<<(wxUint16 c
)
450 str
.Printf(wxT("%u"), (unsigned int)c
);
456 wxTextOutputStream
& wxTextOutputStream::operator<<(wxUint32 c
)
459 str
.Printf(wxT("%lu"), (unsigned long)c
);
465 wxTextOutputStream
&wxTextOutputStream::operator<<(double f
)
471 wxTextOutputStream
& wxTextOutputStream::operator<<(float f
)
473 WriteDouble((double)f
);
477 wxTextOutputStream
&endl( wxTextOutputStream
&stream
)
479 return stream
<< wxT('\n');