@@ -236,7 +236,11 @@ void check_file(const Data& file, zlib_wrapper type, int mode) {
236236 error_exit (" check file: error writing output" , 3 );
237237}
238238
239- void zlib_file (const char * name, zlib_wrapper type, int width, int check) {
239+ void zlib_file (const char * name,
240+ zlib_wrapper type,
241+ int width,
242+ int check,
243+ bool output_csv_format) {
240244 /*
241245 * Read the file data.
242246 */
@@ -257,7 +261,9 @@ void zlib_file(const char* name, zlib_wrapper type, int width, int check) {
257261 * Report compression strategy and file name.
258262 */
259263 const char * strategy = zlib_level_strategy_name (zlib_compression_level);
260- printf (" %s%-40s :\n " , strategy, name);
264+ if (!output_csv_format) {
265+ printf (" %s%-40s :\n " , strategy, name);
266+ }
261267
262268 /*
263269 * Chop the data into blocks.
@@ -329,19 +335,28 @@ void zlib_file(const char* name, zlib_wrapper type, int width, int check) {
329335 std::sort (ctime, ctime + runs);
330336 std::sort (utime, utime + runs);
331337
332- double deflate_rate_med = length * repeats / mega_byte / ctime[runs / 2 ];
333- double inflate_rate_med = length * repeats / mega_byte / utime[runs / 2 ];
334- double deflate_rate_max = length * repeats / mega_byte / ctime[0 ];
335- double inflate_rate_max = length * repeats / mega_byte / utime[0 ];
336-
337- // type, block size, compression ratio, etc
338- printf (" %s: [b %dM] bytes %*d -> %*u %4.2f%%" ,
339- zlib_wrapper_name (type), block_size / (1 << 20 ), width, length, width,
340- unsigned (output_length), output_length * 100.0 / length);
341-
342- // compress / uncompress median (max) rates
343- printf (" comp %5.1f (%5.1f) MB/s uncomp %5.1f (%5.1f) MB/s\n " ,
344- deflate_rate_med, deflate_rate_max, inflate_rate_med, inflate_rate_max);
338+ double deflate_rate_med, inflate_rate_med, deflate_rate_max, inflate_rate_max;
339+ deflate_rate_med = length * repeats / mega_byte / ctime[runs / 2 ];
340+ inflate_rate_med = length * repeats / mega_byte / utime[runs / 2 ];
341+ deflate_rate_max = length * repeats / mega_byte / ctime[0 ];
342+ inflate_rate_max = length * repeats / mega_byte / utime[0 ];
343+ double compress_ratio = output_length * 100.0 / length;
344+
345+ if (!output_csv_format) {
346+ // type, block size, compression ratio, etc
347+ printf (" %s: [b %dM] bytes %*d -> %*u %4.2f%%" , zlib_wrapper_name (type),
348+ block_size / (1 << 20 ), width, length, width,
349+ unsigned (output_length), compress_ratio);
350+
351+ // compress / uncompress median (max) rates
352+ printf (" comp %5.1f (%5.1f) MB/s uncomp %5.1f (%5.1f) MB/s\n " ,
353+ deflate_rate_med, deflate_rate_max, inflate_rate_med,
354+ inflate_rate_max);
355+ } else {
356+ printf (" %s\t %.5lf\t %.5lf\t %.5lf\t %.5lf\t %.5lf\n " , name, deflate_rate_med,
357+ inflate_rate_med, deflate_rate_max, inflate_rate_max,
358+ compress_ratio);
359+ }
345360}
346361
347362static int argn = 1 ;
@@ -363,8 +378,10 @@ void get_field_width(int argc, char* argv[], int& value) {
363378}
364379
365380void usage_exit (const char * program) {
366- static auto * options = " gzip|zlib|raw"
367- " [--compression 0:9] [--huffman|--rle] [--field width] [--check]" ;
381+ static auto * options =
382+ " gzip|zlib|raw"
383+ " [--compression 0:9] [--huffman|--rle] [--field width] [--check]"
384+ " [--csv]" ;
368385 printf (" usage: %s %s files ...\n " , program, options);
369386 printf (" zlib version: %s\n " , ZLIB_VERSION);
370387 exit (1 );
@@ -383,7 +400,7 @@ int main(int argc, char* argv[]) {
383400
384401 int size_field_width = 0 ;
385402 int file_check = 0 ;
386-
403+ bool output_csv = false ;
387404 while (argn < argc && argv[argn][0 ] == ' -' ) {
388405 if (get_option (argc, argv, " --compression" )) {
389406 if (!get_compression (argc, argv, zlib_compression_level))
@@ -398,6 +415,11 @@ int main(int argc, char* argv[]) {
398415 file_check = 2 ;
399416 } else if (get_option (argc, argv, " --field" )) {
400417 get_field_width (argc, argv, size_field_width);
418+ } else if (get_option (argc, argv, " --csv" )) {
419+ output_csv = true ;
420+ printf (
421+ " filename\t compression\t decompression\t comp_max\t "
422+ " decomp_max\t compress_ratio\n " );
401423 } else {
402424 usage_exit (argv[0 ]);
403425 }
@@ -408,8 +430,9 @@ int main(int argc, char* argv[]) {
408430
409431 if (size_field_width < 6 )
410432 size_field_width = 6 ;
411- while (argn < argc)
412- zlib_file (argv[argn++], type, size_field_width, file_check);
433+ while (argn < argc) {
434+ zlib_file (argv[argn++], type, size_field_width, file_check, output_csv);
435+ }
413436
414437 return 0 ;
415438}
0 commit comments