From d44ab700d065a5ac859df24969a4d993cee122ec Mon Sep 17 00:00:00 2001 From: Matthew Hambley Date: Sun, 25 Apr 2004 11:40:05 +0000 Subject: [project @ 2004-04-25 11:40:05 by matthewh] A more human face to download bytecounts. Needs someone who can speak French to correct the fr messages file. svn path=/import/netsurf/; revision=803 --- utils/utils.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) (limited to 'utils/utils.c') diff --git a/utils/utils.c b/utils/utils.c index 1c080e8af..885b57ab7 100644 --- a/utils/utils.c +++ b/utils/utils.c @@ -294,3 +294,54 @@ void clean_cookiejar(void) { xfree(cookies); } #endif + +/** We can have a fairly good estimate of how long the buffer needs to + * be. The unsigned long can store a value representing a maximum size + * of around 4 GB. Therefore the greatest space required is to + * represent 1023MB. Currently that would be represented as "1023MB" so 12 + * including a null terminator. + * Ideally we would be able to know this value for sure, in the mean + * time the following should suffice. + **/ + +#define BYTESIZE_BUFFER_SIZE 20 + +/** + * Does a simple conversion which assumes the user speaks English. The buffer + * returned is one of two static ones so may change each time this call is + * made. Don't store the buffer for later use. It's done this way for + * convenience and to fight possible memory leaks, it is not necesarily pretty. + **/ + +char *human_friendly_bytesize(unsigned long bytesize) { + static char buffer1[BYTESIZE_BUFFER_SIZE]; + static char buffer2[BYTESIZE_BUFFER_SIZE]; + static char *curbuffer = buffer2; + + if (curbuffer == buffer1) + curbuffer = buffer2; + else + curbuffer = buffer1; + + enum {bytes, kilobytes, megabytes, gigabytes} unit = bytes; + static char units[][7] = {"Bytes", "kBytes", "MBytes", "GBytes"}; + + if (bytesize > 1024) { + bytesize /= 1024; + unit = kilobytes; + } + + if (bytesize > 1024) { + bytesize /= 1024; + unit = megabytes; + } + + if (bytesize > 1024) { + bytesize /= 1024; + unit = gigabytes; + } + + sprintf(curbuffer, "%lu%s", bytesize, messages_get(units[unit])); + + return curbuffer; +} -- cgit v1.2.3