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