[MINOR] Add function to parse a size in configuration

This commit is contained in:
Emeric Brun 2010-01-04 14:57:24 +01:00 committed by Willy Tarreau
parent d72758ded1
commit 39132b2165
2 changed files with 52 additions and 0 deletions

View File

@ -334,6 +334,7 @@ static inline void get_gmtime(const time_t now, struct tm *tm)
* <ret> is left untouched.
*/
extern const char *parse_time_err(const char *text, unsigned *ret, unsigned unit_flags);
extern const char *parse_size_err(const char *text, unsigned *ret);
/* unit flags to pass to parse_time_err */
#define TIME_UNIT_US 0x0000

View File

@ -729,6 +729,57 @@ const char *parse_time_err(const char *text, unsigned *ret, unsigned unit_flags)
return NULL;
}
/* this function converts the string starting at <text> to an unsigned int
* stored in <ret>. If an error is detected, the pointer to the unexpected
* character is returned. If the conversio is succesful, NULL is returned.
*/
const char *parse_size_err(const char *text, unsigned *ret) {
unsigned value = 0;
while (1) {
unsigned int j;
j = *text - '0';
if (j > 9)
break;
if (value > ~0U / 10)
return text;
value *= 10;
if (value > (value + j))
return text;
value += j;
text++;
}
switch (*text) {
case '\0':
break;
case 'K':
case 'k':
if (value > ~0U >> 10)
return text;
value = value << 10;
break;
case 'M':
case 'm':
if (value > ~0U >> 20)
return text;
value = value << 20;
break;
case 'G':
case 'g':
if (value > ~0U >> 30)
return text;
value = value << 30;
break;
default:
return text;
}
*ret = value;
return NULL;
}
/* copies at most <n> characters from <src> and always terminates with '\0' */
char *my_strndup(const char *src, int n)
{