Skip to content

Instantly share code, notes, and snippets.

@alex-s168
Created May 8, 2024 11:38
Show Gist options
  • Save alex-s168/cf4af5c069142460a4199df76d3ef885 to your computer and use it in GitHub Desktop.
Save alex-s168/cf4af5c069142460a4199df76d3ef885 to your computer and use it in GitHub Desktop.
auto generate C++ wrappers for C libraries using ctags
import sys
import subprocess
start = sys.argv[2]
ty = sys.argv[3]
newty = sys.argv[4]
cmd = "ctags -x --c-kinds=f --_xformat=\"%N %S\" " + sys.argv[1]
output = subprocess.check_output(cmd, stderr=subprocess.STDOUT).decode('utf-8')
fns = output.split("\n")
print("struct", newty, " {")
print(" ", ty, "_impl;")
print()
for fn in fns:
fns = fn.split(" ", 1)
if len(fns) != 2:
continue
name = fns[0]
args = fns[1].split("(", 1)[1].split(")", 1)[0].strip().split(",")
if not name.startswith(start):
continue
isconst = args[0].startswith("const " + ty)
if not (args[0].startswith(ty) or isconst):
continue
newname = name[len(start):]
print(" inline auto", newname + "(" + ", ".join(args[1:]) + ")", "const {" if isconst else "{")
print(" return", name + "(_impl, " + ",".join([v.split(" ")[-1] for (i, v) in enumerate(args[1:])]) + ");")
print(" }")
print()
print("};")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment