Created
January 4, 2016 06:00
-
-
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.
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
""" | |
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