Skip to content

Commit

Permalink
core: utils functions to converts strz to integer
Browse files Browse the repository at this point in the history
  • Loading branch information
miconda committed Nov 26, 2019
1 parent cef6284 commit b36da1d
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions src/core/ut.h
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,62 @@ static inline int str2sint(str* _s, int* _r)
}


/*
* Convert a str into integer
*/
static inline int strz2int(char* _s, unsigned int* _r)
{
int i;

if (_r == NULL) return -1;
*_r = 0;
if (_s == NULL) return -1;

for(i = 0; _s[i] != '\0'; i++) {
if ((_s[i] >= '0') && (_s[i] <= '9')) {
*_r *= 10;
*_r += _s[i] - '0';
} else {
return -1;
}
}

return 0;
}

/*
* Convert an str to signed integer
*/
static inline int strz2sint(char* _s, int* _r)
{
int i;
int sign;

if (_r == NULL) return -1;
*_r = 0;
if (_s == NULL) return -1;

sign = 1;
i = 0;
if (_s[0] == '+') {
i++;
} else if (_s[0] == '-') {
sign = -1;
i++;
}
for(; _s[i] != '\0'; i++) {
if ((_s[i] >= '0') && (_s[i] <= '9')) {
*_r *= 10;
*_r += _s[i] - '0';
} else {
return -1;
}
}
*_r *= sign;

return 0;
}


/**
* \brief Make a copy of a str structure to a str using shm_malloc
Expand Down

0 comments on commit b36da1d

Please sign in to comment.