]>
git.saurik.com Git - wxWidgets.git/blob - src/common/stream.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxStream base classes
4 // Author: Guilhem Lavaux
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "stream.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
30 #include "wx/stream.h"
31 #include "wx/datstrm.h"
32 #include "wx/objstrm.h"
34 #define BUF_TEMP_SIZE 10000
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
40 #define CHECK_ERROR(err) \
41 if (m_stream->m_lasterror == wxStream_NOERROR) \
42 m_stream->m_lasterror = err
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
)
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
)
56 m_stream
= new wxStreamBase();
59 wxStreamBuffer::wxStreamBuffer(const wxStreamBuffer
& buffer
)
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
;
70 m_destroystream
= FALSE
;
73 wxStreamBuffer::~wxStreamBuffer()
76 wxDELETEA(m_buffer_start
);
81 void wxStreamBuffer::SetBufferIO(char *buffer_start
, char *buffer_end
)
84 wxDELETEA(m_buffer_start
);
85 m_buffer_start
= buffer_start
;
86 m_buffer_end
= buffer_end
;
88 m_buffer_size
= m_buffer_end
-m_buffer_start
;
93 void wxStreamBuffer::SetBufferIO(size_t bufsize
)
98 wxDELETEA(m_buffer_start
);
101 m_buffer_start
= NULL
;
108 b_start
= new char[bufsize
];
109 SetBufferIO(b_start
, b_start
+ bufsize
);
113 void wxStreamBuffer::ResetBuffer()
115 m_stream
->m_lasterror
= wxStream_NOERROR
;
116 m_stream
->m_lastcount
= 0;
117 if (m_mode
== read
&& m_flushable
)
118 m_buffer_pos
= m_buffer_end
;
120 m_buffer_pos
= m_buffer_start
;
123 bool wxStreamBuffer::FillBuffer()
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
;
136 bool wxStreamBuffer::FlushBuffer()
138 size_t count
, current
;
140 if (m_buffer_pos
== m_buffer_start
|| !m_flushable
)
143 current
= m_buffer_pos
-m_buffer_start
;
144 count
= m_stream
->OnSysWrite(m_buffer_start
, current
);
145 if (count
!= current
)
147 m_buffer_pos
= m_buffer_start
;
152 void wxStreamBuffer::GetFromBuffer(void *buffer
, size_t size
)
154 size_t s_toget
= m_buffer_end
-m_buffer_pos
;
159 memcpy(buffer
, m_buffer_pos
, s_toget
);
160 m_buffer_pos
+= s_toget
;
163 void wxStreamBuffer::PutToBuffer(const void *buffer
, size_t size
)
165 size_t s_toput
= m_buffer_end
-m_buffer_pos
;
167 if (s_toput
< size
&& !m_fixed
) {
171 size_t delta
= m_buffer_pos
-m_buffer_start
;
173 m_buffer_start
= (char *)realloc(m_buffer_start
, m_buffer_size
+size
);
174 m_buffer_pos
= m_buffer_start
+ delta
;
176 m_buffer_size
+= size
;
177 m_buffer_end
= m_buffer_start
+m_buffer_size
;
183 memcpy(m_buffer_pos
, buffer
, s_toput
);
184 m_buffer_pos
+= s_toput
;
187 void wxStreamBuffer::PutChar(char c
)
189 wxASSERT(m_stream
!= NULL
);
191 if (!m_buffer_size
) {
192 m_stream
->OnSysWrite(&c
, 1);
196 if (GetDataLeft() == 0 && !FlushBuffer()) {
197 CHECK_ERROR(wxStream_WRITE_ERR
);
202 m_stream
->m_lastcount
= 1;
205 char wxStreamBuffer::Peek()
209 wxASSERT(m_stream
!= NULL
&& m_buffer_size
!= 0);
211 if (!GetDataLeft()) {
212 CHECK_ERROR(wxStream_READ_ERR
);
216 GetFromBuffer(&c
, 1);
222 char wxStreamBuffer::GetChar()
226 wxASSERT(m_stream
!= NULL
);
228 if (!m_buffer_size
) {
229 m_stream
->OnSysRead(&c
, 1);
233 if (!GetDataLeft()) {
234 CHECK_ERROR(wxStream_READ_ERR
);
238 GetFromBuffer(&c
, 1);
240 m_stream
->m_lastcount
= 1;
244 size_t wxStreamBuffer::Read(void *buffer
, size_t size
)
246 wxASSERT(m_stream
!= NULL
);
251 // ------------------
252 // Buffering disabled
253 // ------------------
255 m_stream
->m_lasterror
= wxStream_NOERROR
;
257 return (m_stream
->m_lastcount
+= m_stream
->OnSysRead(buffer
, size
));
262 size_t buf_left
, orig_size
= size
;
265 buf_left
= GetDataLeft();
267 // First case: the requested buffer is larger than the stream buffer,
269 if (size
> buf_left
) {
270 GetFromBuffer(buffer
, buf_left
);
272 buffer
= (char *)buffer
+ buf_left
; // ANSI C++ violation.
275 CHECK_ERROR(wxStream_EOF
);
276 return (m_stream
->m_lastcount
= orig_size
-size
);
280 // Second case: we just copy from the stream buffer.
281 GetFromBuffer(buffer
, size
);
285 return (m_stream
->m_lastcount
+= orig_size
);
288 size_t wxStreamBuffer::Read(wxStreamBuffer
*s_buf
)
290 char buf
[BUF_TEMP_SIZE
];
291 size_t s
= 0, bytes_read
= BUF_TEMP_SIZE
;
296 while (bytes_read
!= 0) {
297 bytes_read
= Read(buf
, bytes_read
);
298 bytes_read
= s_buf
->Write(buf
, bytes_read
);
304 size_t wxStreamBuffer::Write(const void *buffer
, size_t size
)
306 wxASSERT(m_stream
!= NULL
);
311 // ------------------
312 // Buffering disabled
313 // ------------------
315 m_stream
->m_lasterror
= wxStream_NOERROR
;
316 if (!m_buffer_size
&& m_fixed
)
317 return (m_stream
->m_lastcount
= m_stream
->OnSysWrite(buffer
, size
));
319 // ------------------
321 // ------------------
323 size_t buf_left
, orig_size
= size
;
326 buf_left
= m_buffer_end
- m_buffer_pos
;
328 // First case: the buffer to write is larger than the stream buffer,
330 // NB: If stream buffer isn't fixed (as for wxMemoryOutputStream),
331 // we always go to the second case.
332 if (size
> buf_left
&& m_fixed
) {
333 PutToBuffer(buffer
, buf_left
);
335 buffer
= (char *)buffer
+ buf_left
; // ANSI C++ violation.
337 if (!FlushBuffer()) {
338 CHECK_ERROR(wxStream_WRITE_ERR
);
339 return (m_stream
->m_lastcount
= orig_size
-size
);
342 m_buffer_pos
= m_buffer_start
;
346 // Second case: just copy it in the stream buffer.
347 PutToBuffer(buffer
, size
);
351 return (m_stream
->m_lastcount
= orig_size
);
354 size_t wxStreamBuffer::Write(wxStreamBuffer
*sbuf
)
356 char buf
[BUF_TEMP_SIZE
];
357 size_t s
= 0, bytes_count
= BUF_TEMP_SIZE
, b_count2
;
358 wxInputStream
*in_stream
;
363 in_stream
= (wxInputStream
*)sbuf
->Stream();
365 while (bytes_count
== BUF_TEMP_SIZE
) {
366 b_count2
= sbuf
->Read(buf
, bytes_count
);
367 bytes_count
= Write(buf
, b_count2
);
368 if (b_count2
> bytes_count
)
369 in_stream
->Ungetch(buf
+bytes_count
, b_count2
-bytes_count
);
375 off_t
wxStreamBuffer::Seek(off_t pos
, wxSeekMode mode
)
377 off_t ret_off
, diff
, last_access
;
379 last_access
= GetLastAccess();
382 diff
= pos
+ GetIntPosition();
383 if (diff
< 0 || diff
> last_access
)
384 return wxInvalidOffset
;
385 SetIntPosition(diff
);
391 // We'll try to compute an internal position later ...
392 ret_off
= m_stream
->OnSysSeek(pos
, wxFromStart
);
396 case wxFromCurrent
: {
397 diff
= pos
+ GetIntPosition();
399 if ( (diff
> last_access
) || (diff
< 0) ) {
400 // We must take into account the fact that we have read something
402 ret_off
= m_stream
->OnSysSeek(diff
-last_access
, wxFromCurrent
);
406 SetIntPosition(diff
);
411 // Hard to compute: always seek to the requested position.
412 ret_off
= m_stream
->OnSysSeek(pos
, wxFromEnd
);
416 return wxInvalidOffset
;
419 off_t
wxStreamBuffer::Tell() const
424 pos
= m_stream
->OnSysTell();
425 if (pos
== wxInvalidOffset
)
426 return wxInvalidOffset
;
427 return pos
- GetLastAccess() + GetIntPosition();
429 return GetIntPosition();
432 size_t wxStreamBuffer::GetDataLeft()
434 if (m_buffer_end
== m_buffer_pos
&& m_flushable
)
436 return m_buffer_end
-m_buffer_pos
;
439 // ----------------------------------------------------------------------------
441 // ----------------------------------------------------------------------------
443 wxStreamBase::wxStreamBase()
445 m_lasterror
= wxStream_NOERROR
;
449 wxStreamBase::~wxStreamBase()
453 size_t wxStreamBase::OnSysRead(void *WXUNUSED(buffer
), size_t WXUNUSED(size
))
458 size_t wxStreamBase::OnSysWrite(const void *WXUNUSED(buffer
), size_t WXUNUSED(bufsize
))
463 off_t
wxStreamBase::OnSysSeek(off_t
WXUNUSED(seek
), wxSeekMode
WXUNUSED(mode
))
465 return wxInvalidOffset
;
468 off_t
wxStreamBase::OnSysTell() const
470 return wxInvalidOffset
;
473 // ----------------------------------------------------------------------------
475 // ----------------------------------------------------------------------------
477 wxInputStream::wxInputStream()
479 m_wback(NULL
), m_wbacksize(0), m_wbackcur(0)
483 wxInputStream::~wxInputStream()
489 char *wxInputStream::AllocSpaceWBack(size_t needed_size
)
494 old_size
= m_wbacksize
;
495 m_wbacksize
+= needed_size
;
498 temp_b
= (char *)malloc(m_wbacksize
);
500 temp_b
= (char *)realloc(m_wback
, m_wbacksize
);
506 memmove(m_wback
+needed_size
, m_wback
, old_size
);
508 return (char *)(m_wback
);
511 size_t wxInputStream::GetWBack(char *buf
, size_t bsize
)
513 size_t s_toget
= m_wbacksize
-m_wbackcur
;
521 memcpy(buf
, (m_wback
+m_wbackcur
), s_toget
);
523 m_wbackcur
+= s_toget
;
524 if (m_wbackcur
== m_wbacksize
) {
526 m_wback
= (char *)NULL
;
534 size_t wxInputStream::Ungetch(const void *buf
, size_t bufsize
)
538 ptrback
= AllocSpaceWBack(bufsize
);
542 memcpy(ptrback
, buf
, bufsize
);
546 bool wxInputStream::Ungetch(char c
)
550 ptrback
= AllocSpaceWBack(1);
558 char wxInputStream::GetC()
565 wxInputStream
& wxInputStream::Read(void *buffer
, size_t size
)
568 char *buf
= (char *)buffer
;
570 retsize
= GetWBack(buf
, size
);
571 if (retsize
== size
) {
573 m_lasterror
= wxStream_NOERROR
;
579 m_lastcount
= OnSysRead(buf
, size
);
583 char wxInputStream::Peek()
588 if (m_lasterror
== wxStream_NOERROR
) {
595 wxInputStream
& wxInputStream::Read(wxOutputStream
& stream_out
)
597 char buf
[BUF_TEMP_SIZE
];
598 size_t bytes_read
= BUF_TEMP_SIZE
;
600 while (bytes_read
== BUF_TEMP_SIZE
) {
601 bytes_read
= Read(buf
, bytes_read
).LastRead();
602 bytes_read
= stream_out
.Write(buf
, bytes_read
).LastWrite();
607 off_t
wxInputStream::SeekI(off_t pos
, wxSeekMode mode
)
609 //should be check and improve, just to remove a slight bug !
610 // I don't know whether it should be put as well in wxFileInputStream::OnSysSeek ?
611 if (m_lasterror
==wxSTREAM_EOF
) m_lasterror
=wxSTREAM_NOERROR
;
613 return OnSysSeek(pos
, mode
);
616 off_t
wxInputStream::TellI() const
621 // --------------------
622 // Overloaded operators
623 // --------------------
626 wxInputStream
& wxInputStream::operator>>(wxObject
*& obj
)
628 wxObjectInputStream
obj_s(*this);
629 obj
= obj_s
.LoadObject();
635 // ----------------------------------------------------------------------------
637 // ----------------------------------------------------------------------------
638 wxOutputStream::wxOutputStream()
643 wxOutputStream::~wxOutputStream()
647 wxOutputStream
& wxOutputStream::Write(const void *buffer
, size_t size
)
649 m_lastcount
= OnSysWrite(buffer
, size
);
653 wxOutputStream
& wxOutputStream::Write(wxInputStream
& stream_in
)
655 stream_in
.Read(*this);
659 off_t
wxOutputStream::TellO() const
664 off_t
wxOutputStream::SeekO(off_t pos
, wxSeekMode mode
)
666 return OnSysSeek(pos
, mode
);
669 void wxOutputStream::Sync()
674 wxOutputStream
& wxOutputStream::operator<<(wxObject
& obj
)
676 wxObjectOutputStream
obj_s(*this);
677 obj_s
.SaveObject(obj
);
682 // ----------------------------------------------------------------------------
683 // wxCountingOutputStream
684 // ----------------------------------------------------------------------------
686 wxCountingOutputStream::wxCountingOutputStream ()
692 size_t wxCountingOutputStream::GetSize() const
697 size_t wxCountingOutputStream::OnSysWrite(const void *WXUNUSED(buffer
), size_t size
)
699 m_currentPos
+= size
;
700 if (m_currentPos
> m_lastcount
) m_lastcount
= m_currentPos
;
704 off_t
wxCountingOutputStream::OnSysSeek(off_t pos
, wxSeekMode mode
)
706 if (mode
== wxFromStart
)
710 if (mode
== wxFromEnd
)
712 m_currentPos
= m_lastcount
+ pos
;
718 if (m_currentPos
> m_lastcount
) m_lastcount
= m_currentPos
;
720 return m_currentPos
; // ?
723 off_t
wxCountingOutputStream::OnSysTell() const
725 return m_currentPos
; // ?
728 // ----------------------------------------------------------------------------
729 // wxFilterInputStream
730 // ----------------------------------------------------------------------------
732 wxFilterInputStream::wxFilterInputStream()
737 wxFilterInputStream::wxFilterInputStream(wxInputStream
& stream
)
740 m_parent_i_stream
= &stream
;
743 wxFilterInputStream::~wxFilterInputStream()
747 // ----------------------------------------------------------------------------
748 // wxFilterOutputStream
749 // ----------------------------------------------------------------------------
750 wxFilterOutputStream::wxFilterOutputStream()
755 wxFilterOutputStream::wxFilterOutputStream(wxOutputStream
& stream
)
758 m_parent_o_stream
= &stream
;
761 wxFilterOutputStream::~wxFilterOutputStream()
765 // ----------------------------------------------------------------------------
766 // wxBufferedInputStream
767 // ----------------------------------------------------------------------------
768 wxBufferedInputStream::wxBufferedInputStream(wxInputStream
& s
)
769 : wxFilterInputStream(s
)
771 m_i_streambuf
= new wxStreamBuffer(*this, wxStreamBuffer::read
);
772 m_i_streambuf
->SetBufferIO(1024);
775 wxBufferedInputStream::~wxBufferedInputStream()
777 delete m_i_streambuf
;
780 char wxBufferedInputStream::Peek()
782 return m_i_streambuf
->Peek();
785 wxInputStream
& wxBufferedInputStream::Read(void *buffer
, size_t size
)
788 char *buf
= (char *)buffer
;
790 retsize
= GetWBack(buf
, size
);
791 m_lastcount
= retsize
;
792 if (retsize
== size
) {
793 m_lasterror
= wxStream_NOERROR
;
799 m_i_streambuf
->Read(buf
, size
);
804 off_t
wxBufferedInputStream::SeekI(off_t pos
, wxSeekMode mode
)
806 return m_i_streambuf
->Seek(pos
, mode
);
809 off_t
wxBufferedInputStream::TellI() const
811 return m_i_streambuf
->Tell();
814 size_t wxBufferedInputStream::OnSysRead(void *buffer
, size_t bufsize
)
816 return m_parent_i_stream
->Read(buffer
, bufsize
).LastRead();
819 off_t
wxBufferedInputStream::OnSysSeek(off_t seek
, wxSeekMode mode
)
821 return m_parent_i_stream
->SeekI(seek
, mode
);
824 off_t
wxBufferedInputStream::OnSysTell() const
826 return m_parent_i_stream
->TellI();
829 // ----------------------------------------------------------------------------
830 // wxBufferedOutputStream
831 // ----------------------------------------------------------------------------
833 wxBufferedOutputStream::wxBufferedOutputStream(wxOutputStream
& s
)
834 : wxFilterOutputStream(s
)
836 m_o_streambuf
= new wxStreamBuffer(*this, wxStreamBuffer::write
);
837 m_o_streambuf
->SetBufferIO(1024);
840 wxBufferedOutputStream::~wxBufferedOutputStream()
842 delete m_o_streambuf
;
845 wxOutputStream
& wxBufferedOutputStream::Write(const void *buffer
, size_t size
)
848 m_o_streambuf
->Write(buffer
, size
);
852 off_t
wxBufferedOutputStream::SeekO(off_t pos
, wxSeekMode mode
)
854 return m_o_streambuf
->Seek(pos
, mode
);
857 off_t
wxBufferedOutputStream::TellO() const
859 return m_o_streambuf
->Tell();
862 void wxBufferedOutputStream::Sync()
864 m_o_streambuf
->FlushBuffer();
865 m_parent_o_stream
->Sync();
868 size_t wxBufferedOutputStream::OnSysWrite(const void *buffer
, size_t bufsize
)
870 return m_parent_o_stream
->Write(buffer
, bufsize
).LastWrite();
873 off_t
wxBufferedOutputStream::OnSysSeek(off_t seek
, wxSeekMode mode
)
875 return m_parent_o_stream
->SeekO(seek
, mode
);
878 off_t
wxBufferedOutputStream::OnSysTell() const
880 return m_parent_o_stream
->TellO();
883 // ----------------------------------------------------------------------------
884 // Some IOManip function
885 // ----------------------------------------------------------------------------
887 wxOutputStream
& wxEndL(wxOutputStream
& stream
)
890 return stream
.Write("\r\n", 2);
893 return stream
.Write("\r", 1);
895 return stream
.Write("\n", 1);