LightScript/replacePragmaOnce.py

45 lines
1.3 KiB
Python
Raw Normal View History

2023-01-13 00:13:17 +09:00
import os
import glob
def checkContent(lst,oper):
if len(lst) < len(oper):
return False
for i,k in enumerate(oper):
if lst[i].strip() != k:
return False
return True
def replacePragmaOnce(name):
hitContents=False
with open(name,"r") as file:
temp = open(name+".temp","w")
nameheader = os.path.splitext(os.path.split(name)[-1])[0]
for line in file:
words = line.split()
if hitContents:
pass
elif checkContent(words,["#pragma","once"]):
continue
elif line.startswith("#ifndef"):
temp.close()
os.remove(name+".temp")
return None
elif line.startswith("#include"):
pass
elif len(words)==0:
pass
elif line.strip() == "":
pass
else:
temp.write("#ifndef LS_{}_H_\n".format(nameheader))
temp.write("#define LS_{}_H_\n".format(nameheader))
hitContents=True
temp.writelines(line)
temp.write("\n#endif")
temp.close()
os.remove(name)
os.rename(name+".temp",name)
for pth in glob.iglob('LightScript\\*.h'):
replacePragmaOnce(pth)