+ wxInputStream *self = wxConstCast(this, wxInputStream);
+
+ char c;
+ self->Read(&c, 1);
+
+ // 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;
+ }
+
+ self->Ungetch(c);
+
+ return FALSE;
+}
+
+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 bsize)
+{
+ if (!m_wback)
+ return 0;
+
+ // how many bytes do we have in the buffer?
+ size_t toget = m_wbacksize - m_wbackcur;
+
+ 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 )
+ {
+ // 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(char)) != 0;