Add an unsigned int/string pair type, modelled on the value string of

wireshark
This commit is contained in:
Nils O. Selåsdal
2014-02-04 01:37:27 +01:00
parent f1b1ceadca
commit c2af5db86e
4 changed files with 159 additions and 1 deletions
+37
View File
@@ -0,0 +1,37 @@
#ifndef UC_VAL_STR_H_
#define UC_VAL_STR_H_
#include <stddef.h>
struct UCValStr {
unsigned int val;
const char *str;
};
#define UC_VS_ENTRY(constant) {constant, #constant}
#define UC_VS_TERMINATOR {-1, NULL}
/** Find @val in @array.
* The last .str in @array must be a NULL pointer.
* (does a linear search)
*
* @param val value to find
* @param array array to search
* @return the string associated with @val or NULL
*/
const char *uc_vs_search(unsigned int val, const struct UCValStr *array);
/** Find @val in @array.
* @array must be sorted on val and must not be empty
* (does a binary search)
*
* @param val value to find
* @param array array to search
* @return the string associated with @val or NULL
*/
const char *uc_vs_bsearch(unsigned int val,
const struct UCValStr *array,
size_t array_len);
#endif