summaryrefslogtreecommitdiff
path: root/src/utils/size_info.c
blob: b846ae62be3a1c9d694f114a72599484c04a950a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include "size_info.h"

static const char *size_unit_map[] = {"B", "KB", "MB", "GB"};

size_info_t num2sizeinfo(const unsigned long long n) {
  size_info_t size_info = {0};
  long double tmp = (long double)n;
  if ((unsigned long)tmp / 1024 == 0) {
    size_info.unit = SIZE_B;
  } else {
    tmp /= 1024;
    if ((unsigned long)tmp / 1024 == 0) {
      size_info.unit = SIZE_KB;
    } else {
      tmp /= 1024;
      if ((unsigned long)tmp / 1024 == 0) {
        size_info.unit = SIZE_MB;
      } else {
        tmp /= 1024;
        size_info.unit = SIZE_GB;
      }
    }
  }
  size_info.n = tmp;
  size_info.unit_str = size_unit_map[size_info.unit];
  return size_info;
}