utils: Add helper to map string to unsigned int
In subsequent patch need to map a string to a unsigned int. Hence, add an API to map a string to unsigned int. Signed-off-by: Parav Pandit <parav@nvidia.com> Signed-off-by: David Ahern <dsahern@kernel.org>
This commit is contained in:
parent
b822275ad8
commit
6c76994982
|
|
@ -342,10 +342,12 @@ int parse_mapping(int *argcp, char ***argvp, bool allow_all,
|
||||||
|
|
||||||
struct str_num_map {
|
struct str_num_map {
|
||||||
const char *str;
|
const char *str;
|
||||||
int num;
|
unsigned int num;
|
||||||
};
|
};
|
||||||
|
|
||||||
int str_map_lookup_str(const struct str_num_map *map, const char *needle);
|
int str_map_lookup_str(const struct str_num_map *map, const char *needle);
|
||||||
|
const char *str_map_lookup_uint(const struct str_num_map *map,
|
||||||
|
unsigned int val);
|
||||||
const char *str_map_lookup_u16(const struct str_num_map *map, uint16_t val);
|
const char *str_map_lookup_u16(const struct str_num_map *map, uint16_t val);
|
||||||
const char *str_map_lookup_u8(const struct str_num_map *map, uint8_t val);
|
const char *str_map_lookup_u8(const struct str_num_map *map, uint8_t val);
|
||||||
|
|
||||||
|
|
|
||||||
17
lib/utils.c
17
lib/utils.c
|
|
@ -1953,9 +1953,22 @@ int str_map_lookup_str(const struct str_num_map *map, const char *needle)
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const char *str_map_lookup_uint(const struct str_num_map *map, unsigned int val)
|
||||||
|
{
|
||||||
|
unsigned int num = val;
|
||||||
|
|
||||||
|
while (map && map->str) {
|
||||||
|
if (num == map->num)
|
||||||
|
return map->str;
|
||||||
|
|
||||||
|
map++;
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
const char *str_map_lookup_u16(const struct str_num_map *map, uint16_t val)
|
const char *str_map_lookup_u16(const struct str_num_map *map, uint16_t val)
|
||||||
{
|
{
|
||||||
int num = val;
|
unsigned int num = val;
|
||||||
|
|
||||||
while (map && map->str) {
|
while (map && map->str) {
|
||||||
if (num == map->num)
|
if (num == map->num)
|
||||||
|
|
@ -1968,7 +1981,7 @@ const char *str_map_lookup_u16(const struct str_num_map *map, uint16_t val)
|
||||||
|
|
||||||
const char *str_map_lookup_u8(const struct str_num_map *map, uint8_t val)
|
const char *str_map_lookup_u8(const struct str_num_map *map, uint8_t val)
|
||||||
{
|
{
|
||||||
int num = val;
|
unsigned int num = val;
|
||||||
|
|
||||||
while (map && map->str) {
|
while (map && map->str) {
|
||||||
if (num == map->num)
|
if (num == map->num)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue