Skip to content

Instantly share code, notes, and snippets.

@jthistle
Last active June 6, 2019 16:26
Show Gist options
  • Save jthistle/2fecfb968f094e1548c79a37cb7efa38 to your computer and use it in GitHub Desktop.
Save jthistle/2fecfb968f094e1548c79a37cb7efa38 to your computer and use it in GitHub Desktop.
Adds a new field to every struct creation declaration
#!/usr/bin/env python3
import re, os
cwd = os.getcwd()
files = [x for x in os.listdir(cwd) if x[-4:] == ".cpp"]
for fname in files:
if fname[-len("inspectorMeasure.cpp"):] == "inspectorMeasure.cpp":
continue
print("Opening: {}".format(fname))
lines = []
with open(fname, "r") as f:
oc = 0
lines = f.readlines()
inList = False
for i in range(len(lines)):
l = lines[i]
if ("il" in l or "iiList" in l or "iList" in l) and "= {" in l:
inList = True
if "};" in l:
inList = False
if "}" in l and inList and not l.lstrip()[:2] == "//":
comment = ""
commentSpaceDiff = 0
if "//" in l:
comment = l[l.find("//"):].rstrip()
l = l[:l.find("//")]
lcopy = l
l = l.rstrip()
commentSpaceDiff = len(lcopy) - len(l)
l = l.replace("\n", "")
l = l.replace("},", "")
l = l.replace("}", "")
tempL = l
tempL = tempL.rstrip()
spaceDiff = len(l) - len(tempL)
l = "{},{}false {},{}{}\n".format(tempL, " "*spaceDiff, "}", " "*commentSpaceDiff, comment)
lines[i] = l
oc += 1
print("Found and replaced {} occurences, writing".format(oc))
with open(fname, "w") as f:
f.writelines(lines)
print("Wrote.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment