From: Arran Cudbard-Bell Date: Fri, 17 Jun 2022 21:38:50 +0000 (-0500) Subject: Add support for mapping tables which map strings to string X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6242e0da92b88328ffb33288d3008042f3e4051d;p=thirdparty%2Ffreeradius-server.git Add support for mapping tables which map strings to string --- diff --git a/src/lib/util/table.c b/src/lib/util/table.c index 29824107398..7a9152568f6 100644 --- a/src/lib/util/table.c +++ b/src/lib/util/table.c @@ -32,6 +32,23 @@ RCSID("$Id$") #define ELEM_STR(_offset) (*((fr_table_elem_t const *)(_offset))).str #define ELEM_LEN(_offset) (*((fr_table_elem_t const *)(_offset))).len +/** Brute force search a sorted or ordered ptr table, assuming the pointers are strings + * + * @param[in] table to search in. + * @param[in] str_val to compare against the ptr field. + * @param[in] def default value. + */ +char const *_fr_table_ptr_by_str_value(fr_table_ptr_sorted_t const *table, size_t table_len, char const *str_val, char const *def) +{ + size_t i; + + if (!str_val) return NULL; + + for (i = 0; i < table_len; i++) if (strcasecmp(str_val, table[i].value) == 0) return table[i].name.str; + + return def; +} + /** Create type specific string to value functions * * @param[in] _func used for searching. diff --git a/src/lib/util/table.h b/src/lib/util/table.h index ab4044e992f..dbd7d2a15bd 100644 --- a/src/lib/util/table.h +++ b/src/lib/util/table.h @@ -95,6 +95,20 @@ typedef struct { */ #define NAME_NUMBER_NOT_FOUND INT32_MIN +char const *_fr_table_ptr_by_str_value(fr_table_ptr_sorted_t const *table, size_t table_len, char const *str_val, char const *def); + +/** Brute force search a sorted or ordered ptr table, assuming the pointers are strings + * + * @param[in] _table to search in. + * @param[in] _str_value to compare against the ptr field. + * @param[in] _def default value. + */ +#define fr_table_str_by_str_value(_table, _str_value, _def) \ +_Generic((_table), \ + fr_table_ptr_sorted_t const * : _fr_table_ptr_by_str_value((fr_table_ptr_sorted_t const *)_table, _table ## _len, _str_value, _def), \ + fr_table_ptr_ordered_t const * : _fr_table_ptr_by_str_value((fr_table_ptr_sorted_t const *)_table, _table ## _len, _str_value, _def), \ + fr_table_ptr_sorted_t * : _fr_table_ptr_by_str_value((fr_table_ptr_sorted_t const *)_table, _table ## _len, _str_value, _def), \ + fr_table_ptr_ordered_t * : _fr_table_ptr_by_str_value((fr_table_ptr_sorted_t const *)_table, _table ## _len, _str_value, _def)) int fr_table_sorted_num_by_str(fr_table_num_sorted_t const *table, size_t table_len, char const *name, int def);