Created
May 8, 2016 06:41
-
-
Save csm10495/059f6b6cbe66221491bd950dddb38682 to your computer and use it in GitHub Desktop.
Script to make a Python dictionary from C++ enum
This file contains 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 make a Python dict from a c++ enum. | |
Simply copy-paste the enum from typedef to end as input and | |
out.txt will be created with it as a Python dict. | |
Author: | |
Charles Machalow | |
""" | |
try: | |
raw_input | |
except: | |
raw_input = input | |
with open('out.txt', 'w') as f: | |
currentItemValue = 0 | |
while True: | |
line = raw_input() | |
if line.strip() != "quit": | |
if 'typedef' in line: | |
structName = line.replace("typedef enum", "").replace("{", "").strip().strip("_") | |
f.write("%s = {\n" % structName) | |
elif '}' in line: | |
f.write("}\n") | |
break | |
else: | |
line = line.replace(',', '') | |
spEquals = list(map(str.strip, line.split("="))) | |
if len(spEquals) == 2: | |
name, val = spEquals | |
if '0x' in val: | |
val = int(val, 16) | |
else: | |
val = int(val) | |
f.write(' 0x%02X : \"%s\",\n' % (val, name)) | |
currentItemValue = val | |
else: | |
name = line.strip() | |
f.write(' 0x%02X : \"%s\",\n' % (currentItemValue, name)) | |
currentItemValue += 1 | |
else: | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment