enum: Return boolean result for enum_from_name() lookup

Handling the result for enum_from_name() is difficult, as checking for
negative return values requires a cast if the enum type is unsigned. The new
signature clearly differentiates lookup result from lookup value.

Further, this actually allows to convert real -1 enum values, which could not
be distinguished from "not-found" and the -1 return value.

This also fixes several clang warnings where enums are unsigned.
This commit is contained in:
Martin Willi
2014-05-16 15:42:07 +02:00
parent 9ee8b3b41f
commit 064fe9c963
28 changed files with 102 additions and 85 deletions
+30 -21
View File
@@ -120,41 +120,50 @@ END_TEST
*/
static struct {
bool found;
int val;
char *str;
} enum_tests_cont[] = {
{CONT1, "CONT1"},
{CONT2, "CONT2"},
{CONT2, "CoNt2"},
{CONT3, "CONT3"},
{CONT4, "CONT4"},
{CONT5, "CONT5"},
{-1, "asdf"},
{-1, ""},
{-1, NULL},
{TRUE, CONT1, "CONT1"},
{TRUE, CONT2, "CONT2"},
{TRUE, CONT2, "CoNt2"},
{TRUE, CONT3, "CONT3"},
{TRUE, CONT4, "CONT4"},
{TRUE, CONT5, "CONT5"},
{FALSE, 0, "asdf"},
{FALSE, 0, ""},
{FALSE, 0, NULL},
}, enum_tests_split[] = {
{SPLIT1, "SPLIT1"},
{SPLIT1, "split1"},
{SPLIT2, "SPLIT2"},
{SPLIT2, "SpLiT2"},
{SPLIT3, "SPLIT3"},
{SPLIT4, "SPLIT4"},
{SPLIT5, "SPLIT5"},
{-1, "asdf"},
{-1, ""},
{-1, NULL},
{TRUE, SPLIT1, "SPLIT1"},
{TRUE, SPLIT1, "split1"},
{TRUE, SPLIT2, "SPLIT2"},
{TRUE, SPLIT2, "SpLiT2"},
{TRUE, SPLIT3, "SPLIT3"},
{TRUE, SPLIT4, "SPLIT4"},
{TRUE, SPLIT5, "SPLIT5"},
{FALSE, 0, "asdf"},
{FALSE, 0, ""},
{FALSE, 0, NULL},
};
START_TEST(test_enum_from_name_cont)
{
int val = enum_from_name(test_enum_cont_names, enum_tests_cont[_i].str);
int val = 0;
bool found;
found = enum_from_name(test_enum_cont_names, enum_tests_cont[_i].str, &val);
ck_assert(enum_tests_cont[_i].found == found);
ck_assert_int_eq(val, enum_tests_cont[_i].val);
}
END_TEST
START_TEST(test_enum_from_name_split)
{
int val = enum_from_name(test_enum_split_names, enum_tests_split[_i].str);
int val = 0;
bool found;
found = enum_from_name(test_enum_split_names, enum_tests_split[_i].str, &val);
ck_assert(enum_tests_split[_i].found == found);
ck_assert_int_eq(val, enum_tests_split[_i].val);
}
END_TEST
+4 -3
View File
@@ -40,7 +40,7 @@ char *enum_to_name(enum_name_t *e, int val)
/**
* See header.
*/
int enum_from_name(enum_name_t *e, char *name)
bool enum_from_name_as_int(enum_name_t *e, const char *name, int *val)
{
do
{
@@ -50,12 +50,13 @@ int enum_from_name(enum_name_t *e, char *name)
{
if (name && strcaseeq(name, e->names[i]))
{
return e->first + i;
*val = e->first + i;
return TRUE;
}
}
}
while ((e = e->next));
return -1;
return FALSE;
}
/**
+23 -2
View File
@@ -120,9 +120,30 @@ char *enum_to_name(enum_name_t *e, int val);
*
* @param e enum names for this enum value
* @param name name to get enum value for
* @return enum value, -1 if not found
* @þaram valp variable sized pointer receiving value
* @return TRUE if enum name found, FALSE otherwise
*/
int enum_from_name(enum_name_t *e, char *name);
#define enum_from_name(e, name, valp) ({ \
int _val; \
int _found = enum_from_name_as_int(e, name, &_val); \
if (_found) \
{ \
*(valp) = _val; \
} \
_found; })
/**
* Convert a enum string back to its enum value, integer pointer variant.
*
* This variant takes integer pointer only, use enum_from_name() to pass
* enum type pointers for the result.
*
* @param e enum names for this enum value
* @param name name to get enum value for
* @þaram val integer pointer receiving value
* @return TRUE if enum name found, FALSE otherwise
*/
bool enum_from_name_as_int(enum_name_t *e, const char *name, int *val);
/**
* printf hook function for enum_names_t.