X-Git-Url: https://git.saurik.com/apple/system_cmds.git/blobdiff_plain/1a7e3f61d38d679bba59130891c2031b5a0092b6..bd6521f0fc816ab056bc71376f9706a69b3b52c1:/CPPUtil/UtilPrettyPrinting.cpp diff --git a/CPPUtil/UtilPrettyPrinting.cpp b/CPPUtil/UtilPrettyPrinting.cpp new file mode 100644 index 0000000..00c1ea0 --- /dev/null +++ b/CPPUtil/UtilPrettyPrinting.cpp @@ -0,0 +1,26 @@ +// +// UtilPrettyPrinting.cpp +// CPPUtil +// +// Created by James McIlree on 9/8/13. +// Copyright (c) 2013 Apple. All rights reserved. +// + +#include "CPPUtil.h" + +BEGIN_UTIL_NAMESPACE + +std::string formated_byte_size(uint64_t bytes) { + if (bytes) { + char tmp[128]; + const char *si_prefix[] = { "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" }; + const int base = 1024; + int c = std::min((int)(log((double)bytes)/log((double)base)), (int)sizeof(si_prefix) - 1); + snprintf(tmp, sizeof(tmp), "%1.2f %s", bytes / pow((double)base, c), si_prefix[c]); + return std::string(tmp); + } + + return std::string("0.00 B"); +} + +END_UTIL_NAMESPACE