From 692129cb816c695dd6027a7969a7a3b96184a923 Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Wed, 26 Oct 2022 14:15:03 +0200 Subject: [PATCH] add ToHumanReadableSize, which formats data sizes into MiB, GiB, etc. this is needed in order to display data sizes in upcoming changes related to tracking statistics about the server's performance --- include/Common.h | 3 +++ src/Common.cpp | 14 ++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/include/Common.h b/include/Common.h index fdf529c..9a6be61 100644 --- a/include/Common.h +++ b/include/Common.h @@ -178,6 +178,7 @@ void RegisterThread(const std::string& str); #define KB 1024llu #define MB (KB * 1024llu) #define GB (MB * 1024llu) +#define TB (GB * 1024llu) #define SSU_UNRAW SECRET_SENTRY_URL #define _file_basename std::filesystem::path(__FILE__).filename().string() @@ -352,3 +353,5 @@ inline T DeComp(const T& Compressed) { std::string GetPlatformAgnosticErrorString(); #define S_DSN SU_RAW + +std::string ToHumanReadableSize(size_t Size); diff --git a/src/Common.cpp b/src/Common.cpp index 1589b34..10272ef 100644 --- a/src/Common.cpp +++ b/src/Common.cpp @@ -427,3 +427,17 @@ std::string GetPlatformAgnosticErrorString() { return "(no human-readable errors on this platform)"; #endif } + +std::string ToHumanReadableSize(size_t Size) { + if (Size > TB) { + return fmt::format("{:.2f} TiB", double(Size) / TB); + } else if (Size > GB) { + return fmt::format("{:.2f} GiB", double(Size) / GB); + } else if (Size > MB) { + return fmt::format("{:.2f} MiB", double(Size) / MB); + } else if (Size > KB) { + return fmt::format("{:.2f} KiB", double(Size) / KB); + } else { + return fmt::format("{} B", Size); + } +}