Skip to content

Commit

Permalink
mx_util: Add mx_strscan_ull() and mx_strscan_ll()
Browse files Browse the repository at this point in the history
  • Loading branch information
mariux committed Sep 23, 2015
1 parent f42d526 commit bbc2071
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
62 changes: 62 additions & 0 deletions mx_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,68 @@ int mx_read_first_line_from_file(char *fname, char **line)
return res;
}

int mx_strscan_ull(char **str, unsigned long long int *to)
{
unsigned long long int l;
char *s;
char *p;
char o = 0;
int res;

s = *str;

p = strchr(s, ' ');
if (p) {
o = *p;
*p = 0;
p++;
} else {
p = s + strlen(s);
}

res = mx_strtoull(s, &l);
if (o)
*(p-1) = o;

if (res == 0) {
*to = l;
*str = p;
}

return res;
}

int mx_strscan_ll(char **str, long long int *to)
{
long long int l;
char *s;
char *p;
char o = 0;
int res;

s = *str;

p = strchr(s, ' ');
if (p) {
o = *p;
*p = 0;
p++;
} else {
p = s + strlen(s);
}

res = mx_strtoll(s, &l);
if (o)
*(p-1) = o;

if (res == 0) {
*to = l;
*str = p;
}

return res;
}

int mx_sleep(unsigned int seconds)
{
if (seconds)
Expand Down
3 changes: 3 additions & 0 deletions mx_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ int mx_open_newfile(char *fname);

int mx_read_first_line_from_file(char *fname, char **line);

int mx_strscan_ull(char **str, unsigned long long int *to);
int mx_strscan_ll(char **str, long long int *to);

int mx_sleep(unsigned int seconds);
int mx_sleep_nofail(unsigned int seconds);

Expand Down
42 changes: 42 additions & 0 deletions test_mx_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,47 @@ static void test_mx_read_first_line_from_file(void)
mx_free_null(str);
}

static void test_mx_strscan(void)
{
_mx_cleanup_free_ char *s = NULL;
char *str;
unsigned long long int ull;
long long int ll;

assert(s = strdup("123 456 -789 246 abc"));
str = s;

assert(mx_strscan_ull(&str, &ull) == 0);
assert(ull == 123);

assert(mx_strscan_ull(&str, &ull) == 0);
assert(ull == 456);

assert(mx_strscan_ull(&str, &ull) == -ERANGE);
assert(mx_streq(str, "-789 246 abc"));

assert(mx_strscan_ll(&str, &ll) == 0);
assert(ll == -789);
assert(mx_streq(str, "246 abc"));

assert(mx_strscan_ll(&str, &ll) == 0);
assert(ll == 246);
assert(mx_streq(str, "abc"));

assert(mx_strscan_ull(&str, &ull) == -EINVAL);
assert(mx_streq(str, "abc"));
assert(mx_streq(s, "123 456 -789 246 abc"));
mx_free_null(s);

assert(s = strdup("123"));
str = s;
assert(mx_strscan_ull(&str, &ull) == 0);
assert(ull == 123);
assert(mx_streq(str, ""));
assert(mx_streq(s, "123"));

}

int main(int argc, char *argv[])
{
test_mx_strskipwhitespaces();
Expand All @@ -306,5 +347,6 @@ int main(int argc, char *argv[])
test_mx_strtominutes();
test_mx_strtobytes();
test_mx_read_first_line_from_file();
test_mx_strscan();
return 0;
}

0 comments on commit bbc2071

Please sign in to comment.