27 RCSIDH(table_h,
"$Id: 5d94d934fbd9666fabd2f3152c4c4e3a9ffa7829 $")
37 #include <sys/types.h>
100 #define NAME_NUMBER_NOT_FOUND INT32_MIN
104 #define TABLE_IDX(_table, _idx, _element_size) (((uint8_t const *)(_table)) + ((_idx) * (_element_size)))
105 #define ELEM_STR(_offset) (*((fr_table_elem_name_t const *)(_offset))).str
106 #define ELEM_LEN(_offset) (*((fr_table_elem_name_t const *)(_offset))).len
119 #define TABLE_TYPE_NAME_FUNC(_func, _our_table_type, _our_name, _our_def_type, _our_return_type) \
120 _our_return_type _our_name(_our_table_type table, size_t table_len, char const *name, _our_def_type def) \
122 _our_return_type ret; \
123 _our_table_type found; \
124 found = (_our_table_type)_func(table, table_len, sizeof(((_our_table_type)0)[0]), name); \
126 memcpy(&ret, &def, sizeof(ret)); \
129 memcpy(&ret, &found->value, sizeof(ret)); \
144 #define TABLE_TYPE_NAME_FUNC_RPTR(_func, _our_table_type, _our_name, _our_def_type, _our_out_type) \
145 bool _our_name(_our_out_type *out, _our_table_type table, size_t table_len, char const *name, _our_def_type def) \
147 _our_table_type found; \
148 found = (_our_table_type)_func(table, table_len, sizeof(((_our_table_type)0)[0]), name); \
153 *out = &found->value; \
173 #define TABLE_TYPE_NAME_LEN_FUNC(_func, _our_table_type, _our_name, _our_def_type, _our_return_type) \
174 _our_return_type _our_name(_our_table_type table, size_t table_len, char const *name, ssize_t name_len, _our_def_type def) \
176 _our_return_type ret; \
177 _our_table_type found; \
178 found = (_our_table_type)_func(table, table_len, sizeof(((_our_table_type)0)[0]), name, name_len); \
180 memcpy(&ret, &def, sizeof(ret)); \
183 memcpy(&ret, &found->value, sizeof(ret)); \
206 #define TABLE_TYPE_NAME_LEN_FUNC_RPTR(_func, _our_table_type, _our_name, _our_def_type, _our_out_type) \
207 bool _our_name(_our_out_type *out, _our_table_type table, size_t table_len, char const *name, ssize_t name_len, _our_def_type def) \
209 _our_table_type found; \
210 found = (_our_table_type)_func(table, table_len, sizeof(((_our_table_type)0)[0]), name, name_len); \
215 *out = &found->value; \
234 #define TABLE_TYPE_NAME_MATCH_LEN_FUNC(_func, _our_table_type, _our_name, _our_def_type, _our_return_type) \
235 _our_return_type _our_name(size_t *match_len, _our_table_type table, size_t table_len, char const *name, ssize_t name_len, _our_def_type def) \
237 _our_return_type ret; \
238 _our_table_type found; \
239 found = (_our_table_type)_func(match_len, table, table_len, sizeof(((_our_table_type)0)[0]), name, name_len); \
241 memcpy(&ret, &def, sizeof(ret)); \
244 memcpy(&ret, &found->value, sizeof(ret)); \
264 #define TABLE_TYPE_NAME_MATCH_LEN_FUNC_RPTR(_func, _our_table_type, _our_name, _our_def_type, _our_out_type) \
265 bool _our_name(size_t *match_len, _our_out_type *out, _our_table_type table, size_t table_len, char const *name, ssize_t name_len, _our_def_type def) \
267 _our_table_type found; \
268 found = (_our_table_type)_func(match_len, table, table_len, sizeof(((_our_table_type)0)[0]), name, name_len); \
273 *out = &found->value; \
283 #define TABLE_TYPE_VALUE_FUNC(_our_table_type, _our_name, _our_value_type) \
284 char const *_our_name(_our_table_type table, size_t table_len, _our_value_type value, char const *def) \
287 for (i = 0; i < table_len; i++) if (table[i].value == value) return table[i].name.str; \
297 #define TABLE_TYPE_VALUE_INDEX_BIT_FIELD_FUNC(_our_table_type, _our_name, _our_value_type) \
298 char const *_our_name(_our_table_type table, size_t table_len, _our_value_type value, char const *def) \
300 uint8_t idx = fr_high_bit_pos(value); \
301 if (idx >= table_len) return def; \
302 return table[idx].name.str; \
311 #define TABLE_TYPE_VALUE_INDEX_FUNC(_our_table_type, _our_name, _our_value_type) \
312 char const *_our_name(_our_table_type table, size_t table_len, _our_value_type value, char const *def) \
314 if (value >= table_len) return def; \
315 return table[value].name.str; \
337 if (!
name)
return NULL;
339 while (start <= end) {
342 mid = start + ((end - start) / 2);
344 offset =
TABLE_IDX(table, mid, element_size);
346 if (ret == 0)
return offset;
372 if (!
name)
return NULL;
374 for (i = 0; i < table_len; i++) {
375 void const *offset =
TABLE_IDX(table, i, element_size);
404 if (!
name)
return NULL;
406 while (start <= end) {
409 mid = start + ((end - start) / 2);
411 offset =
TABLE_IDX(table, mid, element_size);
417 (name_len < 0) ?
ELEM_LEN(offset) : (
size_t)name_len);
418 if (ret == 0)
return offset;
448 if (!
name)
return NULL;
450 for (i = 0; i < table_len; i++) {
454 offset =
TABLE_IDX(table, i, element_size);
461 if ((name_len > 0) && (name_len < (
int) tlen))
continue;
467 (name_len < 0) ? tlen : (
size_t)name_len) == 0)
return offset;
493 void const *table,
size_t table_len,
size_t element_size,
501 void const *found = NULL;
503 if (!
name)
return NULL;
504 if (name_len < 0) name_len = strlen(
name);
506 while (start <= end) {
511 mid = start + ((end - start) / 2);
513 offset =
TABLE_IDX(table, mid, element_size);
517 ret =
strncasecmp(
name, elem, tlen < (
size_t)name_len ? tlen : (
size_t)name_len);
522 if (tlen == (
size_t)name_len) {
523 if (match_len) *match_len = tlen;
533 if (tlen < (
size_t)name_len) {
535 if (match_len) *match_len = tlen;
549 if (!found && match_len) *match_len = 0;
569 void const *table,
size_t table_len,
size_t element_size,
573 size_t found_len = 0;
574 void const *found = NULL;
576 if (!
name)
return NULL;
577 if (name_len < 0) name_len = strlen(
name);
579 for (i = 0; i < table_len; i++) {
583 offset =
TABLE_IDX(table, i, element_size);
586 (tolower(
name[j]) == tolower((
ELEM_STR(offset))[j])); j++);
592 if ((
ELEM_STR(offset))[j] !=
'\0')
continue;
597 if (j == (
size_t)name_len) {
598 if (match_len) *match_len = name_len;
614 if (match_len) *match_len = found_len;
625 #define fr_table_str_by_str_value(_table, _str_value, _def) \
627 fr_table_ptr_sorted_t const * : _fr_table_ptr_by_str_value((fr_table_ptr_sorted_t const *)_table, _table ## _len, _str_value, _def), \
628 fr_table_ptr_ordered_t const * : _fr_table_ptr_by_str_value((fr_table_ptr_sorted_t const *)_table, _table ## _len, _str_value, _def), \
629 fr_table_ptr_sorted_t * : _fr_table_ptr_by_str_value((fr_table_ptr_sorted_t const *)_table, _table ## _len, _str_value, _def), \
630 fr_table_ptr_ordered_t * : _fr_table_ptr_by_str_value((fr_table_ptr_sorted_t const *)_table, _table ## _len, _str_value, _def))
633 char const *
name,
int def);
636 char const *
name,
int def);
639 char const *
name,
void const *def);
642 char const *
name,
void const *def);
653 #define fr_table_value_by_str(_table, _name, _def) \
655 fr_table_num_sorted_t const * : fr_table_sorted_num_by_str, \
656 fr_table_num_ordered_t const * : fr_table_ordered_num_by_str, \
657 fr_table_num_sorted_t * : fr_table_sorted_num_by_str, \
658 fr_table_num_ordered_t * : fr_table_ordered_num_by_str, \
659 fr_table_ptr_sorted_t const * : fr_table_sorted_ptr_by_str, \
660 fr_table_ptr_ordered_t const * : fr_table_ordered_ptr_by_str, \
661 fr_table_ptr_sorted_t * : fr_table_sorted_ptr_by_str, \
662 fr_table_ptr_ordered_t * : fr_table_ordered_ptr_by_str, \
663 fr_table_num_indexed_bit_pos_t * : fr_table_ordered_num_by_str, \
664 fr_table_num_indexed_bit_pos_t const * : fr_table_ordered_num_by_str, \
665 fr_table_num_indexed_t * : fr_table_ordered_num_by_str, \
666 fr_table_num_indexed_t const * : fr_table_ordered_num_by_str \
667 )(_table, _table ## _len, _name, _def)
676 char const *
name,
ssize_t name_len,
void const *def);
679 char const *
name,
ssize_t name_len,
void const *def);
693 #define fr_table_value_by_substr(_table, _name, _name_len, _def) \
695 fr_table_num_sorted_t const * : fr_table_sorted_num_by_substr, \
696 fr_table_num_ordered_t const * : fr_table_ordered_num_by_substr, \
697 fr_table_num_sorted_t * : fr_table_sorted_num_by_substr, \
698 fr_table_num_ordered_t * : fr_table_ordered_num_by_substr, \
699 fr_table_ptr_sorted_t const * : fr_table_sorted_ptr_by_substr, \
700 fr_table_ptr_ordered_t const * : fr_table_ordered_ptr_by_substr, \
701 fr_table_ptr_sorted_t * : fr_table_sorted_ptr_by_substr, \
702 fr_table_ptr_ordered_t * : fr_table_ordered_ptr_by_substr, \
703 fr_table_num_indexed_bit_pos_t * : fr_table_ordered_num_by_substr, \
704 fr_table_num_indexed_bit_pos_t const * : fr_table_ordered_num_by_substr, \
705 fr_table_num_indexed_t * : fr_table_ordered_num_by_substr, \
706 fr_table_num_indexed_t const * : fr_table_ordered_num_by_substr \
707 )(_table, _table ## _len, _name, _name_len, _def)
716 char const *
name,
ssize_t name_len,
void const *def);
719 char const *
name,
ssize_t name_len,
void const *def);
732 #define fr_table_value_by_longest_prefix(_match_len, _table, _name, _name_len, _def) \
734 fr_table_num_sorted_t const * : fr_table_sorted_num_by_longest_prefix, \
735 fr_table_num_ordered_t const * : fr_table_ordered_num_by_longest_prefix, \
736 fr_table_num_sorted_t * : fr_table_sorted_num_by_longest_prefix, \
737 fr_table_num_ordered_t * : fr_table_ordered_num_by_longest_prefix, \
738 fr_table_ptr_sorted_t const * : fr_table_sorted_ptr_by_longest_prefix, \
739 fr_table_ptr_ordered_t const * : fr_table_ordered_ptr_by_longest_prefix, \
740 fr_table_ptr_sorted_t * : fr_table_sorted_ptr_by_longest_prefix, \
741 fr_table_ptr_ordered_t * : fr_table_ordered_ptr_by_longest_prefix, \
742 fr_table_num_indexed_bit_pos_t * : fr_table_ordered_num_by_longest_prefix, \
743 fr_table_num_indexed_bit_pos_t const * : fr_table_ordered_num_by_longest_prefix, \
744 fr_table_num_indexed_t * : fr_table_ordered_num_by_longest_prefix, \
745 fr_table_num_indexed_t const * : fr_table_ordered_num_by_longest_prefix \
746 )(_match_len, _table, _table ## _len, _name, _name_len, _def)
749 int number,
char const *def);
751 int number,
char const *def);
753 void const *ptr,
char const *def);
755 void const *ptr,
char const *def);
758 uint64_t number,
char const *def);
761 unsigned int number,
char const *def);
772 #define fr_table_str_by_value(_table, _number, _def) \
774 fr_table_num_sorted_t const * : fr_table_sorted_str_by_num, \
775 fr_table_num_ordered_t const * : fr_table_ordered_str_by_num, \
776 fr_table_num_sorted_t * : fr_table_sorted_str_by_num, \
777 fr_table_num_ordered_t * : fr_table_ordered_str_by_num, \
778 fr_table_ptr_sorted_t const * : fr_table_sorted_str_by_ptr, \
779 fr_table_ptr_ordered_t const * : fr_table_ordered_str_by_ptr, \
780 fr_table_ptr_sorted_t * : fr_table_sorted_str_by_ptr, \
781 fr_table_ptr_ordered_t * : fr_table_ordered_str_by_ptr, \
782 fr_table_num_indexed_bit_pos_t * : fr_table_indexed_str_by_bit_field, \
783 fr_table_num_indexed_bit_pos_t const * : fr_table_indexed_str_by_bit_field, \
784 fr_table_num_indexed_t * : fr_table_indexed_str_by_num, \
785 fr_table_num_indexed_t const * : fr_table_indexed_str_by_num \
786 )(_table, _table ## _len, _number, _def)
788 #define TABLE_TYPE_NEEDLE_LEN_FUNC(_our_table_type, _our_name) \
789 static inline size_t _our_name(_our_table_type table, size_t table_len) \
792 for (i = 0; i < table_len; i++) if (table->name.len > max) max = table->name.len; \
803 #define fr_table_max_needle_len(_table) \
805 fr_table_num_sorted_t const * : fr_table_num_sorted_max_needle_len, \
806 fr_table_num_ordered_t const * : fr_table_num_ordered_max_needle_len, \
807 fr_table_num_sorted_t * : fr_table_num_sorted_max_needle_len, \
808 fr_table_num_ordered_t * : fr_table_num_ordered_max_needle_len, \
809 fr_table_ptr_sorted_t const * : fr_table_ptr_sorted_max_needle_len, \
810 fr_table_ptr_ordered_t const * : fr_table_ptr_ordered_max_needle_len, \
811 fr_table_ptr_sorted_t * : fr_table_ptr_sorted_max_needle_len, \
812 fr_table_ptr_ordered_t * : fr_table_ptr_ordered_max_needle_len, \
813 fr_table_num_indexed_bit_pos_t * : fr_table_num_indexed_bit_pos_max_needle_len, \
814 fr_table_num_indexed_bit_pos_t const * : fr_table_num_indexed_bit_pos_max_needle_len, \
815 fr_table_num_indexed_t * : fr_table_num_indexed_max_needle_len, \
816 fr_table_num_indexed_t const * : fr_table_num_indexed_max_needle_len \
817 )(_table, _table ## _len)
int strncasecmp(char *s1, char *s2, int n)
int strcasecmp(char *s1, char *s2)
char const * str
Literal string.
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 ELEM_STR(_offset)
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.
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.
#define TABLE_TYPE_NEEDLE_LEN_FUNC(_our_table_type, _our_name)
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.
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)
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
fr_table_elem_name_t name
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)
fr_table_elem_name_t name
fr_table_elem_name_t name
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)
#define TABLE_IDX(_table, _idx, _element_size)
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.
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.
fr_table_elem_name_t name
fr_table_elem_name_t name
size_t len
Literal string length.
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.
#define ELEM_LEN(_offset)
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.
An element in a table indexed by bit position.
An element in a table indexed by numeric value.
An element in an arbitrarily ordered array of name to num mappings.
An element in a lexicographically sorted array of name to num mappings.
An element in an arbitrarily ordered array of name to ptr mappings.
An element in a lexicographically sorted array of name to ptr mappings.