Skip to content

Commit

Permalink
added routine that builds a file size string from a number of bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
gigapod committed Apr 21, 2023
1 parent 425cf44 commit 71f3ddd
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/Flux/flxUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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]);

}
1 change: 1 addition & 0 deletions src/Flux/flxUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

/*
Expand Down

0 comments on commit 71f3ddd

Please sign in to comment.