From 5de3aa537d99fb98278b69520b19ce30acfcece0 Mon Sep 17 00:00:00 2001 From: rswindell <> Date: Tue, 8 Mar 2005 02:57:51 +0000 Subject: [PATCH] Library type function to return string representation of a file size: file_size_str(size, [bytes]) --- exec/load/file_size.js | 58 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 exec/load/file_size.js diff --git a/exec/load/file_size.js b/exec/load/file_size.js new file mode 100644 index 0000000000..5c8762cf60 --- /dev/null +++ b/exec/load/file_size.js @@ -0,0 +1,58 @@ +// file_size.js + +// $Id$ + +// Function for returning a string representation of a file size + +function file_size_str(size, bytes) +{ + if(bytes) { + if(size < 1000) /* Bytes */ + return format("%ldB",size); + if(size<10000) /* Bytes with comma */ + return format("%ld,%03ldB",(size/1000),(size%1000)); + size = size/1024; + } + if(size<1000) /* KB */ + return format("%ldK",size); + if(size<100000) /* KB With comma */ + return format("%ld,%03ldK",(size/1000),(size%1000)); + size = size/1024; + if(size<1000) /* MB */ + return format("%ldM",size); + if(size<100000) /* MB With comma */ + return format("%ld,%03ldM",(size/1000),(size%1000)); + size = size/1024; + if(size<1000) /* GB */ + return format("%ldG",size); + if(size<100000) /* GB With comma */ + return format("%ld,%03ldG",(size/1000),(size%1000)); + size = size/1024; + if(size<1000) /* TB (Yeah, right) */ + return format("%ldT",size); + if(size<100000) /* TB With comma (Whee!) */ + return format("%ld,%03ldT",(size/1000),(size%1000)); + size = size/1024; + if(size<1000) /* PB (Snicker) */ + return format("%ldP",size); + if(size<100000) /* PB With comma (Cough!) */ + return format("%ld,%03ldP",(size/1000),(size%1000)); + /* Heck, let's go all the way! */ + size = size/1024; + if(size<1000) /* EB */ + return format("%ldE",size); + if(size<100000) /* EB With comma */ + return format("%ld,%03ldE",(size/1000),(size%1000)); + size = size/1024; + if(size<1000) /* ZB */ + return format("%ldZ",size); + if(size<100000) /* ZB With comma */ + return format("%ld,%03ldZ",(size/1000),(size%1000)); + size = size/1024; + if(size<1000) /* YB */ + return format("%ldY",size); + if(size<1000000) /* YB With comma */ + return format("%ld,%03ldY",(size/1000),(size%1000)); + + return "Too damn big to download."; +} -- GitLab