* A few updates (stream doc)
[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() && !FlushBuffer()) {
255 CHECK_ERROR(wxStream_READ_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 // ------------------
289 // Buffering disabled
290 // ------------------
291
292 m_stream->m_lasterror = wxStream_NOERROR;
293 m_stream->m_lastcount = GetWBack((char *)buffer, size);
294 size -= m_stream->m_lastcount;
295 if (size == 0)
296 return m_stream->m_lastcount;
297
298 buffer = (void *)((char *)buffer+m_stream->m_lastcount);
299
300 if (!m_buffer_size)
301 return (m_stream->m_lastcount += m_stream->OnSysRead(buffer, size));
302
303 // -----------------
304 // Buffering enabled
305 // -----------------
306 size_t buf_left, orig_size = size;
307
308 while (size > 0) {
309 buf_left = GetDataLeft();
310
311 // First case: the requested buffer is larger than the stream buffer,
312 // we split it.
313 if (size > buf_left) {
314 GetFromBuffer(buffer, buf_left);
315 size -= buf_left;
316 buffer = (char *)buffer + buf_left; // ANSI C++ violation.
317
318 if (!FillBuffer()) {
319 CHECK_ERROR(wxStream_READ_ERR);
320 return (m_stream->m_lastcount = orig_size-size);
321 }
322 } else {
323
324 // Second case: we just copy from the stream buffer.
325 GetFromBuffer(buffer, size);
326 break;
327 }
328 }
329 return (m_stream->m_lastcount += orig_size);
330 }
331
332 size_t wxStreamBuffer::Read(wxStreamBuffer *s_buf)
333 {
334 char buf[BUF_TEMP_SIZE];
335 size_t s = 0, bytes_read = BUF_TEMP_SIZE;
336
337 while (bytes_read == BUF_TEMP_SIZE) {
338 bytes_read = Read(buf, bytes_read);
339 bytes_read = s_buf->Write(buf, bytes_read);
340 s += bytes_read;
341 }
342 return s;
343 }
344
345 size_t wxStreamBuffer::Write(const void *buffer, size_t size)
346 {
347 wxASSERT(m_stream != NULL);
348
349 // ------------------
350 // Buffering disabled
351 // ------------------
352
353 m_stream->m_lasterror = wxStream_NOERROR;
354 if (!m_buffer_size)
355 return (m_stream->m_lastcount = m_stream->OnSysWrite(buffer, size));
356
357 // ------------------
358 // Buffering enabled
359 // ------------------
360
361 size_t buf_left, orig_size = size;
362
363 while (size > 0) {
364 buf_left = m_buffer_end - m_buffer_pos;
365
366 // First case: the buffer to write is larger than the stream buffer,
367 // we split it
368 if (size > buf_left) {
369 PutToBuffer(buffer, buf_left);
370 size -= buf_left;
371 buffer = (char *)buffer + buf_left; // ANSI C++ violation.
372
373 if (!FlushBuffer()) {
374 CHECK_ERROR(wxStream_WRITE_ERR);
375 return (m_stream->m_lastcount = orig_size-size);
376 }
377
378 m_buffer_pos = m_buffer_start;
379
380 } else {
381
382 // Second case: just copy it in the stream buffer.
383 PutToBuffer(buffer, size);
384 break;
385 }
386 }
387 return (m_stream->m_lastcount = orig_size);
388 }
389
390 size_t wxStreamBuffer::Write(wxStreamBuffer *sbuf)
391 {
392 char buf[BUF_TEMP_SIZE];
393 size_t s = 0, bytes_count = BUF_TEMP_SIZE, b_count2;
394
395 while (bytes_count == BUF_TEMP_SIZE) {
396 b_count2 = sbuf->Read(buf, bytes_count);
397 bytes_count = Write(buf, b_count2);
398 if (b_count2 > bytes_count)
399 sbuf->WriteBack(buf+bytes_count, b_count2-bytes_count);
400 s += bytes_count;
401 }
402 return s;
403 }
404
405 off_t wxStreamBuffer::Seek(off_t pos, wxSeekMode mode)
406 {
407 off_t ret_off, diff, last_access;
408
409 last_access = GetLastAccess();
410
411 if (!m_flushable) {
412 diff = pos + GetIntPosition();
413 if (diff < 0 || diff > last_access)
414 return wxInvalidOffset;
415 SetIntPosition(diff);
416 return diff;
417 }
418
419 switch (mode) {
420 case wxFromStart: {
421 // We'll try to compute an internal position later ...
422 ret_off = m_stream->OnSysSeek(pos, wxFromStart);
423 ResetBuffer();
424 return ret_off;
425 }
426 case wxFromCurrent: {
427 diff = pos + GetIntPosition();
428
429 if ( (diff > last_access) || (diff < 0) ) {
430 ret_off = m_stream->OnSysSeek(pos, wxFromCurrent);
431 ResetBuffer();
432 return ret_off;
433 } else {
434 SetIntPosition(diff);
435 return pos;
436 }
437 }
438 case wxFromEnd:
439 // Hard to compute: always seek to the requested position.
440 ret_off = m_stream->OnSysSeek(pos, wxFromEnd);
441 ResetBuffer();
442 return ret_off;
443 }
444 return wxInvalidOffset;
445 }
446
447 off_t wxStreamBuffer::Tell() const
448 {
449 off_t pos;
450
451 if (m_flushable) {
452 pos = m_stream->OnSysTell();
453 if (pos == wxInvalidOffset)
454 return wxInvalidOffset;
455 return pos - GetLastAccess() + GetIntPosition();
456 } else
457 return GetIntPosition();
458 }
459
460 size_t wxStreamBuffer::GetDataLeft()
461 {
462 if (m_buffer_end == m_buffer_pos && m_flushable)
463 FillBuffer();
464 return m_buffer_end-m_buffer_pos;
465 }
466
467 // ----------------------------------------------------------------------------
468 // wxStreamBase
469 // ----------------------------------------------------------------------------
470
471 wxStreamBase::wxStreamBase()
472 {
473 m_lasterror = wxStream_NOERROR;
474 m_lastcount = 0;
475 }
476
477 wxStreamBase::~wxStreamBase()
478 {
479 }
480
481 size_t wxStreamBase::OnSysRead(void *WXUNUSED(buffer), size_t WXUNUSED(size))
482 {
483 return 0;
484 }
485
486 size_t wxStreamBase::OnSysWrite(const void *WXUNUSED(buffer), size_t WXUNUSED(bufsize))
487 {
488 return 0;
489 }
490
491 off_t wxStreamBase::OnSysSeek(off_t WXUNUSED(seek), wxSeekMode WXUNUSED(mode))
492 {
493 return wxInvalidOffset;
494 }
495
496 off_t wxStreamBase::OnSysTell() const
497 {
498 return wxInvalidOffset;
499 }
500
501 // ----------------------------------------------------------------------------
502 // wxInputStream
503 // ----------------------------------------------------------------------------
504
505 wxInputStream::wxInputStream()
506 : wxStreamBase()
507 {
508 m_i_destroybuf = TRUE;
509 m_i_streambuf = new wxStreamBuffer(*this, wxStreamBuffer::read);
510 }
511
512 wxInputStream::wxInputStream(wxStreamBuffer *buffer)
513 : wxStreamBase()
514 {
515 m_i_destroybuf = FALSE;
516 m_i_streambuf = buffer;
517 }
518
519 wxInputStream::~wxInputStream()
520 {
521 if (m_i_destroybuf)
522 delete m_i_streambuf;
523 }
524
525 char wxInputStream::GetC()
526 {
527 char c;
528 m_i_streambuf->Read(&c, 1);
529 return c;
530 }
531
532 wxInputStream& wxInputStream::Read(void *buffer, size_t size)
533 {
534 m_i_streambuf->Read(buffer, size);
535 // wxStreamBuffer sets all variables for us
536 return *this;
537 }
538
539 char wxInputStream::Peek()
540 {
541 m_i_streambuf->GetDataLeft();
542
543 return *(m_i_streambuf->GetBufferPos());
544 }
545
546
547 wxInputStream& wxInputStream::Read(wxOutputStream& stream_out)
548 {
549 char buf[BUF_TEMP_SIZE];
550 size_t bytes_read = BUF_TEMP_SIZE;
551
552 while (bytes_read == BUF_TEMP_SIZE) {
553 bytes_read = Read(buf, bytes_read).LastRead();
554 bytes_read = stream_out.Write(buf, bytes_read).LastWrite();
555 }
556 return *this;
557 }
558
559 off_t wxInputStream::SeekI(off_t pos, wxSeekMode mode)
560 {
561 return m_i_streambuf->Seek(pos, mode);
562 }
563
564 off_t wxInputStream::TellI() const
565 {
566 return m_i_streambuf->Tell();
567 }
568
569 // --------------------
570 // Overloaded operators
571 // --------------------
572
573 wxInputStream& wxInputStream::operator>>(wxString& line)
574 {
575 wxDataInputStream s(*this);
576
577 line = s.ReadLine();
578 return *this;
579 }
580
581 wxInputStream& wxInputStream::operator>>(char& c)
582 {
583 c = GetC();
584 return *this;
585 }
586
587 wxInputStream& wxInputStream::operator>>(short& i)
588 {
589 long l;
590
591 *this >> l;
592 i = (short)l;
593 return *this;
594 }
595
596 wxInputStream& wxInputStream::operator>>(int& i)
597 {
598 long l;
599
600 *this >> l;
601 i = (short)l;
602 return *this;
603 }
604
605 wxInputStream& wxInputStream::operator>>(long& i)
606 {
607 /* I only implemented a simple integer parser */
608 int c, sign;
609
610 while (isspace( c = GetC() ) )
611 /* Do nothing */ ;
612
613 i = 0;
614 if (! (c == '-' || isdigit(c)) ) {
615 InputStreamBuffer()->WriteBack(c);
616 return *this;
617 }
618
619 if (c == '-') {
620 sign = -1;
621 c = GetC();
622 } else
623 sign = 1;
624
625 while (isdigit(c)) {
626 i = i*10 + c;
627 c = GetC();
628 }
629
630 i *= sign;
631
632 return *this;
633 }
634
635 wxInputStream& wxInputStream::operator>>(double& f)
636 {
637 /* I only implemented a simple float parser */
638 int c, sign;
639
640 while (isspace( c = GetC() ) )
641 /* Do nothing */ ;
642
643 f = 0.0;
644 if (! (c == '-' || isdigit(c)) ) {
645 InputStreamBuffer()->WriteBack(c);
646 return *this;
647 }
648
649 if (c == '-') {
650 sign = -1;
651 c = GetC();
652 } else
653 sign = 1;
654
655 while (isdigit(c)) {
656 f = f*10 + (c - '0');
657 c = GetC();
658 }
659
660 if (c == '.') {
661 double f_multiplicator = (double) 0.1;
662 c = GetC();
663
664 while (isdigit(c)) {
665 f += (c-'0')*f_multiplicator;
666 f_multiplicator /= 10;
667 c = GetC();
668 }
669 }
670
671 f *= sign;
672
673 return *this;
674 }
675
676 #if wxUSE_SERIAL
677 wxInputStream& wxInputStream::operator>>(wxObject *& obj)
678 {
679 wxObjectInputStream obj_s(*this);
680 obj = obj_s.LoadObject();
681 return *this;
682 }
683 #endif
684
685
686 // ----------------------------------------------------------------------------
687 // wxOutputStream
688 // ----------------------------------------------------------------------------
689 wxOutputStream::wxOutputStream()
690 : wxStreamBase()
691 {
692 m_o_destroybuf = TRUE;
693 m_o_streambuf = new wxStreamBuffer(*this, wxStreamBuffer::write);
694 }
695
696 wxOutputStream::wxOutputStream(wxStreamBuffer *buffer)
697 : wxStreamBase()
698 {
699 m_o_destroybuf = FALSE;
700 m_o_streambuf = buffer;
701 }
702
703 wxOutputStream::~wxOutputStream()
704 {
705 if (m_o_destroybuf)
706 delete m_o_streambuf;
707 }
708
709 wxOutputStream& wxOutputStream::Write(const void *buffer, size_t size)
710 {
711 m_o_streambuf->Write(buffer, size);
712 return *this;
713 }
714
715 wxOutputStream& wxOutputStream::Write(wxInputStream& stream_in)
716 {
717 stream_in.Read(*this);
718 return *this;
719 }
720
721 off_t wxOutputStream::TellO() const
722 {
723 return m_o_streambuf->Tell();
724 }
725
726 off_t wxOutputStream::SeekO(off_t pos, wxSeekMode mode)
727 {
728 return m_o_streambuf->Seek(pos, mode);
729 }
730
731 void wxOutputStream::Sync()
732 {
733 m_o_streambuf->FlushBuffer();
734 }
735
736 wxOutputStream& wxOutputStream::operator<<(const char *string)
737 {
738 return Write(string, strlen(string));
739 }
740
741 wxOutputStream& wxOutputStream::operator<<(wxString& string)
742 {
743 return Write(string, string.Len());
744 }
745
746 wxOutputStream& wxOutputStream::operator<<(char c)
747 {
748 return Write(&c, 1);
749 }
750
751 wxOutputStream& wxOutputStream::operator<<(short i)
752 {
753 wxString strint;
754
755 strint.Printf("%i", i);
756 return Write(strint, strint.Len());
757 }
758
759 wxOutputStream& wxOutputStream::operator<<(int i)
760 {
761 wxString strint;
762
763 strint.Printf("%i", i);
764 return Write(strint, strint.Len());
765 }
766
767 wxOutputStream& wxOutputStream::operator<<(long i)
768 {
769 wxString strlong;
770
771 strlong.Printf("%i", i);
772 return Write((const char *)strlong, strlong.Len());
773 }
774
775 wxOutputStream& wxOutputStream::operator<<(double f)
776 {
777 wxString strfloat;
778
779 strfloat.Printf("%f", f);
780 return Write(strfloat, strfloat.Len());
781 }
782
783 #if wxUSE_SERIAL
784 wxOutputStream& wxOutputStream::operator<<(wxObject& obj)
785 {
786 wxObjectOutputStream obj_s(*this);
787 obj_s.SaveObject(obj);
788 return *this;
789 }
790 #endif
791
792 // ----------------------------------------------------------------------------
793 // wxFilterInputStream
794 // ----------------------------------------------------------------------------
795 wxFilterInputStream::wxFilterInputStream()
796 : wxInputStream(NULL)
797 {
798 // WARNING streambuf set to NULL !
799 }
800
801 wxFilterInputStream::wxFilterInputStream(wxInputStream& stream)
802 : wxInputStream(stream.InputStreamBuffer())
803 {
804 m_parent_i_stream = &stream;
805 }
806
807 wxFilterInputStream::~wxFilterInputStream()
808 {
809 }
810
811 // ----------------------------------------------------------------------------
812 // wxFilterOutputStream
813 // ----------------------------------------------------------------------------
814 wxFilterOutputStream::wxFilterOutputStream()
815 : wxOutputStream(NULL)
816 {
817 }
818
819 wxFilterOutputStream::wxFilterOutputStream(wxOutputStream& stream)
820 : wxOutputStream(stream.OutputStreamBuffer())
821 {
822 m_parent_o_stream = &stream;
823 }
824
825 wxFilterOutputStream::~wxFilterOutputStream()
826 {
827 }
828
829 // ----------------------------------------------------------------------------
830 // Some IOManip function
831 // ----------------------------------------------------------------------------
832
833 wxOutputStream& wxEndL(wxOutputStream& stream)
834 {
835 #ifdef __MSW__
836 return stream.Write("\r\n", 2);
837 #else
838 return stream.Write("\n", 1);
839 #endif
840 }