X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/6d44bf31a6f0b4a261280e57842bbd53b5e26cd5..4c7e33c5ea8f4e68a1468333e26251d0271613b0:/src/common/stream.cpp diff --git a/src/common/stream.cpp b/src/common/stream.cpp index 0186a9548c..3c038e14d9 100644 --- a/src/common/stream.cpp +++ b/src/common/stream.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #ifdef __BORLANDC__ #pragma hdrstop @@ -41,6 +42,7 @@ wxStreamBuffer::wxStreamBuffer(wxOutputStream& o_stream) wxStreamBuffer::~wxStreamBuffer() { + wxDELETEA(m_buffer_start); } void wxStreamBuffer::WriteBack(char c) @@ -50,6 +52,8 @@ void wxStreamBuffer::WriteBack(char c) // Assume that if we write "back" we have read a few bytes: so we have some // space. + if (m_buffer_pos == m_buffer_start) + return; m_buffer_pos--; *m_buffer_pos = c; @@ -57,31 +61,37 @@ void wxStreamBuffer::WriteBack(char c) void wxStreamBuffer::SetBufferIO(char *buffer_start, char *buffer_end) { - m_buffer_pos = m_buffer_start = buffer_start; - m_buffer_end = buffer_end; + size_t ret; + + m_buffer_start = buffer_start; + m_buffer_end = buffer_end; m_buffer_size = m_buffer_end-m_buffer_start; + + if (m_istream) { + ret = m_istream->DoRead(m_buffer_start, m_buffer_size); + m_buffer_end = m_buffer_start + ret; + } + m_buffer_pos = m_buffer_start; } void wxStreamBuffer::SetBufferIO(size_t bufsize) { - if (m_buffer_start) - delete[] m_buffer_start; + char *b_start; + + wxDELETE(m_buffer_start); if (!bufsize) { m_buffer_start = NULL; m_buffer_end = NULL; m_buffer_pos = NULL; m_buffer_size = 0; + return; } - m_buffer_start = new char[bufsize]; - m_buffer_end = m_buffer_start + bufsize; - if (m_istream) - m_buffer_pos = m_buffer_end; - else - m_buffer_pos = m_buffer_start; - m_buffer_size = bufsize; + b_start = new char[bufsize]; + + SetBufferIO(b_start, b_start + bufsize); } void wxStreamBuffer::ResetBuffer() @@ -123,9 +133,10 @@ void wxStreamBuffer::Read(void *buffer, size_t size) read_ret = m_istream->DoRead(m_buffer_start, m_buffer_size); - // Read failed + // Read failed: EOF if (read_ret == 0) { m_istream->m_lastread = orig_size-size; + m_istream->m_eof = TRUE; m_buffer_pos = m_buffer_end = m_buffer_start; return; } else { @@ -202,12 +213,16 @@ wxInputStream::wxInputStream() { m_i_destroybuf = TRUE; m_i_streambuf = new wxStreamBuffer(*this); + m_eof = FALSE; + m_lastread = 0; } wxInputStream::wxInputStream(wxStreamBuffer *buffer) { m_i_destroybuf = FALSE; m_i_streambuf = buffer; + m_eof = FALSE; + m_lastread = 0; } wxInputStream::~wxInputStream() @@ -268,6 +283,15 @@ wxInputStream& wxInputStream::operator>>(short& i) return *this; } +wxInputStream& wxInputStream::operator>>(int& i) +{ + long l; + + *this >> l; + i = (short)l; + return *this; +} + wxInputStream& wxInputStream::operator>>(long& i) { /* I only implemented a simple integer parser */ @@ -324,7 +348,7 @@ wxInputStream& wxInputStream::operator>>(float& f) } if (c == '.') { - float f_multiplicator = 0.1; + float f_multiplicator = (float) 0.1; c = GetC(); while (isdigit(c)) { @@ -339,27 +363,41 @@ wxInputStream& wxInputStream::operator>>(float& f) return *this; } +#if wxUSE_SERIAL +wxInputStream& wxInputStream::operator>>(wxObject *& obj) +{ + wxObjectInputStream obj_s(*this); + obj = obj_s.LoadObject(); + return *this; +} +#endif + off_t wxInputStream::SeekI(off_t pos, wxSeekMode mode) { - off_t ret_off; + off_t ret_off, diff, last_access; + + last_access = m_i_streambuf->GetLastAccess(); switch (mode) { case wxFromStart: - if ( (unsigned)abs (DoTellInput()-pos) > m_i_streambuf->GetLastAccess() ) { + diff = DoTellInput() - pos; + if ( diff < 0 || diff > last_access ) { ret_off = DoSeekInput(pos, wxFromStart); m_i_streambuf->ResetBuffer(); return ret_off; } else { - m_i_streambuf->SetIntPosition(DoTellInput() - pos); + m_i_streambuf->SetIntPosition(last_access - diff); return pos; } case wxFromCurrent: - if ( ((unsigned)pos > m_i_streambuf->GetLastAccess()) || (pos < 0) ) { + diff = pos + m_i_streambuf->GetIntPosition(); + + if ( (diff > last_access) || (diff < 0) ) { ret_off = DoSeekInput(pos, wxFromCurrent); m_i_streambuf->ResetBuffer(); return ret_off; } else { - m_i_streambuf->SetIntPosition(pos); + m_i_streambuf->SetIntPosition(diff); return pos; } case wxFromEnd: @@ -384,12 +422,16 @@ wxOutputStream::wxOutputStream() { m_o_destroybuf = TRUE; m_o_streambuf = new wxStreamBuffer(*this); + m_bad = FALSE; + m_lastwrite = 0; } wxOutputStream::wxOutputStream(wxStreamBuffer *buffer) { m_o_destroybuf = FALSE; m_o_streambuf = buffer; + m_bad = FALSE; + m_lastwrite = 0; } wxOutputStream::~wxOutputStream() @@ -502,13 +544,38 @@ wxOutputStream& wxOutputStream::operator<<(double f) return Write(strfloat, strfloat.Len()); } +#if wxUSE_SERIAL +wxOutputStream& wxOutputStream::operator<<(wxObject& obj) +{ + wxObjectOutputStream obj_s(*this); + obj_s.SaveObject(obj); + return *this; +} +#endif + +// ---------------------------------------------------------------------------- +// wxStream +// ---------------------------------------------------------------------------- + +wxStream::wxStream() + : wxInputStream(), wxOutputStream() +{ +} + // ---------------------------------------------------------------------------- // wxFilterInputStream // ---------------------------------------------------------------------------- +wxFilterInputStream::wxFilterInputStream() + : wxInputStream(NULL) +{ +} + wxFilterInputStream::wxFilterInputStream(wxInputStream& stream) : wxInputStream(NULL) { m_parent_i_stream = &stream; + wxDELETE(m_i_streambuf); // In case m_i_streambuf has been initialized. + m_i_destroybuf = FALSE; m_i_streambuf = stream.InputStreamBuffer(); } @@ -531,14 +598,20 @@ off_t wxFilterInputStream::DoTellInput() const return m_parent_i_stream->TellI(); } - // ---------------------------------------------------------------------------- // wxFilterOutputStream // ---------------------------------------------------------------------------- +wxFilterOutputStream::wxFilterOutputStream() + : wxOutputStream(NULL) +{ +} + wxFilterOutputStream::wxFilterOutputStream(wxOutputStream& stream) : wxOutputStream(NULL) { m_parent_o_stream = &stream; + wxDELETE(m_o_streambuf); // In case m_o_streambuf has been initialized. + m_o_destroybuf = FALSE; m_o_streambuf = stream.OutputStreamBuffer(); } @@ -561,6 +634,19 @@ off_t wxFilterOutputStream::DoTellOutput() const return m_parent_o_stream->TellO(); } +// ---------------------------------------------------------------------------- +// wxFilterStream +// ---------------------------------------------------------------------------- + +wxFilterStream::wxFilterStream() +{ +} + +wxFilterStream::wxFilterStream(wxStream& stream) + : wxFilterInputStream(stream), wxFilterOutputStream(stream) +{ +} + // ---------------------------------------------------------------------------- // Some IOManip function // ----------------------------------------------------------------------------