*/
char *enum_to_name(enum_name_t *e, int val)
{
- do
+ enum_name_elem_t *current;
+
+ current = e->elem;
+ while (current)
{
- if (val >= e->first && val <= e->last)
+ if (val >= current->first && val <= current->last)
{
- return e->names[val - e->first];
+ return current->names[val - current->first];
}
+ current = current->next;
}
- while ((e = e->next));
return NULL;
}
*/
int enum_from_name(enum_name_t *e, char *name)
{
- do
+ enum_name_elem_t *current;
+
+ current = e->elem;
+ while (current)
{
- int i, count = e->last - e->first + 1;
+ int i, count = current->last - current->first + 1;
for (i = 0; i < count; i++)
{
- if (strcaseeq(name, e->names[i]))
+ if (strcaseeq(name, current->names[i]))
{
- return e->first + i;
+ return current->first + i;
}
}
+ current = current->next;
}
- while ((e = e->next));
return -1;
}
#include "printf_hook.h"
typedef struct enum_name_t enum_name_t;
+typedef struct enum_name_elem_t enum_name_elem_t;
/**
* Callback function to get an enum name using an index variable.
* up using the function and the index argument.
*/
struct enum_name_t {
+ /** first enum_name_elem_t in chain */
+ enum_name_elem_t *elem;
+};
+
+struct enum_name_elem_t {
/** value of the first enum string */
int first;
/** value of the last enum string */
int last;
/** next enum_name_t in list */
- enum_name_t *next;
+ enum_name_elem_t *next;
/** array of strings containing names from first to last */
char *names[];
};
* @param last enum value of the last enum string
* @param ... a list of strings
*/
-#define ENUM_BEGIN(name, first, last, ...) static enum_name_t name##last = {first, last, NULL, { __VA_ARGS__ }}
+#define ENUM_BEGIN(name, first, last, ...) \
+ static enum_name_elem_t name##last = {first, last, NULL, { __VA_ARGS__ }}
/**
* Continue a enum name list startetd with ENUM_BEGIN.
* @param prev enum value of the "last" defined in ENUM_BEGIN/previous ENUM_NEXT
* @param ... a list of strings
*/
-#define ENUM_NEXT(name, first, last, prev, ...) static enum_name_t name##last = {first, last, &name##prev, { __VA_ARGS__ }}
+#define ENUM_NEXT(name, first, last, prev, ...) \
+ static enum_name_elem_t name##last = {first, last, &name##prev, { __VA_ARGS__ }}
/**
* Complete enum name list started with ENUM_BEGIN.
* @param name name of the enum_name list
* @param prev enum value of the "last" defined in ENUM_BEGIN/previous ENUM_NEXT
*/
-#define ENUM_END(name, prev) enum_name_t *name = &name##prev;
+#define ENUM_END(name, prev) \
+ static enum_name_t name##head = { .elem = &name##prev }; \
+ enum_name_t *name = &name##head
/**
* Define a enum name with only one range.
* @param last enum value of the last enum string
* @param ... a list of strings
*/
-#define ENUM(name, first, last, ...) ENUM_BEGIN(name, first, last, __VA_ARGS__); ENUM_END(name, last)
+#define ENUM(name, first, last, ...) \
+ ENUM_BEGIN(name, first, last, __VA_ARGS__); ENUM_END(name, last)
/**
* Convert a enum value to its string representation.