#!/usr/bin/env python3 """ backdate.py -- mechanically back-date a WinTesla-era engine header (BT411/BT412 engine/MUNGA*/X.h) to its 1995 form (source410/MUNGA/X.HPP). Transforms (the mechanical part -- ALWAYS eyeball the result and write the .NOTES.md sidecar; content drift is NOT detected here): #pragma once -> #if !defined(X_HPP) / #define X_HPP ... #endif #include "y.h" -> guarded #if !defined(Y_HPP) #include #endif (system includes like are left alone) Usage: py -3 backdate.py """ import os import re import sys def main(): donor, out = sys.argv[1], sys.argv[2] name = os.path.splitext(os.path.basename(out))[0].upper() guard = "%s_HPP" % name body = open(donor, "r", errors="replace").read().splitlines() res = [] warned = set() for line in body: s = line.strip() if s == "#pragma once": continue # local include, possibly with a relative path -- keep the basename m = re.match(r'#include\s+"(?:[.\\/A-Za-z0-9_]*[\\/])?([A-Za-z0-9_]+)\.h(?:pp)?"', s) if m: inc = m.group(1).lower() g = inc.upper() + "_HPP" res.append("#\tif !defined(%s)" % g) res.append("#\t\tinclude <%s.hpp>" % inc) res.append("#\tendif") continue # post-1994 C++ the 4.52 compiler rejects: de-modernize line = re.sub(r"\bstd::", "", line) line = re.sub(r"\btrue\b", "True", line) line = re.sub(r"\bfalse\b", "False", line) line = re.sub(r"\bbool\b", "Logical", line) for bad in ("",): if bad in line: line = None break if line is None: continue for marker in ("namespace", "static_cast", "reinterpret_cast", "const_cast"): if marker in s and marker not in warned: warned.add(marker) print("WARNING: post-1994 construct '%s' present -- fix by hand" % marker) res.append(line) # strip leading blank lines while res and not res[0].strip(): res.pop(0) with open(out, "w", newline="\r\n") as f: f.write("#if !defined(%s)\n#\tdefine %s\n\n" % (guard, guard)) f.write("\n".join(res)) f.write("\n\n#endif\n") print("wrote %s (%d lines from %s)" % (out, len(res) + 5, donor)) if __name__ == "__main__": main()