I am writing a program that reads the name of the signal (e.g. SIGSTOP, SIGKILL etc) as a string from the command line and calls the kill() system call to send the signal. I was wondering if there is a simple way to convert the string to signal codes (in signal.h).
Currently, I'm doing this by writing my own map that looks like this:
signal_map["SIGSTOP"] = SIGSTOP;
signal_map["SIGKILL"] = SIGKILL;
....
But its tedious to write this for all signals. So, I was looking for a more elegant way, if it exists.
Using C++0x you can use initializer lists to make this more simple:
This get's you the map at less code to write. If you want to you could also some preprocessor magic to get the code even simpler and avoid writing the name multiple times. But most often abusing the preprocessor just leads to less usable code not better code. (Note that preprocessor magic can still be used if you decide not to use C++0x and keep your way).
You can use a command line like this
It will write your map for you.
Not sure if that is what you are loking for, but: strerror() converts a error code to the error message, similar strsignal() converts a signal to the signal message.