+ free(m_wback);
+}
+
+bool wxInputStream::CanRead() const
+{
+ // we don't know if there is anything to read or not and by default we
+ // prefer to be optimistic and try to read data unless we know for sure
+ // there is no more of it
+ return m_lasterror != wxSTREAM_EOF;
+}
+
+bool wxInputStream::Eof() const
+{
+ // the only way the base class can know we're at EOF is when we'd already
+ // tried to read beyond it in which case last error is set accordingly
+ return GetLastError() == wxSTREAM_EOF;
+}
+
+char *wxInputStream::AllocSpaceWBack(size_t needed_size)
+{
+ // get number of bytes left from previous wback buffer
+ size_t toget = m_wbacksize - m_wbackcur;
+
+ // allocate a buffer large enough to hold prev + new data
+ char *temp_b = (char *)malloc(needed_size + toget);
+
+ if (!temp_b)
+ return NULL;
+
+ // copy previous data (and free old buffer) if needed
+ if (m_wback)
+ {
+ memmove(temp_b + needed_size, m_wback + m_wbackcur, toget);
+ free(m_wback);
+ }
+
+ // done
+ m_wback = temp_b;
+ m_wbackcur = 0;
+ m_wbacksize = needed_size + toget;
+
+ return m_wback;
+}
+
+size_t wxInputStream::GetWBack(void *buf, size_t size)
+{
+ wxASSERT_MSG( buf, wxT("Warning: Null pointer is about to be used") );
+
+ /* Clear buffer first */
+ memset(buf, 0x00, size);
+
+ if (!m_wback)
+ return 0;
+
+ // how many bytes do we have in the buffer?
+ size_t toget = m_wbacksize - m_wbackcur;
+
+ if ( size < toget )
+ {
+ // we won't read everything
+ toget = size;
+ }
+
+ // copy the data from the cache
+ memcpy(buf, m_wback + m_wbackcur, toget);
+
+ m_wbackcur += toget;
+ 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;
+}
+
+size_t wxInputStream::Ungetch(const void *buf, size_t bufsize)
+{
+ wxASSERT_MSG( buf, wxT("Warning: Null pointer is about to be used in Ungetch()") );
+
+ if ( m_lasterror != wxSTREAM_NO_ERROR && m_lasterror != wxSTREAM_EOF )
+ {
+ // can't operate on this stream until the error is cleared
+ return 0;
+ }
+
+ char *ptrback = AllocSpaceWBack(bufsize);
+ if (!ptrback)
+ return 0;
+
+ // Eof() shouldn't return true any longer
+ if ( m_lasterror == wxSTREAM_EOF )
+ m_lasterror = wxSTREAM_NO_ERROR;
+
+ memcpy(ptrback, buf, bufsize);
+ return bufsize;
+}
+
+bool wxInputStream::Ungetch(char c)
+{
+ return Ungetch(&c, sizeof(c)) != 0;
+}
+
+int wxInputStream::GetC()
+{
+ unsigned char c;
+ Read(&c, sizeof(c));
+ return LastRead() ? c : wxEOF;
+}
+
+wxInputStream& wxInputStream::Read(void *buf, size_t size)
+{
+ wxASSERT_MSG( buf, wxT("Warning: Null pointer is about to be read") );
+
+ char *p = (char *)buf;
+ m_lastcount = 0;
+
+ size_t read = GetWBack(buf, size);
+ for ( ;; )
+ {
+ size -= read;
+ m_lastcount += read;
+ p += read;
+
+ if ( !size )
+ {
+ // we read the requested amount of data
+ break;
+ }
+
+ if ( p != buf && !CanRead() )
+ {
+ // we have already read something and we would block in OnSysRead()
+ // now: don't do it but return immediately
+ break;
+ }
+
+ read = OnSysRead(p, size);
+ if ( !read )
+ {
+ // no more data available
+ break;
+ }
+ }
+
+ return *this;