-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreform.py
57 lines (45 loc) · 1.39 KB
/
reform.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 sys
# hard-coded tab setting @ 4 spaces !!!
tabSize = 4
def rest_of_line( line, idx ):
return line[idx:-1] # trim off the trailing '\n'
def process( char, context ):
if context == 'start':
context = 'test'
if char == '\t':
return ' ' * tabSize, context
elif char == '/':
return char, context
elif char == '\n':
return '', context
return char, context
def reformat( fname ):
cfile = open( fname ).readlines()
context = 'start'
current_line = 0
for line in cfile:
prevchar = '\n'
modified_line = ''
idx = 0
for char in line:
if char == '/':
if prevchar == '/':
# ignore comments
rest = rest_of_line( line, idx )
modified_line += rest
break
elif char == '#':
# ignore preprocessor directives
modified_line += rest_of_line( line, idx )
break
newchar, context = process( char, context )
modified_line += newchar
prevchar = char
idx += 1
print( modified_line )
current_line += 1
#print( 'context is ' + context )
if __name__ == '__main__':
if len(sys.argv) > 1:
reformat( sys.argv[1] )