Created
June 27, 2018 13:08
-
-
Save MLKrisJohnson/94beb4e550f49ecd1e9c124f256a2e46 to your computer and use it in GitHub Desktop.
AWK script for generating switch/case statements to stringify #define'd macro names
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env awk -f | |
# Given a C-like header file, this AWK script will generate a sequence | |
# of case statements for stringifying macro names. | |
# | |
# For example, if the input file looks like this: | |
# | |
# #define kIOReturnSuccess KERN_SUCCESS // OK | |
# #define kIOReturnError iokit_common_err(0x2bc) // general error | |
# #define kIOReturnNoMemory iokit_common_err(0x2bd) // can't allocate memory | |
# | |
# Then the output will look like this: | |
# | |
# case kIOReturnSuccess: | |
# return "kIOReturnSuccess"; | |
# case kIOReturnError: | |
# return "kIOReturnError"; | |
# case kIOReturnNoMemory: | |
# return "kIOReturnNoMemory"; | |
# | |
# Any input lines that don't start with "#define" are ignored. | |
/^ *#define [a-zA-Z0-9_]+/ { printf "case %s:\n return \"%s\";\n", $2, $2 } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment