Commit 491b9fa1 authored by rswindell's avatar rswindell
Browse files

Fixed strcasestr() for Windows - oops.

Created byte_estimate_to_str(), much like byte_count_to_str(), except the
byte count is rounded to the nearest tera/giga/mega/kilo-byte
(technically tebi/gibi/mebi/kibi-byte, but ugh, I'm old-sk00l!).
parent 14a98efc
......@@ -89,11 +89,12 @@ char* DLLCALL strcasestr(const char* haystack, const char* needle)
char* p = NULL;
if(h != NULL && n != NULL)
p = strstr(strupr(h), strupr(n));
int offset = p - h;
FREE_AND_NULL(h);
FREE_AND_NULL(n);
if(p == NULL)
return NULL;
return (char*)haystack + (p-h);
return (char*)haystack + offset;
}
#endif
......@@ -280,26 +281,53 @@ int64_t DLLCALL parse_byte_count(const char* str, ulong unit)
return((int64_t)(unit>1 ? (bytes/unit):bytes));
}
/* Convert a byte count to a string with a floating point value
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;
static const double one_kibibyte = 1024.0;
/* Convert an exact byte count to a string with a floating point value
and a single letter multiplier/suffix.
For values evenly divisible by 1024, no suffix otherwise.
*/
char* DLLCALL byte_count_to_str(int64_t bytes, char* str, size_t size)
{
if(bytes && fmod((double)bytes,1024.0*1024.0*1024.0*1024.0)==0)
safe_snprintf(str, size, "%gT",bytes/(1024.0*1024.0*1024.0*1024.0));
else if(bytes && fmod((double)bytes,1024.0*1024.0*1024.0)==0)
safe_snprintf(str, size, "%gG",bytes/(1024.0*1024.0*1024.0));
else if(bytes && fmod((double)bytes,1024.0*1024.0)==0)
safe_snprintf(str, size, "%gM",bytes/(1024.0*1024.0));
else if(bytes && fmod((double)bytes,1024.0)==0)
safe_snprintf(str, size, "%gK",bytes/1024.0);
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);
else if(bytes && fmod((double)bytes,one_mebibyte)==0)
safe_snprintf(str, size, "%gM",bytes/one_mebibyte);
else if(bytes && fmod((double)bytes,one_kibibyte)==0)
safe_snprintf(str, size, "%gK",bytes/one_kibibyte);
else
safe_snprintf(str, size, "%"PRIi64, bytes);
return str;
}
/* 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* DLLCALL byte_estimate_to_str(int64_t bytes, char* str, size_t size, ulong unit)
{
if(bytes >= one_tebibyte)
safe_snprintf(str, size, "%1.1fT", bytes/one_tebibyte);
else if(bytes >= one_gibibyte || unit == one_gibibyte)
safe_snprintf(str, size, "%1.1fG", bytes/one_gibibyte);
else if(bytes >= one_mebibyte || unit == one_mebibyte)
safe_snprintf(str, size, "%1.1fM", bytes/one_mebibyte);
else if(bytes >= one_kibibyte || unit == one_kibibyte)
safe_snprintf(str, size, "%1.1fK", bytes/one_kibibyte);
else
safe_snprintf(str, size, "%"PRIi64"B", bytes);
return str;
}
/* Parse a duration string, default unit is in seconds */
/* (Y)ears, (W)eeks, (D)ays, (H)ours, and (M)inutes */
/* suffixes/multipliers are supported. */
......
......@@ -374,6 +374,7 @@ DLLEXPORT double DLLCALL parse_duration(const char*);
DLLEXPORT char* DLLCALL duration_to_str(double value, char* str, size_t size);
DLLEXPORT char* DLLCALL duration_to_vstr(double value, char* str, size_t size);
DLLEXPORT char* DLLCALL byte_count_to_str(int64_t bytes, char* str, size_t size);
DLLEXPORT char* DLLCALL byte_estimate_to_str(int64_t bytes, char* str, size_t size, ulong unit);
/* 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