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('#include <'):
21 gincludes
.append(line
)
23 elif line
.startswith('#include'):
28 precontent
.append(line
)
30 postcontent
.append(line
)
34 # If there are no includes, don't touch the file
35 if len(includes
) == 0 and len(gincludes
) == 0:
38 if len(gincludes
) > 0:
40 gincludes
.append("\n")
51 # Clean up any trailing spaces before the includes
52 for line
in precontent
:
53 if cleaning
and (len(line
) == 0 or line
.expandtabs().isspace() ):
56 precontentclean
.append(line
)
59 # Reverse the content again
60 precontentclean
.reverse()
61 precontentclean
.append("\n")
66 # Clean up any leading spaces after the includes
67 for line
in postcontent
:
68 if cleaning
and (len(line
) == 0 or line
.expandtabs().isspace() ):
71 postcontentclean
.append(line
)
74 # Assemble the resulting file
75 result
= precontentclean
+ gincludes
+ includes
+ postcontentclean
77 # Write the new and improved file
79 fixfile
= open(name
, 'w')
80 fixfile
.writelines(result
)
86 if __name__
== "__main__":
91 if len(filenames
) == 1:
93 print("no args, using all *.cpp and *.h files")
95 filenames
= glob
.glob('*.cpp') + glob
.glob('*.h')
98 for filename
in filenames
[1:]:
99 # doRemoveWhitespace(filename)
100 doSortIncludes(filename
)