/* Flush a compression buffer. */ static int compress_buffer_flush (void *closure) { struct compress_buffer *cb = closure; /* This is only used within the while loop below, but allocated here for * efficiency. */ static char *buffer = NULL; if (!buffer) buffer = pagealign_xalloc (BUFFER_DATA_SIZE); cb->zstr.avail_in = 0; cb->zstr.next_in = NULL; while (1) { int zstatus; cb->zstr.avail_out = BUFFER_DATA_SIZE; cb->zstr.next_out = (unsigned char *) buffer; zstatus = deflate (&cb->zstr, Z_SYNC_FLUSH); /* The deflate function will return Z_BUF_ERROR if it can't do anything, which in this case means that all data has been flushed. */ if (zstatus == Z_BUF_ERROR) break; if (zstatus != Z_OK) { compress_error (0, zstatus, &cb->zstr, "deflate flush"); return EIO; } if (cb->zstr.avail_out != BUFFER_DATA_SIZE) buf_output (cb->buf, buffer, BUFFER_DATA_SIZE - cb->zstr.avail_out); /* If the deflate function did not fill the output buffer, then all data has been flushed. */ if (cb->zstr.avail_out > 0) break; } /* Now flush the underlying buffer. Note that if the original call to buf_flush passed 1 for the BLOCK argument, then the buffer will already have been set into blocking mode, so we should always pass 0 here. */ return buf_flush (cb->buf, 0); }
/* The input function for a log buffer. */ static int log_buffer_input (void *closure, char *data, size_t need, size_t size, size_t *got) { struct log_buffer *lb = closure; int status; assert (lb->buf->input); status = (*lb->buf->input) (lb->buf->closure, data, need, size, got); if (status != 0) return status; if ( #ifdef PROXY_SUPPORT !lb->disabled && #endif /* PROXY_SUPPORT */ *got > 0) { #ifdef PROXY_SUPPORT if (!lb->tofile && xsum (*got, buf_count_mem (lb->back_buf)) >= lb->max) lb->tofile = true; if (lb->tofile) { if (!lb->log) log_buffer_force_file (lb); if (lb->log) { #endif /* PROXY_SUPPORT */ if (fwrite (data, 1, *got, lb->log) != *got) error ( #ifdef PROXY_SUPPORT lb->fatal_errors, #else /* !PROXY_SUPPORT */ false, #endif /* PROXY_SUPPORT */ errno, "writing to log file"); fflush (lb->log); #ifdef PROXY_SUPPORT } } else /* Log to memory buffer. */ buf_output (lb->back_buf, data, *got); #endif /* PROXY_SUPPORT */ } return 0; }
/* Output data to a compression buffer. * * GLOBALS * gzip_level If GZIP_LEVEL has changed to a value different from * CLOSURE->level, then set the compression level on the * stream to the new value. */ static int compress_buffer_output (void *closure, const char *data, size_t have, size_t *wrote) { struct compress_buffer *cb = closure; /* This is only used within the while loop below, but allocated here for * efficiency. */ static char *buffer = NULL; if (!buffer) buffer = pagealign_xalloc (BUFFER_DATA_SIZE); if (cb->level != gzip_level) { cb->level = gzip_level; deflateParams (&cb->zstr, gzip_level, Z_DEFAULT_STRATEGY); } cb->zstr.avail_in = have; cb->zstr.next_in = (unsigned char *) data; while (cb->zstr.avail_in > 0) { int zstatus; cb->zstr.avail_out = BUFFER_DATA_SIZE; cb->zstr.next_out = (unsigned char *) buffer; zstatus = deflate (&cb->zstr, Z_NO_FLUSH); if (zstatus != Z_OK) { compress_error (0, zstatus, &cb->zstr, "deflate"); return EIO; } if (cb->zstr.avail_out != BUFFER_DATA_SIZE) buf_output (cb->buf, buffer, BUFFER_DATA_SIZE - cb->zstr.avail_out); } *wrote = have; /* We will only be here because buf_send_output was called on the compression buffer. That means that we should now call buf_send_output on the underlying buffer. */ return buf_send_output (cb->buf); }
/* Shut down an output buffer. */ static int compress_buffer_shutdown_output (struct buffer *buf) { struct compress_buffer *cb = buf->closure; int zstatus, status; /* This is only used within the while loop below, but allocated here for * efficiency. */ static char *buffer = NULL; if (!buffer) buffer = pagealign_xalloc (BUFFER_DATA_SIZE); do { cb->zstr.avail_out = BUFFER_DATA_SIZE; cb->zstr.next_out = (unsigned char *) buffer; zstatus = deflate (&cb->zstr, Z_FINISH); if (zstatus != Z_OK && zstatus != Z_STREAM_END) { compress_error (0, zstatus, &cb->zstr, "deflate finish"); return EIO; } if (cb->zstr.avail_out != BUFFER_DATA_SIZE) buf_output (cb->buf, buffer, BUFFER_DATA_SIZE - cb->zstr.avail_out); } while (zstatus != Z_STREAM_END); zstatus = deflateEnd (&cb->zstr); if (zstatus != Z_OK) { compress_error (0, zstatus, &cb->zstr, "deflateEnd"); return EIO; } status = buf_flush (cb->buf, 1); if (status != 0) return status; return buf_shutdown (cb->buf); }
void MyApp::DoStreamDemo2(wxCommandEvent& WXUNUSED(event)) { wxTextCtrl& textCtrl = * GetTextCtrl(); textCtrl.Clear(); textCtrl << _T("\nTesting wxBufferedStream:\n\n"); char ch,ch2; textCtrl.WriteText( _T("Writing number 0 to 9 to buffered wxFileOutputStream:\n\n") ); wxFileOutputStream file_output( file_name ); wxBufferedOutputStream buf_output( file_output ); for (ch = 0; ch < 10; ch++) buf_output.Write( &ch, 1 ); buf_output.Sync(); wxFileInputStream file_input( file_name ); for (ch2 = 0; ch2 < 10; ch2++) { file_input.Read( &ch, 1 ); textCtrl.WriteText( (wxChar)(ch + _T('0')) ); } textCtrl.WriteText( _T("\n\n\n") ); textCtrl.WriteText( _T("Writing number 0 to 9 to buffered wxFileOutputStream, then\n") ); textCtrl.WriteText( _T("seeking back to #3 and writing 0:\n\n") ); wxFileOutputStream file_output2( file_name2 ); wxBufferedOutputStream buf_output2( file_output2 ); for (ch = 0; ch < 10; ch++) buf_output2.Write( &ch, 1 ); buf_output2.SeekO( 3 ); ch = 0; buf_output2.Write( &ch, 1 ); buf_output2.Sync(); wxFileInputStream file_input2( file_name2 ); for (ch2 = 0; ch2 < 10; ch2++) { file_input2.Read( &ch, 1 ); textCtrl.WriteText( (wxChar)(ch + _T('0')) ); } textCtrl.WriteText( _T("\n\n\n") ); // now append 2000 bytes to file (bigger than buffer) buf_output2.SeekO( 0, wxFromEnd ); ch = 1; for (int i = 0; i < 2000; i++) buf_output2.Write( &ch, 1 ); buf_output2.Sync(); textCtrl.WriteText( _T("Reading number 0 to 9 from buffered wxFileInputStream, then\n") ); textCtrl.WriteText( _T("seeking back to #3 and reading the 0:\n\n") ); wxFileInputStream file_input3( file_name2 ); wxBufferedInputStream buf_input3( file_input3 ); for (ch2 = 0; ch2 < 10; ch2++) { buf_input3.Read( &ch, 1 ); textCtrl.WriteText( (wxChar)(ch + _T('0')) ); } for (int j = 0; j < 2000; j++) buf_input3.Read( &ch, 1 ); textCtrl.WriteText( _T("\n") ); buf_input3.SeekI( 3 ); buf_input3.Read( &ch, 1 ); textCtrl.WriteText( (wxChar)(ch + _T('0')) ); textCtrl.WriteText( _T("\n\n\n") ); }
void MyApp::DoStreamDemo(wxCommandEvent& WXUNUSED(event)) { wxTextCtrl& textCtrl = * GetTextCtrl(); textCtrl.Clear(); textCtrl << _T("\nTest fstream vs. wxFileStream:\n\n"); textCtrl.WriteText( _T("Writing to ofstream and wxFileOutputStream:\n") ); wxSTD ofstream std_file_output( "test_std.dat" ); wxFileOutputStream file_output( file_name ); wxBufferedOutputStream buf_output( file_output ); wxTextOutputStream text_output( buf_output ); wxString tmp; signed int si = 0xFFFFFFFF; tmp.Printf( _T("Signed int: %d\n"), si ); textCtrl.WriteText( tmp ); text_output << si << _T("\n"); std_file_output << si << "\n"; unsigned int ui = 0xFFFFFFFF; tmp.Printf( _T("Unsigned int: %u\n"), ui ); textCtrl.WriteText( tmp ); text_output << ui << _T("\n"); std_file_output << ui << "\n"; double d = 2.01234567890123456789; tmp.Printf( _T("Double: %f\n"), d ); textCtrl.WriteText( tmp ); text_output << d << _T("\n"); std_file_output << d << "\n"; float f = (float)0.00001; tmp.Printf( _T("Float: %f\n"), f ); textCtrl.WriteText( tmp ); text_output << f << _T("\n"); std_file_output << f << "\n"; wxString str( _T("Hello!") ); tmp.Printf( _T("String: %s\n"), str.c_str() ); textCtrl.WriteText( tmp ); text_output << str << _T("\n"); std_file_output << str.ToAscii() << "\n"; textCtrl.WriteText( _T("\nReading from ifstream:\n") ); wxSTD ifstream std_file_input( "test_std.dat" ); std_file_input >> si; tmp.Printf( _T("Signed int: %d\n"), si ); textCtrl.WriteText( tmp ); std_file_input >> ui; tmp.Printf( _T("Unsigned int: %u\n"), ui ); textCtrl.WriteText( tmp ); std_file_input >> d; tmp.Printf( _T("Double: %f\n"), d ); textCtrl.WriteText( tmp ); std_file_input >> f; tmp.Printf( _T("Float: %f\n"), f ); textCtrl.WriteText( tmp ); char std_buf[200]; std_file_input >> std_buf; str = wxString::FromAscii(std_buf); tmp.Printf( _T("String: %s\n"), str.c_str() ); textCtrl.WriteText( tmp ); textCtrl.WriteText( _T("\nReading from wxFileInputStream:\n") ); buf_output.Sync(); wxFileInputStream file_input( file_name ); wxBufferedInputStream buf_input( file_input ); wxTextInputStream text_input( file_input ); text_input >> si; tmp.Printf( _T("Signed int: %d\n"), si ); textCtrl.WriteText( tmp ); text_input >> ui; tmp.Printf( _T("Unsigned int: %u\n"), ui ); textCtrl.WriteText( tmp ); text_input >> d; tmp.Printf( _T("Double: %f\n"), d ); textCtrl.WriteText( tmp ); text_input >> f; tmp.Printf( _T("Float: %f\n"), f ); textCtrl.WriteText( tmp ); text_input >> str; tmp.Printf( _T("String: %s\n"), str.c_str() ); textCtrl.WriteText( tmp ); textCtrl << _T("\nTest for wxDataStream:\n\n"); textCtrl.WriteText( _T("Writing to wxDataOutputStream:\n") ); file_output.SeekO( 0 ); wxDataOutputStream data_output( buf_output ); wxInt16 i16 = (unsigned short)0xFFFF; tmp.Printf( _T("Signed int16: %d\n"), (int)i16 ); textCtrl.WriteText( tmp ); data_output.Write16( i16 ); wxUint16 ui16 = 0xFFFF; tmp.Printf( _T("Unsigned int16: %u\n"), (unsigned int) ui16 ); textCtrl.WriteText( tmp ); data_output.Write16( ui16 ); d = 2.01234567890123456789; tmp.Printf( _T("Double: %f\n"), d ); textCtrl.WriteText( tmp ); data_output.WriteDouble( d ); str = _T("Hello!"); tmp.Printf( _T("String: %s\n"), str.c_str() ); textCtrl.WriteText( tmp ); data_output.WriteString( str ); buf_output.Sync(); textCtrl.WriteText( _T("\nReading from wxDataInputStream:\n") ); file_input.SeekI( 0 ); wxDataInputStream data_input( buf_input ); i16 = data_input.Read16(); tmp.Printf( _T("Signed int16: %d\n"), (int)i16 ); textCtrl.WriteText( tmp ); ui16 = data_input.Read16(); tmp.Printf( _T("Unsigned int16: %u\n"), (unsigned int) ui16 ); textCtrl.WriteText( tmp ); d = data_input.ReadDouble(); tmp.Printf( _T("Double: %f\n"), d ); textCtrl.WriteText( tmp ); str = data_input.ReadString(); tmp.Printf( _T("String: %s\n"), str.c_str() ); textCtrl.WriteText( tmp ); }