]>
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;
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
) {
168 m_buffer_start
= (char *)realloc(m_buffer_start
, m_buffer_size
+size
);
170 m_buffer_size
+= size
;
171 m_buffer_end
= m_buffer_start
+m_buffer_size
;
176 memcpy(m_buffer_pos
, buffer
, s_toput
);
177 m_buffer_pos
+= s_toput
;
180 void wxStreamBuffer::PutChar(char c
)
182 wxASSERT(m_stream
!= NULL
);
184 if (!m_buffer_size
) {
185 m_stream
->OnSysWrite(&c
, 1);
189 if (GetDataLeft() == 0 && !FlushBuffer()) {
190 CHECK_ERROR(wxStream_WRITE_ERR
);
195 m_stream
->m_lastcount
= 1;
198 char wxStreamBuffer::Peek()
202 wxASSERT(m_stream
!= NULL
&& m_buffer_size
!= 0);
204 if (!GetDataLeft()) {
205 CHECK_ERROR(wxStream_READ_ERR
);
209 GetFromBuffer(&c
, 1);
215 char wxStreamBuffer::GetChar()
219 wxASSERT(m_stream
!= NULL
);
221 if (!m_buffer_size
) {
222 m_stream
->OnSysRead(&c
, 1);
226 if (!GetDataLeft()) {
227 CHECK_ERROR(wxStream_READ_ERR
);
231 GetFromBuffer(&c
, 1);
233 m_stream
->m_lastcount
= 1;
237 size_t wxStreamBuffer::Read(void *buffer
, size_t size
)
239 wxASSERT(m_stream
!= NULL
);
244 // ------------------
245 // Buffering disabled
246 // ------------------
248 m_stream
->m_lasterror
= wxStream_NOERROR
;
250 return (m_stream
->m_lastcount
+= m_stream
->OnSysRead(buffer
, size
));
255 size_t buf_left
, orig_size
= size
;
258 buf_left
= GetDataLeft();
260 // First case: the requested buffer is larger than the stream buffer,
262 if (size
> buf_left
) {
263 GetFromBuffer(buffer
, buf_left
);
265 buffer
= (char *)buffer
+ buf_left
; // ANSI C++ violation.
268 CHECK_ERROR(wxStream_EOF
);
269 return (m_stream
->m_lastcount
= orig_size
-size
);
273 // Second case: we just copy from the stream buffer.
274 GetFromBuffer(buffer
, size
);
278 return (m_stream
->m_lastcount
+= orig_size
);
281 size_t wxStreamBuffer::Read(wxStreamBuffer
*s_buf
)
283 char buf
[BUF_TEMP_SIZE
];
284 size_t s
= 0, bytes_read
= BUF_TEMP_SIZE
;
289 while (bytes_read
!= 0) {
290 bytes_read
= Read(buf
, bytes_read
);
291 bytes_read
= s_buf
->Write(buf
, bytes_read
);
297 size_t wxStreamBuffer::Write(const void *buffer
, size_t size
)
299 wxASSERT(m_stream
!= NULL
);
304 // ------------------
305 // Buffering disabled
306 // ------------------
308 m_stream
->m_lasterror
= wxStream_NOERROR
;
310 return (m_stream
->m_lastcount
= m_stream
->OnSysWrite(buffer
, size
));
312 // ------------------
314 // ------------------
316 size_t buf_left
, orig_size
= size
;
319 buf_left
= m_buffer_end
- m_buffer_pos
;
321 // First case: the buffer to write is larger than the stream buffer,
323 if (size
> buf_left
) {
324 PutToBuffer(buffer
, buf_left
);
326 buffer
= (char *)buffer
+ buf_left
; // ANSI C++ violation.
328 if (!FlushBuffer()) {
329 CHECK_ERROR(wxStream_WRITE_ERR
);
330 return (m_stream
->m_lastcount
= orig_size
-size
);
333 m_buffer_pos
= m_buffer_start
;
337 // Second case: just copy it in the stream buffer.
338 PutToBuffer(buffer
, size
);
342 return (m_stream
->m_lastcount
= orig_size
);
345 size_t wxStreamBuffer::Write(wxStreamBuffer
*sbuf
)
347 char buf
[BUF_TEMP_SIZE
];
348 size_t s
= 0, bytes_count
= BUF_TEMP_SIZE
, b_count2
;
349 wxInputStream
*in_stream
;
354 in_stream
= (wxInputStream
*)sbuf
->Stream();
356 while (bytes_count
== BUF_TEMP_SIZE
) {
357 b_count2
= sbuf
->Read(buf
, bytes_count
);
358 bytes_count
= Write(buf
, b_count2
);
359 if (b_count2
> bytes_count
)
360 in_stream
->Ungetch(buf
+bytes_count
, b_count2
-bytes_count
);
366 off_t
wxStreamBuffer::Seek(off_t pos
, wxSeekMode mode
)
368 off_t ret_off
, diff
, last_access
;
370 last_access
= GetLastAccess();
373 diff
= pos
+ GetIntPosition();
374 if (diff
< 0 || diff
> last_access
)
375 return wxInvalidOffset
;
376 SetIntPosition(diff
);
382 // We'll try to compute an internal position later ...
383 ret_off
= m_stream
->OnSysSeek(pos
, wxFromStart
);
387 case wxFromCurrent
: {
388 diff
= pos
+ GetIntPosition();
390 if ( (diff
> last_access
) || (diff
< 0) ) {
391 // We must take into account the fact that we have read something
393 ret_off
= m_stream
->OnSysSeek(diff
-last_access
, wxFromCurrent
);
397 SetIntPosition(diff
);
402 // Hard to compute: always seek to the requested position.
403 ret_off
= m_stream
->OnSysSeek(pos
, wxFromEnd
);
407 return wxInvalidOffset
;
410 off_t
wxStreamBuffer::Tell() const
415 pos
= m_stream
->OnSysTell();
416 if (pos
== wxInvalidOffset
)
417 return wxInvalidOffset
;
418 return pos
- GetLastAccess() + GetIntPosition();
420 return GetIntPosition();
423 size_t wxStreamBuffer::GetDataLeft()
425 if (m_buffer_end
== m_buffer_pos
&& m_flushable
)
427 return m_buffer_end
-m_buffer_pos
;
430 // ----------------------------------------------------------------------------
432 // ----------------------------------------------------------------------------
434 wxStreamBase::wxStreamBase()
436 m_lasterror
= wxStream_NOERROR
;
440 wxStreamBase::~wxStreamBase()
444 size_t wxStreamBase::OnSysRead(void *WXUNUSED(buffer
), size_t WXUNUSED(size
))
449 size_t wxStreamBase::OnSysWrite(const void *WXUNUSED(buffer
), size_t WXUNUSED(bufsize
))
454 off_t
wxStreamBase::OnSysSeek(off_t
WXUNUSED(seek
), wxSeekMode
WXUNUSED(mode
))
456 return wxInvalidOffset
;
459 off_t
wxStreamBase::OnSysTell() const
461 return wxInvalidOffset
;
464 // ----------------------------------------------------------------------------
466 // ----------------------------------------------------------------------------
468 wxInputStream::wxInputStream()
470 m_wback(NULL
), m_wbacksize(0), m_wbackcur(0)
474 wxInputStream::~wxInputStream()
480 char *wxInputStream::AllocSpaceWBack(size_t needed_size
)
485 old_size
= m_wbacksize
;
486 m_wbacksize
+= needed_size
;
489 temp_b
= (char *)malloc(m_wbacksize
);
491 temp_b
= (char *)realloc(m_wback
, m_wbacksize
);
496 m_wbackcur
+= needed_size
;
498 memmove(m_wback
+needed_size
, m_wback
, old_size
);
500 return (char *)(m_wback
);
503 size_t wxInputStream::GetWBack(char *buf
, size_t bsize
)
505 size_t s_toget
= m_wbackcur
;
513 memcpy(buf
, (m_wback
+m_wbackcur
-bsize
), s_toget
);
515 m_wbackcur
-= s_toget
;
516 if (m_wbackcur
== 0) {
518 m_wback
= (char *)NULL
;
526 size_t wxInputStream::Ungetch(const void *buf
, size_t bufsize
)
530 ptrback
= AllocSpaceWBack(bufsize
);
534 memcpy(ptrback
, buf
, bufsize
);
538 bool wxInputStream::Ungetch(char c
)
542 ptrback
= AllocSpaceWBack(1);
550 char wxInputStream::GetC()
557 wxInputStream
& wxInputStream::Read(void *buffer
, size_t size
)
560 char *buf
= (char *)buffer
;
562 retsize
= GetWBack(buf
, size
);
563 if (retsize
== size
) {
565 m_lasterror
= wxStream_NOERROR
;
571 m_lastcount
= OnSysRead(buf
, size
);
575 char wxInputStream::Peek()
580 if (m_lasterror
== wxStream_NOERROR
) {
587 wxInputStream
& wxInputStream::Read(wxOutputStream
& stream_out
)
589 char buf
[BUF_TEMP_SIZE
];
590 size_t bytes_read
= BUF_TEMP_SIZE
;
592 while (bytes_read
== BUF_TEMP_SIZE
) {
593 bytes_read
= Read(buf
, bytes_read
).LastRead();
594 bytes_read
= stream_out
.Write(buf
, bytes_read
).LastWrite();
599 off_t
wxInputStream::SeekI(off_t pos
, wxSeekMode mode
)
601 //should be check and improve, just to remove a slight bug !
602 // I don't know whether it should be put as well in wxFileInputStream::OnSysSeek ?
603 if (m_lasterror
==wxSTREAM_EOF
) m_lasterror
=wxSTREAM_NOERROR
;
605 return OnSysSeek(pos
, mode
);
608 off_t
wxInputStream::TellI() const
613 // --------------------
614 // Overloaded operators
615 // --------------------
618 wxInputStream
& wxInputStream::operator>>(wxObject
*& obj
)
620 wxObjectInputStream
obj_s(*this);
621 obj
= obj_s
.LoadObject();
627 // ----------------------------------------------------------------------------
629 // ----------------------------------------------------------------------------
630 wxOutputStream::wxOutputStream()
635 wxOutputStream::~wxOutputStream()
639 wxOutputStream
& wxOutputStream::Write(const void *buffer
, size_t size
)
641 m_lastcount
= OnSysWrite(buffer
, size
);
645 wxOutputStream
& wxOutputStream::Write(wxInputStream
& stream_in
)
647 stream_in
.Read(*this);
651 off_t
wxOutputStream::TellO() const
656 off_t
wxOutputStream::SeekO(off_t pos
, wxSeekMode mode
)
658 return OnSysSeek(pos
, mode
);
661 void wxOutputStream::Sync()
666 wxOutputStream
& wxOutputStream::operator<<(wxObject
& obj
)
668 wxObjectOutputStream
obj_s(*this);
669 obj_s
.SaveObject(obj
);
674 // ----------------------------------------------------------------------------
675 // wxCountingOutputStream
676 // ----------------------------------------------------------------------------
678 wxCountingOutputStream::wxCountingOutputStream ()
684 size_t wxCountingOutputStream::GetSize() const
689 size_t wxCountingOutputStream::OnSysWrite(const void *WXUNUSED(buffer
), size_t size
)
691 m_currentPos
+= size
;
692 if (m_currentPos
> m_lastcount
) m_lastcount
= m_currentPos
;
696 off_t
wxCountingOutputStream::OnSysSeek(off_t pos
, wxSeekMode mode
)
698 if (mode
== wxFromStart
)
702 if (mode
== wxFromEnd
)
704 m_currentPos
= m_lastcount
+ pos
;
710 if (m_currentPos
> m_lastcount
) m_lastcount
= m_currentPos
;
712 return m_currentPos
; // ?
715 off_t
wxCountingOutputStream::OnSysTell() const
717 return m_currentPos
; // ?
720 // ----------------------------------------------------------------------------
721 // wxFilterInputStream
722 // ----------------------------------------------------------------------------
724 wxFilterInputStream::wxFilterInputStream()
729 wxFilterInputStream::wxFilterInputStream(wxInputStream
& stream
)
732 m_parent_i_stream
= &stream
;
735 wxFilterInputStream::~wxFilterInputStream()
739 // ----------------------------------------------------------------------------
740 // wxFilterOutputStream
741 // ----------------------------------------------------------------------------
742 wxFilterOutputStream::wxFilterOutputStream()
747 wxFilterOutputStream::wxFilterOutputStream(wxOutputStream
& stream
)
750 m_parent_o_stream
= &stream
;
753 wxFilterOutputStream::~wxFilterOutputStream()
757 // ----------------------------------------------------------------------------
758 // wxBufferedInputStream
759 // ----------------------------------------------------------------------------
760 wxBufferedInputStream::wxBufferedInputStream(wxInputStream
& s
)
761 : wxFilterInputStream(s
)
763 m_i_streambuf
= new wxStreamBuffer(*this, wxStreamBuffer::read
);
764 m_i_streambuf
->SetBufferIO(1024);
767 wxBufferedInputStream::~wxBufferedInputStream()
769 delete m_i_streambuf
;
772 char wxBufferedInputStream::Peek()
774 return m_i_streambuf
->Peek();
777 wxInputStream
& wxBufferedInputStream::Read(void *buffer
, size_t size
)
780 char *buf
= (char *)buffer
;
782 retsize
= GetWBack(buf
, size
);
783 m_lastcount
= retsize
;
784 if (retsize
== size
) {
785 m_lasterror
= wxStream_NOERROR
;
791 m_i_streambuf
->Read(buf
, size
);
796 off_t
wxBufferedInputStream::SeekI(off_t pos
, wxSeekMode mode
)
798 return m_i_streambuf
->Seek(pos
, mode
);
801 off_t
wxBufferedInputStream::TellI() const
803 return m_i_streambuf
->Tell();
806 size_t wxBufferedInputStream::OnSysRead(void *buffer
, size_t bufsize
)
808 return m_parent_i_stream
->Read(buffer
, bufsize
).LastRead();
811 off_t
wxBufferedInputStream::OnSysSeek(off_t seek
, wxSeekMode mode
)
813 return m_parent_i_stream
->SeekI(seek
, mode
);
816 off_t
wxBufferedInputStream::OnSysTell() const
818 return m_parent_i_stream
->TellI();
821 // ----------------------------------------------------------------------------
822 // wxBufferedOutputStream
823 // ----------------------------------------------------------------------------
825 wxBufferedOutputStream::wxBufferedOutputStream(wxOutputStream
& s
)
826 : wxFilterOutputStream(s
)
828 m_o_streambuf
= new wxStreamBuffer(*this, wxStreamBuffer::write
);
829 m_o_streambuf
->SetBufferIO(1024);
832 wxBufferedOutputStream::~wxBufferedOutputStream()
834 delete m_o_streambuf
;
837 wxOutputStream
& wxBufferedOutputStream::Write(const void *buffer
, size_t size
)
840 m_o_streambuf
->Write(buffer
, size
);
844 off_t
wxBufferedOutputStream::SeekO(off_t pos
, wxSeekMode mode
)
846 return m_o_streambuf
->Seek(pos
, mode
);
849 off_t
wxBufferedOutputStream::TellO() const
851 return m_o_streambuf
->Tell();
854 void wxBufferedOutputStream::Sync()
856 m_o_streambuf
->FlushBuffer();
857 m_parent_o_stream
->Sync();
860 size_t wxBufferedOutputStream::OnSysWrite(const void *buffer
, size_t bufsize
)
862 return m_parent_o_stream
->Write(buffer
, bufsize
).LastWrite();
865 off_t
wxBufferedOutputStream::OnSysSeek(off_t seek
, wxSeekMode mode
)
867 return m_parent_o_stream
->SeekO(seek
, mode
);
870 off_t
wxBufferedOutputStream::OnSysTell() const
872 return m_parent_o_stream
->TellO();
875 // ----------------------------------------------------------------------------
876 // Some IOManip function
877 // ----------------------------------------------------------------------------
879 wxOutputStream
& wxEndL(wxOutputStream
& stream
)
882 return stream
.Write("\r\n", 2);
885 return stream
.Write("\r", 1);
887 return stream
.Write("\n", 1);