enum: Extend printf hook to print flags
Signed-off-by: Thomas Egerer <[email protected]>
This commit is contained in:
committed by
Martin Willi
parent
53013b5f0c
commit
4dc72f5e57
@@ -26,6 +26,11 @@
|
||||
|
||||
typedef struct enum_name_t enum_name_t;
|
||||
|
||||
/**
|
||||
* Magic enum_name_t pointer indicating this is an enum name for flags
|
||||
*/
|
||||
#define ENUM_FLAG_MAGIC ((enum_name_t*)~(uintptr_t)0)
|
||||
|
||||
/**
|
||||
* Struct to store names for enums.
|
||||
*
|
||||
@@ -58,7 +63,7 @@ struct enum_name_t {
|
||||
int first;
|
||||
/** value of the last enum string */
|
||||
int last;
|
||||
/** next enum_name_t in list */
|
||||
/** next enum_name_t in list, or ENUM_FLAG_MAGIC */
|
||||
enum_name_t *next;
|
||||
/** array of strings containing names from first to last */
|
||||
char *names[];
|
||||
@@ -106,6 +111,23 @@ struct enum_name_t {
|
||||
*/
|
||||
#define ENUM(name, first, last, ...) ENUM_BEGIN(name, first, last, __VA_ARGS__); ENUM_END(name, last)
|
||||
|
||||
/**
|
||||
* Define a enum name with only one range for flags.
|
||||
*
|
||||
* Using an enum list for flags would be overkill. Hence we use a single
|
||||
* range with all values in range. The next pointer is abused to mark
|
||||
* that the enum name is for flags only. Use NULL if a particular flag
|
||||
* is not meant to be printed.
|
||||
*
|
||||
* @param name name of the enum_name list
|
||||
* @param first enum value of the first enum string
|
||||
* @param last enum value of the last enum string
|
||||
* @param ... a list of strings
|
||||
*/
|
||||
#define ENUM_FLAGS(name, first, last, ...) \
|
||||
static enum_name_t name##last = {first, last, ENUM_FLAG_MAGIC, { __VA_ARGS__ }}; \
|
||||
ENUM_END(name, last)
|
||||
|
||||
/**
|
||||
* Convert a enum value to its string representation.
|
||||
*
|
||||
@@ -145,6 +167,17 @@ char *enum_to_name(enum_name_t *e, int val);
|
||||
*/
|
||||
bool enum_from_name_as_int(enum_name_t *e, const char *name, int *val);
|
||||
|
||||
/**
|
||||
* Convert a enum value containing flags to its string representation.
|
||||
*
|
||||
* @param e enum names for this enum value suitable for flags
|
||||
* @param val enum value to get string for
|
||||
* @param buf buffer to write flag string to
|
||||
* @param len buffer size
|
||||
* @return buf, NULL if buffer too small
|
||||
*/
|
||||
char *enum_flags_to_string(enum_name_t *e, u_int val, char *buf, size_t buflen);
|
||||
|
||||
/**
|
||||
* printf hook function for enum_names_t.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user