]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
[MINOR] Add function to parse a size in configuration
authorEmeric Brun <ebrun@exceliance.fr>
Mon, 4 Jan 2010 13:57:24 +0000 (14:57 +0100)
committerWilly Tarreau <w@1wt.eu>
Tue, 12 Jan 2010 10:23:15 +0000 (11:23 +0100)
include/common/standard.h
src/standard.c

index 4dd622ac4b85c5990e809c3d9f7d886a29bb6efa..7a637298411f252f96b0dc80889993c98d1a0de1 100644 (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
index 6ae4dd7842aa3c4a85aed414843445802939a508..81cdda407b827b855f9f2312ce4fa27937647e25 100644 (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)
 {