From 2f32ff99f90689b364b86d4c12f3a1b8b318991e Mon Sep 17 00:00:00 2001 From: Martin Szulecki Date: Wed, 22 Oct 2014 21:28:04 +0200 Subject: common: Move size format helper to utils and use it in idevicebackup tools --- common/utils.c | 22 ++++++++++++++++++++++ common/utils.h | 1 + 2 files changed, 23 insertions(+) (limited to 'common') diff --git a/common/utils.c b/common/utils.c index f95ecfd..4a45d95 100644 --- a/common/utils.c +++ b/common/utils.c @@ -139,6 +139,28 @@ char *string_build_path(const char *elem, ...) return out; } +char *string_format_size(uint64_t size) +{ + char buf[80]; + double sz; + if (size >= 1000000000000LL) { + sz = ((double)size / 1000000000000.0f); + sprintf(buf, "%0.1f TB", sz); + } else if (size >= 1000000000LL) { + sz = ((double)size / 1000000000.0f); + sprintf(buf, "%0.1f GB", sz); + } else if (size >= 1000000LL) { + sz = ((double)size / 1000000.0f); + sprintf(buf, "%0.1f MB", sz); + } else if (size >= 1000LL) { + sz = ((double)size / 1000.0f); + sprintf(buf, "%0.1f KB", sz); + } else { + sprintf(buf, "%d Bytes", (int)size); + } + return strdup(buf); +} + char *string_toupper(char* str) { char *res = strdup(str); diff --git a/common/utils.h b/common/utils.h index 5cd4a53..97d3748 100644 --- a/common/utils.h +++ b/common/utils.h @@ -38,6 +38,7 @@ char *stpcpy(char *s1, const char *s2); #endif char *string_concat(const char *str, ...); char *string_build_path(const char *elem, ...); +char *string_format_size(uint64_t size); char *string_toupper(char *str); char *generate_uuid(); -- cgit v1.1-32-gdbae