An exercise from the book The Pragmatic Programmer.
Write a script that generates a name.h
and name.c
file with enums supplied from within the names.txt
file.
This would be useful to be able to print out the state as a string (as opposed to a number) for debugging purposes.
python3 generate_names.py
clang main.c --include name.c -o generated_names
./generated_names
// name.h
#ifndef NAME_H
#define NAME_H
extern const char* NAME_names[];
typedef enum {
state_ace,
state_act,
...
state_zap,
state_zip,
} NAME;
#endif // NAME_H
// name.c
const char* NAME_names[] = {
"state_ace",
"state_act",
...
"state_zap",
"state_zip",
};