60 lines
1.5 KiB
C
60 lines
1.5 KiB
C
#ifndef UC_VAL_STR_H_
|
|
#define UC_VAL_STR_H_
|
|
|
|
#include <stddef.h>
|
|
|
|
/** @file
|
|
* Utility functions to map integers to strings and strings to strings.
|
|
*/
|
|
|
|
/** Mapping from an unsigned int to a string*/
|
|
struct UCValStr {
|
|
unsigned int val;
|
|
const char *str;
|
|
};
|
|
|
|
/** Mapping from a string to another string*/
|
|
struct UCStrStr {
|
|
const char *val;
|
|
const char *str;
|
|
};
|
|
|
|
/** Helper macro to define an entry in a UCValStr array */
|
|
#define UC_VS_ENTRY(constant) {constant, #constant}
|
|
#define UC_VS_TERMINATOR {-1, NULL}
|
|
#define UC_SS_TERMINATOR {NULL, 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_val_2_str(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_val_2_str_bs(unsigned int val,
|
|
const struct UCValStr *array,
|
|
size_t array_len);
|
|
|
|
/** find @val in @array.
|
|
* the last .val in @array must be a null pointer.
|
|
* (does a linear search)
|
|
*
|
|
* @param val value to find, using strcmp
|
|
* @param array array to search
|
|
* @return the string associated with @val or null
|
|
*/
|
|
const char *uc_str_2_str(const char *restrict val, const struct UCStrStr *array);
|
|
|
|
#endif
|