Skip to content

Commit

Permalink
edit.cc: introduced SizeAsSiPrefixFormat() and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pstorz committed Sep 10, 2020
1 parent 769ea67 commit a16c6fb
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 34 deletions.
38 changes: 38 additions & 0 deletions core/src/lib/edit.cc
Expand Up @@ -415,6 +415,44 @@ static bool strunit_to_uint64(char* str, uint64_t* value, const char** mod)
return true;
}

// convert uint64 number to size string
std::string SizeAsSiPrefixFormat(uint64_t value_in)
{
uint64_t value = value_in;
int factor;
std::string result{};
/*
* convert default value string to numeric value
*/
static const char* modifier[] = {"e", "p", "t", "g", "m", "k", "", NULL};
const uint64_t multiplier[] = {1152921504606846976, // EiB Exbibyte
1125899906842624, // PiB Pebibyte
1099511627776, // TiB Tebibyte
1073741824, // GiB Gibibyte
1048576, // MiB Mebibyte
1024, // KiB Kibibyte
1};

if (value == 0) {
result += "0";
} else {
for (int t = 0; modifier[t]; t++) {
factor = value / multiplier[t];
value = value % multiplier[t];
if (factor > 0) {
result += std::to_string(factor);
result += " ";
result += modifier[t];
if (!(bstrcmp(modifier[t], ""))) { result += " "; }
}
if (value == 0) { break; }
}
}
result.pop_back();
return result;
}


/*
* Convert a size in bytes to uint64_t
* Returns false: if error
Expand Down
1 change: 1 addition & 0 deletions core/src/lib/edit.h
Expand Up @@ -48,5 +48,6 @@ bool IsNameValid(const char* name);
bool IsAclEntryValid(const char* acl, std::vector<char>& msg);
bool IsAclEntryValid(const char* acl);
bool size_to_uint64(char* str, uint64_t* value);
std::string SizeAsSiPrefixFormat(uint64_t value_in);

#endif // BAREOS_LIB_EDIT_H_
35 changes: 1 addition & 34 deletions core/src/lib/res.cc
Expand Up @@ -1503,40 +1503,7 @@ void IndentConfigItem(PoolMem& cfg_str,

std::string PrintNumberSiPrefixFormat(ResourceItem* item, uint64_t value_in)
{
PoolMem temp;
PoolMem volspec; /* vol specification string*/
uint64_t value = value_in;
int factor;

/*
* convert default value string to numeric value
*/
static const char* modifier[] = {"e", "p", "t", "g", "m", "k", "", NULL};
const uint64_t multiplier[] = {1152921504606846976, // EiB Exbibyte
1125899906842624, // PiB Pebibyte
1099511627776, // TiB Tebibyte
1073741824, // GiB Gibibyte
1048576, // MiB Mebibyte
1024, // KiB Kibibyte
1};

if (value == 0) {
PmStrcat(volspec, "0");
} else {
for (int t = 0; modifier[t]; t++) {
Dmsg2(200, " %s bytes: %lld\n", item->name, value);
factor = value / multiplier[t];
value = value % multiplier[t];
if (factor > 0) {
Mmsg(temp, "%d %s ", factor, modifier[t]);
PmStrcat(volspec, temp.c_str());
Dmsg1(200, " volspec: %s\n", volspec.c_str());
}
if (value == 0) { break; }
}
}

return std::string(volspec.c_str());
return SizeAsSiPrefixFormat(value_in);
}

std::string Print32BitConfigNumberSiPrefixFormat(ResourceItem* item)
Expand Down
17 changes: 17 additions & 0 deletions core/src/tests/test_edit.cc
Expand Up @@ -28,6 +28,23 @@

#include "lib/edit.h"

TEST(edit, format_1k)
{
ASSERT_STREQ(SizeAsSiPrefixFormat(1).c_str(), "1");
ASSERT_STREQ(SizeAsSiPrefixFormat(1024).c_str(), "1 k");
ASSERT_STREQ(SizeAsSiPrefixFormat(1048576).c_str(), "1 m");
ASSERT_STREQ(SizeAsSiPrefixFormat(1073741824).c_str(), "1 g");
ASSERT_STREQ(SizeAsSiPrefixFormat(1099511627776).c_str(), "1 t");
ASSERT_STREQ(SizeAsSiPrefixFormat(1125899906842624).c_str(), "1 p");
ASSERT_STREQ(SizeAsSiPrefixFormat(1152921504606846976).c_str(), "1 e");

ASSERT_STREQ(
SizeAsSiPrefixFormat(1152921504606846976 + 1125899906842624 +
1099511627776 + 1073741824 + 1048576 + 1024 + 1)
.c_str(),
"1 e 1 p 1 t 1 g 1 m 1 k 1");
}

// kibibyte
TEST(edit, k)
{
Expand Down

0 comments on commit a16c6fb

Please sign in to comment.