I did a simple example - that allows macros with names 'A' to 'Z' - note that if you want to use lower case - you can either alter the call to .c - 'a' (instead of (.c - 'A')) - or convert the lower case letter to upper case.
I use a sparse array of 26 function pointers (0..25) - this could be extended (for example fn_table[128] then call fn_table['A']( ))
I added a small macro CallString that calls each character of a string passed as an argument in turn - there a couple of examples. Note that 'undefined' macros - are ignored (I threw an 'E' into one of the strings)
for example CallString("ABCDABCDZ") or CallString(.cmd_string)
The function table is initialised in supplementary code....
Code: Select all
typedef void (*function)();
function fn_table[26] = {
[0] = FCM_A,
[1] = FCM_B,
[2] = FCM_C,
[3] = FCM_D,
['Z' - 'A'] = FCM_Z}; // I handle 'A' to 'Z' as 0..25
I would recommend giving functions (and variables) meaningful names rather than single characters - but in this instance it is quicker to do it this way (storing a string of each function name and then searching would add overhead at runtime)
The function signature could be altered to allow arguments to be passed (or a value returned by the macros)
Martin