Commit fcb6846b authored by Rob Swindell's avatar Rob Swindell 💬
Browse files

byte-count to string routines type unsigned 64-bit int now

We don't have a use case negative byte values in strings anywhere that I can think of.

Created wrapper for MSFT _ui64toa_() - unsigned 64-bit integer to ASCII string.

Add Petabyte (actually, Pebibyte) support to byte_count_to_str()  and byte_estimate_to_str().

Removed 'B' suffix from byte_estimate_to_str() output for values < 1024. Callers may display this returned string followed by "bytes" (or similar) and thus the 'B' suffix is redundant in that case. If the caller needs a 'B' suffix, they can detect no suffix and add it.
parent 06605539
......@@ -292,6 +292,7 @@ int64_t parse_byte_count(const char* str, ulong unit)
return((int64_t)(unit>1 ? (bytes/unit):bytes));
}
static const double one_pebibyte = 1024.0*1024.0*1024.0*1024.0*1024.0;
static const double one_tebibyte = 1024.0*1024.0*1024.0*1024.0;
static const double one_gibibyte = 1024.0*1024.0*1024.0;
static const double one_mebibyte = 1024.0*1024.0;
......@@ -301,9 +302,11 @@ static const double one_kibibyte = 1024.0;
and a single letter multiplier/suffix.
For values evenly divisible by 1024, no suffix otherwise.
*/
char* byte_count_to_str(int64_t bytes, char* str, size_t size)
char* byte_count_to_str(uint64_t bytes, char* str, size_t size)
{
if(bytes && fmod((double)bytes,one_tebibyte)==0)
if(bytes && fmod((double)bytes,one_pebibyte)==0)
safe_snprintf(str, size, "%gP",bytes/one_pebibyte);
else if(bytes && fmod((double)bytes,one_tebibyte)==0)
safe_snprintf(str, size, "%gT",bytes/one_tebibyte);
else if(bytes && fmod((double)bytes,one_gibibyte)==0)
safe_snprintf(str, size, "%gG",bytes/one_gibibyte);
......@@ -319,12 +322,13 @@ char* byte_count_to_str(int64_t bytes, char* str, size_t size)
/* Convert a rounded byte count to a string with a floating point value
with a single decimal place and a single letter multiplier/suffix.
This function also appends 'B' for exact byte counts (< 1024).
'unit' is the smallest divisor used.
*/
char* byte_estimate_to_str(int64_t bytes, char* str, size_t size, ulong unit, int precision)
char* byte_estimate_to_str(uint64_t bytes, char* str, size_t size, ulong unit, int precision)
{
if(bytes >= one_tebibyte)
if(bytes >= one_pebibyte)
safe_snprintf(str, size, "%1.*fP", precision, bytes/one_pebibyte);
else if(bytes >= one_tebibyte || unit == one_tebibyte)
safe_snprintf(str, size, "%1.*fT", precision, bytes/one_tebibyte);
else if(bytes >= one_gibibyte || unit == one_gibibyte)
safe_snprintf(str, size, "%1.*fG", precision, bytes/one_gibibyte);
......@@ -333,7 +337,7 @@ char* byte_estimate_to_str(int64_t bytes, char* str, size_t size, ulong unit, in
else if(bytes >= one_kibibyte || unit == one_kibibyte)
safe_snprintf(str, size, "%1.*fK", precision, bytes/one_kibibyte);
else
safe_snprintf(str, size, "%"PRIi64"B", bytes);
safe_snprintf(str, size, "%"PRIi64, bytes);
return str;
}
......@@ -620,6 +624,25 @@ char* _i64toa(int64_t val, char* str, int radix)
}
return str;
}
char* _ui64toa(uint64_t val, char* str, int radix)
{
switch(radix) {
case 8:
sprintf(str,"%"PRIo64,val);
break;
case 10:
sprintf(str,"%"PRIu64,val);
break;
case 16:
sprintf(str,"%"PRIx64,val);
break;
default:
sprintf(str,"bad radix: %d",radix);
break;
}
return str;
}
#endif
/****************************************************************************/
......
......@@ -229,6 +229,7 @@ extern "C" {
#if !defined(_MSC_VER) && !defined(__BORLANDC__) && !defined(__WATCOMC__)
DLLEXPORT char* ultoa(ulong, char*, int radix);
DLLEXPORT char* _i64toa(int64_t, char*, int radix);
DLLEXPORT char* _ui64toa(uint64_t, char*, int radix);
#endif
#if defined(__unix__)
......@@ -376,8 +377,8 @@ DLLEXPORT int64_t parse_byte_count(const char*, ulong unit);
DLLEXPORT double parse_duration(const char*);
DLLEXPORT char* duration_to_str(double value, char* str, size_t size);
DLLEXPORT char* duration_to_vstr(double value, char* str, size_t size);
DLLEXPORT char* byte_count_to_str(int64_t bytes, char* str, size_t size);
DLLEXPORT char* byte_estimate_to_str(int64_t bytes, char* str, size_t size, ulong unit, int precision);
DLLEXPORT char* byte_count_to_str(uint64_t bytes, char* str, size_t size);
DLLEXPORT char* byte_estimate_to_str(uint64_t bytes, char* str, size_t size, ulong unit, int precision);
/* Microsoft (e.g. DOS/Win32) real-time system clock API (ticks since process started) */
typedef clock_t msclock_t;
......
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