-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbytes.c
48 lines (40 loc) · 935 Bytes
/
bytes.c
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//
// bytes.c
//
// Copyright (c) 2012 TJ Holowaychuk <[email protected]>
//
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "bytes.h"
// bytes
#define KB 1024
#define MB 1024 * KB
#define GB 1024 * MB
/*
* Convert the given `str` to byte count.
*/
long long
string_to_bytes(const char *str) {
long long val = strtoll(str, NULL, 10);
if (!val) return -1;
if (strstr(str, "kb")) return val * KB;
if (strstr(str, "mb")) return val * MB;
if (strstr(str, "gb")) return val * GB;
return val;
}
/*
* Convert the given `bytes` to a string. This
* value must be `free()`d by the user.
*/
char *
bytes_to_string(long long bytes) {
long div = 1;
char *str, *fmt;
if (bytes < KB) { fmt = "%lldb"; }
else if (bytes < MB) { fmt = "%lldkb"; div = KB; }
else if (bytes < GB) { fmt = "%lldmb"; div = MB; }
else { fmt = "%lldgb"; div = GB; }
asprintf(&str, fmt, bytes / div);
return str;
}