X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/123a7fddb89d0d39f8c280aea95182d054c8887b..524bf39144b894ebc489d599a99272540941debd:/src/common/stream.cpp?ds=sidebyside diff --git a/src/common/stream.cpp b/src/common/stream.cpp index 44226e5721..3c038e14d9 100644 --- a/src/common/stream.cpp +++ b/src/common/stream.cpp @@ -42,6 +42,7 @@ wxStreamBuffer::wxStreamBuffer(wxOutputStream& o_stream) wxStreamBuffer::~wxStreamBuffer() { + wxDELETEA(m_buffer_start); } void wxStreamBuffer::WriteBack(char c) @@ -51,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; @@ -58,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() @@ -124,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 { @@ -204,6 +214,7 @@ wxInputStream::wxInputStream() m_i_destroybuf = TRUE; m_i_streambuf = new wxStreamBuffer(*this); m_eof = FALSE; + m_lastread = 0; } wxInputStream::wxInputStream(wxStreamBuffer *buffer) @@ -211,6 +222,7 @@ wxInputStream::wxInputStream(wxStreamBuffer *buffer) m_i_destroybuf = FALSE; m_i_streambuf = buffer; m_eof = FALSE; + m_lastread = 0; } wxInputStream::~wxInputStream() @@ -336,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)) { @@ -351,34 +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: @@ -403,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() @@ -521,20 +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(); } @@ -557,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(); } @@ -587,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 // ----------------------------------------------------------------------------