forked from moosefs/moosefs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsizestr.h
85 lines (85 loc) · 1.27 KB
/
sizestr.h
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
static inline double sizestrtod(const char *restrict str, const char **restrict endptr) {
double val,frac;
int f;
val = 0.0;
f = 0;
while (*str>='0' && *str<='9') {
val *= 10.0;
val += (*str-'0');
str++;
f = 1;
}
if (*str=='.' && str[1]>='0' && str[1]<='9') {
f = 1;
str++;
frac = 1.0;
while (*str>='0' && *str<='9') {
frac /= 10.0;
val += (*str-'0') * frac;
str++;
}
}
if (f) {
switch (*str) {
case 'k':
str++;
val *= 1e3;
break;
case 'K':
if (str[1]=='i') {
str+=2;
val *= 1024.0;
}
break;
case 'M':
str++;
if (*str=='i') {
str++;
val *= 1048576.0;
} else {
val *= 1e6;
}
break;
case 'G':
str++;
if (*str=='i') {
str++;
val *= 1073741824.0;
} else {
val *= 1e9;
}
break;
case 'T':
str++;
if (*str=='i') {
str++;
val *= 1099511627776.0;
} else {
val *= 1e12;
}
break;
case 'P':
str++;
if (*str=='i') {
str++;
val *= 1125899906842624.0;
} else {
val *= 1e15;
}
break;
case 'E':
str++;
if (*str=='i') {
str++;
val *= 1152921504606846976.0;
} else {
val *= 1e18;
}
break;
}
}
if (endptr!=NULL) {
*endptr = str;
}
return val;
}