/src/freeradius-server/src/lib/util/table.c
Line | Count | Source |
1 | | /* |
2 | | * This library is free software; you can redistribute it and/or |
3 | | * modify it under the terms of the GNU Lesser General Public |
4 | | * License as published by the Free Software Foundation; either |
5 | | * version 2.1 of the License, or (at your option) any later version. |
6 | | * |
7 | | * This library is distributed in the hope that it will be useful, |
8 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
9 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
10 | | * Lesser General Public License for more details. |
11 | | * |
12 | | * You should have received a copy of the GNU Lesser General Public |
13 | | * License along with this library; if not, write to the Free Software |
14 | | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA |
15 | | */ |
16 | | |
17 | | /** Functions to convert strings to integers and vice versa |
18 | | * |
19 | | * @file lib/util/table.c |
20 | | * |
21 | | * @copyright 2019 The FreeRADIUS server project |
22 | | * @copyright 2019 Arran Cudbard-Bell (a.cudbardb@freeradius.org) |
23 | | */ |
24 | | RCSID("$Id: a8184ef5af35c13d9707c99902b9e42e322260b5 $") |
25 | | |
26 | | #include <freeradius-devel/util/table.h> |
27 | | #include <freeradius-devel/util/misc.h> |
28 | | |
29 | | /** Brute force search a sorted or ordered ptr table, assuming the pointers are strings |
30 | | * |
31 | | * @param[in] table to search in. |
32 | | * @param[in] table_len Number of elements in the table. |
33 | | * @param[in] str_val to compare against the ptr field. |
34 | | * @param[in] def default value. |
35 | | */ |
36 | | 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) |
37 | 0 | { |
38 | 0 | size_t i; |
39 | |
|
40 | 0 | if (!str_val) return NULL; |
41 | | |
42 | 0 | for (i = 0; i < table_len; i++) if (strcasecmp(str_val, table[i].value) == 0) return table[i].name.str; |
43 | | |
44 | 0 | return def; |
45 | 0 | } |
46 | | |
47 | | TABLE_TYPE_NAME_FUNC(table_sorted_value_by_str, fr_table_num_sorted_t const *, |
48 | | fr_table_sorted_num_by_str, int, int) |
49 | | TABLE_TYPE_NAME_FUNC(table_sorted_value_by_str, fr_table_ptr_sorted_t const *, |
50 | | fr_table_sorted_ptr_by_str, void const *, void *) |
51 | | |
52 | | TABLE_TYPE_NAME_FUNC(table_ordered_value_by_str, fr_table_num_ordered_t const *, |
53 | | fr_table_ordered_num_by_str, int, int) |
54 | | TABLE_TYPE_NAME_FUNC(table_ordered_value_by_str, fr_table_ptr_ordered_t const *, |
55 | | fr_table_ordered_ptr_by_str, void const *, void *) |
56 | | |
57 | | TABLE_TYPE_NAME_LEN_FUNC(table_sorted_value_by_substr, fr_table_num_sorted_t const *, |
58 | | fr_table_sorted_num_by_substr, int, int) |
59 | | TABLE_TYPE_NAME_LEN_FUNC(table_sorted_value_by_substr, fr_table_ptr_sorted_t const *, |
60 | | fr_table_sorted_ptr_by_substr, void const *, void *) |
61 | | |
62 | | TABLE_TYPE_NAME_LEN_FUNC(table_ordered_value_by_substr, fr_table_num_ordered_t const *, |
63 | | fr_table_ordered_num_by_substr, int, int) |
64 | | TABLE_TYPE_NAME_LEN_FUNC(table_ordered_value_by_substr, fr_table_ptr_ordered_t const *, |
65 | | fr_table_ordered_ptr_by_substr, void const *, void *) |
66 | | |
67 | | TABLE_TYPE_NAME_MATCH_LEN_FUNC(table_sorted_value_by_longest_prefix, fr_table_num_sorted_t const *, |
68 | | fr_table_sorted_num_by_longest_prefix, int, int) |
69 | | TABLE_TYPE_NAME_MATCH_LEN_FUNC(table_sorted_value_by_longest_prefix, fr_table_ptr_sorted_t const *, |
70 | | fr_table_sorted_ptr_by_longest_prefix, void const *, void *) |
71 | | |
72 | | TABLE_TYPE_NAME_MATCH_LEN_FUNC(table_ordered_value_by_longest_prefix, fr_table_num_ordered_t const *, |
73 | | fr_table_ordered_num_by_longest_prefix, int, int) |
74 | | TABLE_TYPE_NAME_MATCH_LEN_FUNC(table_ordered_value_by_longest_prefix, fr_table_ptr_ordered_t const *, |
75 | | fr_table_ordered_ptr_by_longest_prefix, void const *, void *) |
76 | | |
77 | | /* |
78 | | * Value to string conversion functions |
79 | | */ |
80 | | TABLE_TYPE_VALUE_FUNC(fr_table_num_sorted_t const *, fr_table_sorted_str_by_num, int) |
81 | | TABLE_TYPE_VALUE_FUNC(fr_table_num_ordered_t const *, fr_table_ordered_str_by_num, int) |
82 | | TABLE_TYPE_VALUE_FUNC(fr_table_ptr_sorted_t const *, fr_table_sorted_str_by_ptr, void const *) |
83 | | TABLE_TYPE_VALUE_FUNC(fr_table_ptr_ordered_t const *, fr_table_ordered_str_by_ptr, void const *) |
84 | | |
85 | | /* |
86 | | * Indexed value to string conversion functions |
87 | | * These are O(1) for bitfields, and are |
88 | | * particularly useful for looking up string |
89 | | * definitions for flag values. |
90 | | */ |
91 | | TABLE_TYPE_VALUE_INDEX_BIT_FIELD_FUNC(fr_table_num_indexed_bit_pos_t const *, fr_table_indexed_str_by_bit_field, uint64_t) |
92 | | |
93 | | /* |
94 | | * Array lookup based on numeric value |
95 | | */ |
96 | | TABLE_TYPE_VALUE_INDEX_FUNC(fr_table_num_indexed_t const *, fr_table_indexed_str_by_num, unsigned int) |