#include <stdio.h>
#include <assert.h>
char *enum_string_(int in, int enum_string_length, char *enum_string) {
// strip whitespace and replace ',' with '\0' terminator.
for(int i = 0, j = 0; i < enum_string_length; ++i, ++j)
{
while(enum_string[i] == ' ') ++i;
enum_string[j] = enum_string[i];
if(enum_string[j] == ',') enum_string[j] = 0;
}
// Find the correct string.
for(int i = 0, count = 0; i < enum_string_length; ++i)
{
if(count == in)
{
return &enum_string[i];
}
if(enum_string[i] == 0) count++;
}
//assert(0);
return "ENUM_STRING_ERROR";
}
#define ENUM(type, ...) \
enum struct type{__VA_ARGS__}; \
static char type##_RAW_STRING[] = #__VA_ARGS__; \
char *enum_string(type in) \
{return enum_string_((int)in, sizeof(type##_RAW_STRING), type##_RAW_STRING);}
ENUM(eple ,a ,b ,c)
ENUM(pera ,a , b , dets)
int main(void)
{
printf("%s\n", enum_string(pera::dets));
printf("%s\n", enum_string((eple) 32));
return 0;
}