]>
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
= (char*)NULL
;
102 m_buffer_end
= (char*)NULL
;
103 m_buffer_pos
= (char*)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();
383 case wxFromStart
: diff
= pos
; break;
384 case wxFromCurrent
: diff
= pos
+ GetIntPosition(); break;
385 case wxFromEnd
: diff
= pos
+ last_access
; break;
386 default: return wxInvalidOffset
;
388 if (diff
< 0 || diff
> last_access
)
389 return wxInvalidOffset
;
390 SetIntPosition(diff
);
396 // We'll try to compute an internal position later ...
397 ret_off
= m_stream
->OnSysSeek(pos
, wxFromStart
);
401 case wxFromCurrent
: {
402 diff
= pos
+ GetIntPosition();
404 if ( (diff
> last_access
) || (diff
< 0) ) {
405 // We must take into account the fact that we have read something
407 ret_off
= m_stream
->OnSysSeek(diff
-last_access
, wxFromCurrent
);
411 SetIntPosition(diff
);
416 // Hard to compute: always seek to the requested position.
417 ret_off
= m_stream
->OnSysSeek(pos
, wxFromEnd
);
421 return wxInvalidOffset
;
424 off_t
wxStreamBuffer::Tell() const
429 pos
= m_stream
->OnSysTell();
430 if (pos
== wxInvalidOffset
)
431 return wxInvalidOffset
;
432 return pos
- GetLastAccess() + GetIntPosition();
434 return GetIntPosition();
437 size_t wxStreamBuffer::GetDataLeft()
439 if (m_buffer_end
== m_buffer_pos
&& m_flushable
)
441 return m_buffer_end
-m_buffer_pos
;
444 // ----------------------------------------------------------------------------
446 // ----------------------------------------------------------------------------
448 wxStreamBase::wxStreamBase()
450 m_lasterror
= wxStream_NOERROR
;
454 wxStreamBase::~wxStreamBase()
458 size_t wxStreamBase::OnSysRead(void *WXUNUSED(buffer
), size_t WXUNUSED(size
))
463 size_t wxStreamBase::OnSysWrite(const void *WXUNUSED(buffer
), size_t WXUNUSED(bufsize
))
468 off_t
wxStreamBase::OnSysSeek(off_t
WXUNUSED(seek
), wxSeekMode
WXUNUSED(mode
))
470 return wxInvalidOffset
;
473 off_t
wxStreamBase::OnSysTell() const
475 return wxInvalidOffset
;
478 // ----------------------------------------------------------------------------
480 // ----------------------------------------------------------------------------
482 wxInputStream::wxInputStream()
484 m_wback(NULL
), m_wbacksize(0), m_wbackcur(0)
488 wxInputStream::~wxInputStream()
494 char *wxInputStream::AllocSpaceWBack(size_t needed_size
)
499 old_size
= m_wbacksize
;
500 m_wbacksize
+= needed_size
;
503 temp_b
= (char *)malloc(m_wbacksize
);
505 temp_b
= (char *)realloc(m_wback
, m_wbacksize
);
511 memmove(m_wback
+needed_size
, m_wback
, old_size
);
513 return (char *)(m_wback
);
516 size_t wxInputStream::GetWBack(char *buf
, size_t bsize
)
518 size_t s_toget
= m_wbacksize
-m_wbackcur
;
526 memcpy(buf
, (m_wback
+m_wbackcur
), s_toget
);
528 m_wbackcur
+= s_toget
;
529 if (m_wbackcur
== m_wbacksize
) {
531 m_wback
= (char *)NULL
;
539 size_t wxInputStream::Ungetch(const void *buf
, size_t bufsize
)
543 ptrback
= AllocSpaceWBack(bufsize
);
547 memcpy(ptrback
, buf
, bufsize
);
551 bool wxInputStream::Ungetch(char c
)
555 ptrback
= AllocSpaceWBack(1);
563 char wxInputStream::GetC()
570 wxInputStream
& wxInputStream::Read(void *buffer
, size_t size
)
573 char *buf
= (char *)buffer
;
575 retsize
= GetWBack(buf
, size
);
576 if (retsize
== size
) {
578 m_lasterror
= wxStream_NOERROR
;
584 m_lastcount
= OnSysRead(buf
, size
);
588 char wxInputStream::Peek()
593 if (m_lasterror
== wxStream_NOERROR
) {
600 wxInputStream
& wxInputStream::Read(wxOutputStream
& stream_out
)
602 char buf
[BUF_TEMP_SIZE
];
603 size_t bytes_read
= BUF_TEMP_SIZE
;
605 while (bytes_read
== BUF_TEMP_SIZE
) {
606 bytes_read
= Read(buf
, bytes_read
).LastRead();
607 bytes_read
= stream_out
.Write(buf
, bytes_read
).LastWrite();
612 off_t
wxInputStream::SeekI(off_t pos
, wxSeekMode mode
)
614 // Should be check and improve, just to remove a slight bug !
615 // I don't know whether it should be put as well in wxFileInputStream::OnSysSeek ?
616 if (m_lasterror
==wxSTREAM_EOF
)
617 m_lasterror
=wxSTREAM_NOERROR
;
619 // A call to SeekI() will automatically invalidate any previous call
620 // to Ungetch(), otherwise it would be possible to SeeI() to one
621 // one position, unread some bytes there, SeekI() to another position
622 // and the data would be corrupted.
625 m_wback
= (char*) NULL
;
630 return OnSysSeek(pos
, mode
);
633 off_t
wxInputStream::TellI() const
638 // --------------------
639 // Overloaded operators
640 // --------------------
643 wxInputStream
& wxInputStream::operator>>(wxObject
*& obj
)
645 wxObjectInputStream
obj_s(*this);
646 obj
= obj_s
.LoadObject();
652 // ----------------------------------------------------------------------------
654 // ----------------------------------------------------------------------------
655 wxOutputStream::wxOutputStream()
660 wxOutputStream::~wxOutputStream()
664 wxOutputStream
& wxOutputStream::Write(const void *buffer
, size_t size
)
666 m_lastcount
= OnSysWrite(buffer
, size
);
670 wxOutputStream
& wxOutputStream::Write(wxInputStream
& stream_in
)
672 stream_in
.Read(*this);
676 off_t
wxOutputStream::TellO() const
681 off_t
wxOutputStream::SeekO(off_t pos
, wxSeekMode mode
)
683 return OnSysSeek(pos
, mode
);
686 void wxOutputStream::Sync()
691 wxOutputStream
& wxOutputStream::operator<<(wxObject
& obj
)
693 wxObjectOutputStream
obj_s(*this);
694 obj_s
.SaveObject(obj
);
699 // ----------------------------------------------------------------------------
700 // wxCountingOutputStream
701 // ----------------------------------------------------------------------------
703 wxCountingOutputStream::wxCountingOutputStream ()
709 size_t wxCountingOutputStream::GetSize() const
714 size_t wxCountingOutputStream::OnSysWrite(const void *WXUNUSED(buffer
), size_t size
)
716 m_currentPos
+= size
;
717 if (m_currentPos
> m_lastcount
) m_lastcount
= m_currentPos
;
721 off_t
wxCountingOutputStream::OnSysSeek(off_t pos
, wxSeekMode mode
)
723 if (mode
== wxFromStart
)
727 if (mode
== wxFromEnd
)
729 m_currentPos
= m_lastcount
+ pos
;
735 if (m_currentPos
> m_lastcount
) m_lastcount
= m_currentPos
;
737 return m_currentPos
; // ?
740 off_t
wxCountingOutputStream::OnSysTell() const
742 return m_currentPos
; // ?
745 // ----------------------------------------------------------------------------
746 // wxFilterInputStream
747 // ----------------------------------------------------------------------------
749 wxFilterInputStream::wxFilterInputStream()
754 wxFilterInputStream::wxFilterInputStream(wxInputStream
& stream
)
757 m_parent_i_stream
= &stream
;
760 wxFilterInputStream::~wxFilterInputStream()
764 // ----------------------------------------------------------------------------
765 // wxFilterOutputStream
766 // ----------------------------------------------------------------------------
767 wxFilterOutputStream::wxFilterOutputStream()
772 wxFilterOutputStream::wxFilterOutputStream(wxOutputStream
& stream
)
775 m_parent_o_stream
= &stream
;
778 wxFilterOutputStream::~wxFilterOutputStream()
782 // ----------------------------------------------------------------------------
783 // wxBufferedInputStream
784 // ----------------------------------------------------------------------------
785 wxBufferedInputStream::wxBufferedInputStream(wxInputStream
& s
)
786 : wxFilterInputStream(s
)
788 m_i_streambuf
= new wxStreamBuffer(*this, wxStreamBuffer::read
);
789 m_i_streambuf
->SetBufferIO(1024);
792 wxBufferedInputStream::~wxBufferedInputStream()
794 delete m_i_streambuf
;
797 char wxBufferedInputStream::Peek()
799 return m_i_streambuf
->Peek();
802 wxInputStream
& wxBufferedInputStream::Read(void *buffer
, size_t size
)
805 char *buf
= (char *)buffer
;
807 retsize
= GetWBack(buf
, size
);
808 m_lastcount
= retsize
;
809 if (retsize
== size
) {
810 m_lasterror
= wxStream_NOERROR
;
816 m_i_streambuf
->Read(buf
, size
);
821 off_t
wxBufferedInputStream::SeekI(off_t pos
, wxSeekMode mode
)
823 return m_i_streambuf
->Seek(pos
, mode
);
826 off_t
wxBufferedInputStream::TellI() const
828 return m_i_streambuf
->Tell();
831 size_t wxBufferedInputStream::OnSysRead(void *buffer
, size_t bufsize
)
833 return m_parent_i_stream
->Read(buffer
, bufsize
).LastRead();
836 off_t
wxBufferedInputStream::OnSysSeek(off_t seek
, wxSeekMode mode
)
838 return m_parent_i_stream
->SeekI(seek
, mode
);
841 off_t
wxBufferedInputStream::OnSysTell() const
843 return m_parent_i_stream
->TellI();
846 // ----------------------------------------------------------------------------
847 // wxBufferedOutputStream
848 // ----------------------------------------------------------------------------
850 wxBufferedOutputStream::wxBufferedOutputStream(wxOutputStream
& s
)
851 : wxFilterOutputStream(s
)
853 m_o_streambuf
= new wxStreamBuffer(*this, wxStreamBuffer::write
);
854 m_o_streambuf
->SetBufferIO(1024);
857 wxBufferedOutputStream::~wxBufferedOutputStream()
860 delete m_o_streambuf
;
863 wxOutputStream
& wxBufferedOutputStream::Write(const void *buffer
, size_t size
)
866 m_o_streambuf
->Write(buffer
, size
);
870 off_t
wxBufferedOutputStream::SeekO(off_t pos
, wxSeekMode mode
)
873 return m_o_streambuf
->Seek(pos
, mode
);
876 off_t
wxBufferedOutputStream::TellO() const
878 return m_o_streambuf
->Tell();
881 void wxBufferedOutputStream::Sync()
883 m_o_streambuf
->FlushBuffer();
884 m_parent_o_stream
->Sync();
887 size_t wxBufferedOutputStream::OnSysWrite(const void *buffer
, size_t bufsize
)
889 return m_parent_o_stream
->Write(buffer
, bufsize
).LastWrite();
892 off_t
wxBufferedOutputStream::OnSysSeek(off_t seek
, wxSeekMode mode
)
894 return m_parent_o_stream
->SeekO(seek
, mode
);
897 off_t
wxBufferedOutputStream::OnSysTell() const
899 return m_parent_o_stream
->TellO();
902 // ----------------------------------------------------------------------------
903 // Some IOManip function
904 // ----------------------------------------------------------------------------
906 wxOutputStream
& wxEndL(wxOutputStream
& stream
)
909 return stream
.Write("\r\n", 2);
912 return stream
.Write("\r", 1);
914 return stream
.Write("\n", 1);