]> git.saurik.com Git - apple/system_cmds.git/blobdiff - CPPUtil/UtilPrettyPrinting.cpp
system_cmds-643.30.1.tar.gz
[apple/system_cmds.git] / CPPUtil / UtilPrettyPrinting.cpp
diff --git a/CPPUtil/UtilPrettyPrinting.cpp b/CPPUtil/UtilPrettyPrinting.cpp
new file mode 100644 (file)
index 0000000..00c1ea0
--- /dev/null
@@ -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