+ // 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)
+{
+ 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)
+{
+ 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;