Python script that converts .h file to fasm .inc file
My first attempt at Python so maybe not very clean code.
import string
sourcefile = 'c:/test/d3d9types.h'
destfile = sourcefile[:-2] + '.inc'
source = open(sourcefile, 'r')
dest = open(destfile, 'w')
nocomments = 1
tocomment = ['#define', '#if', '#ifndef', '#ifdef', '#endif', '#include', '#pragma', '#else']
wordreplace = {'DWORD': 'dd ?', 'float': 'dd ?', 'BOOL': 'dd ?', 'UINT': 'dd ?', 'INT': 'dd ?', 'BYTE': 'db ?', 'WORD': 'dw ?', 'LONG': 'dd ?'}
replace = {';': '', '|': ' or ', '&': ' and ', '<<': ' shl ', '>>': ' shr '}
coms = ['//']
comstart = '/*'
comend = '*/'
comment = 0
struc = -1
enum = -1
strucnames = []
lines=source.readlines()
for line in lines:
lineend = '\n'
if comment == 0 and line.find(comstart) <> -1 and line.find(comend) <> -1:
lineend = '; ' + line[line.find(comstart):]
line = line[:line.find(comstart)]
elif comment == 0 and line.find(comstart) <> -1:
comment = 1
lineend = '; ' + line
line = ''
elif comment == 1 and line.find(comend) <> -1:
comment = 0
lineend = '; ' + line
line = ''
elif comment == 1:
lineend = '; ' + line
line = ''
elif comment == 0:
for c in coms:
if line.find(c) <> -1:
lineend = '; ' + line[line.find(c):]
line = line[:line.find(c)]
words=line.split()
if len(words) >= 1:
if len(words) >= 3 and words[0] == '#define':
line = words[1] + ' = ' + string.join(words[2:])
elif words[0] == 'typedef' and words[1] == 'struct':
if struc == -1: struc = 0
if string.find(line,'{') <> -1:
struc = struc + 1
strucnames.append(string.strip(words[2], '_'))
line = 'struc ' + string.strip(words[2], '_') + ' {'
elif words[0] == 'typedef' and words[1] == 'enum':
line = ''
lineend = ''
enum = 0
elif string.find(line,'}') <> -1 and struc > 0:
struc = struc - 1
if nocomments == 1:
line = '\t'*struc + '}\n' + '\t' * struc + 'struct ' + strucnames.pop()
else:
line = '\t'*struc + '}'
lineend = '; ' + line.lstrip(' }') + '\t' * struc + 'struct ' + strucnames.pop() + '\n'
if struc == 0: struc = -1
elif string.find(line,'}') <> -1 and enum > -1:
line = ''
lineend = ''
enum = -1
elif string.find(line,'{') <> -1 and enum > -1:
line = line.strip('{')
elif string.find(line,'{') <> -1 and struc > -1:
if struc == 0:
line = ''
lineend = ''
struc = struc + 1
if len(strucnames) < struc: strucnames.append('')
elif enum <> -1:
if line.find('=') == -1:
if wrongenum == 1:
print "Can't continue conversion"
raise
else:
line = words[0] + '\t= ' + str(enum)
else:
wrongenum = 0
for i in range(1,len(words)):
words[i]=string.strip(words[i],',')
try:
enum = eval(string.strip(string.join(words[1:]),'='))
except:
wrongenum = 1
pass
line = string.lstrip(line.replace(',',''))
enum = enum + 1
elif struc > 0 and len(words) == 2:
line = '\t'*struc + '.' + words[1] + ' ' + words[0]
elif words[0] == 'typedef':
line = words[2] + ' equ ' + words[1]
elif words[0] in tocomment:
lineend = '; ' + line
line = ''
line = line.strip('\n')
for r1, r2 in replace.items():
line = line.replace(r1, r2)
for word in words:
newword = word
for r1, r2 in wordreplace.items():
if r1 == word:
newword = r2
break
if newword.find('0x') == 0:
newword = string.rstrip(newword.replace('0x','0'),'UL') + 'h'
line = line.replace(word,newword)
if nocomments == 1:
if lineend == '\n' or line <> '': dest.write(line + '\n')
else:
dest.write(line + lineend)
dest.close()
source.close()
Feel free to do improvements and if you do please post here