-
Notifications
You must be signed in to change notification settings - Fork 4
/
fix-includes.py
38 lines (29 loc) · 1.08 KB
/
fix-includes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import os
import re
root_ = '.'
headers = dict()
for root, dirs, files in os.walk('.'):
for f in [f for f in files if os.path.splitext(f)[1] in ['.h'] ]:
p = os.path.join(root,f)
x = os.path.relpath(p,root_).replace('\\','/')
headers[f] = x
#print(headers)
for root, dirs, files in os.walk('.'):
for f in [f for f in files if os.path.splitext(f)[1] in ['.cpp','.h'] ]:
p = os.path.join(root,f)
with open(p,encoding='utf-8') as x:
lines = x.readlines()
changed = False
for i,line in enumerate(lines):
m = re.match('#include "(.*)"',line)
if (m):
b = os.path.basename(m.group(1))
if b in headers:
if headers[b] != b:
line_ = line.replace(m.group(1),headers[b])
lines[i] = line_
changed = True
if changed:
print('writing',p)
with open(p,'wb') as x:
x.write(''.join(lines).encode('utf-8'))