From: Aurelien DARRAGON Date: Thu, 19 Dec 2024 17:29:53 +0000 (+0100) Subject: MINOR: stktable: add stktable_get_data_type_idx() helper function X-Git-Tag: v3.2-dev2~14 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=c0dc7769d49db93165283496409dc0a2230b420c;p=thirdparty%2Fhaproxy.git MINOR: stktable: add stktable_get_data_type_idx() helper function 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. --- diff --git a/src/stick_table.c b/src/stick_table.c index 1da4296b85..775cd1e18c 100644 --- a/src/stick_table.c +++ b/src/stick_table.c @@ -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 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