Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 25th, 2012  |  syntax: None  |  size: 2.70 KB  |  hits: 8  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. bytes to friendly NSString format with MB or KB [closed]
  2. unsigned  long long size= ([[[NSFileManager defaultManager] attributesOfItemAtPath:fullPath error:nil] fileSize]);
  3.        
  4. - (id)transformedValue:(id)value
  5. {
  6.  
  7.     double convertedValue = [value doubleValue];
  8.     int multiplyFactor = 0;
  9.  
  10.     NSArray *tokens = [NSArray arrayWithObjects:@"bytes",@"KiB",@"MiB",@"GiB",@"TiB",nil];
  11.  
  12.     while (convertedValue > 1024) {
  13.         convertedValue /= 1024;
  14.         multiplyFactor++;
  15.     }
  16.  
  17.     return [NSString stringWithFormat:@"%4.2f %@",convertedValue, [tokens objectAtIndex:multiplyFactor],value];
  18. }
  19.        
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <stdint.h>
  23.  
  24. #define SIZE_BUFSZ 7
  25. static char const SIZE_PREFIXES[] = "kMGTPEZY";
  26.  
  27. void
  28. format_size(char buf[SIZE_BUFSZ], uint64_t sz)
  29. {
  30.     int pfx = 0;
  31.     unsigned int m, n, rem, hrem;
  32.     uint64_t a;
  33.     if (sz <= 0) {
  34.         memcpy(buf, "0 B", 3);
  35.         return;
  36.     }
  37.     a = sz;
  38.     if (a < 1000) {
  39.         n = a;
  40.         snprintf(buf, SIZE_BUFSZ, "%u B", n);
  41.         return;
  42.     }
  43.     for (pfx = 0, hrem = 0; ; pfx++) {
  44.         rem = a % 1000ULL;
  45.         a = a / 1000ULL;
  46.         if (!SIZE_PREFIXES[pfx + 1] || a < 1000ULL)
  47.             break;
  48.         hrem |= rem;
  49.     }
  50.     n = a;
  51.     if (n < 10) {
  52.         if (rem >= 950) {
  53.             buf[0] = '1';
  54.             buf[1] = '0';
  55.             buf[2] = ' ';
  56.             buf[3] = SIZE_PREFIXES[pfx];
  57.             buf[4] = 'B';
  58.             buf[5] = '';
  59.             return;
  60.         } else {
  61.             m = rem / 100;
  62.             rem = rem % 100;
  63.             if (rem > 50 || (rem == 50 && ((m & 1) || hrem)))
  64.                 m++;
  65.             snprintf(buf, SIZE_BUFSZ,
  66.                      "%u.%u %cB", n, m, SIZE_PREFIXES[pfx]);
  67.         }
  68.     } else {
  69.         if (rem > 500 || (rem == 500 && ((n & 1) || hrem)))
  70.             n++;
  71.         if (n >= 1000 && SIZE_PREFIXES[pfx + 1]) {
  72.             buf[0] = '1';
  73.             buf[1] = '.';
  74.             buf[2] = '0';
  75.             buf[3] = ' ';
  76.             buf[4] = SIZE_PREFIXES[pfx+1];
  77.             buf[5] = 'B';
  78.             buf[6] = '';
  79.         } else {
  80.             snprintf(buf, SIZE_BUFSZ,
  81.                      "%u %cB", n, SIZE_PREFIXES[pfx]);
  82.         }
  83.     }
  84. }
  85.        
  86. { 0, "0 B" },
  87. { 5, "5 B" },
  88. { 20, "20 B" },
  89. { 100, "100 B" },
  90. { 500, "500 B" },
  91. { 999, "999 B" },
  92. { 1000, "1.0 kB" },
  93. { 1050, "1.0 kB" },
  94. { 1051, "1.1 kB" },
  95. { 2349, "2.3 kB" },
  96. { 2350, "2.4 kB" },
  97. { 9949, "9.9 kB" },
  98. { 9950, "10 kB" },
  99. { 10000, "10 kB" },
  100. { 10500, "10 kB" },
  101. { 10501, "11 kB" },
  102. { 99499, "99 kB" },
  103. { 99500, "100 kB" },
  104. { 999499, "999 kB" },
  105. { 999500, "1.0 MB" },
  106. { 1000000, "1.0 MB" },
  107. { 952500000, "952 MB" },
  108. { 952500001, "953 MB" },
  109. { 1000000000, "1.0 GB" },
  110. { 2300000000000ULL, "2.3 TB" },
  111. { 9700000000000000ULL, "9.7 PB" }