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