Synchronet now requires the libarchive development package (e.g. libarchive-dev on Debian-based Linux distros, libarchive.org for more info) to build successfully.

Commit 5e6b0ef8 authored by Rob Swindell's avatar Rob Swindell 💬

Add optional/better format to file_size_str() by passing a precision as 3rd arg

Returns a floating-point formatted size estimate more in-line with my
preference (e.g. 17.0G instead of 17,000M).
parent e7efe6d3
......@@ -4,8 +4,10 @@
// Function for returning a string representation of a file size
function file_size_str(size, bytes)
function file_size_str(size, bytes, float)
{
if(float !== undefined)
return file_size_float(size, /* unit: */bytes, /* precision */float);
if(bytes) {
if(size < 1000) /* Bytes */
return format("%ldB",size);
......@@ -56,3 +58,24 @@ function file_size_str(size, bytes)
return "Too damn big to download.";
}
// ported from xpdev/genwrap.c byte_estimate_to_str()
function file_size_float(bytes, unit, precision)
{
const one_tebibyte = 1024.0*1024.0*1024.0*1024.0;
const one_gibibyte = 1024.0*1024.0*1024.0;
const one_mebibyte = 1024.0*1024.0;
const one_kibibyte = 1024.0;
if(bytes >= one_tebibyte)
return format("%1.*fT", precision, bytes/one_tebibyte);
if(bytes >= one_gibibyte || unit == one_gibibyte)
return format("%1.*fG", precision, bytes/one_gibibyte);
if(bytes >= one_mebibyte || unit == one_mebibyte)
return format("%1.*fM", precision, bytes/one_mebibyte);
else if(bytes >= one_kibibyte || unit == one_kibibyte)
return format("%1.*fK", precision, bytes/one_kibibyte);
else
return format("%luB", bytes);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment