Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.48 KB | None | 0 0
  1. #include "blaDefines.hpp"
  2. #include "prettyPrintFilesize.hpp"
  3. #include <cstdio>
  4.  
  5. static const char * getUnitForSize(bla::s64 fsize)
  6. {
  7.     if(fsize < 1024)
  8.         return "bytes";
  9.  
  10.     fsize /= 1024;
  11.     if(fsize < 1024)
  12.         return "KiB";
  13.  
  14.     fsize /= 1024;
  15.     if(fsize < 1024)
  16.         return "MiB";
  17.  
  18.     fsize /= 1024;
  19.     if(fsize < 1024)
  20.         return "GiB";
  21.  
  22.     fsize /= 1024;
  23.     if(fsize < 1024)
  24.         return "TiB";
  25.  
  26.     fsize /= 1024;
  27.     if(fsize < 1024)
  28.         return "PiB";
  29.  
  30.     fsize /= 1024;
  31.     if(fsize < 1024)
  32.         return "EiB";
  33.  
  34.     return "ZiB";
  35. }
  36.  
  37. static double adjustSize(bla::s64 bytes)
  38. {
  39.     if(bytes < (1ll << 10)) return bytes / (double)(1ll << 0);
  40.     if(bytes < (1ll << 20)) return bytes / (double)(1ll << 10);
  41.     if(bytes < (1ll << 30)) return bytes / (double)(1ll << 20);
  42.     if(bytes < (1ll << 40)) return bytes / (double)(1ll << 30);
  43.     if(bytes < (1ll << 50)) return bytes / (double)(1ll << 40);
  44.     if(bytes < (1ll << 60)) return bytes / (double)(1ll << 50);
  45.     return (bytes >> 10) / (double)(1ll << 50);
  46. }
  47.  
  48. std::string prettyPrintFilesize(bla::s64 fsize)
  49. {
  50.     if(fsize < 0)
  51.         return "negative-file-size";
  52.  
  53.     if(fsize == 1)
  54.         return "1 byte";
  55.  
  56.     if(fsize < 1024)
  57.         return std::to_string(fsize) + " bytes";
  58.  
  59.     char buff[50];
  60.     const double value = adjustSize(fsize);
  61.     const char * unit = getUnitForSize(fsize);
  62.     sprintf(buff, "%.3f %s", value, unit);
  63.     return buff;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement