The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
table.c
Go to the documentation of this file.
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 {
38  size_t i;
39 
40  if (!str_val) return NULL;
41 
42  for (i = 0; i < table_len; i++) if (strcasecmp(str_val, table[i].value) == 0) return table[i].name.str;
43 
44  return def;
45 }
46 
50  fr_table_sorted_ptr_by_str, void const *, void *)
51 
55  fr_table_ordered_ptr_by_str, void const *, void *)
56 
60  fr_table_sorted_ptr_by_substr, void const *, void *)
61 
65  fr_table_ordered_ptr_by_substr, void const *, void *)
66 
70  fr_table_sorted_ptr_by_longest_prefix, void const *, void *)
71 
75  fr_table_ordered_ptr_by_longest_prefix, void const *, void *)
76 
77 /*
78  * Value to string conversion functions
79  */
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  */
92 
93 /*
94  * Array lookup based on numeric value
95  */
#define RCSID(id)
Definition: build.h:481
Test enumeration values.
Definition: dict_test.h:92
int strcasecmp(char *s1, char *s2)
Definition: missing.c:66
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.
Definition: table.c:36
#define TABLE_TYPE_VALUE_FUNC(_our_table_type, _our_name, _our_value_type)
Create a type-specific value-to-name function.
Definition: table.h:283
char const * str
Literal string.
Definition: table.h:42
int fr_table_ordered_num_by_substr(fr_table_num_ordered_t const *table, size_t table_len, char const *name, ssize_t name_len, int def)
#define TABLE_TYPE_VALUE_INDEX_BIT_FIELD_FUNC(_our_table_type, _our_name, _our_value_type)
Create a type-specific value-to-name function, which uses the highest bit set in the value as an inde...
Definition: table.h:297
static void const * table_ordered_value_by_longest_prefix(size_t *match_len, void const *table, size_t table_len, size_t element_size, char const *name, ssize_t name_len)
Find the longest string match using an arbitrarily ordered table.
Definition: table.h:568
#define TABLE_TYPE_NAME_FUNC(_func, _our_table_type, _our_name, _our_def_type, _our_return_type)
Create a type-specific name-to-value function.
Definition: table.h:119
#define TABLE_TYPE_NAME_MATCH_LEN_FUNC(_func, _our_table_type, _our_name, _our_def_type, _our_return_type)
Create a type-specific name-to-value function that can perform substring matching with a 'name_len' a...
Definition: table.h:234
char const * fr_table_sorted_str_by_ptr(fr_table_ptr_sorted_t const *table, size_t table_len, void const *ptr, char const *def)
int fr_table_ordered_num_by_longest_prefix(size_t *match_len, fr_table_num_ordered_t const *table, size_t table_len, char const *name, ssize_t name_len, int def)
void * fr_table_ordered_ptr_by_str(fr_table_ptr_ordered_t const *table, size_t table_len, char const *name, void const *def)
void * fr_table_ordered_ptr_by_longest_prefix(size_t *match_len, fr_table_ptr_ordered_t const *table, size_t table_len, char const *name, ssize_t name_len, void const *def)
static void const * table_sorted_value_by_longest_prefix(size_t *match_len, void const *table, size_t table_len, size_t element_size, char const *name, ssize_t name_len)
Find the longest string match using a lexicographically sorted table.
Definition: table.h:492
static void const * table_ordered_value_by_substr(void const *table, size_t table_len, size_t element_size, char const *name, ssize_t name_len)
Convert a string matching part of name to an integer using an arbitrarily ordered table.
Definition: table.h:443
int fr_table_sorted_num_by_substr(fr_table_num_sorted_t const *table, size_t table_len, char const *name, ssize_t name_len, int def)
char const * fr_table_ordered_str_by_num(fr_table_num_ordered_t const *table, size_t table_len, int number, char const *def)
#define TABLE_TYPE_VALUE_INDEX_FUNC(_our_table_type, _our_name, _our_value_type)
Create a type-specific value-to-name function, which uses the value as an index into the table.
Definition: table.h:311
void * fr_table_sorted_ptr_by_str(fr_table_ptr_sorted_t const *table, size_t table_len, char const *name, void const *def)
fr_table_elem_name_t name
Definition: table.h:66
char const * fr_table_ordered_str_by_ptr(fr_table_ptr_ordered_t const *table, size_t table_len, void const *ptr, char const *def)
void * fr_table_sorted_ptr_by_substr(fr_table_ptr_sorted_t const *table, size_t table_len, char const *name, ssize_t name_len, void const *def)
char const * fr_table_sorted_str_by_num(fr_table_num_sorted_t const *table, size_t table_len, int number, char const *def)
int fr_table_sorted_num_by_str(fr_table_num_sorted_t const *table, size_t table_len, char const *name, int def)
char const * fr_table_indexed_str_by_num(fr_table_num_indexed_t const *table, size_t table_len, unsigned int number, char const *def)
void * fr_table_ordered_ptr_by_substr(fr_table_ptr_ordered_t const *table, size_t table_len, char const *name, ssize_t name_len, void const *def)
int fr_table_ordered_num_by_str(fr_table_num_ordered_t const *table, size_t table_len, char const *name, int def)
static void const * table_sorted_value_by_str(void const *table, size_t table_len, size_t element_size, char const *name)
Convert a string to a value using a lexicographically sorted table.
Definition: table.h:328
void * fr_table_sorted_ptr_by_longest_prefix(size_t *match_len, fr_table_ptr_sorted_t const *table, size_t table_len, char const *name, ssize_t name_len, void const *def)
static void const * table_ordered_value_by_str(void const *table, size_t table_len, size_t element_size, char const *name)
Convert a string to a value using an arbitrarily ordered table.
Definition: table.h:367
#define TABLE_TYPE_NAME_LEN_FUNC(_func, _our_table_type, _our_name, _our_def_type, _our_return_type)
Create a type-specific name-to-value function that can perform substring matching with a 'name_len' a...
Definition: table.h:173
char const * fr_table_indexed_str_by_bit_field(fr_table_num_indexed_bit_pos_t const *table, size_t table_len, uint64_t number, char const *def)
int fr_table_sorted_num_by_longest_prefix(size_t *match_len, fr_table_num_sorted_t const *table, size_t table_len, char const *name, ssize_t name_len, int def)
static void const * table_sorted_value_by_substr(void const *table, size_t table_len, size_t element_size, char const *name, ssize_t name_len)
Convert a string matching part of name to an integer using a lexicographically sorted table.
Definition: table.h:395
An element in a table indexed by bit position.
Definition: table.h:83
An element in a table indexed by numeric value.
Definition: table.h:92
An element in an arbitrarily ordered array of name to num mappings.
Definition: table.h:57
An element in a lexicographically sorted array of name to num mappings.
Definition: table.h:49
An element in an arbitrarily ordered array of name to ptr mappings.
Definition: table.h:73
An element in a lexicographically sorted array of name to ptr mappings.
Definition: table.h:65