Skip to content

Instantly share code, notes, and snippets.

@AndrewBelt
Created May 30, 2018 01:24
Show Gist options
  • Save AndrewBelt/ad4e84f4a6d25141524e0cea2c104194 to your computer and use it in GitHub Desktop.
Save AndrewBelt/ad4e84f4a6d25141524e0cea2c104194 to your computer and use it in GitHub Desktop.
Convert C++ headers to boilerplate source files
import sys
import re
import os
header_filename = sys.argv[1]
if not header_filename:
raise "No filename given"
header_basename = os.path.basename(header_filename)
class_name = None
namespace_name = None
print("#include \"" + header_basename + "\"")
print("")
with open(header_filename) as f:
for line in f:
# Check for namespace
r = re.match(r'\s*(namespace)\s+(\w+)', line)
if r:
namespace_name = r.group(2)
print("namespace " + namespace_name + " {")
print("")
# Check for class
r = re.match(r'\s*(class|struct)\s+(\w+)', line)
if r:
class_name = r.group(2)
# Check for end of class
r = re.match(r'};', line)
if r:
class_name = None
# Check for function/method
r = re.match(r' *(virtual +)?(.*?)([~\w]+)\((.*?)\) *(override)?;', line)
if r:
ret = r.group(2).strip()
if ret and ret[-1] != "*":
ret += " "
method = r.group(3)
if class_name:
method = class_name + "::" + method
args = r.group(4)
print(ret + method + "(" + args + ") {")
print("}")
print("")
if namespace_name:
print("} // namespace " + namespace_name)
print("")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment