]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: sample: add type_to_smp() helper function
authorAurelien DARRAGON <adarragon@haproxy.com>
Wed, 10 Jan 2024 13:00:15 +0000 (14:00 +0100)
committerAurelien DARRAGON <adarragon@haproxy.com>
Tue, 20 Feb 2024 14:18:39 +0000 (15:18 +0100)
type_to_smp(type) does the reverse operation of smp_to_type[smp]: it takes
a type name as input string and tries to return the corresponding SMP_T_*
smp type or SMP_TYPES if not found.

include/haproxy/sample.h
src/sample.c

index 7e05e784009a57add4122e38076a0e17b1862950..e8694c656a0d102dc685b4e456e3bf922cde63bb 100644 (file)
@@ -31,6 +31,7 @@
 extern sample_cast_fct sample_casts[SMP_TYPES][SMP_TYPES];
 extern const unsigned int fetch_cap[SMP_SRC_ENTRIES];
 extern const char *smp_to_type[SMP_TYPES];
+int type_to_smp(const char *type);
 
 struct sample_expr *sample_parse_expr(char **str, int *idx, const char *file, int line, char **err, struct arg_list *al, char **endptr);
 int sample_parse_expr_cnv(char **str, int *idx, char **endptr, char **err_msg, struct arg_list *al, const char *file, int line,
index a7b8a23a7950866c290e18a14be69777008af45f..cbb959161b09f26a0b5655e8d5dd2eb62b88424d 100644 (file)
@@ -61,6 +61,21 @@ const char *smp_to_type[SMP_TYPES] = {
        [SMP_T_METH] = "meth",
 };
 
+/* Returns SMP_T_* smp matching with <type> name or SMP_TYPES if
+ * not found.
+ */
+int type_to_smp(const char *type)
+{
+       int it = 0;
+
+       while (it < SMP_TYPES) {
+               if (!strcmp(type, smp_to_type[it]))
+                       break; // found
+               it += 1;
+       }
+       return it;
+}
+
 /* static sample used in sample_process() when <p> is NULL */
 static THREAD_LOCAL struct sample temp_smp;