Implemented generic enum name to enum value mapping

This commit is contained in:
Martin Willi
2010-08-23 09:47:03 +02:00
parent f154e30431
commit f9efac2ba3
2 changed files with 44 additions and 3 deletions
+26 -3
View File
@@ -16,12 +16,14 @@
#include <stddef.h>
#include <stdio.h>
#include <library.h>
#include "enum.h"
/**
* get the name of an enum value in a enum_name_t list
* See header.
*/
static char *enum_name(enum_name_t *e, int val)
char *enum_to_name(enum_name_t *e, int val)
{
do
{
@@ -34,6 +36,27 @@ static char *enum_name(enum_name_t *e, int val)
return NULL;
}
/**
* See header.
*/
int enum_from_name(enum_name_t *e, char *name)
{
do
{
int i, count = e->last - e->first;
for (i = 0; i < count; i++)
{
if (strcaseeq(name, e->names[i]))
{
return e->first + i;
}
}
}
while ((e = e->next));
return -1;
}
/**
* Described in header.
*/
@@ -43,7 +66,7 @@ int enum_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec,
enum_name_t *ed = *((enum_name_t**)(args[0]));
int val = *((int*)(args[1]));
char *name = enum_name(ed, val);
char *name = enum_to_name(ed, val);
if (name == NULL)
{