-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtocpp.py
111 lines (91 loc) · 4.42 KB
/
htocpp.py
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
import os
ext = input("Do you want to convert .c or .cpp files? (default .cpp) (.cpp/.c)")
if ext == ".c":
ext = ".c"
else:
ext = ".cpp"
override = input("Do you want to override any existing {0} files? (You might lose contents of your {0} files) (Y/N) ".format(ext))
override = str.upper(override)
if override.startswith("Y"):
override = input("Are you sure? (Y/N) ")
override = str.upper(override)
if override.startswith('Y'):
override = 'w+'
else:
override = 'a'
for filename in os.listdir():
if filename.endswith(".h"):
# Open/create header file
header = open(filename, 'r')
# Open/create .c/.cpp file
file = open(filename.replace(".h", "{}".format(ext)), override)
# Add include to file
file.write('#include "{}"\n\n'.format(filename))
# some variables
inClass = 0
inFunction = 0
skipOneLine = False
className = []
# Checks each line in header
for line in header:
if skipOneLine:
skipOneLine = False
continue
# Removes spacing from each line
line = line.lstrip()
# This string marks end of a class
if "};" in line:
inClass -= 1
className.pop()
# This checks if function has body inside header file
# and skips over this body
if line.startswith("{"):
inFunction += 1
if line.startswith("}"):
inFunction -= 1
# Check if inside a function's body, and skip this line if True
skipLine = inFunction > 0
# List of ignore strings that skip over this line
ignoreStrings = ["//", "#", "public:", "private:", "protected:", "{", "};", "friend"]
for i in ignoreStrings:
if i in line:
skipLine = True
# List of required strings in order to create a function from this line
requiredStrings = ["(", ";"]
for r in requiredStrings:
if r not in line:
skipLine = True
# If this line isn't skipped
if not skipLine:
# Remove virutal keywords
function = line.replace("virtual ", "")
if inClass > 0:
# Check of this is constructor/destructor and add className:: to thier names
if function.startswith("operator") or function.replace("~", "").startswith(className[-1]):
function = "{}::".format(className[-1]) + function
# Else add className:: before function's name which should be right after first whitespace
else:
function = line.replace("virtual ", "")
function = function.replace(" ", " {}::".format(className[-1]), 1)
# Replace semicolon ; with empty body of a function
function = function.replace(";", "\n{\n\n}\n")
# Write function to file
file.write(function)
# If class/struct string is in line extract it's name and append it to className list
# and in next iteration start looking for class/struct functions
if "class " in line:
inClass += 1
className.append(line.split("class ")[1].split(":")[0].rstrip())
className[-1] = className[-1].replace("\n", "")
skipOneLine = True
if "struct " in line:
inClass += 1
className.append(line.split("struct ")[1].split(":")[0].rstrip())
className[-1] = className[-1].replace("\n", "")
skipOneLine = True
# This is only here because I'm lazy, but hey whatever works
if "enum " in line:
inClass += 1
className.append(line.split("enum ")[1].split(":")[0].rstrip())
className[-1] = className[-1].replace("\n", "")
skipOneLine = True