From afe761f3b2a97873cfe6363cefacaf1aafc22d84 Mon Sep 17 00:00:00 2001 From: simon Date: Tue, 17 Nov 2009 18:20:48 +0000 Subject: [PATCH] Flexibly report sizes in Kb, Mb, Gb etc as appropriate. The previous fixed Mb was inconvenient at both ends. Original patch from James Beal, though I've polished it pretty much into unrecognisability. git-svn-id: svn://svn.tartarus.org/sgt/agedu@8751 cda61777-01e9-0310-a592-d414129be87e --- html.c | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/html.c b/html.c index 22cdc17..dacd68c 100644 --- a/html.c +++ b/html.c @@ -24,7 +24,7 @@ struct html { time_t now; }; -static void vhtprintf(struct html *ctx, char *fmt, va_list ap) +static void vhtprintf(struct html *ctx, const char *fmt, va_list ap) { va_list ap2; int size, size2; @@ -50,7 +50,7 @@ static void vhtprintf(struct html *ctx, char *fmt, va_list ap) ctx->buflen += size; } -static void htprintf(struct html *ctx, char *fmt, ...) +static void htprintf(struct html *ctx, const char *fmt, ...) { va_list ap; va_start(ap, fmt); @@ -317,12 +317,31 @@ static void print_heading(struct html *ctx, const char *title) "%s\n\n", title); } +static void compute_display_size(unsigned long long size, + const char **fmt, double *display_size) +{ + static const char *const fmts[] = { + "%g b", "%g Kb", "%#.1f Mb", "%#.1f Gb", "%#.1f Tb", + "%#.1f Pb", "%#.1f Eb", "%#.1f Zb", "%#.1f Yb" + }; + int shift = 0; + + while (size >= 1024 && shift < lenof(fmts)-1) { + size >>= 10; + shift++; + } + *display_size = (double)size; + *fmt = fmts[shift]; +} + #define PIXEL_SIZE 600 /* FIXME: configurability? */ static void write_report_line(struct html *ctx, struct vector *vec) { unsigned long long size, asize, divisor; + double display_size; int pix, newpix; int i; + const char *unitsfmt; /* * A line with literally zero space usage should not be @@ -344,9 +363,11 @@ static void write_report_line(struct html *ctx, struct vector *vec) * Find the total size of this subdirectory. */ size = vec->sizes[MAXCOLOUR]; + compute_display_size(size, &unitsfmt, &display_size); htprintf(ctx, "\n" - "%lluMb\n", - ((size + ((1<<20)-1)) >> 20)); /* convert to Mb, rounding up */ + ""); + htprintf(ctx, unitsfmt, display_size); + htprintf(ctx, "\n"); /* * Generate a colour bar. -- 2.11.0