-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathHmacro.py
executable file
·58 lines (53 loc) · 1.12 KB
/
Hmacro.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/python
import os
def getMacro(fileName):
#Delete '/' '.'
sta =0
while sta <len(fileName):
if (fileName[sta]=='/'):
break
sta+=1
fin = sta+1
while fin < len(fileName):
if (fileName[fin]=='.'):
break
fin+=1
fileName = fileName[sta+1 : fin]
dirs = fileName.split('/')
macro = ''
for d in dirs:
macro += d.upper() + '_'
macro+= 'H'
return macro
def replaceMacro(fileName, macro):
f = open(fileName)
lines = f.read().split('\n')
f.close()
#Find if the file has macro
hasmacro = False
for line in lines:
if (line.find('_H')!=-1 or line.find('_h')!=-1) and line.find('#ifndef')!=-1:
hasmacro = True
break
if hasmacro == False:
print fileName
fc = ''
fc += '#ifndef ' + macro + '\n'
fc += '#define ' + macro + '\n'
for line in lines:
if len(line) <=0:
continue
fc += line + '\n'
fc += '#endif'+'\n'
f = open(fileName, 'w')
f.write(fc);
f.close()
return
if __name__=='__main__':
cmd = 'find include -name \"*.h\"'
files = os.popen(cmd).read().split('\n')
for file in files:
if len(file) <=0:
continue
macro = getMacro(file);
replaceMacro(file, macro);