diff options
author | Alexander Tkachev | 2019-07-15 18:35:24 +0700 |
---|---|---|
committer | Matan Bareket | 2019-07-30 14:51:41 -0400 |
commit | d04c1dfad422d9ea8c2b460ff01b88911d9fd3ef (patch) | |
tree | 5f29d43b0b19b8c5c018386a38572e20af84ed48 /common | |
parent | faa19c7bf097e6a7f1f76c29b0faba12d5781d19 (diff) | |
download | scummvm-rg350-d04c1dfad422d9ea8c2b460ff01b88911d9fd3ef.tar.gz scummvm-rg350-d04c1dfad422d9ea8c2b460ff01b88911d9fd3ef.tar.bz2 scummvm-rg350-d04c1dfad422d9ea8c2b460ff01b88911d9fd3ef.zip |
COMMON: Add getHumanReadableBytes() in util.h
This function was used in cloud-related DownloadDialog before,
and now it is also used in Options > Cloud tab.
Diffstat (limited to 'common')
-rw-r--r-- | common/util.cpp | 39 | ||||
-rw-r--r-- | common/util.h | 12 |
2 files changed, 51 insertions, 0 deletions
diff --git a/common/util.cpp b/common/util.cpp index 9a4214eda8..7c309ce364 100644 --- a/common/util.cpp +++ b/common/util.cpp @@ -163,4 +163,43 @@ bool isGraph(int c) { return isgraph((byte)c); } + +#pragma mark - + + +Common::String getHumanReadableBytes(uint64 bytes, Common::String &unitsOut) { + Common::String result = Common::String::format("%lu", bytes); + unitsOut = "B"; + + if (bytes >= 1024) { + bytes /= 1024; + result = Common::String::format("%lu", bytes); + unitsOut = "KB"; + } + + double floating = bytes; + + if (bytes >= 1024) { + bytes /= 1024; + floating /= 1024.0; + unitsOut = "MB"; + } + + if (bytes >= 1024) { + bytes /= 1024; + floating /= 1024.0; + unitsOut = "GB"; + } + + if (bytes >= 1024) { // woah + bytes /= 1024; + floating /= 1024.0; + unitsOut = "TB"; + } + + // print one digit after floating point + result = Common::String::format("%.1f", floating); + return result; +} + } // End of namespace Common diff --git a/common/util.h b/common/util.h index a90ea72167..8254e972d0 100644 --- a/common/util.h +++ b/common/util.h @@ -220,6 +220,18 @@ bool isCntrl(int c); */ bool isGraph(int c); + +/** + * Represent bytes size of a file as a number with floating point and + * largest suitable units. For example, 1474560 bytes as 1.4 MB. + * + * @param bytes size in bytes to be represented + * @param unitsOut (out-parameter) string with units + * @note use _() to translate units correctly + * @return string with a floating point number representing given size + */ +Common::String getHumanReadableBytes(uint64 bytes, Common::String &unitsOut); + } // End of namespace Common #endif |