-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddFunction.py
More file actions
87 lines (63 loc) · 2.79 KB
/
Copy pathaddFunction.py
File metadata and controls
87 lines (63 loc) · 2.79 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import os
import sys
SOURCE_DIR = "./SOURCE"
def find_insertion_point(header_lines, class_name):
"""Finds the insertion point for function declarations inside the class."""
inside_class = False
for i, line in enumerate(header_lines):
if f"class {class_name}" in line:
inside_class = True
if inside_class and "};" in line: # End of class
return i # Insert before the closing brace
return None
def add_function_to_header(header_path, class_name, function_signature):
"""Adds a function declaration to the class in the header file."""
if not os.path.exists(header_path):
print(f"❌ Error: Header file {header_path} not found.")
return
with open(header_path, "r") as file:
lines = file.readlines()
insert_index = find_insertion_point(lines, class_name)
if insert_index is None:
print(f"❌ Could not find class {class_name} in {header_path}.")
return
declaration = f" {function_signature};\n"
if declaration in lines:
print(f"⚠️ Function already exists in {header_path}. Skipping...")
return
lines.insert(insert_index, declaration)
with open(header_path, "w") as file:
file.writelines(lines)
print(f"✅ Added function declaration to {header_path}")
def add_function_to_cpp(cpp_path, class_name, function_signature):
"""Adds a function definition to the .cpp file."""
if not os.path.exists(cpp_path):
print(f"❌ Error: Source file {cpp_path} not found.")
return
return_type, function_name = function_signature.split(" ", 1)
definition = f"{return_type} {class_name}::{function_name}\n{{\n // TODO: Implement\n}}\n\n"
with open(cpp_path, "r") as file:
content = file.read()
if definition in content:
print(f"⚠️ Function already exists in {cpp_path}. Skipping...")
return
with open(cpp_path, "a") as file:
file.write(definition)
print(f"✅ Added function definition to {cpp_path}")
def main():
if len(sys.argv) < 3:
print("Usage: python3 addFunction.py ClassName 'returnType functionName(args)'")
sys.exit(1)
class_name = sys.argv[1]
function_signature = sys.argv[2]
header_path = os.path.join(SOURCE_DIR, f"{class_name}.h")
cpp_path = os.path.join(SOURCE_DIR, f"{class_name}.cpp")
add_function_to_header(header_path, class_name, function_signature)
add_function_to_cpp(cpp_path, class_name, function_signature)
if __name__ == "__main__":
main()
#############
### USAGE ###
# ryandevens@Ryans-iMac GrainMaker % python3 addFunction.py Granulator "void prepare()"
# Class must exist in file.
# TODO: Doesn't work with files in subdirectories of SOURCE yet