Skip to content

Instantly share code, notes, and snippets.

@csm10495
Created January 4, 2016 06:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save csm10495/764375d9177ed193b76f to your computer and use it in GitHub Desktop.
Save csm10495/764375d9177ed193b76f to your computer and use it in GitHub Desktop.
Python Script to make a function to get a std::string for a given C++ enum's values.
"""
Brief:
This file can be used to convert C++ enum code into a function to get a std::string from enum value.
To use: Copy and paste the entire enum code (from MSDN) as input to the script.
Output goes to an 'out.txt' file.
Author:
Charles Machalow
"""
lowCaseFirstLetter = lambda s: s[:1].lower() + s[1:] if s else ''
with open('out.txt', 'w') as f:
firstLine = input().replace('enum', '').replace('typedef', '').replace('{', '')
name = firstLine.strip().lstrip("_")
firstLine = firstLine.replace('_', ' ').strip().title().replace(' ', '')
firstLine = lowCaseFirstLetter(firstLine)
f.write("std::string %sToString(%s tmp)\n{\nswitch(tmp)\n{" % (firstLine, name))
while True:
a = input().replace(",", "").replace(" ", "").replace("=", " ").split(" ")
if "}" in a[0]:
break
else:
f.write('case(%s::%s):\nreturn \"%s\";\n' % (name, a[0], a[0]))
f.write("}\nreturn \"No Enum Value Matches, Unknown\";\n}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment