Skip to content

Instantly share code, notes, and snippets.

@redhog
Created September 4, 2020 15:44
Show Gist options
  • Save redhog/94de90c3d4d9597c7ca7d33753a2080d to your computer and use it in GitHub Desktop.
Save redhog/94de90c3d4d9597c7ca7d33753a2080d to your computer and use it in GitHub Desktop.
#! /usr/bin/env python
#
# Run this before running this script
# pip install CppHeaderParser
# Run as python gen.py header.h
# Prints C file with printer-funbctions.
import json, CppHeaderParser, sys
cppHeader = CppHeaderParser.CppHeader(sys.argv[1])
header = json.loads(cppHeader.toJSON())
res = ""
for name, defi in header["classes"].items():
res += "void print_%s(%s *o) {\n" % (name, name)
for prop in defi["properties"]["public"]:
if prop["type"] == "int":
res += ' printf("%s=%%d\\n", o.%s);\n' % (prop["name"], prop["name"])
elif prop["type"] == "char *":
res += ' printf("%s=%%s\\n", o.%s);\n' % (prop["name"], prop["name"])
res += "}\n"
print(res)
@redhog
Copy link
Author

redhog commented Sep 4, 2020

Above code is in the public domain, or where that is not legally possible, CC0. Have fun :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment