3 def doSortIncludes(name
):
4 print('Processing ' + name
)
14 # Open the file and read it
16 lines
= file.readlines()
18 # Parse the content, seperating between includes and 'the rest'
20 if line
.startswith('//NOSORT'):
23 if line
.startswith('#include <'):
24 gincludes
.append(line
)
26 elif line
.startswith('#include'):
31 precontent
.append(line
)
33 postcontent
.append(line
)
37 # If there are no includes, don't touch the file
38 if len(includes
) == 0 and len(gincludes
) == 0:
41 if len(gincludes
) > 0:
43 gincludes
.append("\n")
54 # Clean up any trailing spaces before the includes
55 for line
in precontent
:
56 if cleaning
and (len(line
) == 0 or line
.expandtabs().isspace() ):
59 precontentclean
.append(line
)
62 # Reverse the content again
63 precontentclean
.reverse()
64 precontentclean
.append("\n")
69 # Clean up any leading spaces after the includes
70 for line
in postcontent
:
71 if cleaning
and (len(line
) == 0 or line
.expandtabs().isspace() ):
74 postcontentclean
.append(line
)
77 # Assemble the resulting file
78 result
= precontentclean
+ gincludes
+ includes
+ postcontentclean
80 # Write the new and improved file
82 fixfile
= open(name
, 'w')
83 fixfile
.writelines(result
)
89 if __name__
== "__main__":
94 if len(filenames
) == 1:
96 print("no args, using all *.cpp and *.h files")
98 filenames
= filenames
[0:1] + glob
.glob('*.cpp') + glob
.glob('*.h')
101 for filename
in filenames
[1:]:
102 # doRemoveWhitespace(filename)
103 doSortIncludes(filename
)