I am writing a program that will call a function from an external library, given a list of bitfield flags provided by the end user of my program. I was initially planning on maintaining a lookup table of all the possible flags, but the list of flags varies by platform and I don’t have complete documentation for the library. I had planned to use the c preprocessor to include only the defined flags for the current platform in the table, but this is turning out to be hard to maintain because I have to rely on users reporting missing flags for platforms to which I don’t have access.
I then thought I could allow the users to pass in the flag names as strings. But I can’t find a way to test if a macro is defined, given only the string name of the macro. So if a user wanted to set the flag FLAG_ABC, they’d pass in “FLAG_ABC” as an argument and I’d have to do some magic to check if &FLAG_ABC != NULL. Is this possible?
All
#define‘s are lost after preprocessing. If you want to have the information of preprocessor-symbols available at run-time you have to have the names (and values of course) of these macros as strings in your executable OR as a separate file which you then fread at run-time. I could imagine it would be anoying to have two lists of flags, one with the #define-symbols and one with the strings and then manually caring that if you update the one you also update the other without error.I would go with the separate file you load at run-time but instead of having the updating-trouble just have that file be a header-file you also
#include. Specifically what i suggest is: move all your flag-definition#define'sinto a separate header-file, include it in your sources so you can use it as you are used to, create a mini-parser reading in the file at run-time, i.e. parsing each line#define asdf 0xF1A6into a string and an int. If this works in practice and you then want to prevent the user from accidentally changing that header-file you could either hash it or binary-encode it.The alternative with which you can have the
#define-symbols as strings actually in your binary would be to generate a.rc-file from that header file (or both the header-file and the.rcfrom a.csv-file) and then using the resource-compiler to inline it in your binary’s.rsrc-section, you have to write the helper-program (which transforms the file) though and integrate a call to that transformer-program into your make-script or build-system to always have the.rcup-to-date.