From: Emeric Brun Date: Mon, 4 Jan 2010 13:57:24 +0000 (+0100) Subject: [MINOR] Add function to parse a size in configuration X-Git-Tag: v1.4-dev7~27 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=39132b2165c48e01a2306e9c18aae8c368182e69;p=thirdparty%2Fhaproxy.git [MINOR] Add function to parse a size in configuration --- diff --git a/include/common/standard.h b/include/common/standard.h index 4dd622ac4b..7a63729841 100644 --- a/include/common/standard.h +++ b/include/common/standard.h @@ -334,6 +334,7 @@ static inline void get_gmtime(const time_t now, struct tm *tm) * 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 diff --git a/src/standard.c b/src/standard.c index 6ae4dd7842..81cdda407b 100644 --- a/src/standard.c +++ b/src/standard.c @@ -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 to an unsigned int + * stored in . 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 characters from and always terminates with '\0' */ char *my_strndup(const char *src, int n) {