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 gincludesold
= gincludes
[:]
40 gincludes
.append("\n")
42 includesold
= includes
[:]
49 # Clean up any leading spaces before the includes
50 for line
in postcontent
:
51 if cleaning
and (len(line
) == 0 or line
.expandtabs().isspace() ):
54 postcontentclean
.append(line
)
57 # Assemble the resulting file
58 result
= precontent
+ gincludes
+ includes
+ postcontent
60 # Write the new and improved file
62 fixfile
= open(name
, 'w')
63 fixfile
.writelines(result
)
69 if __name__
== "__main__":
74 if len(filenames
) == 1:
76 print("no args, using all *.cpp and *.h files")
78 filenames
= glob
.glob('*.cpp') + glob
.glob('*.h')
81 for filename
in filenames
[1:]:
82 # doRemoveWhitespace(filename)
83 doSortIncludes(filename
)