Last active
February 21, 2019 21:45
-
-
Save benjaminfuchs/52022f13cee59acc05ab448d3bcd8c5d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
import tempfile | |
import os | |
import re | |
from string import Template | |
STRING = """ | |
#include <iostream> | |
#include <string> | |
#define STR(x) std::cout << #x"=" << x << std::endl | |
#include "$header_file" | |
int main() | |
{ | |
$main | |
} | |
""" | |
names = [] | |
with open("test.hpp", "r") as fp: | |
for line in fp.readlines(): | |
match = re.match(r"^\s*#define\s+\b(\w+)\b", line) | |
if match: | |
names.append(match.group(1)) | |
main = "" | |
for name in names: | |
main = main + "STR(" + name + "); " | |
t = Template(STRING) | |
string = t.substitute(header_file="test.hpp", main=main) | |
with open("main.c", "w") as fp: | |
fp.write(string) | |
p = subprocess.Popen(["g++", "-o", "main", "-xc++", "main.c"], stdout=subprocess.PIPE) | |
out, error = p.communicate() | |
p = subprocess.Popen(["./main"], stdout=subprocess.PIPE) | |
out, error = p.communicate() | |
print(out) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment