From 9e33cfc24af569490b3408a6223b305502110787 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Sat, 17 Mar 2012 15:30:43 +1100 Subject: [PATCH] Implement progress counter on zpaq compress/decompress per thread within the Reader function. --- libzpaq/libzpaq.h | 44 +++++++++++++++++++++++++++++++++++++++----- lrzip.h | 6 ++++-- stream.c | 6 ++++-- 3 files changed, 47 insertions(+), 9 deletions(-) diff --git a/libzpaq/libzpaq.h b/libzpaq/libzpaq.h index 5836153..b10bf71 100644 --- a/libzpaq/libzpaq.h +++ b/libzpaq/libzpaq.h @@ -441,6 +441,7 @@ void compress(Reader* in, Writer* out, int level); /////////////////////////// lrzip functions ////////////////// +#include #ifndef uchar #define uchar unsigned char #endif @@ -453,9 +454,32 @@ typedef long long int i64; struct bufRead: public libzpaq::Reader { uchar *s_buf; i64 *s_len; - bufRead(uchar *buf_, i64 *n_): s_buf(buf_), s_len(n_) {} + i64 total_len; + int *last_pct; + bool progress; + long thread; + FILE *msgout; + + bufRead(uchar *buf_, i64 *n_, i64 total_len_, int *last_pct_, bool progress_, long thread_, FILE *msgout_): + s_buf(buf_), s_len(n_), total_len(total_len_), last_pct(last_pct_), progress(progress_), thread(thread_), msgout(msgout_) {} int get() { + if (progress && !(*s_len % 128)) { + int pct = (total_len - *s_len) * 100 / total_len; + + if (pct / 10 != *last_pct / 10) { + int i; + + fprintf(msgout, "\r\t\t\t\tZPAQ\t"); + for (i = 0; i < thread; i++) + fprintf(msgout, "\t"); + fprintf(msgout, "%ld: %i%% \r", + thread + 1, pct); + fflush(msgout); + *last_pct = pct; + } + } + if (likely(*s_len > 0)) { (*s_len)--; return ((int)(uchar)*s_buf++); @@ -490,15 +514,25 @@ struct bufWrite: public libzpaq::Writer { } }; -extern "C" void zpaq_compress(uchar *c_buf, i64 *c_len, uchar *s_buf, i64 s_len, int level) { - bufRead bufR(s_buf, &s_len); +extern "C" void zpaq_compress(uchar *c_buf, i64 *c_len, uchar *s_buf, i64 s_len, int level, + FILE *msgout, bool progress, long thread) +{ + i64 total_len = s_len; + int last_pct = 100; + + bufRead bufR(s_buf, &s_len, total_len, &last_pct, progress, thread, msgout); bufWrite bufW(c_buf, c_len); compress (&bufR, &bufW, level); } -extern "C" void zpaq_decompress(uchar *s_buf, i64 *d_len, uchar *c_buf, i64 c_len) { - bufRead bufR(c_buf, &c_len); +extern "C" void zpaq_decompress(uchar *s_buf, i64 *d_len, uchar *c_buf, i64 c_len, + FILE *msgout, bool progress, long thread) +{ + i64 total_len = c_len; + int last_pct = 100; + + bufRead bufR(c_buf, &c_len, total_len, &last_pct, progress, thread, msgout); bufWrite bufW(s_buf, d_len); decompress(&bufR, &bufW); diff --git a/lrzip.h b/lrzip.h index 297e227..704ebb8 100644 --- a/lrzip.h +++ b/lrzip.h @@ -42,6 +42,8 @@ void clear_tmpinbuf(rzip_control *control); bool clear_tmpinfile(rzip_control *control); void close_tmpinbuf(rzip_control *control); bool initialize_control(rzip_control *control); -extern void zpaq_compress(uchar *c_buf, long long *c_len, uchar *s_buf, long long s_len, int level); -extern void zpaq_decompress(uchar *s_buf, long long *d_len, uchar *c_buf, long long c_len); +extern void zpaq_compress(uchar *c_buf, i64 *c_len, uchar *s_buf, i64 s_len, int level, + FILE *msgout, bool progress, long thread); +extern void zpaq_decompress(uchar *s_buf, i64 *d_len, uchar *c_buf, i64 c_len, + FILE *msgout, bool progress, long thread); #endif diff --git a/stream.c b/stream.c index 7dc56f1..ea96e55 100644 --- a/stream.c +++ b/stream.c @@ -191,7 +191,8 @@ static int zpaq_compress_buf(rzip_control *control, struct compress_thread *cthr c_len = 0; - zpaq_compress(c_buf, &c_len, cthread->s_buf, cthread->s_len, control->compression_level / 4 + 1); + zpaq_compress(c_buf, &c_len, cthread->s_buf, cthread->s_len, control->compression_level / 4 + 1, + control->msgout, SHOW_PROGRESS ? true: false, thread); if (unlikely(c_len >= cthread->c_len)) { print_maxverbose("Incompressible block\n"); @@ -444,7 +445,8 @@ static int zpaq_decompress_buf(rzip_control *control __UNUSED__, struct uncomp_t } dlen = 0; - zpaq_decompress(ucthread->s_buf, &dlen, c_buf, ucthread->c_len); + zpaq_decompress(ucthread->s_buf, &dlen, c_buf, ucthread->c_len, + control->msgout, SHOW_PROGRESS ? true: false, thread); if (unlikely(dlen != ucthread->u_len)) { print_err("Inconsistent length after decompression. Got %ld bytes, expected %lld\n", dlen, ucthread->u_len);