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