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