]>
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 license 
  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 // ---------------------------------------------------------------------------- 
  41 wxTextInputStream::wxTextInputStream(wxInputStream 
&s
, const wxString 
&sep
) 
  42   : m_input(s
), m_separators(sep
) 
  46 wxTextInputStream::~wxTextInputStream() 
  50 wxChar 
wxTextInputStream::NextNonSeparators() 
  52     wxChar c 
= (wxChar
) 0; 
  55         if (!m_input
) return (wxChar
) 0; 
  60             !m_separators
.Contains(c
))  
  66 inline bool wxTextInputStream::EatEOL(const wxChar 
&c
) 
  68   if (c 
== wxT('\n')) return TRUE
; // eat on UNIX 
  70   if (c 
== wxT('\r')) // eat on both Mac and DOS 
  72       if (!m_input
) return TRUE
; 
  73       wxChar c2 
= m_input
.GetC(); 
  75       if (c2 
!= wxT('\n'))  m_input
.Ungetch( c2 
); // Don't eat on Mac 
  82 void wxTextInputStream::SkipIfEndOfLine( wxChar c 
) 
  84     if (EatEOL(c
)) return; 
  85     else m_input
.Ungetch( c 
);  // no line terminator 
  88 wxUint32 
wxTextInputStream::Read32() 
  90     /* I only implemented a simple integer parser */ 
  94     if (!m_input
) return 0; 
  95     int c 
= NextNonSeparators();  
  96     if (c
==(wxChar
)0) return 0; 
  99     if (! (c 
== wxT('-') || c 
== wxT('+') || isdigit(c
)) ) 
 121         i 
= i
*10 + (c 
- (int)wxT('0')); 
 125     SkipIfEndOfLine( c 
); 
 132 wxUint16 
wxTextInputStream::Read16() 
 134     return (wxUint16
)Read32(); 
 137 wxUint8 
wxTextInputStream::Read8() 
 139     return (wxUint8
)Read32(); 
 142 double wxTextInputStream::ReadDouble() 
 144     /* I only implemented a simple float parser */ 
 148     if (!m_input
) return 0; 
 149     int c 
= NextNonSeparators(); 
 150     if (c
==(wxChar
)0) return 0; 
 153     if (! (c 
== wxT('.') || c 
== wxT(',') || c 
== wxT('-') || c 
== wxT('+') || isdigit(c
)) ) 
 176         f 
= f
*10 + (c 
- wxT('0')); 
 180     if (c 
== wxT('.') || c 
== wxT(',')) 
 182         double f_multiplicator 
= (double) 0.1; 
 188             f 
+= (c
-wxT('0'))*f_multiplicator
; 
 189             f_multiplicator 
/= 10; 
 195             double f_multiplicator 
= 0.0; 
 202                 case wxT('-'): f_multiplicator 
= 0.1;  break; 
 203                 case wxT('+'): f_multiplicator 
= 10.0; break; 
 206             e 
= Read8();  // why only max 256 ? 
 209                 f 
*= f_multiplicator
; 
 212             SkipIfEndOfLine( c 
); 
 224 wxString 
wxTextInputStream::ReadString() 
 229 wxString 
wxTextInputStream::ReadLine() 
 239         if (EatEOL(c
)) break; 
 247 wxString 
wxTextInputStream::ReadWord() 
 249     if (!m_input
) return ""; 
 252     wxChar c
=NextNonSeparators(); 
 253     if (c
==(wxChar
)0) return ""; 
 257         if (m_separators
.Contains(c
)) break; 
 259         if (EatEOL(c
)) break; 
 270 wxTextInputStream
& wxTextInputStream::operator>>(wxString
& word
) 
 276 wxTextInputStream
& wxTextInputStream::operator>>(wxChar
& c
) 
 286     if (EatEOL(c
)) c
=wxT('\n'); 
 290 wxTextInputStream
& wxTextInputStream::operator>>(wxInt16
& i
) 
 292     i 
= (wxInt16
)Read16(); 
 296 wxTextInputStream
& wxTextInputStream::operator>>(wxInt32
& i
) 
 298     i 
= (wxInt32
)Read32(); 
 302 wxTextInputStream
& wxTextInputStream::operator>>(wxUint16
& i
) 
 308 wxTextInputStream
& wxTextInputStream::operator>>(wxUint32
& i
) 
 314 wxTextInputStream
& wxTextInputStream::operator>>(double& i
) 
 320 wxTextInputStream
& wxTextInputStream::operator>>(float& f
) 
 322     f 
= (float)ReadDouble(); 
 326 wxTextOutputStream::wxTextOutputStream(wxOutputStream
& s
, wxEOL mode
) 
 330     if (m_mode 
== wxEOL_NATIVE
) 
 332 #if defined(__WXMSW__) || defined(__WXPM__) 
 334 #elif defined(__WXMAC__) 
 342 wxTextOutputStream::~wxTextOutputStream() 
 346 void wxTextOutputStream::SetMode(wxEOL mode
) 
 349     if (m_mode 
== wxEOL_NATIVE
) 
 351 #if defined(__WXMSW__) || defined(__WXPM__) 
 353 #elif defined(__WXMAC__) 
 361 void wxTextOutputStream::Write32(wxUint32 i
) 
 364     str
.Printf(wxT("%u"), i
); 
 369 void wxTextOutputStream::Write16(wxUint16 i
) 
 372     str
.Printf(wxT("%u"), i
); 
 377 void wxTextOutputStream::Write8(wxUint8 i
) 
 380     str
.Printf(wxT("%u"), i
); 
 385 void wxTextOutputStream::WriteDouble(double d
) 
 389     str
.Printf(wxT("%f"), d
); 
 393 void wxTextOutputStream::WriteString(const wxString
& string
) 
 395     for (size_t i 
= 0; i 
< string
.Len(); i
++) 
 397         wxChar c 
= string
[i
]; 
 400             if (m_mode 
== wxEOL_DOS
) 
 403                  m_output
.Write( (const void*)(&c
), sizeof(wxChar
) ); 
 405                  m_output
.Write( (const void*)(&c
), sizeof(wxChar
) ); 
 407             if (m_mode 
== wxEOL_MAC
) 
 410                  m_output
.Write( (const void*)(&c
), sizeof(wxChar
) ); 
 414                  m_output
.Write( (const void*)(&c
), sizeof(wxChar
) ); 
 419             m_output
.Write( (const void*)(&c
), sizeof(wxChar
) ); 
 424 wxTextOutputStream
& wxTextOutputStream::operator<<(const wxChar 
*string
) 
 426     WriteString( wxString(string
) ); 
 430 wxTextOutputStream
& wxTextOutputStream::operator<<(const wxString
& string
) 
 432     WriteString( string 
); 
 436 wxTextOutputStream
& wxTextOutputStream::operator<<(wxChar c
) 
 438     WriteString( wxString(c
) ); 
 442 wxTextOutputStream
& wxTextOutputStream::operator<<(wxInt16 c
) 
 445     str
.Printf(wxT("%d"), (signed int)c
); 
 451 wxTextOutputStream
& wxTextOutputStream::operator<<(wxInt32 c
) 
 454     str
.Printf(wxT("%ld"), (signed long)c
); 
 460 wxTextOutputStream
& wxTextOutputStream::operator<<(wxUint16 c
) 
 463     str
.Printf(wxT("%u"), (unsigned int)c
); 
 469 wxTextOutputStream
& wxTextOutputStream::operator<<(wxUint32 c
) 
 472     str
.Printf(wxT("%lu"), (unsigned long)c
); 
 478 wxTextOutputStream 
&wxTextOutputStream::operator<<(double f
) 
 484 wxTextOutputStream
& wxTextOutputStream::operator<<(float f
) 
 486     WriteDouble((double)f
); 
 490 wxTextOutputStream 
&endl( wxTextOutputStream 
&stream 
) 
 492     return stream 
<< wxT('\n');