- // First case: the buffer to write is larger than the stream buffer,
- // we split it
- if (size > buf_left) {
- PutToBuffer(buffer, buf_left);
- size -= buf_left;
- buffer = (char *)buffer + buf_left; // ANSI C++ violation.
+size_t wxStreamBuffer::Write(const void *buffer, size_t size)
+{
+ if (m_stream)
+ {
+ // lasterror is reset before all new IO calls
+ m_stream->Reset();
+ }
+
+ size_t ret = 0;
+
+ if ( !HasBuffer() && m_fixed )
+ {
+ wxOutputStream *outStream = GetOutputStream();
+
+ wxCHECK_MSG( outStream, 0, _T("should have a stream in wxStreamBuffer") );
+
+ // no buffer, just forward the call to the stream
+ ret = outStream->OnSysWrite(buffer, size);
+ }
+ else // we [may] have a buffer, use it
+ {
+ size_t orig_size = size;
+
+ while ( size > 0 )
+ {
+ size_t left = GetBytesLeft();
+
+ // if the buffer is too large to fit in the stream buffer, split
+ // it in smaller parts
+ //
+ // NB: If stream buffer isn't fixed (as for wxMemoryOutputStream),
+ // we always go to the second case.
+ //
+ // FIXME: fine, but if it fails we should (re)try writing it by
+ // chunks as this will (hopefully) always work (VZ)
+
+ if ( size > left && m_fixed )
+ {
+ PutToBuffer(buffer, left);
+ size -= left;
+ buffer = (char *)buffer + left;
+
+ if ( !FlushBuffer() )
+ {
+ SetError(wxSTREAM_WRITE_ERROR);
+
+ break;
+ }
+
+ m_buffer_pos = m_buffer_start;
+ }
+ else // we can do it in one gulp
+ {
+ PutToBuffer(buffer, size);
+ size = 0;
+ }
+ }
+
+ ret = orig_size - size;
+ }