From a16c6fbc2bae640bf93ee906a661bca21dfef7a1 Mon Sep 17 00:00:00 2001 From: Philipp Storz Date: Sun, 6 Sep 2020 21:52:35 +0200 Subject: [PATCH] edit.cc: introduced SizeAsSiPrefixFormat() and tests --- core/src/lib/edit.cc | 38 +++++++++++++++++++++++++++++++++++++ core/src/lib/edit.h | 1 + core/src/lib/res.cc | 35 +--------------------------------- core/src/tests/test_edit.cc | 17 +++++++++++++++++ 4 files changed, 57 insertions(+), 34 deletions(-) diff --git a/core/src/lib/edit.cc b/core/src/lib/edit.cc index e93e2b43407..cfb43d03331 100644 --- a/core/src/lib/edit.cc +++ b/core/src/lib/edit.cc @@ -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 diff --git a/core/src/lib/edit.h b/core/src/lib/edit.h index 9d2cf3367cb..a2c6a939be6 100644 --- a/core/src/lib/edit.h +++ b/core/src/lib/edit.h @@ -48,5 +48,6 @@ bool IsNameValid(const char* name); bool IsAclEntryValid(const char* acl, std::vector& 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_ diff --git a/core/src/lib/res.cc b/core/src/lib/res.cc index ae1799964c0..bb0959828b2 100644 --- a/core/src/lib/res.cc +++ b/core/src/lib/res.cc @@ -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) diff --git a/core/src/tests/test_edit.cc b/core/src/tests/test_edit.cc index a5a2de43195..270dd021417 100644 --- a/core/src/tests/test_edit.cc +++ b/core/src/tests/test_edit.cc @@ -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) {