From 41edc4e45fffb14a674ac999f62ada96685a7936 Mon Sep 17 00:00:00 2001 From: Vladyslav Baranovskyi Date: Tue, 11 Jun 2024 14:02:08 +0300 Subject: [PATCH] Added PrettyBytes function to Utils class --- openVulkanoCpp/Base/Utils.cpp | 42 +++++++++++++++++++++++++++++++++++ openVulkanoCpp/Base/Utils.hpp | 1 + 2 files changed, 43 insertions(+) diff --git a/openVulkanoCpp/Base/Utils.cpp b/openVulkanoCpp/Base/Utils.cpp index 95c2ca7..57533e3 100644 --- a/openVulkanoCpp/Base/Utils.cpp +++ b/openVulkanoCpp/Base/Utils.cpp @@ -74,4 +74,46 @@ namespace OpenVulkano file.close(); return data; } + + std::string Utils::PrettyBytes(uint64_t bytes) + { + static const uint64_t TERABYTES = 1024ULL*1024*1024*1024; + static const uint64_t GIGABYTES = 1024ULL*1024*1024; + static const uint64_t MEGABYTES = 1024*1024; + static const uint64_t KILOBYTES = 1024; + + int TB, GB, MB, KB; + TB = bytes / TERABYTES; + bytes -= TB * TERABYTES; + GB = bytes / GIGABYTES; + bytes -= GB * GIGABYTES; + MB = bytes / MEGABYTES; + bytes -= MB * MEGABYTES; + KB = bytes / KILOBYTES; + bytes -= KB * KILOBYTES; + + std::string result; + if(TB) + { + result = std::to_string(TB) + " TB"; + } + else if(GB) + { + result = std::to_string(GB) + " GB"; + } + else if(MB) + { + result = std::to_string(MB) + " MB"; + } + else if(KB) + { + result = std::to_string(KB) + " KB"; + } + else + { + result = std::to_string(bytes) + " B"; + } + + return result; + } } diff --git a/openVulkanoCpp/Base/Utils.hpp b/openVulkanoCpp/Base/Utils.hpp index 34fc5dc..0f6c499 100644 --- a/openVulkanoCpp/Base/Utils.hpp +++ b/openVulkanoCpp/Base/Utils.hpp @@ -138,5 +138,6 @@ namespace OpenVulkano } static Array ReadFile(const std::string& filePath, bool emptyOnMissing = false); + static std::string PrettyBytes(uint64_t bytes); }; }