1. 先从freeimage.dll中复制出函数的名字, 就是以文本的方式打开freeimage.dll, 查找FreeImage_Initialise , 然后附近的一大段函数都复制下来, 得到 free.txt文件
_FreeImage_AcquireMemory@12
_FreeImage_AdjustBrightness@12
_FreeImage_AdjustColors@32
_FreeImage_AdjustContrast@12
_FreeImage_AdjustCurve@12
_FreeImage_AdjustGamma@12
_FreeImage_Allocate@24
_FreeImage_AllocateEx@36
_FreeImage_AllocateExT@40
已上只复制了一小部分
2. 上代码批量替换
主要用正则表达式:
import re
funcs={}
for line in open('free.txt','r'):
line = line.strip()
#print line
real = line
key = line.split('@')[0]
if key[0]=='_':
key=key[1:]
funcs[key]=line
#print funcs
text = open('freeimage.nim','r').read()
#for x in re.findall('importc: "([\w\_\d]+)",', text):
# print x
def dashrepl(matchobj):
#print matchobj.group(1), type(matchobj.group(1))
k= matchobj.group(1)
return 'importc: "%s"' % funcs.get(k, k)
print re.sub('importc: "([\w\_\d]+)"',dashrepl, text)
最后print 得到的就是最终结果了