J
Joakim Hove
Hello,
I am writing a C program which among other things need to read and
write textfiles from an external third party application. Typical
content in these files are string fields which can have a limited set
of values, e.g. the three values {"OPEN" , "SHUT", "AUTO"}. The
available strings have been unchanged for ~ 25 years - so I think they
can be safely assumed to not change.
I do not like to carry around strings to hold state information, so in
my own code I check the string content and internalize an enum value.
My implemenentation looks like this:
#define OPEN_STRING "OPEN"
#define SHUT_STRING "SHUT"
#define AUTO_STRING "AUTO"
typedef enum {AUTO , SHUT , OPEN } state_enum;
static state_enum state_from_string(const char * string) {
if (strcmp(string , OPEN_STRING) == 0)
return OPEN;
else if (strcmp(string , SHUT_STRING) == 0)
return SHUT;
else if (strcmp(string , AUTO_STRING) == 0)
return AUTO;
else {
fprintf(stderr , " Failed to interpret:%s as %s|%s|%s - going
home\n",string , OPEN_STRING, SHUT_STRING,AUTO_STRING);
exit(1);
}
}
static const char * string_from_state(state_enum state) {
switch(state) {
case(OPEN):
return OPEN_STRING;
case(SHUT):
return SHUT_STRING;
case(AUTO):
return AUTO_STRING;
default:
fprintf(stderr,"Internal error ??");
exit();
}
}
Now - this is of course very simple code to write; but I have many
such sets, so I end up hand-writing these functions many times - it
does not feel good! It would of course be simple to write e.g. a
Python script to generate these functions, but I am reluctant to
introduce arbitrary different programs in the build process (maybe I
should not be reluctant to that??)
So - is it possible to generate the functions state_from_string() and
string_from_state() automagically by using the preprocessor?
Best Regards
Jaokim
I am writing a C program which among other things need to read and
write textfiles from an external third party application. Typical
content in these files are string fields which can have a limited set
of values, e.g. the three values {"OPEN" , "SHUT", "AUTO"}. The
available strings have been unchanged for ~ 25 years - so I think they
can be safely assumed to not change.
I do not like to carry around strings to hold state information, so in
my own code I check the string content and internalize an enum value.
My implemenentation looks like this:
#define OPEN_STRING "OPEN"
#define SHUT_STRING "SHUT"
#define AUTO_STRING "AUTO"
typedef enum {AUTO , SHUT , OPEN } state_enum;
static state_enum state_from_string(const char * string) {
if (strcmp(string , OPEN_STRING) == 0)
return OPEN;
else if (strcmp(string , SHUT_STRING) == 0)
return SHUT;
else if (strcmp(string , AUTO_STRING) == 0)
return AUTO;
else {
fprintf(stderr , " Failed to interpret:%s as %s|%s|%s - going
home\n",string , OPEN_STRING, SHUT_STRING,AUTO_STRING);
exit(1);
}
}
static const char * string_from_state(state_enum state) {
switch(state) {
case(OPEN):
return OPEN_STRING;
case(SHUT):
return SHUT_STRING;
case(AUTO):
return AUTO_STRING;
default:
fprintf(stderr,"Internal error ??");
exit();
}
}
Now - this is of course very simple code to write; but I have many
such sets, so I end up hand-writing these functions many times - it
does not feel good! It would of course be simple to write e.g. a
Python script to generate these functions, but I am reluctant to
introduce arbitrary different programs in the build process (maybe I
should not be reluctant to that??)
So - is it possible to generate the functions state_from_string() and
string_from_state() automagically by using the preprocessor?
Best Regards
Jaokim