Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C how to convert large list of defines to strings for comparison to user input string

Tags:

c

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.

like image 490
confused Avatar asked Jan 21 '26 06:01

confused


1 Answers

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.

like image 151
Craig S. Anderson Avatar answered Jan 23 '26 20:01

Craig S. Anderson