| 1 |
# Copyright (c) 2015 Microsoft Corporation |
| 2 |
|
| 3 |
import os |
| 4 |
import re |
| 5 |
|
| 6 |
ifndef = re.compile("#ifndef \_(.*)\_H\_") |
| 7 |
doubleu = re.compile("#(.*) (.*)\_\_H\_") |
| 8 |
defn = re.compile("#define \_(.*)\_H\_") |
| 9 |
endif = re.compile("#endif /\* \_(.*)\_H\_") |
| 10 |
|
| 11 |
|
| 12 |
def fix_hdr(file): |
| 13 |
print(file) |
| 14 |
tmp = "%s.tmp" % file |
| 15 |
ins = open(file) |
| 16 |
ous = open(tmp,'w') |
| 17 |
line = ins.readline() |
| 18 |
found = False |
| 19 |
while line: |
| 20 |
m = doubleu.search(line) |
| 21 |
if m: |
| 22 |
ous.write("#") |
| 23 |
ous.write(m.group(1)) |
| 24 |
ous.write(" ") |
| 25 |
ous.write(m.group(2)) |
| 26 |
ous.write("_H_\n") |
| 27 |
line = ins.readline() |
| 28 |
found = True |
| 29 |
continue |
| 30 |
m = ifndef.search(line) |
| 31 |
if m: |
| 32 |
print(m.group(1)) |
| 33 |
ous.write("#ifndef ") |
| 34 |
ous.write(m.group(1)) |
| 35 |
ous.write("_H_\n") |
| 36 |
line = ins.readline() |
| 37 |
found = True |
| 38 |
continue |
| 39 |
m = defn.search(line) |
| 40 |
if m: |
| 41 |
ous.write("#define ") |
| 42 |
ous.write(m.group(1)) |
| 43 |
ous.write("_H_\n") |
| 44 |
line = ins.readline() |
| 45 |
found = True |
| 46 |
continue |
| 47 |
m = endif.search(line) |
| 48 |
if m: |
| 49 |
ous.write("#endif /* ") |
| 50 |
ous.write(m.group(1)) |
| 51 |
ous.write("_H_ */\n") |
| 52 |
line = ins.readline() |
| 53 |
found = True |
| 54 |
continue |
| 55 |
ous.write(line) |
| 56 |
line = ins.readline() |
| 57 |
ins.close() |
| 58 |
ous.close() |
| 59 |
if found: |
| 60 |
os.system("move %s %s" % (tmp, file)) |
| 61 |
else: |
| 62 |
os.system("del %s" % tmp) |
| 63 |
|
| 64 |
def fixup(dir): |
| 65 |
for root, dirs, files in os.walk(dir): |
| 66 |
for f in files: |
| 67 |
if f.endswith('.h'): |
| 68 |
path = "%s\\%s" % (root, f) |
| 69 |
fix_hdr(path) |
| 70 |
|
| 71 |
fixup('src') |