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