X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/421db2ada5702924d874c8c11e4a6fa0129ecaa5..67fade3398f9c35a8e042749ed340c898f7e7bdd:/src/common/stream.cpp diff --git a/src/common/stream.cpp b/src/common/stream.cpp index 15d6b73c36..c6ffc3895b 100644 --- a/src/common/stream.cpp +++ b/src/common/stream.cpp @@ -38,7 +38,7 @@ #include #include "wx/stream.h" #include "wx/datstrm.h" -#include "wx/objstrm.h" +#include "wx/textfile.h" // ---------------------------------------------------------------------------- // constants @@ -68,8 +68,8 @@ void wxStreamBuffer::InitBuffer() m_buffer_pos = NULL; m_buffer_size = 0; - // there is nothing to destroy anyhow - m_destroybuf = FALSE; + // if we are going to allocate the buffer, we should free it later as well + m_destroybuf = TRUE; } void wxStreamBuffer::Init() @@ -94,7 +94,14 @@ wxStreamBuffer::wxStreamBuffer(BufMode mode) { Init(); - m_stream = new wxStreamBase; + wxASSERT_MSG(mode != read_write, wxT("you have to use the other ctor for read_write mode") ); + if ( mode == read ) + m_stream = new wxInputStream; + else if ( mode == write) + m_stream = new wxOutputStream; + else + m_stream = NULL; + m_mode = mode; m_flushable = FALSE; @@ -669,7 +676,12 @@ bool wxInputStream::Eof() const char c; self->Read(&c, 1); - if ( GetLastError() == wxSTREAM_EOF ) + + // some streams can know that they're at EOF before actually trying to + // read beyond the end of stream (e.g. files) while others have no way of + // knowing it, so to provide the same behaviour in all cases we only + // return TRUE from here if the character really couldn't be read + if ( !self->LastRead() && GetLastError() == wxSTREAM_EOF ) { return TRUE; } @@ -707,25 +719,32 @@ char *wxInputStream::AllocSpaceWBack(size_t needed_size) size_t wxInputStream::GetWBack(void *buf, size_t bsize) { - size_t toget = m_wbacksize-m_wbackcur; - if (!m_wback) return 0; - if (bsize < toget) - toget = bsize; + // how many bytes do we have in the buffer? + size_t toget = m_wbacksize - m_wbackcur; - memcpy(buf, (m_wback+m_wbackcur), toget); + if ( bsize < toget ) + { + // we won't read everything + toget = bsize; + } + + // copy the data from the cache + memcpy(buf, m_wback + m_wbackcur, toget); m_wbackcur += toget; - if (m_wbackcur == m_wbacksize) + if ( m_wbackcur == m_wbacksize ) { + // TODO: should we really free it here all the time? maybe keep it? free(m_wback); m_wback = NULL; m_wbacksize = 0; m_wbackcur = 0; } + // return the number of bytes copied return toget; } @@ -837,19 +856,6 @@ off_t wxInputStream::TellI() const return pos; } -// -------------------- -// Overloaded operators -// -------------------- - -#if wxUSE_SERIAL -wxInputStream& wxInputStream::operator>>(wxObject *& obj) -{ - wxObjectInputStream obj_s(*this); - obj = obj_s.LoadObject(); - return *this; -} -#endif // wxUSE_SERIAL - // ---------------------------------------------------------------------------- // wxOutputStream @@ -900,14 +906,6 @@ void wxOutputStream::Sync() { } -#if wxUSE_SERIAL -wxOutputStream& wxOutputStream::operator<<(wxObject& obj) -{ - wxObjectOutputStream obj_s(*this); - obj_s.SaveObject(obj); - return *this; -} -#endif // wxUSE_SERIAL // ---------------------------------------------------------------------------- // wxCountingOutputStream @@ -1037,19 +1035,26 @@ char wxBufferedInputStream::Peek() wxInputStream& wxBufferedInputStream::Read(void *buf, size_t size) { - size_t retsize; + // reset the error flag + m_lasterror = wxStream_NOERROR; + + // first read from the already cached data + m_lastcount = GetWBack(buf, size); - retsize = GetWBack(buf, size); - m_lastcount = retsize; - if ( retsize == size ) + // do we have to read anything more? + if ( m_lastcount < size ) { - m_lasterror = wxStream_NOERROR; - return *this; - } - size -= retsize; - buf = (char *)buf + retsize; + size -= m_lastcount; + buf = (char *)buf + m_lastcount; + + // the call to wxStreamBuffer::Read() below will reset our m_lastcount, + // so save it + size_t countOld = m_lastcount; - m_i_streambuf->Read(buf, size); + m_i_streambuf->Read(buf, size); + + m_lastcount += countOld; + } return *this; } @@ -1171,15 +1176,9 @@ void wxBufferedOutputStream::SetOutputStreamBuffer(wxStreamBuffer *buffer) wxOutputStream& wxEndL(wxOutputStream& stream) { -#ifdef __MSW__ - return stream.Write("\r\n", 2); -#else -#ifdef __WXMAC__ - return stream.Write("\r", 1); -#else - return stream.Write("\n", 1); -#endif -#endif + static const wxChar *eol = wxTextFile::GetEOL(); + + return stream.Write(eol, wxStrlen(eol)); } #endif