Skip to content

Commit

Permalink
mx_util: Add mxq_strtobytes()
Browse files Browse the repository at this point in the history
  • Loading branch information
mariux committed Aug 26, 2015
1 parent 366ae7f commit b92053b
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
66 changes: 66 additions & 0 deletions mx_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,72 @@ inline int mx_stribeginswithany(char *str, char **starts, char **endptr)
return _mx_strbeginswithany(str, starts, endptr, 1);
}

inline int mx_strtobytes(char *str, unsigned long long int *bytes)
{
unsigned long long int s = 0;
unsigned long long int t;

char *end;

if (!str || !*str)
return -(errno=EINVAL);

if (strchr(str, '-'))
return -(errno=ERANGE);

do {
errno = 0;
t = strtoull(str, &end, 10);

if (errno)
return -errno;

if (str == end)
return -(errno=EINVAL);

for (;*end && *end == ' '; end++)
/* empty */;

switch (*end) {

case 'T': /* tebi */
t *= 1024;

case 'G': /* gibi */
t *= 1024;

case 'M': /* mebi */
t *= 1024;

case 'k': /* kibi */
case 'K':
t *= 1024;

case 'B': /* bytes */
end++;
break;

default:
return -(errno=EINVAL);
}

if (s+t < s)
return -(errno=ERANGE);

s += t;

for (;*end && *end == ' '; end++)
/* empty */;

str = end;

} while (*str);

*bytes = s;

return 0;
}

inline int mx_strtoseconds(char *str, unsigned long long int *seconds)
{
unsigned long long int s = 0;
Expand Down
2 changes: 2 additions & 0 deletions mx_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ int mx_strbeginswithany(char *str, char **starts, char **endptr);

char *mx_strskipwhitespaces(char *str);

int mx_strtobytes(char *str, unsigned long long int *bytes);

int mx_strtoseconds(char *str, unsigned long long int *seconds);
int mx_strtominutes(char *str, unsigned long long int *minutes);

Expand Down
28 changes: 28 additions & 0 deletions test_mx_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,33 @@ static void test_mx_strtominutes(void)
assert(mx_strtominutes("test", &l) == -EINVAL);
}

static void test_mx_strtobytes(void)
{
unsigned long long int l;

assert(mx_strtobytes("123B", &l) == 0);
assert(l == 123);

assert(mx_strtobytes("2M", &l) == 0);
assert(l == 2*1024*1024);

assert(mx_strtobytes("1M1024k", &l) == 0);
assert(l == 2*1024*1024);

assert(mx_strtobytes("1024k1024K", &l) == 0);
assert(l == 2*1024*1024);

assert(mx_strtobytes("-1", &l) == -ERANGE);
assert(mx_strtobytes(" -1", &l) == -ERANGE);

assert(mx_strtobytes("2.5M", &l) == -EINVAL);
assert(mx_strtobytes("123", &l) == -EINVAL);
assert(mx_strtobytes("0123", &l) == -EINVAL);
assert(mx_strtobytes("1.2", &l) == -EINVAL);
assert(mx_strtobytes("1,2", &l) == -EINVAL);
assert(mx_strtobytes("test", &l) == -EINVAL);
}

int main(int argc, char *argv[])
{
test_mx_strskipwhitespaces();
Expand All @@ -256,5 +283,6 @@ int main(int argc, char *argv[])
test_mx_strbeginswithany();
test_mx_strtoseconds();
test_mx_strtominutes();
test_mx_strtobytes();
return 0;
}

0 comments on commit b92053b

Please sign in to comment.