-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpp_joiner.py
More file actions
50 lines (35 loc) · 1.43 KB
/
Copy pathcpp_joiner.py
File metadata and controls
50 lines (35 loc) · 1.43 KB
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
import os
def get_includes(file_name, standard_includes, file_order):
with open(file_name, 'r') as file:
for line in file:
if line.startswith('#include'):
if '<' in line:
standard_includes.add(line)
else:
my_include = line[line.find('"') + 1:line.rfind('"')]
if my_include not in file_order:
get_includes(my_include, standard_includes, file_order)
file_order.append(file_name)
if file_name.endswith('.h'):
cpp_file = '{}.cpp'.format(file_name[:-2])
if os.path.isfile(cpp_file):
get_includes(cpp_file, standard_includes, file_order)
def copy_without_includes(file_name, target_file):
target_file.write('// {}\n\n'.format(file_name))
with open(file_name, 'r') as source:
for line in source:
if not line.startswith(('#include', '#pragma once')):
target_file.write(line)
target_file.write('\n\n')
def joiner():
standard_includes = set()
file_order = list()
get_includes('main.cpp', standard_includes, file_order)
with open('autoJoinedMain.cpp', 'w') as joined_file:
for line in standard_includes:
joined_file.write(line)
joined_file.write('\n')
for file_name in file_order:
copy_without_includes(file_name, joined_file)
if __name__ == '__main__':
joiner()