Skip to content

Instantly share code, notes, and snippets.

@gbmele
Forked from MLKrisJohnson/defines_to_cases.awk
Created July 19, 2023 02:15
Show Gist options
  • Save gbmele/191689a94a0414033f58749ec20e810a to your computer and use it in GitHub Desktop.
Save gbmele/191689a94a0414033f58749ec20e810a to your computer and use it in GitHub Desktop.
AWK script for generating switch/case statements to stringify #define'd macro names
#!/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