]> git.saurik.com Git - wxWidgets.git/blame - src/common/txtstrm.cpp
compilation fix
[wxWidgets.git] / src / common / txtstrm.cpp
CommitLineData
5a96d2f4 1///////////////////////////////////////////////////////////////////////////////
fae05df5
GL
2// Name: txtstrm.cpp
3// Purpose: Text stream classes
4// Author: Guilhem Lavaux
5// Modified by:
6// Created: 28/06/98
7// RCS-ID: $Id$
717b9bf2 8// Copyright: (c) Guilhem Lavaux
55d99c7a 9// Licence: wxWindows licence
fae05df5
GL
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation "txtstrm.h"
14#endif
15
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
19#ifdef __BORLANDC__
20 #pragma hdrstop
21#endif
22
23#if wxUSE_STREAMS
24
25#include "wx/txtstrm.h"
c980c992 26#include <ctype.h>
fae05df5 27
cd25b18c
RR
28
29// ----------------------------------------------------------------------------
30// constants
31// ----------------------------------------------------------------------------
32
33// Unix: "\n"
34// Dos: "\r\n"
35// Mac: "\r"
36
37// ----------------------------------------------------------------------------
38// wxTextInputStream
39// ----------------------------------------------------------------------------
40
2b5f62a0
VZ
41#if wxUSE_UNICODE
42wxTextInputStream::wxTextInputStream(wxInputStream &s, const wxString &sep, wxMBConv& conv)
43 : m_input(s), m_separators(sep), m_conv(conv)
44{
2348a842 45 memset((void*)m_lastBytes, 0, 10);
2b5f62a0
VZ
46}
47#else
191549ed
SB
48wxTextInputStream::wxTextInputStream(wxInputStream &s, const wxString &sep)
49 : m_input(s), m_separators(sep)
fae05df5 50{
2348a842 51 memset((void*)m_lastBytes, 0, 10);
fae05df5 52}
2b5f62a0 53#endif
fae05df5
GL
54
55wxTextInputStream::~wxTextInputStream()
56{
57}
58
2348a842
VZ
59void wxTextInputStream::UngetLast()
60{
61 size_t byteCount = 0;
62 while(m_lastBytes[byteCount]) // pseudo ANSI strlen (even for Unicode!)
63 byteCount++;
64 m_input.Ungetch(m_lastBytes, byteCount);
65 memset((void*)m_lastBytes, 0, 10);
66}
67
68wxChar wxTextInputStream::NextChar()
69{
70#if wxUSE_UNICODE
71 wxChar wbuf[2];
72 memset((void*)m_lastBytes, 0, 10);
73 for(size_t inlen = 0; inlen < 9; inlen++)
74 {
75 // actually read the next character
76 m_lastBytes[inlen] = m_input.GetC();
77
78 if(m_input.LastRead() <= 0)
79 return wxEOT;
80
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
83 return wbuf[0];
84 }
85 // there should be no encoding which requires more than nine bytes for one character...
86 return wxEOT;
87#else
88 m_lastBytes[0] = m_input.GetC();
89
90 if(m_input.LastRead() <= 0)
91 return wxEOT;
92
93 return m_lastBytes[0];
94#endif
95
96}
97
191549ed 98wxChar wxTextInputStream::NextNonSeparators()
fae05df5 99{
cd25b18c
RR
100 wxChar c = (wxChar) 0;
101 for (;;)
102 {
2348a842
VZ
103 c = NextChar();
104 if (c == wxEOT) return (wxChar) 0;
cd6ce4a9
VZ
105
106 if (c != wxT('\n') &&
107 c != wxT('\r') &&
108 !m_separators.Contains(c))
109 return c;
cd25b18c 110 }
717b9bf2 111
cd25b18c 112}
fae05df5 113
f6bcfd97 114bool wxTextInputStream::EatEOL(const wxChar &c)
cd25b18c 115{
f6bcfd97 116 if (c == wxT('\n')) return TRUE; // eat on UNIX
cd6ce4a9 117
f6bcfd97 118 if (c == wxT('\r')) // eat on both Mac and DOS
717b9bf2 119 {
2348a842
VZ
120 wxChar c2 = NextChar();
121 if(c2 == wxEOT) return TRUE; // end of stream reached, had enough :-)
cd6ce4a9 122
2348a842 123 if (c2 != wxT('\n')) UngetLast(); // Don't eat on Mac
f6bcfd97 124 return TRUE;
cd25b18c 125 }
717b9bf2 126
f6bcfd97 127 return FALSE;
191549ed
SB
128}
129
2348a842 130wxUint32 wxTextInputStream::Read32(int base)
191549ed 131{
2348a842
VZ
132 wxASSERT_MSG( !base || (base > 1 && base <= 36), _T("invalid base") );
133 if(!m_input) return 0;
134
135 wxString word = ReadWord();
136 if(word.IsEmpty())
137 return 0;
138 return wxStrtoul(word.c_str(), 0, base);
cd25b18c 139}
fae05df5 140
2348a842 141wxUint16 wxTextInputStream::Read16(int base)
cd25b18c 142{
2348a842
VZ
143 return (wxUint16)Read32(base);
144}
cd6ce4a9 145
2348a842
VZ
146wxUint8 wxTextInputStream::Read8(int base)
147{
148 return (wxUint8)Read32(base);
149}
717b9bf2 150
2348a842
VZ
151wxInt32 wxTextInputStream::Read32S(int base)
152{
153 wxASSERT_MSG( !base || (base > 1 && base <= 36), _T("invalid base") );
154 if(!m_input) return 0;
cd25b18c 155
2348a842
VZ
156 wxString word = ReadWord();
157 if(word.IsEmpty())
cd25b18c 158 return 0;
2348a842 159 return wxStrtol(word.c_str(), 0, base);
fae05df5
GL
160}
161
2348a842 162wxInt16 wxTextInputStream::Read16S(int base)
fae05df5 163{
2348a842 164 return (wxInt16)Read32S(base);
fae05df5
GL
165}
166
2348a842 167wxInt8 wxTextInputStream::Read8S(int base)
fae05df5 168{
2348a842 169 return (wxInt8)Read32S(base);
fae05df5
GL
170}
171
172double wxTextInputStream::ReadDouble()
173{
2348a842
VZ
174 if(!m_input) return 0;
175 wxString word = ReadWord();
176 if(word.IsEmpty())
f6bcfd97 177 return 0;
2348a842 178 return wxStrtod(word.c_str(), 0);
fae05df5
GL
179}
180
181wxString wxTextInputStream::ReadString()
9853d977 182{
cd6ce4a9 183 return ReadLine();
9853d977
SB
184}
185
186wxString wxTextInputStream::ReadLine()
fae05df5 187{
cd25b18c
RR
188 wxString line;
189
cd6ce4a9 190 while ( !m_input.Eof() )
cd25b18c 191 {
2348a842
VZ
192 wxChar c = NextChar();
193 if(c == wxEOT)
194 break;
f6bcfd97 195
cd6ce4a9
VZ
196 if ( !m_input )
197 break;
198
199 if (EatEOL(c))
200 break;
201
cd25b18c
RR
202 line += c;
203 }
717b9bf2 204
cd25b18c 205 return line;
fae05df5 206}
717b9bf2 207
9853d977
SB
208wxString wxTextInputStream::ReadWord()
209{
9853d977 210 wxString word;
9853d977 211
cd6ce4a9
VZ
212 if ( !m_input )
213 return word;
214
215 wxChar c = NextNonSeparators();
216 if ( !c )
217 return word;
218
f6bcfd97
BP
219 word += c;
220
cd6ce4a9 221 while ( !m_input.Eof() )
9853d977 222 {
2348a842
VZ
223 c = NextChar();
224 if(c == wxEOT)
f6bcfd97
BP
225 break;
226
cd6ce4a9
VZ
227 if (m_separators.Contains(c))
228 break;
229
230 if (EatEOL(c))
231 break;
232
9853d977 233 word += c;
9853d977
SB
234 }
235
236 return word;
237}
238
239wxTextInputStream& wxTextInputStream::operator>>(wxString& word)
fae05df5 240{
cd6ce4a9
VZ
241 word = ReadWord();
242 return *this;
fae05df5
GL
243}
244
f6bcfd97 245wxTextInputStream& wxTextInputStream::operator>>(char& c)
fae05df5 246{
191549ed 247 c = m_input.GetC();
2348a842 248 if(m_input.LastRead() <= 0) c = 0;
717b9bf2 249
f6bcfd97
BP
250 if (EatEOL(c))
251 c = '\n';
252
cd25b18c 253 return *this;
fae05df5
GL
254}
255
256wxTextInputStream& wxTextInputStream::operator>>(wxInt16& i)
257{
cd25b18c
RR
258 i = (wxInt16)Read16();
259 return *this;
fae05df5
GL
260}
261
262wxTextInputStream& wxTextInputStream::operator>>(wxInt32& i)
263{
cd25b18c
RR
264 i = (wxInt32)Read32();
265 return *this;
fae05df5
GL
266}
267
fae05df5
GL
268wxTextInputStream& wxTextInputStream::operator>>(wxUint16& i)
269{
cd25b18c
RR
270 i = Read16();
271 return *this;
fae05df5
GL
272}
273
274wxTextInputStream& wxTextInputStream::operator>>(wxUint32& i)
275{
cd25b18c
RR
276 i = Read32();
277 return *this;
fae05df5
GL
278}
279
280wxTextInputStream& wxTextInputStream::operator>>(double& i)
281{
cd25b18c
RR
282 i = ReadDouble();
283 return *this;
fae05df5
GL
284}
285
286wxTextInputStream& wxTextInputStream::operator>>(float& f)
287{
cd25b18c
RR
288 f = (float)ReadDouble();
289 return *this;
fae05df5
GL
290}
291
2b5f62a0
VZ
292
293
294#if wxUSE_UNICODE
295wxTextOutputStream::wxTextOutputStream(wxOutputStream& s, wxEOL mode, wxMBConv& conv)
296 : m_output(s), m_conv(conv)
297#else
c7a9fa36 298wxTextOutputStream::wxTextOutputStream(wxOutputStream& s, wxEOL mode)
191549ed 299 : m_output(s)
2b5f62a0 300#endif
fae05df5 301{
c7a9fa36
RR
302 m_mode = mode;
303 if (m_mode == wxEOL_NATIVE)
304 {
305#if defined(__WXMSW__) || defined(__WXPM__)
306 m_mode = wxEOL_DOS;
f3ff3813 307#elif defined(__WXMAC__) && !defined(__DARWIN__)
c7a9fa36
RR
308 m_mode = wxEOL_MAC;
309#else
310 m_mode = wxEOL_UNIX;
311#endif
312 }
fae05df5
GL
313}
314
315wxTextOutputStream::~wxTextOutputStream()
316{
317}
318
cd0b1709 319void wxTextOutputStream::SetMode(wxEOL mode)
c7a9fa36
RR
320{
321 m_mode = mode;
322 if (m_mode == wxEOL_NATIVE)
323 {
324#if defined(__WXMSW__) || defined(__WXPM__)
325 m_mode = wxEOL_DOS;
f3ff3813 326#elif defined(__WXMAC__) && !defined(__DARWIN__)
c7a9fa36
RR
327 m_mode = wxEOL_MAC;
328#else
329 m_mode = wxEOL_UNIX;
330#endif
331 }
332}
333
fae05df5
GL
334void wxTextOutputStream::Write32(wxUint32 i)
335{
cd25b18c 336 wxString str;
223d09f6 337 str.Printf(wxT("%u"), i);
717b9bf2 338
cd25b18c 339 WriteString(str);
fae05df5
GL
340}
341
342void wxTextOutputStream::Write16(wxUint16 i)
343{
cd25b18c 344 wxString str;
223d09f6 345 str.Printf(wxT("%u"), i);
717b9bf2 346
cd25b18c 347 WriteString(str);
fae05df5
GL
348}
349
350void wxTextOutputStream::Write8(wxUint8 i)
351{
cd25b18c 352 wxString str;
223d09f6 353 str.Printf(wxT("%u"), i);
717b9bf2 354
cd25b18c 355 WriteString(str);
fae05df5
GL
356}
357
358void wxTextOutputStream::WriteDouble(double d)
359{
cd25b18c 360 wxString str;
fae05df5 361
223d09f6 362 str.Printf(wxT("%f"), d);
cd25b18c 363 WriteString(str);
fae05df5
GL
364}
365
366void wxTextOutputStream::WriteString(const wxString& string)
367{
20ea6894
VZ
368 size_t len = string.length();
369
370 wxString out;
371 out.reserve(len);
372
373 for ( size_t i = 0; i < len; i++ )
cd25b18c 374 {
20ea6894
VZ
375 const wxChar c = string[i];
376 if ( c == wxT('\n') )
cd25b18c 377 {
20ea6894 378 switch ( m_mode )
cd6ce4a9 379 {
20ea6894
VZ
380 case wxEOL_DOS:
381 out << _T("\r\n");
382 continue;
383
384 case wxEOL_MAC:
385 out << _T('\r');
386 continue;
387
388 default:
389 wxFAIL_MSG( _T("unknown EOL mode in wxTextOutputStream") );
390 // fall through
391
392 case wxEOL_UNIX:
393 // don't treat '\n' specially
394 ;
c7a9fa36 395 }
cd25b18c 396 }
20ea6894
VZ
397
398 out << c;
cd25b18c 399 }
20ea6894 400
2b5f62a0
VZ
401 // We must not write the trailing NULL here
402#if wxUSE_UNICODE
403 wxCharBuffer buffer = m_conv.cWC2MB( out );
404 m_output.Write( (const char*) buffer, strlen( (const char*) buffer ) );
405#else
406 m_output.Write(out.c_str(), out.length() );
407#endif
fae05df5
GL
408}
409
410wxTextOutputStream& wxTextOutputStream::operator<<(const wxChar *string)
411{
cd25b18c
RR
412 WriteString( wxString(string) );
413 return *this;
fae05df5
GL
414}
415
416wxTextOutputStream& wxTextOutputStream::operator<<(const wxString& string)
417{
cd25b18c
RR
418 WriteString( string );
419 return *this;
fae05df5
GL
420}
421
f6bcfd97 422wxTextOutputStream& wxTextOutputStream::operator<<(char c)
fae05df5 423{
2b5f62a0
VZ
424 WriteString( wxString::FromAscii(c) );
425
cd25b18c 426 return *this;
fae05df5
GL
427}
428
429wxTextOutputStream& wxTextOutputStream::operator<<(wxInt16 c)
430{
78e848ca
RR
431 wxString str;
432 str.Printf(wxT("%d"), (signed int)c);
433 WriteString(str);
cd6ce4a9 434
cd25b18c 435 return *this;
fae05df5
GL
436}
437
438wxTextOutputStream& wxTextOutputStream::operator<<(wxInt32 c)
439{
78e848ca
RR
440 wxString str;
441 str.Printf(wxT("%ld"), (signed long)c);
442 WriteString(str);
cd6ce4a9 443
cd25b18c 444 return *this;
fae05df5
GL
445}
446
fae05df5
GL
447wxTextOutputStream& wxTextOutputStream::operator<<(wxUint16 c)
448{
78e848ca
RR
449 wxString str;
450 str.Printf(wxT("%u"), (unsigned int)c);
451 WriteString(str);
cd6ce4a9 452
cd25b18c 453 return *this;
fae05df5
GL
454}
455
456wxTextOutputStream& wxTextOutputStream::operator<<(wxUint32 c)
457{
78e848ca
RR
458 wxString str;
459 str.Printf(wxT("%lu"), (unsigned long)c);
460 WriteString(str);
461
cd25b18c 462 return *this;
fae05df5
GL
463}
464
465wxTextOutputStream &wxTextOutputStream::operator<<(double f)
466{
cd25b18c
RR
467 WriteDouble(f);
468 return *this;
fae05df5
GL
469}
470
471wxTextOutputStream& wxTextOutputStream::operator<<(float f)
472{
cd25b18c
RR
473 WriteDouble((double)f);
474 return *this;
fae05df5
GL
475}
476
ed58dbea
RR
477wxTextOutputStream &endl( wxTextOutputStream &stream )
478{
223d09f6 479 return stream << wxT('\n');
ed58dbea
RR
480}
481
fae05df5
GL
482#endif
483 // wxUSE_STREAMS