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