I read an interesting answer on how to convert a list of error code defines from the error code to the error string using a giant switch statement. I have a slightly different question though.
Given a large text file of thousands of register defines ie:
#define setup_register 0x01
I'd like to be able to take input from the command line like write setup_register 0xFF. Then in my code I'd lookup what setup_register's address was (0x01) and write to it.
Is there a C way to get at the setup_register address? I suppose I could parse the file and create a new one, or do it manually, but that means it has to be done each time it changes.
The setup_register is a C preprocessor symbol. Such symbols can be stringified if it is a macro argument. Otherwise as far I as know it cannot be done.
I would write a script in your favorite scripting language (Ruby, Python, Perl) that looks for lines of the form "#define [A-Za-Z_]+ 0x[0-9a-fA-f]+", and then parses out the name and generates an array of structures that has the string and corresponding value. Hash the string, put the structure in a hash table, and you are good to go.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With