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