]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: stktable: add stktable_get_data_type_idx() helper function
authorAurelien DARRAGON <adarragon@haproxy.com>
Thu, 19 Dec 2024 17:29:53 +0000 (18:29 +0100)
committerAurelien DARRAGON <adarragon@haproxy.com>
Mon, 23 Dec 2024 16:32:09 +0000 (17:32 +0100)
Same as stktable_get_data_type(), but tries to parse optional index in
the form "name[idx]" (only for array types).

Falls back to stktable_get_data_type() when no index is provided.

src/stick_table.c

index 1da4296b85db42646d5e09a87e60b79093e70906..775cd1e18c2ef7bb3e49e9bf557bd7d9397bc3fa 100644 (file)
@@ -1647,6 +1647,47 @@ int stktable_get_data_type(char *name)
        return -1;
 }
 
+/*
+ * Same as stktable_get_data_type() but also expects optional index after the
+ * name in the form "name[idx]", but only for array types
+ * If index optional argument is not provided, default value (0) is applied
+ *
+ * Returns the data type number on success, or < 0 if not found.
+ */
+int stktable_get_data_type_idx(char *name, unsigned int *idx)
+{
+       int type;
+       size_t stop = strcspn(name, "[");
+
+       if (!name[stop]) {
+               /* no idx argument */
+               *idx = 0;
+               return stktable_get_data_type(name);
+       }
+
+       for (type = 0; type < STKTABLE_DATA_TYPES; type++) {
+               char *ret;
+
+               if (!stktable_data_types[type].name ||
+                   !stktable_data_types[type].is_array)
+                       continue;
+               if (strncmp(name, stktable_data_types[type].name, stop))
+                       continue;
+
+               /* we've got a match */
+               name += stop + 1;
+               *idx = strtoul(name, &ret, 10);
+               if (ret == name || *ret != ']')
+                       return -1; // bad value
+               if (ret[1])
+                       return -1; // unexpected data
+
+               return type;
+       }
+
+       return -1; // not found
+}
+
 /* Casts sample <smp> to the type of the table specified in arg(0), and looks
  * it up into this table. Returns true if found, false otherwise. The input
  * type is STR so that input samples are converted to string (since all types