diff --git a/src/Flux/flxUtils.cpp b/src/Flux/flxUtils.cpp index da57350a..1e3d07b9 100644 --- a/src/Flux/flxUtils.cpp +++ b/src/Flux/flxUtils.cpp @@ -397,4 +397,32 @@ void flx_utils::timestampISO8601( time_t &t_time, char * buffer, size_t length, snprintf(szTmp, sizeof(szTmp), "%c%02d:%02d", chSign, tz_hrs, tz_min); strlcat(buffer, szTmp, length); +} + +//--------------------------------------------------------------------------------------------------- +// formatBytes() +// +// Return a formatted byte string. This returns values of B (1000), not iB (1024) + +void flx_utils::formatByteString(uint64_t nBytes, uint prec, char *szBuffer, size_t len) +{ + + char *sizeNames[] = {"Bytes", "KB", "MB", "GB", "TB"}; + + if (nBytes < 0) + nBytes = 0; + + if (prec < 0) + prec = 0; + + double tmp1 = floor( (nBytes ? log(nBytes) : 0) / log(1000.)); + + // overflow our name array? + if ( tmp1 > (sizeof(sizeNames)/sizeof(char*))-1) + tmp1 = (sizeof(sizeNames)/sizeof(char*))-1; + + char szFormat[32]; + snprintf(szFormat, sizeof(szFormat), "%%.%df%%s", prec); + snprintf(szBuffer, len, szFormat, nBytes/pow(1000., tmp1), sizeNames[(int)tmp1]); + } \ No newline at end of file diff --git a/src/Flux/flxUtils.h b/src/Flux/flxUtils.h index c2146eba..6393ab84 100644 --- a/src/Flux/flxUtils.h +++ b/src/Flux/flxUtils.h @@ -59,6 +59,7 @@ void uptime(uint32_t &days, uint32_t &hours, uint32_t &minutes, uint32_t &secs, void timestampISO8601(time_t &theTime, char * buffer, size_t length, bool bTZ=false); +void formatByteString(uint64_t nBytes, uint prec, char *szBuffer, size_t len); } // namespace flx_utils /*