I have a configuration file that the user can modify.
In this configuration file there exists a #define ListOfWords
with a list of words to which the user can add or remove any custom words. For example: #define ListOfWords black,bear,Mouse
.
User then also defines, using #define SequenceOfWords
, an arbitrary sequence of words. For example: #define SequenceOfWords In a forest, a brown-bear saw a black mouse
.
I want to extract every word from #define ListOfWords
that appears in #SequenceOfWords
and create a compile-time string array of extracted words const char* extractedWords[] = {bear, black}
.
Note: Instead of #define SequenceOfWords
being a define it can also be a compile-time string constant if it makes it easier to solve this problem. The important thing is that this must be solved at the compile-time or preprocessing time.
CodePudding user response:
The only way I can think of is:
Macro defining the array of pointers to strings:
#define LIST_OF_WORDS(...) const char *words[] = {__VA_ARGS__, NULL}
User can define them somewhere (in one of the compilation units available for him):
LIST_OF_WORDS("bar", "foo", "goo", "zoo");
and use the somewhere else:
extern *words[];
int main(void)
{
const char **wrds = words;
while(*wrds)
{
printf("WORD: `%s`\n", *wrds );
}
}
https://godbolt.org/z/7qenaTxjq
CodePudding user response:
I want to extract every word from #define ListOfWords that appears in #SequenceOfWords and create a compile-time string array of extracted words const char* extractedWords[] = {bear, black}.
It is not possible. This is (way, way!) too complicated task for C preprocessor. Preprocessor offers no string parsing functions.
Usually in C, user has to satisfy himself some constraint, otherwise he will get "undefined behavior". You could add a #define ExtractedWords
where user has to himself extract the words. It's on the user to generate it correctly.
Or you could use something else than C preprocessor to parse user configuration and generate a C header file from it. Python, Bash, Perl, M4 and other languages with the help of a build system like CMake or Automake are used for that.