1
0
mirror of https://github.com/blawar/ooot.git synced 2024-07-04 18:13:37 +00:00
ooot/tools/remove_typedef.py

72 lines
1.9 KiB
Python
Raw Normal View History

2021-12-27 19:23:03 +00:00
import os.path
from pathlib import Path
import re
def writeFile(path, buffer):
with open(path, 'r', encoding="UTF-8") as f:
if f.read() == buffer:
return
print('writing %s' % path)
with open(path, 'w', encoding="UTF-8") as f:
f.write(buffer)
dir = 'include/'
structs = {}
for path in Path(dir).rglob('*.h'):
x = str(path).replace('\\', '/')
2022-02-12 19:50:06 +00:00
#if 'ultra64' in x:
# continue
2021-12-27 19:23:03 +00:00
#print(x)
with open(x, 'r', encoding="UTF-8") as f:
buffer = f.read().replace('\r', '')
2022-02-12 19:50:06 +00:00
buffer = re.sub(r'typedef \s*struct\s*(\{[^\}]*\n\})\s*([A-Z0-9_]+)\s*;', r'struct \2 \1;', buffer, flags = re.I | re.S)
buffer = re.sub(r'typedef \s*union\s*(\{[^\}]*\n\})\s*([A-Z0-9_]+)\s*;', r'union \2 \1;', buffer, flags = re.I | re.S)
buffer = re.sub(r'typedef \s*enum\s*(\{[^\}]*\n\})\s*([A-Z0-9_]+)\s*;', r'enum \2 \1;', buffer, flags = re.I | re.S)
buffer = re.sub(r'typedef \s*struct\s*([A-Za-z0-9_]*)\s*(\{[^\}]*\n\})\s*([A-Z0-9_]+)\s*;', r'struct \3 \2;', buffer, flags = re.I | re.S)
buffer = buffer.replace('struct Gfx*', 'Gfx*')
2021-12-27 19:23:03 +00:00
match = re.search(r'struct\s+([A-Za-z0-9_]+)[^A-Za-z0-9_]', buffer)
if match:
structs[match.group(1)] = 1
writeFile(x, buffer)
2022-02-12 19:50:06 +00:00
exit(0)
2021-12-27 19:23:03 +00:00
files = []
for path in Path(dir).rglob('*.h'):
x = str(path).replace('\\', '/')
if 'ultra64' in x:
continue
files.append(x)
for path in Path('src').rglob('*.c'):
x = str(path).replace('\\', '/')
if 'ultra64' in x:
continue
files.append(x)
for x in files:
with open(x, 'r', encoding="UTF-8") as f:
buffer = f.read().replace('\r', '')
buffer2 = buffer
for struct, junk in structs.items():
buffer = re.sub(r'(?!struct)(\s+)%s([^A-Za-z0-9_])' % struct, r'\1struct %s\2' % struct, buffer, flags = re.I)
buffer = buffer.replace('struct struct ', 'struct ')
'''
if buffer != buffer2:
print(buffer + '\n\n--------------------\n\n' + buffer2)
exit(0)
'''
writeFile(x, buffer)