]>
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"
19 #include <wx/stream.h>
20 #include <wx/datstrm.h>
21 #include <wx/objstrm.h>
27 #define BUF_TEMP_SIZE 10000
29 // ----------------------------------------------------------------------------
31 // ----------------------------------------------------------------------------
33 #define CHECK_ERROR(err) \
34 if (m_stream->m_lasterror == wxStream_NOERROR) \
35 m_stream->m_lasterror = err
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
)
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
)
51 m_stream
= new wxStreamBase();
54 wxStreamBuffer::wxStreamBuffer(const wxStreamBuffer
& buffer
)
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
;
65 m_destroystream
= FALSE
;
71 wxStreamBuffer::~wxStreamBuffer()
76 wxDELETEA(m_buffer_start
);
81 size_t wxStreamBuffer::WriteBack(const char *buf
, size_t bufsize
)
85 ptrback
= AllocSpaceWBack(bufsize
);
89 memcpy(ptrback
, buf
, bufsize
);
93 bool wxStreamBuffer::WriteBack(char c
)
97 ptrback
= AllocSpaceWBack(1);
105 void wxStreamBuffer::SetBufferIO(char *buffer_start
, char *buffer_end
)
108 wxDELETEA(m_buffer_start
);
109 m_buffer_start
= buffer_start
;
110 m_buffer_end
= buffer_end
;
112 m_buffer_size
= m_buffer_end
-m_buffer_start
;
113 m_destroybuf
= FALSE
;
117 void wxStreamBuffer::SetBufferIO(size_t bufsize
)
122 wxDELETEA(m_buffer_start
);
125 m_buffer_start
= NULL
;
132 b_start
= new char[bufsize
];
133 SetBufferIO(b_start
, b_start
+ bufsize
);
137 void wxStreamBuffer::ResetBuffer()
139 m_stream
->m_lasterror
= wxStream_NOERROR
;
141 m_buffer_pos
= m_buffer_end
;
143 m_buffer_pos
= m_buffer_start
;
146 char *wxStreamBuffer::AllocSpaceWBack(size_t needed_size
)
150 m_wbacksize
+= needed_size
;
153 temp_b
= (char *)malloc(m_wbacksize
);
155 temp_b
= (char *)realloc(m_wback
, m_wbacksize
);
161 return (char *)(m_wback
+(m_wbacksize
-needed_size
));
164 size_t wxStreamBuffer::GetWBack(char *buf
, size_t bsize
)
166 size_t s_toget
= m_wbacksize
-m_wbackcur
;
171 memcpy(buf
, (m_wback
+m_wbackcur
), s_toget
);
173 m_wbackcur
+= s_toget
;
174 if (m_wbackcur
== m_wbacksize
) {
176 m_wback
= (char *)NULL
;
184 bool wxStreamBuffer::FillBuffer()
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
;
197 bool wxStreamBuffer::FlushBuffer()
199 size_t count
, current
;
201 if (m_buffer_pos
== m_buffer_start
|| !m_flushable
)
204 current
= m_buffer_pos
-m_buffer_start
;
205 count
= m_stream
->OnSysWrite(m_buffer_start
, current
);
206 if (count
!= current
)
208 m_buffer_pos
= m_buffer_start
;
213 void wxStreamBuffer::GetFromBuffer(void *buffer
, size_t size
)
215 size_t s_toget
= m_buffer_end
-m_buffer_pos
;
220 memcpy(buffer
, m_buffer_pos
, s_toget
);
221 m_buffer_pos
+= s_toget
;
224 void wxStreamBuffer::PutToBuffer(const void *buffer
, size_t size
)
226 size_t s_toput
= m_buffer_end
-m_buffer_pos
;
228 if (s_toput
< size
&& !m_fixed
) {
229 m_buffer_start
= (char *)realloc(m_buffer_start
, m_buffer_size
+size
);
231 m_buffer_size
+= size
;
232 m_buffer_end
= m_buffer_start
+m_buffer_size
;
237 memcpy(m_buffer_pos
, buffer
, s_toput
);
238 m_buffer_pos
+= s_toput
;
241 void wxStreamBuffer::PutChar(char c
)
243 wxASSERT(m_stream
!= NULL
);
245 if (!m_buffer_size
) {
246 m_stream
->OnSysWrite(&c
, 1);
250 if (!GetDataLeft() && !FlushBuffer()) {
251 CHECK_ERROR(wxStream_READ_ERR
);
256 m_stream
->m_lastcount
= 1;
259 char wxStreamBuffer::GetChar()
263 wxASSERT(m_stream
!= NULL
);
265 if (!m_buffer_size
) {
266 m_stream
->OnSysRead(&c
, 1);
270 if (!GetDataLeft()) {
271 CHECK_ERROR(wxStream_READ_ERR
);
275 GetFromBuffer(&c
, 1);
276 m_stream
->m_lastcount
= 1;
280 size_t wxStreamBuffer::Read(void *buffer
, size_t size
)
282 wxASSERT(m_stream
!= NULL
);
284 // ------------------
285 // Buffering disabled
286 // ------------------
288 m_stream
->m_lasterror
= wxStream_NOERROR
;
289 m_stream
->m_lastcount
= GetWBack((char *)buffer
, size
);
290 size
-= m_stream
->m_lastcount
;
292 return m_stream
->m_lastcount
;
294 buffer
= (void *)((char *)buffer
+m_stream
->m_lastcount
);
297 return (m_stream
->m_lastcount
+= m_stream
->OnSysRead(buffer
, size
));
302 size_t buf_left
, orig_size
= size
;
305 buf_left
= GetDataLeft();
307 // First case: the requested buffer is larger than the stream buffer,
309 if (size
> buf_left
) {
310 GetFromBuffer(buffer
, buf_left
);
312 buffer
= (char *)buffer
+ buf_left
; // ANSI C++ violation.
315 CHECK_ERROR(wxStream_READ_ERR
);
316 return (m_stream
->m_lastcount
= orig_size
-size
);
320 // Second case: we just copy from the stream buffer.
321 GetFromBuffer(buffer
, size
);
325 return (m_stream
->m_lastcount
+= orig_size
);
328 size_t wxStreamBuffer::Read(wxStreamBuffer
*s_buf
)
330 char buf
[BUF_TEMP_SIZE
];
331 size_t s
= 0, bytes_read
= BUF_TEMP_SIZE
;
333 while (bytes_read
== BUF_TEMP_SIZE
) {
334 bytes_read
= Read(buf
, bytes_read
);
335 bytes_read
= s_buf
->Write(buf
, bytes_read
);
341 size_t wxStreamBuffer::Write(const void *buffer
, size_t size
)
343 wxASSERT(m_stream
!= NULL
);
345 // ------------------
346 // Buffering disabled
347 // ------------------
349 m_stream
->m_lasterror
= wxStream_NOERROR
;
351 return (m_stream
->m_lastcount
= m_stream
->OnSysWrite(buffer
, size
));
353 // ------------------
355 // ------------------
357 size_t buf_left
, orig_size
= size
;
360 buf_left
= m_buffer_end
- m_buffer_pos
;
362 // First case: the buffer to write is larger than the stream buffer,
364 if (size
> buf_left
) {
365 PutToBuffer(buffer
, buf_left
);
367 buffer
= (char *)buffer
+ buf_left
; // ANSI C++ violation.
369 if (!FlushBuffer()) {
370 CHECK_ERROR(wxStream_WRITE_ERR
);
371 return (m_stream
->m_lastcount
= orig_size
-size
);
374 m_buffer_pos
= m_buffer_start
;
378 // Second case: just copy it in the stream buffer.
379 PutToBuffer(buffer
, size
);
383 return (m_stream
->m_lastcount
= orig_size
);
386 size_t wxStreamBuffer::Write(wxStreamBuffer
*sbuf
)
388 char buf
[BUF_TEMP_SIZE
];
389 size_t s
= 0, bytes_count
= BUF_TEMP_SIZE
, b_count2
;
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
);
401 off_t
wxStreamBuffer::Seek(off_t pos
, wxSeekMode mode
)
403 off_t ret_off
, diff
, last_access
;
405 last_access
= GetLastAccess();
408 diff
= pos
+ GetIntPosition();
409 if (diff
< 0 || diff
> last_access
)
410 return wxInvalidOffset
;
411 SetIntPosition(diff
);
417 // We'll try to compute an internal position later ...
418 ret_off
= m_stream
->OnSysSeek(pos
, wxFromStart
);
422 case wxFromCurrent
: {
423 diff
= pos
+ GetIntPosition();
425 if ( (diff
> last_access
) || (diff
< 0) ) {
426 ret_off
= m_stream
->OnSysSeek(pos
, wxFromCurrent
);
430 SetIntPosition(diff
);
435 // Hard to compute: always seek to the requested position.
436 ret_off
= m_stream
->OnSysSeek(pos
, wxFromEnd
);
440 return wxInvalidOffset
;
443 off_t
wxStreamBuffer::Tell() const
448 pos
= m_stream
->OnSysTell();
449 if (pos
== wxInvalidOffset
)
450 return wxInvalidOffset
;
451 return pos
- GetLastAccess() + GetIntPosition();
453 return GetIntPosition();
456 size_t wxStreamBuffer::GetDataLeft()
458 if (m_buffer_end
== m_buffer_pos
&& m_flushable
)
460 return m_buffer_end
-m_buffer_pos
;
463 // ----------------------------------------------------------------------------
465 // ----------------------------------------------------------------------------
467 wxStreamBase::wxStreamBase()
469 m_lasterror
= wxStream_NOERROR
;
473 wxStreamBase::~wxStreamBase()
477 size_t wxStreamBase::OnSysRead(void *WXUNUSED(buffer
), size_t WXUNUSED(size
))
482 size_t wxStreamBase::OnSysWrite(const void *WXUNUSED(buffer
), size_t WXUNUSED(bufsize
))
487 off_t
wxStreamBase::OnSysSeek(off_t
WXUNUSED(seek
), wxSeekMode
WXUNUSED(mode
))
489 return wxInvalidOffset
;
492 off_t
wxStreamBase::OnSysTell() const
494 return wxInvalidOffset
;
497 // ----------------------------------------------------------------------------
499 // ----------------------------------------------------------------------------
501 wxInputStream::wxInputStream()
504 m_i_destroybuf
= TRUE
;
505 m_i_streambuf
= new wxStreamBuffer(*this, wxStreamBuffer::read
);
508 wxInputStream::wxInputStream(wxStreamBuffer
*buffer
)
511 m_i_destroybuf
= FALSE
;
512 m_i_streambuf
= buffer
;
515 wxInputStream::~wxInputStream()
518 delete m_i_streambuf
;
521 char wxInputStream::GetC()
524 m_i_streambuf
->Read(&c
, 1);
528 wxInputStream
& wxInputStream::Read(void *buffer
, size_t size
)
530 m_i_streambuf
->Read(buffer
, size
);
531 // wxStreamBuffer sets all variables for us
535 char wxInputStream::Peek()
537 m_i_streambuf
->GetDataLeft();
539 return *(m_i_streambuf
->GetBufferPos());
543 wxInputStream
& wxInputStream::Read(wxOutputStream
& stream_out
)
545 char buf
[BUF_TEMP_SIZE
];
546 size_t bytes_read
= BUF_TEMP_SIZE
;
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();
555 off_t
wxInputStream::SeekI(off_t pos
, wxSeekMode mode
)
557 return m_i_streambuf
->Seek(pos
, mode
);
560 off_t
wxInputStream::TellI() const
562 return m_i_streambuf
->Tell();
565 // --------------------
566 // Overloaded operators
567 // --------------------
569 wxInputStream
& wxInputStream::operator>>(wxString
& line
)
571 wxDataInputStream
s(*this);
577 wxInputStream
& wxInputStream::operator>>(char& c
)
583 wxInputStream
& wxInputStream::operator>>(short& i
)
592 wxInputStream
& wxInputStream::operator>>(int& i
)
601 wxInputStream
& wxInputStream::operator>>(long& i
)
603 /* I only implemented a simple integer parser */
606 while (isspace( c
= GetC() ) )
610 if (! (c
== '-' || isdigit(c
)) ) {
611 InputStreamBuffer()->WriteBack(c
);
631 wxInputStream
& wxInputStream::operator>>(double& f
)
633 /* I only implemented a simple float parser */
636 while (isspace( c
= GetC() ) )
640 if (! (c
== '-' || isdigit(c
)) ) {
641 InputStreamBuffer()->WriteBack(c
);
652 f
= f
*10 + (c
- '0');
657 double f_multiplicator
= (double) 0.1;
661 f
+= (c
-'0')*f_multiplicator
;
662 f_multiplicator
/= 10;
673 wxInputStream
& wxInputStream::operator>>(wxObject
*& obj
)
675 wxObjectInputStream
obj_s(*this);
676 obj
= obj_s
.LoadObject();
682 // ----------------------------------------------------------------------------
684 // ----------------------------------------------------------------------------
685 wxOutputStream::wxOutputStream()
688 m_o_destroybuf
= TRUE
;
689 m_o_streambuf
= new wxStreamBuffer(*this, wxStreamBuffer::write
);
692 wxOutputStream::wxOutputStream(wxStreamBuffer
*buffer
)
695 m_o_destroybuf
= FALSE
;
696 m_o_streambuf
= buffer
;
699 wxOutputStream::~wxOutputStream()
702 delete m_o_streambuf
;
705 wxOutputStream
& wxOutputStream::Write(const void *buffer
, size_t size
)
707 m_o_streambuf
->Write(buffer
, size
);
711 wxOutputStream
& wxOutputStream::Write(wxInputStream
& stream_in
)
713 stream_in
.Read(*this);
717 off_t
wxOutputStream::TellO() const
719 return m_o_streambuf
->Tell();
722 off_t
wxOutputStream::SeekO(off_t pos
, wxSeekMode mode
)
724 return m_o_streambuf
->Seek(pos
, mode
);
727 void wxOutputStream::Sync()
729 m_o_streambuf
->FlushBuffer();
732 wxOutputStream
& wxOutputStream::operator<<(const char *string
)
734 return Write(string
, strlen(string
));
737 wxOutputStream
& wxOutputStream::operator<<(wxString
& string
)
739 return Write(string
, string
.Len());
742 wxOutputStream
& wxOutputStream::operator<<(char c
)
747 wxOutputStream
& wxOutputStream::operator<<(short i
)
751 strint
.Printf("%i", i
);
752 return Write(strint
, strint
.Len());
755 wxOutputStream
& wxOutputStream::operator<<(int i
)
759 strint
.Printf("%i", i
);
760 return Write(strint
, strint
.Len());
763 wxOutputStream
& wxOutputStream::operator<<(long i
)
767 strlong
.Printf("%i", i
);
768 return Write((const char *)strlong
, strlong
.Len());
771 wxOutputStream
& wxOutputStream::operator<<(double f
)
775 strfloat
.Printf("%f", f
);
776 return Write(strfloat
, strfloat
.Len());
780 wxOutputStream
& wxOutputStream::operator<<(wxObject
& obj
)
782 wxObjectOutputStream
obj_s(*this);
783 obj_s
.SaveObject(obj
);
788 // ----------------------------------------------------------------------------
789 // wxFilterInputStream
790 // ----------------------------------------------------------------------------
791 wxFilterInputStream::wxFilterInputStream()
792 : wxInputStream(NULL
)
794 // WARNING streambuf set to NULL !
797 wxFilterInputStream::wxFilterInputStream(wxInputStream
& stream
)
798 : wxInputStream(stream
.InputStreamBuffer())
800 m_parent_i_stream
= &stream
;
803 wxFilterInputStream::~wxFilterInputStream()
807 // ----------------------------------------------------------------------------
808 // wxFilterOutputStream
809 // ----------------------------------------------------------------------------
810 wxFilterOutputStream::wxFilterOutputStream()
811 : wxOutputStream(NULL
)
815 wxFilterOutputStream::wxFilterOutputStream(wxOutputStream
& stream
)
816 : wxOutputStream(stream
.OutputStreamBuffer())
818 m_parent_o_stream
= &stream
;
821 wxFilterOutputStream::~wxFilterOutputStream()
825 // ----------------------------------------------------------------------------
826 // Some IOManip function
827 // ----------------------------------------------------------------------------
829 wxOutputStream
& wxEndL(wxOutputStream
& stream
)
832 return stream
.Write("\r\n", 2);
834 return stream
.Write("\n", 1);