-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcppFileGenerator.py
More file actions
220 lines (169 loc) · 6.48 KB
/
cppFileGenerator.py
File metadata and controls
220 lines (169 loc) · 6.48 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import os
import re
inFileText = "Replace this text with your variable names and types,\nuse returnType methodName() to create a blank method with a return type, \nand add #include <file> to add an include to the auto-generated header file.\nSemicolons (;) will be removed automatically\nExample:\n\n#include \"exampleInclude.h\"\nfloat x\nfloat y\nfloat z\nvoid doSomething()\nfloat doSomethingElse()\nvoid voidTestMethod(float testInput)\nfloat testMethod(float testInput)\nfloat testMethod(float testInput, int testInput2)\n\nThen run the generator with:\npython3 fileGenerator.py\nYou will be asked to input a class name\n\nPlease note that #include generation and method generation are only available when using the file generator"
def fixCase(key):
key = re.sub('([a-zA-Z])', lambda x: x.groups()[0].upper(), key, 1)
return key
#Create empty dictionary
varDict = {}
includeList = []
methodList = []
#Get user input for the class name
className = input("Please enter class name:\n")
#className = "testClass"
print("\n")
#Read input
inFile = open("input.txt","r")
lines = inFile.readlines()
if(os.path.exists((className + ".h"))):
os.remove((className + ".h")) #Clear output file to get rid of any old output
outFile = open((className + ".h"), "w+")
#Loop to read all lines from the file
try:
for line in lines:
line = line.strip()
print("Read line \"" + line + "\"")
if(line.startswith("#include")):
includeList.append(line)
elif(line.endswith(")")):
methodList.append(line)
else:
#Convert to name and type
line = line.replace(";","")
splitLine = line.split(" ")
varName = splitLine[1]
varType = splitLine[0]
#Add variable name to dictionary
varDict[varName] = varType
except:
#Print error to user
print("\n\n")
print("*******************************************************")
print(" Input file could not be read successfully,\n did you save input.txt before running the generator?")
print("*******************************************************\n")
#Delete the header file because there was an error
if(os.path.exists((className + ".h"))):
os.remove((className + ".h"))
#Exit program
exit()
keys = varDict.keys()
#Writing header file
inFile.close()
print("Finished reading variable list, writing to output file...")
print("Writing .h file")
#Will write the .h file first
#Setting up the header file
outFile.write("//Header file auto-generated using CPP-Getter-Setter-Generator\n")
outFile.write("#ifndef " + className.upper() + "_H\n")
outFile.write("#define " + className.upper() + "_H\n\n")
outFile.write("//Includes\n")
for i in includeList:
outFile.write(i + "\n")
outFile.write("\n\nclass " + className + " {\n\tpublic:\n")
print("Writing Constructor and Destructor")
#Constructor and Destructor
outFile.write("\t\t//Constructor and Destructor\n")
outFile.write("\t\t" + className + "();\n")
outFile.write("\t\t~" + className + "();\n")
outFile.write("\n\t\t//Overloaded constructors\n")
#Setup for writing constructors
keysList = list(keys)
count = 0
#Write constructors
for key in keysList:
count += 1
outFile.write("\t\t" + className + "(")
for i in range(count):
outFile.write(varDict[keysList[i]] + " _" + keysList[i])
if(i != count-1):
outFile.write(", ")
outFile.write(");\n")
outFile.write("\n")
print("Writing Getters and Setters")
#Getters
outFile.write("\t\t//Getters and Setters\n")
for key in keys:
outFile.write("\t\t" + varDict[key] + " get" + fixCase(key) + "();\n")
outFile.write("\n")
#Setters
for key in keys:
outFile.write("\t\tvoid set" + fixCase(key) + "( " + varDict[key] + " _" + key + " );\n")
outFile.write("\n")
#Other methods
outFile.write("\t\t//Other methods\n")
for i in methodList:
outFile.write("\t\t" + i + ";\n")
print("Writing other methods to header file")
print("Writing Variable Declarations")
#Private variables
outFile.write("\n\tprivate:\n")
outFile.write("\t\t//Variables\n")
#Variable declarations
for key in keys:
outFile.write("\t\t" + varDict[key] + " " + key + ";\n")
outFile.write("\n")
#End of header file
outFile.write("};\n#endif")
#Tidy up
outFile.close()
print("Finished writing header file")
print("Writing source file")
#Set up for creating the source file
if(os.path.exists((className + ".cpp"))):
os.remove((className + ".cpp")) #Clear output file to get rid of any old output
outFile = open((className + ".cpp"), "w+")
#Now to write the source file
outFile.write("//Source file auto-generated using CPP-Getter-Setter-Generator\n\n")
outFile.write("//Includes\n")
outFile.write("#include <" + className + ".h>\n")
print("Writing constructors and destructors")
outFile.write("\n//Constructor and Destructor\n")
outFile.write("" + className + "::" + className + "(){}\n")
outFile.write("" + className + "::~" + className + "(){}\n")
outFile.write("\n//Overloaded constructors\n")
#Setup for writing constructors
keysList = list(keys)
count = 0
#Write constructors
for key in keysList:
count += 1
outFile.write("" + className + "::" + className + "(")
for i in range(count):
outFile.write(varDict[keysList[i]] + " _" + keysList[i])
if(i != count-1):
outFile.write(", ")
outFile.write("){\n")
for i in range(count):
outFile.write("\t" +
keysList[i] + " = _" + keysList[i])
outFile.write(";\n")
outFile.write("}\n")
print("Writing Getters and setters")
outFile.write("\n//Getters and Setters\n")
#Getters
for key in keys:
outFile.write(varDict[key] + " " + className + "::get" + fixCase(key) + "(){ return " + key + "; }\n")
outFile.write("\n")
#Setters
for key in keys:
outFile.write("void " + className + "::set" + fixCase(key) + "( " + varDict[key] + " _" + key + " ){ " + key + " = _" + key + "; }\n")
#Write other methods
print("Writing other methods to source file")
outFile.write("\n//Other methods\n")
for i in methodList:
splitStr = i.split(" ")
outFile.write(splitStr[0] + " " + className + "::" + splitStr[1])
for i in range(len(splitStr)):
if(i < 2):
continue
outFile.write(" " + splitStr[i])
outFile.write("{\n\t\n}\n")
#All done writing files, time to clean up
print("Done writing to source file, closing file...")
outFile.close()
print("Reformatting input file...")
os.remove("input.txt")
inFile = open("input.txt", "w+")
inFile.write(inFileText)
inFile.close()
print("Done!")