]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
[MINOR] tools: add a get_std_op() function to parse operators
authorWilly Tarreau <w@1wt.eu>
Sun, 18 Jul 2010 08:40:48 +0000 (10:40 +0200)
committerWilly Tarreau <w@1wt.eu>
Tue, 10 Aug 2010 12:03:25 +0000 (14:03 +0200)
We already have several places where we use operators to compare
values. Each time the parsing is done again. Let's have a central
function for this.

include/common/standard.h
src/standard.c

index fcfb52f8bc22883a05801dfba2835ac2d6cd052b..cb5a3ec56bcf37d84d1bccbc550a63353a795339 100644 (file)
  * power of 2, and 0 otherwise */
 #define POWEROF2(x) (((x) & ((x)-1)) == 0)
 
+/* operators to compare values. They're ordered that way so that the lowest bit
+ * serves as a negation for the test and contains all tests that are not equal.
+ */
+enum {
+       STD_OP_LE = 0, STD_OP_GT = 1,
+       STD_OP_EQ = 2, STD_OP_NE = 3,
+       STD_OP_GE = 4, STD_OP_LT = 5,
+};
+
 /*
  * copies at most <size-1> chars from <src> to <dst>. Last char is always
  * set to 0, unless <size> is 0. The number of chars copied is returned
@@ -407,4 +416,7 @@ int buf2ip(const char *buf, size_t len, struct in_addr *dst);
  */
 const char *quote_arg(const char *ptr);
 
+/* returns an operator among STD_OP_* for string <str> or < 0 if unknown */
+int get_std_op(const char *str);
+
 #endif /* _COMMON_STANDARD_H */
index 12954b0c7c5787feb1be66f57d4cac119469900e..8aaa594dad6e8878d9e2063dd4e56865b4a6a135 100644 (file)
@@ -1078,6 +1078,29 @@ const char *quote_arg(const char *ptr)
        return val;
 }
 
+/* returns an operator among STD_OP_* for string <str> or < 0 if unknown */
+int get_std_op(const char *str)
+{
+       int ret = -1;
+
+       if (*str == 'e' && str[1] == 'q')
+               ret = STD_OP_EQ;
+       else if (*str == 'n' && str[1] == 'e')
+               ret = STD_OP_NE;
+       else if (*str == 'l') {
+               if (str[1] == 'e') ret = STD_OP_LE;
+               else if (str[1] == 't') ret = STD_OP_LT;
+       }
+       else if (*str == 'g') {
+               if (str[1] == 'e') ret = STD_OP_GE;
+               else if (str[1] == 't') ret = STD_OP_GT;
+       }
+
+       if (ret == -1 || str[2] != '\0')
+               return -1;
+       return ret;
+}
+
 /*
  * Local variables:
  *  c-indent-level: 8