]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
Add support for mapping tables which map strings to string
authorArran Cudbard-Bell <a.cudbardb@freeradius.org>
Fri, 17 Jun 2022 21:38:50 +0000 (16:38 -0500)
committerArran Cudbard-Bell <a.cudbardb@freeradius.org>
Sat, 18 Jun 2022 02:04:55 +0000 (21:04 -0500)
src/lib/util/table.c
src/lib/util/table.h

index 29824107398f86dc4de611b584d2364970071a75..7a9152568f67a7291950f2d718b1515e1839d0c1 100644 (file)
@@ -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.
index ab4044e992f2ce8b9c2fb44464fb7f9ab6a1d941..dbd7d2a15bdd169c3185cec891e357e1feb6ede3 100644 (file)
@@ -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);