Skip to content

Instantly share code, notes, and snippets.

@marxin
Created June 10, 2019 08:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marxin/945c7bc01fe375141526284031e85db6 to your computer and use it in GitHub Desktop.
Save marxin/945c7bc01fe375141526284031e85db6 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import os
import re
"""
Transform:
#define LDAP_CONTROL_VLVRESPONSE_W (const WCHAR []){'2','.','1','6','.', \
'8','4','0','.','1','.','1','1','3','7','3','0','.','3','.','4','.','1','0',0}
into:
static const WCHAR LDAP_CONTROL_VLVRESPONSE_W[] = {'2','.','1','6','.', \
'8','4','0','.','1','.','1','1','3','7','3','0','.','3','.','4','.','1','0',0};
"""
r = re.compile('^#\s*define\s*([^\s]*)\s*\(const WCHAR\s*\[\]\)')
for root, dirs, files in os.walk('.'):
for f in files:
if f.endswith('.h'):
p = os.path.join(root, f)
lines = list(open(p).readlines())
for i, l in enumerate(lines):
l = l.strip()
m = r.match(l)
if m != None:
start = m.start(0)
end = m.end(0)
newl = 'static const WCHAR ' + m.group(1) + '[] = ' + l[end:]
print(newl)
if l.endswith('}'):
newl += ';\n'
else:
newl += '\n'
c = 1
while True:
l2 = lines[i + c].strip()
if l2.endswith('}'):
lines[i + c] = l2 + ';\n'
break
c += 1
lines[i] = newl
with open(p, 'w') as f:
for l in lines:
f.write(l)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment