Skip to content

Commit

Permalink
Add strSizeFormat() to String object.
Browse files Browse the repository at this point in the history
Converts sizes in bytes to a more human-readable form, .e.g. 1KB, 1.1GB.

Contributed by Cynthia Shang.
  • Loading branch information
cmwshang authored and dwsteele committed Dec 10, 2018
1 parent 947fa6f commit 80a3e21
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 1 deletion.
8 changes: 8 additions & 0 deletions doc/xml/release.xml
Expand Up @@ -107,6 +107,14 @@
<p>Improve error message when info files are missing/corrupt.</p>
</release-item>

<release-item>
<release-item-contributor-list>
<release-item-contributor id="cynthia.shang"/>
</release-item-contributor-list>

<p>Add <code>strSizeFormat()</code> to <code>String</code> object.</p>
</release-item>

<release-item>
<p>Rename <code>PGBACKREST</code>/<code>BACKREST</code> constants to <code>PROJECT</code>.</p>
</release-item>
Expand Down
40 changes: 40 additions & 0 deletions src/common/type/string.c
Expand Up @@ -795,6 +795,46 @@ strToLog(const String *this)
return strNewFmt("{\"%s\"}", strPtr(this));
}

/***********************************************************************************************************************************
Format sizes (file, buffer, etc.) in human-readable form
***********************************************************************************************************************************/
String *
strSizeFormat(const uint64_t size)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(UINT64, size);

FUNCTION_TEST_END();

String *result = NULL;

if (size < 1024)
result = strNewFmt("%" PRIu64 "B", size);
else if (size < (1024 * 1024))
{
if ((int)((double)size / 102.4) % 10 != 0)
result = strNewFmt("%.1fKB", ((double)size / 102.4) / 10);
else
result = strNewFmt("%dKB", (int)((double)size / 102.4) / 10);
}
else if (size < (1024 * 1024 * 1024))
{
if ((int)((double)size / 1024 / 102.4) % 10 != 0)
result = strNewFmt("%.1fMB", ((double)size / 1024 / 102.4) / 10);
else
result = strNewFmt("%dMB", (int)((double)size / 1024 / 102.4) / 10);
}
else
{
if ((int)((double)size / 1024 / 1024 / 102.4) % 10 != 0)
result = strNewFmt("%.1fGB", ((double)size / 1024 / 1024 / 102.4) / 10);
else
result = strNewFmt("%dGB", (int)((double)size / 1024 / 1024 / 102.4) / 10);
}

FUNCTION_TEST_RESULT(STRING, result);
}

/***********************************************************************************************************************************
Free the string
***********************************************************************************************************************************/
Expand Down
3 changes: 3 additions & 0 deletions src/common/type/string.h
Expand Up @@ -18,6 +18,8 @@ old context and then back. Below is a simplified example:
#ifndef COMMON_TYPE_STRING_H
#define COMMON_TYPE_STRING_H

#include <stdint.h>

/***********************************************************************************************************************************
String object
***********************************************************************************************************************************/
Expand Down Expand Up @@ -62,6 +64,7 @@ String *strSubN(const String *this, size_t start, size_t size);
String *strTrim(String *this);
int strChr(const String *this, char chr);
String *strTrunc(String *this, int idx);
String *strSizeFormat(const uint64_t fileSize);

void strFree(String *this);

Expand Down
2 changes: 1 addition & 1 deletion test/define.yaml
Expand Up @@ -155,7 +155,7 @@ unit:

# ----------------------------------------------------------------------------------------------------------------------------
- name: type-string
total: 15
total: 16

coverage:
common/type/string: full
Expand Down
13 changes: 13 additions & 0 deletions test/src/module/common/typeStringTest.c
Expand Up @@ -210,5 +210,18 @@ testRun(void)
TEST_RESULT_STR(strPtr(strToLog(strNew("test"))), "{\"test\"}", "format string");
}

// *****************************************************************************************************************************
if (testBegin("strSizeFormat()"))
{
TEST_RESULT_STR(strPtr(strSizeFormat(0)), "0B", "zero bytes");
TEST_RESULT_STR(strPtr(strSizeFormat(1023)), "1023B", "1023 bytes");
TEST_RESULT_STR(strPtr(strSizeFormat(1024)), "1KB", "1 KB");
TEST_RESULT_STR(strPtr(strSizeFormat(2200)), "2.1KB", "2.1 KB");
TEST_RESULT_STR(strPtr(strSizeFormat(1048576)), "1MB", "1 MB");
TEST_RESULT_STR(strPtr(strSizeFormat(20162900)), "19.2MB", "19.2 MB");
TEST_RESULT_STR(strPtr(strSizeFormat(1073741824)), "1GB", "1 GB");
TEST_RESULT_STR(strPtr(strSizeFormat(UINT64_MAX)), "17179869184.0GB", "uint64 max");
}

FUNCTION_HARNESS_RESULT_VOID();
}

0 comments on commit 80a3e21

Please sign in to comment.