* Moved ReadLine()/WriteLine() to wxIn/OutputStream
[wxWidgets.git] / src / common / stream.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: stream.cpp
3 // Purpose: wxStream base classes
4 // Author: Guilhem Lavaux
5 // Modified by:
6 // Created: 11/07/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "stream.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 #ifndef WX_PRECOMP
24 #include "wx/defs.h"
25 #endif
26
27 #if wxUSE_STREAMS
28
29 #include <ctype.h>
30 #include <wx/stream.h>
31 #include <wx/datstrm.h>
32 #include <wx/objstrm.h>
33
34 #define BUF_TEMP_SIZE 10000
35
36 // ----------------------------------------------------------------------------
37 // wxStreamBuffer
38 // ----------------------------------------------------------------------------
39
40 #define CHECK_ERROR(err) \
41 if (m_stream->m_lasterror == wxStream_NOERROR) \
42 m_stream->m_lasterror = err
43
44 wxStreamBuffer::wxStreamBuffer(wxStreamBase& stream, BufMode mode)
45 : m_buffer_start(NULL), m_buffer_end(NULL), m_buffer_pos(NULL),
46 m_buffer_size(0), m_wback(NULL), m_wbacksize(0), m_wbackcur(0),
47 m_fixed(TRUE), m_flushable(TRUE), m_stream(&stream),
48 m_mode(mode), m_destroybuf(FALSE), m_destroystream(FALSE)
49 {
50 }
51
52 wxStreamBuffer::wxStreamBuffer(BufMode mode)
53 : m_buffer_start(NULL), m_buffer_end(NULL), m_buffer_pos(NULL),
54 m_buffer_size(0), m_wback(NULL), m_wbacksize(0), m_wbackcur(0),
55 m_fixed(TRUE), m_flushable(FALSE), m_stream(NULL),
56 m_mode(mode), m_destroybuf(FALSE), m_destroystream(TRUE)
57 {
58 m_stream = new wxStreamBase();
59 }
60
61 wxStreamBuffer::wxStreamBuffer(const wxStreamBuffer& buffer)
62 {
63 m_buffer_start = buffer.m_buffer_start;
64 m_buffer_end = buffer.m_buffer_end;
65 m_buffer_pos = buffer.m_buffer_pos;
66 m_buffer_size = buffer.m_buffer_size;
67 m_fixed = buffer.m_fixed;
68 m_flushable = buffer.m_flushable;
69 m_stream = buffer.m_stream;
70 m_mode = buffer.m_mode;
71 m_destroybuf = FALSE;
72 m_destroystream = FALSE;
73 m_wback = NULL;
74 m_wbacksize = 0;
75 m_wbackcur = 0;
76 }
77
78 wxStreamBuffer::~wxStreamBuffer()
79 {
80 if (m_wback)
81 free(m_wback);
82 if (m_destroybuf)
83 wxDELETEA(m_buffer_start);
84 if (m_destroystream)
85 delete m_stream;
86 }
87
88 size_t wxStreamBuffer::WriteBack(const char *buf, size_t bufsize)
89 {
90 char *ptrback;
91
92 if (m_mode != read)
93 return 0;
94
95 ptrback = AllocSpaceWBack(bufsize);
96 if (!ptrback)
97 return 0;
98
99 memcpy(ptrback, buf, bufsize);
100 return bufsize;
101 }
102
103 bool wxStreamBuffer::WriteBack(char c)
104 {
105 char *ptrback;
106
107 ptrback = AllocSpaceWBack(1);
108 if (!ptrback)
109 return FALSE;
110
111 *ptrback = c;
112 return TRUE;
113 }
114
115 void wxStreamBuffer::SetBufferIO(char *buffer_start, char *buffer_end)
116 {
117 if (m_destroybuf)
118 wxDELETEA(m_buffer_start);
119 m_buffer_start = buffer_start;
120 m_buffer_end = buffer_end;
121
122 m_buffer_size = m_buffer_end-m_buffer_start;
123 m_destroybuf = FALSE;
124 ResetBuffer();
125 }
126
127 void wxStreamBuffer::SetBufferIO(size_t bufsize)
128 {
129 char *b_start;
130
131 if (m_destroybuf)
132 wxDELETEA(m_buffer_start);
133
134 if (!bufsize) {
135 m_buffer_start = NULL;
136 m_buffer_end = NULL;
137 m_buffer_pos = NULL;
138 m_buffer_size = 0;
139 return;
140 }
141
142 b_start = new char[bufsize];
143 SetBufferIO(b_start, b_start + bufsize);
144 m_destroybuf = TRUE;
145 }
146
147 void wxStreamBuffer::ResetBuffer()
148 {
149 m_stream->m_lasterror = wxStream_NOERROR;
150 m_stream->m_lastcount = 0;
151 if (m_mode == read)
152 m_buffer_pos = m_buffer_end;
153 else
154 m_buffer_pos = m_buffer_start;
155 }
156
157 char *wxStreamBuffer::AllocSpaceWBack(size_t needed_size)
158 {
159 char *temp_b;
160
161 m_wbacksize += needed_size;
162
163 if (!m_wback)
164 temp_b = (char *)malloc(m_wbacksize);
165 else
166 temp_b = (char *)realloc(m_wback, m_wbacksize);
167
168 if (!temp_b)
169 return NULL;
170 m_wback = temp_b;
171
172 return (char *)(m_wback+(m_wbacksize-needed_size));
173 }
174
175 size_t wxStreamBuffer::GetWBack(char *buf, size_t bsize)
176 {
177 size_t s_toget = m_wbacksize-m_wbackcur;
178
179 if (bsize < s_toget)
180 s_toget = bsize;
181
182 memcpy(buf, (m_wback+m_wbackcur), s_toget);
183
184 m_wbackcur += s_toget;
185 if (m_wbackcur == m_wbacksize) {
186 free(m_wback);
187 m_wback = (char *)NULL;
188 m_wbacksize = 0;
189 m_wbackcur = 0;
190 }
191
192 return s_toget;
193 }
194
195 bool wxStreamBuffer::FillBuffer()
196 {
197 size_t count;
198
199 count = m_stream->OnSysRead(m_buffer_start, m_buffer_size);
200 m_buffer_end = m_buffer_start+count;
201 m_buffer_pos = m_buffer_start;
202
203 if (count == 0)
204 return FALSE;
205 return TRUE;
206 }
207
208 bool wxStreamBuffer::FlushBuffer()
209 {
210 size_t count, current;
211
212 if (m_buffer_pos == m_buffer_start || !m_flushable)
213 return FALSE;
214
215 current = m_buffer_pos-m_buffer_start;
216 count = m_stream->OnSysWrite(m_buffer_start, current);
217 if (count != current)
218 return FALSE;
219 m_buffer_pos = m_buffer_start;
220
221 return TRUE;
222 }
223
224 void wxStreamBuffer::GetFromBuffer(void *buffer, size_t size)
225 {
226 size_t s_toget = m_buffer_end-m_buffer_pos;
227
228 if (size < s_toget)
229 s_toget = size;
230
231 memcpy(buffer, m_buffer_pos, s_toget);
232 m_buffer_pos += s_toget;
233 }
234
235 void wxStreamBuffer::PutToBuffer(const void *buffer, size_t size)
236 {
237 size_t s_toput = m_buffer_end-m_buffer_pos;
238
239 if (s_toput < size && !m_fixed) {
240 m_buffer_start = (char *)realloc(m_buffer_start, m_buffer_size+size);
241 // I round a bit
242 m_buffer_size += size;
243 m_buffer_end = m_buffer_start+m_buffer_size;
244 s_toput = size;
245 }
246 if (s_toput > size)
247 s_toput = size;
248 memcpy(m_buffer_pos, buffer, s_toput);
249 m_buffer_pos += s_toput;
250 }
251
252 void wxStreamBuffer::PutChar(char c)
253 {
254 wxASSERT(m_stream != NULL);
255
256 if (!m_buffer_size) {
257 m_stream->OnSysWrite(&c, 1);
258 return;
259 }
260
261 if (GetDataLeft() == 0 && !FlushBuffer()) {
262 CHECK_ERROR(wxStream_WRITE_ERR);
263 return;
264 }
265
266 PutToBuffer(&c, 1);
267 m_stream->m_lastcount = 1;
268 }
269
270 char wxStreamBuffer::GetChar()
271 {
272 char c;
273
274 wxASSERT(m_stream != NULL);
275
276 if (!m_buffer_size) {
277 m_stream->OnSysRead(&c, 1);
278 return c;
279 }
280
281 if (!GetDataLeft()) {
282 CHECK_ERROR(wxStream_READ_ERR);
283 return 0;
284 }
285
286 GetFromBuffer(&c, 1);
287
288 m_stream->m_lastcount = 1;
289 return c;
290 }
291
292 size_t wxStreamBuffer::Read(void *buffer, size_t size)
293 {
294 wxASSERT(m_stream != NULL);
295
296 if (m_mode == write)
297 return 0;
298
299 // ------------------
300 // Buffering disabled
301 // ------------------
302
303 m_stream->m_lasterror = wxStream_NOERROR;
304 m_stream->m_lastcount = GetWBack((char *)buffer, size);
305 size -= m_stream->m_lastcount;
306 if (size == 0)
307 return m_stream->m_lastcount;
308
309 buffer = (void *)((char *)buffer+m_stream->m_lastcount);
310
311 if (!m_buffer_size)
312 return (m_stream->m_lastcount += m_stream->OnSysRead(buffer, size));
313
314 // -----------------
315 // Buffering enabled
316 // -----------------
317 size_t buf_left, orig_size = size;
318
319 while (size > 0) {
320 buf_left = GetDataLeft();
321
322 // First case: the requested buffer is larger than the stream buffer,
323 // we split it.
324 if (size > buf_left) {
325 GetFromBuffer(buffer, buf_left);
326 size -= buf_left;
327 buffer = (char *)buffer + buf_left; // ANSI C++ violation.
328
329 if (!FillBuffer()) {
330 CHECK_ERROR(wxStream_READ_ERR);
331 return (m_stream->m_lastcount = orig_size-size);
332 }
333 } else {
334
335 // Second case: we just copy from the stream buffer.
336 GetFromBuffer(buffer, size);
337 break;
338 }
339 }
340 return (m_stream->m_lastcount += orig_size);
341 }
342
343 size_t wxStreamBuffer::Read(wxStreamBuffer *s_buf)
344 {
345 char buf[BUF_TEMP_SIZE];
346 size_t s = 0, bytes_read = BUF_TEMP_SIZE;
347
348 if (m_mode == write)
349 return 0;
350
351 while (bytes_read != 0) {
352 bytes_read = Read(buf, bytes_read);
353 bytes_read = s_buf->Write(buf, bytes_read);
354 s += bytes_read;
355 }
356 return s;
357 }
358
359 size_t wxStreamBuffer::Write(const void *buffer, size_t size)
360 {
361 wxASSERT(m_stream != NULL);
362
363 if (m_mode == read)
364 return 0;
365
366 // ------------------
367 // Buffering disabled
368 // ------------------
369
370 m_stream->m_lasterror = wxStream_NOERROR;
371 if (!m_buffer_size)
372 return (m_stream->m_lastcount = m_stream->OnSysWrite(buffer, size));
373
374 // ------------------
375 // Buffering enabled
376 // ------------------
377
378 size_t buf_left, orig_size = size;
379
380 while (size > 0) {
381 buf_left = m_buffer_end - m_buffer_pos;
382
383 // First case: the buffer to write is larger than the stream buffer,
384 // we split it
385 if (size > buf_left) {
386 PutToBuffer(buffer, buf_left);
387 size -= buf_left;
388 buffer = (char *)buffer + buf_left; // ANSI C++ violation.
389
390 if (!FlushBuffer()) {
391 CHECK_ERROR(wxStream_WRITE_ERR);
392 return (m_stream->m_lastcount = orig_size-size);
393 }
394
395 m_buffer_pos = m_buffer_start;
396
397 } else {
398
399 // Second case: just copy it in the stream buffer.
400 PutToBuffer(buffer, size);
401 break;
402 }
403 }
404 return (m_stream->m_lastcount = orig_size);
405 }
406
407 size_t wxStreamBuffer::Write(wxStreamBuffer *sbuf)
408 {
409 char buf[BUF_TEMP_SIZE];
410 size_t s = 0, bytes_count = BUF_TEMP_SIZE, b_count2;
411
412 if (m_mode == read)
413 return 0;
414
415 while (bytes_count == BUF_TEMP_SIZE) {
416 b_count2 = sbuf->Read(buf, bytes_count);
417 bytes_count = Write(buf, b_count2);
418 if (b_count2 > bytes_count)
419 sbuf->WriteBack(buf+bytes_count, b_count2-bytes_count);
420 s += bytes_count;
421 }
422 return s;
423 }
424
425 off_t wxStreamBuffer::Seek(off_t pos, wxSeekMode mode)
426 {
427 off_t ret_off, diff, last_access;
428
429 last_access = GetLastAccess();
430
431 if (!m_flushable) {
432 diff = pos + GetIntPosition();
433 if (diff < 0 || diff > last_access)
434 return wxInvalidOffset;
435 SetIntPosition(diff);
436 return diff;
437 }
438
439 switch (mode) {
440 case wxFromStart: {
441 // We'll try to compute an internal position later ...
442 ret_off = m_stream->OnSysSeek(pos, wxFromStart);
443 ResetBuffer();
444 return ret_off;
445 }
446 case wxFromCurrent: {
447 diff = pos + GetIntPosition();
448
449 if ( (diff > last_access) || (diff < 0) ) {
450 ret_off = m_stream->OnSysSeek(pos, wxFromCurrent);
451 ResetBuffer();
452 return ret_off;
453 } else {
454 SetIntPosition(diff);
455 return pos;
456 }
457 }
458 case wxFromEnd:
459 // Hard to compute: always seek to the requested position.
460 ret_off = m_stream->OnSysSeek(pos, wxFromEnd);
461 ResetBuffer();
462 return ret_off;
463 }
464 return wxInvalidOffset;
465 }
466
467 off_t wxStreamBuffer::Tell() const
468 {
469 off_t pos;
470
471 if (m_flushable) {
472 pos = m_stream->OnSysTell();
473 if (pos == wxInvalidOffset)
474 return wxInvalidOffset;
475 return pos - GetLastAccess() + GetIntPosition();
476 } else
477 return GetIntPosition();
478 }
479
480 size_t wxStreamBuffer::GetDataLeft()
481 {
482 if (m_buffer_end == m_buffer_pos && m_flushable)
483 FillBuffer();
484 return m_buffer_end-m_buffer_pos;
485 }
486
487 // ----------------------------------------------------------------------------
488 // wxStreamBase
489 // ----------------------------------------------------------------------------
490
491 wxStreamBase::wxStreamBase()
492 {
493 m_lasterror = wxStream_NOERROR;
494 m_lastcount = 0;
495 }
496
497 wxStreamBase::~wxStreamBase()
498 {
499 }
500
501 size_t wxStreamBase::OnSysRead(void *WXUNUSED(buffer), size_t WXUNUSED(size))
502 {
503 return 0;
504 }
505
506 size_t wxStreamBase::OnSysWrite(const void *WXUNUSED(buffer), size_t WXUNUSED(bufsize))
507 {
508 return 0;
509 }
510
511 off_t wxStreamBase::OnSysSeek(off_t WXUNUSED(seek), wxSeekMode WXUNUSED(mode))
512 {
513 return wxInvalidOffset;
514 }
515
516 off_t wxStreamBase::OnSysTell() const
517 {
518 return wxInvalidOffset;
519 }
520
521 // ----------------------------------------------------------------------------
522 // wxInputStream
523 // ----------------------------------------------------------------------------
524
525 wxInputStream::wxInputStream()
526 : wxStreamBase()
527 {
528 m_i_destroybuf = TRUE;
529 m_i_streambuf = new wxStreamBuffer(*this, wxStreamBuffer::read);
530 }
531
532 wxInputStream::wxInputStream(wxStreamBuffer *buffer)
533 : wxStreamBase()
534 {
535 m_i_destroybuf = FALSE;
536 m_i_streambuf = buffer;
537 }
538
539 wxInputStream::~wxInputStream()
540 {
541 if (m_i_destroybuf)
542 delete m_i_streambuf;
543 }
544
545 char wxInputStream::GetC()
546 {
547 char c;
548 m_i_streambuf->Read(&c, 1);
549 return c;
550 }
551
552
553 wxString wxInputStream::ReadLine()
554 {
555 char c, last_endl = 0;
556 bool end_line = FALSE;
557 wxString line;
558
559 while (!end_line) {
560 c = GetC();
561 if (LastError() != wxStream_NOERROR)
562 break;
563
564 switch (c) {
565 case '\n':
566 end_line = TRUE;
567 break;
568 case '\r':
569 last_endl = '\r';
570 break;
571 default:
572 if (last_endl == '\r') {
573 end_line = TRUE;
574 InputStreamBuffer()->WriteBack(c);
575 break;
576 }
577 line += c;
578 break;
579 }
580 }
581 return line;
582 }
583
584 wxInputStream& wxInputStream::Read(void *buffer, size_t size)
585 {
586 m_i_streambuf->Read(buffer, size);
587 // wxStreamBuffer sets all variables for us
588 return *this;
589 }
590
591 char wxInputStream::Peek()
592 {
593 m_i_streambuf->GetDataLeft();
594
595 return *(m_i_streambuf->GetBufferPos());
596 }
597
598
599 wxInputStream& wxInputStream::Read(wxOutputStream& stream_out)
600 {
601 char buf[BUF_TEMP_SIZE];
602 size_t bytes_read = BUF_TEMP_SIZE;
603
604 while (bytes_read == BUF_TEMP_SIZE) {
605 bytes_read = Read(buf, bytes_read).LastRead();
606 bytes_read = stream_out.Write(buf, bytes_read).LastWrite();
607 }
608 return *this;
609 }
610
611 off_t wxInputStream::SeekI(off_t pos, wxSeekMode mode)
612 {
613 return m_i_streambuf->Seek(pos, mode);
614 }
615
616 off_t wxInputStream::TellI() const
617 {
618 return m_i_streambuf->Tell();
619 }
620
621 // --------------------
622 // Overloaded operators
623 // --------------------
624
625 wxInputStream& wxInputStream::operator>>(wxString& line)
626 {
627 line = ReadLine();
628 return *this;
629 }
630
631 wxInputStream& wxInputStream::operator>>(char& c)
632 {
633 c = GetC();
634 return *this;
635 }
636
637 wxInputStream& wxInputStream::operator>>(signed short& i)
638 {
639 signed long l;
640
641 *this >> l;
642 i = (signed short)l;
643 return *this;
644 }
645
646 wxInputStream& wxInputStream::operator>>(signed int& i)
647 {
648 signed long l;
649
650 *this >> l;
651 i = (signed int)l;
652 return *this;
653 }
654
655 wxInputStream& wxInputStream::operator>>(signed long& i)
656 {
657 /* I only implemented a simple integer parser */
658 int c;
659 int sign;
660
661 while (isspace( c = GetC() ) )
662 /* Do nothing */ ;
663
664 i = 0;
665 if (! (c == '-' || isdigit(c)) ) {
666 InputStreamBuffer()->WriteBack(c);
667 return *this;
668 }
669
670 if (c == '-') {
671 sign = -1;
672 c = GetC();
673 } else if (c == '+') {
674 sign = 1;
675 c = GetC();
676 } else {
677 sign = 1;
678 }
679
680 while (isdigit(c)) {
681 i = i*10 + (c - (int)'0');
682 c = GetC();
683 }
684
685 i *= sign;
686
687 return *this;
688 }
689
690 wxInputStream& wxInputStream::operator>>(unsigned short& i)
691 {
692 unsigned long l;
693
694 *this >> l;
695 i = (unsigned short)l;
696 return *this;
697 }
698
699 wxInputStream& wxInputStream::operator>>(unsigned int& i)
700 {
701 unsigned long l;
702
703 *this >> l;
704 i = (unsigned int)l;
705 return *this;
706 }
707
708 wxInputStream& wxInputStream::operator>>(unsigned long& i)
709 {
710 /* I only implemented a simple integer parser */
711 int c;
712
713 while (isspace( c = GetC() ) )
714 /* Do nothing */ ;
715
716 i = 0;
717 if (!isdigit(c)) {
718 InputStreamBuffer()->WriteBack(c);
719 return *this;
720 }
721
722 while (isdigit(c)) {
723 i = i*10 + c - '0';
724 c = GetC();
725 }
726
727 return *this;
728 }
729
730 wxInputStream& wxInputStream::operator>>(double& f)
731 {
732 /* I only implemented a simple float parser */
733 int c, sign;
734
735 while (isspace( c = GetC() ) )
736 /* Do nothing */ ;
737
738 f = 0.0;
739 if (! (c == '-' || isdigit(c)) ) {
740 InputStreamBuffer()->WriteBack(c);
741 return *this;
742 }
743
744 if (c == '-') {
745 sign = -1;
746 c = GetC();
747 } else if (c == '+') {
748 sign = 1;
749 c = GetC();
750 } else {
751 sign = 1;
752 }
753
754 while (isdigit(c)) {
755 f = f*10 + (c - '0');
756 c = GetC();
757 }
758
759 if (c == '.') {
760 double f_multiplicator = (double) 0.1;
761 c = GetC();
762
763 while (isdigit(c)) {
764 f += (c-'0')*f_multiplicator;
765 f_multiplicator /= 10;
766 c = GetC();
767 }
768 }
769
770 f *= sign;
771
772 return *this;
773 }
774
775 #if wxUSE_SERIAL
776 wxInputStream& wxInputStream::operator>>(wxObject *& obj)
777 {
778 wxObjectInputStream obj_s(*this);
779 obj = obj_s.LoadObject();
780 return *this;
781 }
782 #endif
783
784
785 // ----------------------------------------------------------------------------
786 // wxOutputStream
787 // ----------------------------------------------------------------------------
788 wxOutputStream::wxOutputStream()
789 : wxStreamBase()
790 {
791 m_o_destroybuf = TRUE;
792 m_o_streambuf = new wxStreamBuffer(*this, wxStreamBuffer::write);
793 }
794
795 wxOutputStream::wxOutputStream(wxStreamBuffer *buffer)
796 : wxStreamBase()
797 {
798 m_o_destroybuf = FALSE;
799 m_o_streambuf = buffer;
800 }
801
802 wxOutputStream::~wxOutputStream()
803 {
804 if (m_o_destroybuf)
805 delete m_o_streambuf;
806 }
807
808 wxOutputStream& wxOutputStream::Write(const void *buffer, size_t size)
809 {
810 m_o_streambuf->Write(buffer, size);
811 return *this;
812 }
813
814 wxOutputStream& wxOutputStream::Write(wxInputStream& stream_in)
815 {
816 stream_in.Read(*this);
817 return *this;
818 }
819
820 off_t wxOutputStream::TellO() const
821 {
822 return m_o_streambuf->Tell();
823 }
824
825 off_t wxOutputStream::SeekO(off_t pos, wxSeekMode mode)
826 {
827 return m_o_streambuf->Seek(pos, mode);
828 }
829
830 void wxOutputStream::Sync()
831 {
832 m_o_streambuf->FlushBuffer();
833 }
834
835 void wxOutputStream::WriteLine(const wxString& line)
836 {
837 #ifdef __WXMSW__
838 wxString tmp_string = line + _T("\r\n");
839 #else
840 #ifdef __WXMAC__
841 wxString tmp_string = line + _T('\r');
842 #else
843 wxString tmp_string = line + _T('\n');
844 #endif
845 #endif
846
847 Write((const wxChar *) tmp_string, tmp_string.Length()*sizeof(wxChar));
848 }
849
850 wxOutputStream& wxOutputStream::operator<<(const char *string)
851 {
852 return Write(string, strlen(string));
853 }
854
855 wxOutputStream& wxOutputStream::operator<<(wxString& string)
856 {
857 #if wxUSE_UNICODE
858 const wxWX2MBbuf buf = string.mb_str();
859 return *this << buf;
860 #else
861 return Write(string, string.Len());
862 #endif
863 }
864
865 wxOutputStream& wxOutputStream::operator<<(char c)
866 {
867 return Write(&c, 1);
868 }
869
870 wxOutputStream& wxOutputStream::operator<<(signed short i)
871 {
872 signed long l = (signed long)i;
873 return *this << l;
874 }
875
876 wxOutputStream& wxOutputStream::operator<<(signed int i)
877 {
878 signed long l = (signed long)i;
879 return *this << l;
880 }
881
882 wxOutputStream& wxOutputStream::operator<<(signed long i)
883 {
884 wxString strlong;
885 strlong.Printf(_T("%ld"), i);
886 return *this << strlong;
887 }
888
889 wxOutputStream& wxOutputStream::operator<<(unsigned short i)
890 {
891 unsigned long l = (unsigned long)i;
892 return *this << l;
893 }
894
895 wxOutputStream& wxOutputStream::operator<<(unsigned int i)
896 {
897 unsigned long l = (unsigned long)i;
898 return *this << l;
899 }
900
901 wxOutputStream& wxOutputStream::operator<<(unsigned long i)
902 {
903 wxString strlong;
904 strlong.Printf(_T("%lu"), i);
905 return *this << strlong;
906 }
907
908 wxOutputStream& wxOutputStream::operator<<(double f)
909 {
910 wxString strfloat;
911
912 strfloat.Printf(_T("%f"), f);
913 return *this << strfloat;
914 }
915
916 #if wxUSE_SERIAL
917 wxOutputStream& wxOutputStream::operator<<(wxObject& obj)
918 {
919 wxObjectOutputStream obj_s(*this);
920 obj_s.SaveObject(obj);
921 return *this;
922 }
923 #endif
924
925 // ----------------------------------------------------------------------------
926 // wxFilterInputStream
927 // ----------------------------------------------------------------------------
928 wxFilterInputStream::wxFilterInputStream()
929 : wxInputStream(NULL)
930 {
931 // WARNING streambuf set to NULL !
932 }
933
934 wxFilterInputStream::wxFilterInputStream(wxInputStream& stream)
935 : wxInputStream(stream.InputStreamBuffer())
936 {
937 m_parent_i_stream = &stream;
938 }
939
940 wxFilterInputStream::~wxFilterInputStream()
941 {
942 }
943
944 // ----------------------------------------------------------------------------
945 // wxFilterOutputStream
946 // ----------------------------------------------------------------------------
947 wxFilterOutputStream::wxFilterOutputStream()
948 : wxOutputStream(NULL)
949 {
950 }
951
952 wxFilterOutputStream::wxFilterOutputStream(wxOutputStream& stream)
953 : wxOutputStream(stream.OutputStreamBuffer())
954 {
955 m_parent_o_stream = &stream;
956 }
957
958 wxFilterOutputStream::~wxFilterOutputStream()
959 {
960 }
961
962 // ----------------------------------------------------------------------------
963 // Some IOManip function
964 // ----------------------------------------------------------------------------
965
966 wxOutputStream& wxEndL(wxOutputStream& stream)
967 {
968 #ifdef __MSW__
969 return stream.Write("\r\n", 2);
970 #else
971 #ifdef __WXMAC__
972 return stream.Write("\r", 1);
973 #else
974 return stream.Write("\n", 1);
975 #endif
976 #endif
977 }
978
979 #endif
980 // wxUSE_STREAMS