]> git.saurik.com Git - wxWidgets.git/blame - src/common/txtstrm.cpp
Added RTLD_GLOBAL to dlopen() flags which is needed if libraries depend
[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
cd6ce4a9 9// Licence: wxWindows license
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
191549ed
SB
41wxTextInputStream::wxTextInputStream(wxInputStream &s, const wxString &sep)
42 : m_input(s), m_separators(sep)
fae05df5
GL
43{
44}
45
46wxTextInputStream::~wxTextInputStream()
47{
48}
49
191549ed 50wxChar wxTextInputStream::NextNonSeparators()
fae05df5 51{
cd25b18c
RR
52 wxChar c = (wxChar) 0;
53 for (;;)
54 {
cd6ce4a9 55 if (!m_input) return (wxChar) 0;
191549ed 56 c = m_input.GetC();
cd6ce4a9
VZ
57
58 if (c != wxT('\n') &&
59 c != wxT('\r') &&
60 !m_separators.Contains(c))
61 return c;
cd25b18c 62 }
717b9bf2 63
cd25b18c 64}
fae05df5 65
191549ed 66inline bool wxTextInputStream::EatEOL(const wxChar &c)
cd25b18c 67{
191549ed 68 if (c == wxT('\n')) return TRUE; // eat on UNIX
cd6ce4a9 69
191549ed 70 if (c == wxT('\r')) // eat on both Mac and DOS
717b9bf2 71 {
191549ed
SB
72 if (!m_input) return TRUE;
73 wxChar c2 = m_input.GetC();
cd6ce4a9 74
191549ed
SB
75 if (c2 != wxT('\n')) m_input.Ungetch( c2 ); // Don't eat on Mac
76 return TRUE;
cd25b18c 77 }
717b9bf2 78
191549ed
SB
79 return FALSE;
80}
81
82void wxTextInputStream::SkipIfEndOfLine( wxChar c )
83{
84 if (EatEOL(c)) return;
85 else m_input.Ungetch( c ); // no line terminator
cd25b18c 86}
fae05df5 87
cd25b18c
RR
88wxUint32 wxTextInputStream::Read32()
89{
90 /* I only implemented a simple integer parser */
cd6ce4a9
VZ
91 // VZ: what about using strtol()?? (TODO)
92
cd25b18c
RR
93 int sign;
94 wxInt32 i;
717b9bf2 95
cd25b18c 96 if (!m_input) return 0;
cd6ce4a9 97 int c = NextNonSeparators();
191549ed 98 if (c==(wxChar)0) return 0;
cd25b18c
RR
99
100 i = 0;
223d09f6 101 if (! (c == wxT('-') || c == wxT('+') || isdigit(c)) )
cd25b18c 102 {
191549ed 103 m_input.Ungetch(c);
cd25b18c
RR
104 return 0;
105 }
fae05df5 106
223d09f6 107 if (c == wxT('-'))
cd25b18c
RR
108 {
109 sign = -1;
191549ed 110 c = m_input.GetC();
717b9bf2 111 } else
223d09f6 112 if (c == wxT('+'))
cd25b18c
RR
113 {
114 sign = 1;
191549ed 115 c = m_input.GetC();
717b9bf2 116 } else
cd25b18c
RR
117 {
118 sign = 1;
119 }
fae05df5 120
717b9bf2 121 while (isdigit(c))
cd25b18c 122 {
223d09f6 123 i = i*10 + (c - (int)wxT('0'));
191549ed 124 c = m_input.GetC();
cd25b18c 125 }
fae05df5 126
cd25b18c 127 SkipIfEndOfLine( c );
fae05df5 128
cd25b18c 129 i *= sign;
fae05df5 130
cd25b18c 131 return (wxUint32)i;
fae05df5
GL
132}
133
134wxUint16 wxTextInputStream::Read16()
135{
cd25b18c 136 return (wxUint16)Read32();
fae05df5
GL
137}
138
139wxUint8 wxTextInputStream::Read8()
140{
cd25b18c 141 return (wxUint8)Read32();
fae05df5
GL
142}
143
144double wxTextInputStream::ReadDouble()
145{
cd25b18c 146 /* I only implemented a simple float parser */
cd6ce4a9 147 // VZ: what about using strtod()?? (TODO)
cd25b18c
RR
148 double f;
149 int sign;
150
191549ed
SB
151 if (!m_input) return 0;
152 int c = NextNonSeparators();
153 if (c==(wxChar)0) return 0;
cd25b18c
RR
154
155 f = 0.0;
78e848ca 156 if (! (c == wxT('.') || c == wxT(',') || c == wxT('-') || c == wxT('+') || isdigit(c)) )
cd25b18c 157 {
191549ed 158 m_input.Ungetch(c);
cd25b18c
RR
159 return 0.0;
160 }
161
223d09f6 162 if (c == wxT('-'))
cd25b18c
RR
163 {
164 sign = -1;
191549ed 165 c = m_input.GetC();
717b9bf2 166 } else
223d09f6 167 if (c == wxT('+'))
cd25b18c
RR
168 {
169 sign = 1;
191549ed 170 c = m_input.GetC();
717b9bf2
DW
171 }
172 else
cd25b18c
RR
173 {
174 sign = 1;
175 }
176
717b9bf2 177 while (isdigit(c))
cd25b18c 178 {
223d09f6 179 f = f*10 + (c - wxT('0'));
191549ed 180 c = m_input.GetC();
fae05df5
GL
181 }
182
78e848ca 183 if (c == wxT('.') || c == wxT(','))
cd25b18c
RR
184 {
185 double f_multiplicator = (double) 0.1;
fae05df5 186
191549ed 187 c = m_input.GetC();
fae05df5 188
717b9bf2 189 while (isdigit(c))
cd6ce4a9 190 {
223d09f6 191 f += (c-wxT('0'))*f_multiplicator;
cd25b18c 192 f_multiplicator /= 10;
191549ed 193 c = m_input.GetC();
cd25b18c 194 }
fae05df5 195
223d09f6 196 if (c == wxT('e'))
cd6ce4a9 197 {
cd25b18c
RR
198 double f_multiplicator = 0.0;
199 int i, e;
fae05df5 200
191549ed 201 c = m_input.GetC();
fae05df5 202
717b9bf2 203 switch (c)
cd6ce4a9 204 {
223d09f6 205 case wxT('-'): f_multiplicator = 0.1; break;
cd6ce4a9
VZ
206 case wxT('+'): f_multiplicator = 10.0; break;
207 }
fae05df5 208
cd25b18c
RR
209 e = Read8(); // why only max 256 ?
210
211 for (i=0;i<e;i++)
212 f *= f_multiplicator;
717b9bf2 213 }
cd6ce4a9
VZ
214 else
215 SkipIfEndOfLine( c );
cd25b18c
RR
216 }
217 else
218 {
191549ed 219 m_input.Ungetch(c);
cd25b18c
RR
220 }
221
222 f *= sign;
223
224 return f;
fae05df5
GL
225}
226
227wxString wxTextInputStream::ReadString()
9853d977 228{
cd6ce4a9 229 return ReadLine();
9853d977
SB
230}
231
232wxString wxTextInputStream::ReadLine()
fae05df5 233{
cd25b18c
RR
234 wxChar c;
235 wxString line;
236
cd6ce4a9 237 while ( !m_input.Eof() )
cd25b18c 238 {
191549ed 239 c = m_input.GetC();
cd6ce4a9
VZ
240 if ( !m_input )
241 break;
242
243 if (EatEOL(c))
244 break;
245
cd25b18c
RR
246 line += c;
247 }
717b9bf2 248
cd25b18c 249 return line;
fae05df5 250}
717b9bf2 251
9853d977
SB
252wxString wxTextInputStream::ReadWord()
253{
9853d977 254 wxString word;
9853d977 255
cd6ce4a9
VZ
256 if ( !m_input )
257 return word;
258
259 wxChar c = NextNonSeparators();
260 if ( !c )
261 return word;
262
263 while ( !m_input.Eof() )
9853d977 264 {
cd6ce4a9
VZ
265 if (m_separators.Contains(c))
266 break;
267
268 if (EatEOL(c))
269 break;
270
9853d977 271 word += c;
191549ed 272
191549ed 273 c = m_input.GetC();
cd6ce4a9
VZ
274 if (!m_input)
275 break;
9853d977
SB
276 }
277
278 return word;
279}
280
281wxTextInputStream& wxTextInputStream::operator>>(wxString& word)
fae05df5 282{
cd6ce4a9
VZ
283 word = ReadWord();
284 return *this;
fae05df5
GL
285}
286
940ddb19 287wxTextInputStream& wxTextInputStream::operator>>(wxChar& c)
fae05df5 288{
cd25b18c
RR
289 if (!m_input)
290 {
291 c = (wxChar) 0;
292 return *this;
293 }
717b9bf2 294
191549ed 295 c = m_input.GetC();
717b9bf2 296
191549ed 297 if (EatEOL(c)) c=wxT('\n');
cd25b18c 298 return *this;
fae05df5
GL
299}
300
301wxTextInputStream& wxTextInputStream::operator>>(wxInt16& i)
302{
cd25b18c
RR
303 i = (wxInt16)Read16();
304 return *this;
fae05df5
GL
305}
306
307wxTextInputStream& wxTextInputStream::operator>>(wxInt32& i)
308{
cd25b18c
RR
309 i = (wxInt32)Read32();
310 return *this;
fae05df5
GL
311}
312
fae05df5
GL
313wxTextInputStream& wxTextInputStream::operator>>(wxUint16& i)
314{
cd25b18c
RR
315 i = Read16();
316 return *this;
fae05df5
GL
317}
318
319wxTextInputStream& wxTextInputStream::operator>>(wxUint32& i)
320{
cd25b18c
RR
321 i = Read32();
322 return *this;
fae05df5
GL
323}
324
325wxTextInputStream& wxTextInputStream::operator>>(double& i)
326{
cd25b18c
RR
327 i = ReadDouble();
328 return *this;
fae05df5
GL
329}
330
331wxTextInputStream& wxTextInputStream::operator>>(float& f)
332{
cd25b18c
RR
333 f = (float)ReadDouble();
334 return *this;
fae05df5
GL
335}
336
c7a9fa36 337wxTextOutputStream::wxTextOutputStream(wxOutputStream& s, wxEOL mode)
191549ed 338 : m_output(s)
fae05df5 339{
c7a9fa36
RR
340 m_mode = mode;
341 if (m_mode == wxEOL_NATIVE)
342 {
343#if defined(__WXMSW__) || defined(__WXPM__)
344 m_mode = wxEOL_DOS;
345#elif defined(__WXMAC__)
346 m_mode = wxEOL_MAC;
347#else
348 m_mode = wxEOL_UNIX;
349#endif
350 }
fae05df5
GL
351}
352
353wxTextOutputStream::~wxTextOutputStream()
354{
355}
356
cd0b1709 357void wxTextOutputStream::SetMode(wxEOL mode)
c7a9fa36
RR
358{
359 m_mode = mode;
360 if (m_mode == wxEOL_NATIVE)
361 {
362#if defined(__WXMSW__) || defined(__WXPM__)
363 m_mode = wxEOL_DOS;
364#elif defined(__WXMAC__)
365 m_mode = wxEOL_MAC;
366#else
367 m_mode = wxEOL_UNIX;
368#endif
369 }
370}
371
fae05df5
GL
372void wxTextOutputStream::Write32(wxUint32 i)
373{
cd25b18c 374 wxString str;
223d09f6 375 str.Printf(wxT("%u"), i);
717b9bf2 376
cd25b18c 377 WriteString(str);
fae05df5
GL
378}
379
380void wxTextOutputStream::Write16(wxUint16 i)
381{
cd25b18c 382 wxString str;
223d09f6 383 str.Printf(wxT("%u"), i);
717b9bf2 384
cd25b18c 385 WriteString(str);
fae05df5
GL
386}
387
388void wxTextOutputStream::Write8(wxUint8 i)
389{
cd25b18c 390 wxString str;
223d09f6 391 str.Printf(wxT("%u"), i);
717b9bf2 392
cd25b18c 393 WriteString(str);
fae05df5
GL
394}
395
396void wxTextOutputStream::WriteDouble(double d)
397{
cd25b18c 398 wxString str;
fae05df5 399
223d09f6 400 str.Printf(wxT("%f"), d);
cd25b18c 401 WriteString(str);
fae05df5
GL
402}
403
404void wxTextOutputStream::WriteString(const wxString& string)
405{
cd25b18c
RR
406 for (size_t i = 0; i < string.Len(); i++)
407 {
408 wxChar c = string[i];
223d09f6 409 if (c == wxT('\n'))
cd25b18c 410 {
cd6ce4a9
VZ
411 if (m_mode == wxEOL_DOS)
412 {
c7a9fa36
RR
413 c = wxT('\r');
414 m_output.Write( (const void*)(&c), sizeof(wxChar) );
415 c = wxT('\n');
416 m_output.Write( (const void*)(&c), sizeof(wxChar) );
cd6ce4a9
VZ
417 } else
418 if (m_mode == wxEOL_MAC)
419 {
c7a9fa36
RR
420 c = wxT('\r');
421 m_output.Write( (const void*)(&c), sizeof(wxChar) );
cd6ce4a9
VZ
422 } else
423 {
c7a9fa36
RR
424 c = wxT('\n');
425 m_output.Write( (const void*)(&c), sizeof(wxChar) );
426 }
cd25b18c
RR
427 }
428 else
429 {
191549ed 430 m_output.Write( (const void*)(&c), sizeof(wxChar) );
cd25b18c
RR
431 }
432 }
fae05df5
GL
433}
434
435wxTextOutputStream& wxTextOutputStream::operator<<(const wxChar *string)
436{
cd25b18c
RR
437 WriteString( wxString(string) );
438 return *this;
fae05df5
GL
439}
440
441wxTextOutputStream& wxTextOutputStream::operator<<(const wxString& string)
442{
cd25b18c
RR
443 WriteString( string );
444 return *this;
fae05df5
GL
445}
446
940ddb19 447wxTextOutputStream& wxTextOutputStream::operator<<(wxChar c)
fae05df5 448{
cd25b18c
RR
449 WriteString( wxString(c) );
450 return *this;
fae05df5
GL
451}
452
453wxTextOutputStream& wxTextOutputStream::operator<<(wxInt16 c)
454{
78e848ca
RR
455 wxString str;
456 str.Printf(wxT("%d"), (signed int)c);
457 WriteString(str);
cd6ce4a9 458
cd25b18c 459 return *this;
fae05df5
GL
460}
461
462wxTextOutputStream& wxTextOutputStream::operator<<(wxInt32 c)
463{
78e848ca
RR
464 wxString str;
465 str.Printf(wxT("%ld"), (signed long)c);
466 WriteString(str);
cd6ce4a9 467
cd25b18c 468 return *this;
fae05df5
GL
469}
470
fae05df5
GL
471wxTextOutputStream& wxTextOutputStream::operator<<(wxUint16 c)
472{
78e848ca
RR
473 wxString str;
474 str.Printf(wxT("%u"), (unsigned int)c);
475 WriteString(str);
cd6ce4a9 476
cd25b18c 477 return *this;
fae05df5
GL
478}
479
480wxTextOutputStream& wxTextOutputStream::operator<<(wxUint32 c)
481{
78e848ca
RR
482 wxString str;
483 str.Printf(wxT("%lu"), (unsigned long)c);
484 WriteString(str);
485
cd25b18c 486 return *this;
fae05df5
GL
487}
488
489wxTextOutputStream &wxTextOutputStream::operator<<(double f)
490{
cd25b18c
RR
491 WriteDouble(f);
492 return *this;
fae05df5
GL
493}
494
495wxTextOutputStream& wxTextOutputStream::operator<<(float f)
496{
cd25b18c
RR
497 WriteDouble((double)f);
498 return *this;
fae05df5
GL
499}
500
ed58dbea
RR
501wxTextOutputStream &endl( wxTextOutputStream &stream )
502{
223d09f6 503 return stream << wxT('\n');
ed58dbea
RR
504}
505
fae05df5
GL
506#endif
507 // wxUSE_STREAMS