- // 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 {
- m_buffer_end = m_buffer_start+read_ret;
- m_buffer_pos = m_buffer_start;
- }
- } else {
+// copy up to size bytes from our buffer into the provided one
+void wxStreamBuffer::GetFromBuffer(void *buffer, size_t size)
+{
+ // don't get more bytes than left in the buffer
+ size_t left = GetBytesLeft();
+
+ if ( size > left )
+ size = left;
+
+ memcpy(buffer, m_buffer_pos, size);
+ m_buffer_pos += size;
+}
+
+// copy the contents of the provided buffer into this one
+void wxStreamBuffer::PutToBuffer(const void *buffer, size_t size)
+{
+ size_t left = GetBytesLeft();
+
+ if ( size > left )
+ {
+ if ( m_fixed )
+ {
+ // we can't realloc the buffer, so just copy what we can
+ size = left;
+ }
+ else // !m_fixed
+ {
+ // realloc the buffer to have enough space for the data
+ if ( m_buffer_pos + size > m_buffer_end )
+ {
+ size_t delta = m_buffer_pos - m_buffer_start;
+ size_t new_size = delta + size;
+
+ char *startOld = m_buffer_start;
+ m_buffer_start = (char *)realloc(m_buffer_start, new_size);
+ if ( !m_buffer_start )
+ {
+ // don't leak memory if realloc() failed
+ m_buffer_start = startOld;
+
+ // what else can we do?
+ return;
+ }
+
+ // adjust the pointers invalidated by realloc()
+ m_buffer_pos = m_buffer_start + delta;
+ m_buffer_end = m_buffer_start + new_size;
+ } // else: the buffer is big enough
+ }
+ }
+
+ memcpy(m_buffer_pos, buffer, size);
+ m_buffer_pos += size;
+}
+
+void wxStreamBuffer::PutChar(char c)
+{
+ wxOutputStream *outStream = GetOutputStream();
+
+ wxCHECK_RET( outStream, _T("should have a stream in wxStreamBuffer") );