]> git.saurik.com Git - apple/system_cmds.git/blob - CPPUtil/UtilPrettyPrinting.cpp
system_cmds-671.10.3.tar.gz
[apple/system_cmds.git] / CPPUtil / UtilPrettyPrinting.cpp
1 //
2 // UtilPrettyPrinting.cpp
3 // CPPUtil
4 //
5 // Created by James McIlree on 9/8/13.
6 // Copyright (c) 2013 Apple. All rights reserved.
7 //
8
9 #include "CPPUtil.h"
10
11 BEGIN_UTIL_NAMESPACE
12
13 std::string formated_byte_size(uint64_t bytes) {
14 if (bytes) {
15 char tmp[128];
16 const char *si_prefix[] = { "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
17 const int base = 1024;
18 int c = std::min((int)(log((double)bytes)/log((double)base)), (int)sizeof(si_prefix) - 1);
19 snprintf(tmp, sizeof(tmp), "%1.2f %s", bytes / pow((double)base, c), si_prefix[c]);
20 return std::string(tmp);
21 }
22
23 return std::string("0.00 B");
24 }
25
26 END_UTIL_NAMESPACE