From ccbc3e109e4cf634d1da17815e244003008c33f3 Mon Sep 17 00:00:00 2001 From: taynpg Date: Tue, 7 Jan 2025 12:04:00 +0800 Subject: [PATCH] =?UTF-8?q?add=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=AD=97?= =?UTF-8?q?=E8=8A=82=E6=95=B0=E8=BD=AC=E5=8F=AF=E8=AF=BB=E5=AD=97=E7=AC=A6?= =?UTF-8?q?=E4=B8=B2=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/of_util.h | 1 + src/of_util.cpp | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/include/of_util.h b/include/of_util.h index c7f454a..e0b145e 100644 --- a/include/of_util.h +++ b/include/of_util.h @@ -46,6 +46,7 @@ public: public: static ofString now_time(); + static ofString get_file_size(long long bytes); }; class CMutBuffer diff --git a/src/of_util.cpp b/src/of_util.cpp index 9a7eeaf..2f59154 100644 --- a/src/of_util.cpp +++ b/src/of_util.cpp @@ -85,6 +85,32 @@ ofString OfUtil::now_time() #endif } +ofString OfUtil::get_file_size(long long bytes) +{ + constexpr long long gbblk = 1024 * 1024 * 1024; + constexpr long long mbblk = 1024 * 1024; + + long long gb = bytes / gbblk; + bytes %= gbblk; + + long long mb = bytes / mbblk; + bytes %= mbblk; + + long long kb = bytes / 1024; + + std::ostringstream oss; + if (gb > 0) { + oss << gb << ofT("GB"); + } + if (mb > 0 || gb > 0) { + oss << mb << ofT("MB"); + } + if (kb > 0 || mb > 0 || gb > 0) { + oss << kb << ofT("KB"); + } + return oss.str(); +} + #ifdef _WIN32 std::string CCodec::u8ToGBK(const std::string& str) {