2014-11-20 01:27:21 +00:00
|
|
|
#!/usr/bin/env python
|
2018-04-05 22:55:51 +00:00
|
|
|
import os, sys, string, io
|
2017-04-21 14:39:03 +00:00
|
|
|
try:
|
2018-04-05 22:55:51 +00:00
|
|
|
from StringIO import StringIO
|
2017-07-27 21:02:01 +00:00
|
|
|
except ImportError:
|
2018-04-05 22:55:51 +00:00
|
|
|
from io import StringIO
|
2006-03-23 18:45:14 +00:00
|
|
|
|
|
|
|
ANY=1
|
|
|
|
COPY=2
|
|
|
|
SKIP=3
|
|
|
|
SKIPONE=4
|
2018-01-31 02:52:08 +00:00
|
|
|
COPYDEFINE=5
|
2006-03-23 18:45:14 +00:00
|
|
|
|
|
|
|
state = ANY
|
2006-06-06 20:28:09 +00:00
|
|
|
static = 0
|
2006-03-23 18:45:14 +00:00
|
|
|
|
2018-04-05 22:55:51 +00:00
|
|
|
file = io.open(sys.argv[1], "r", encoding="utf-8")
|
2006-03-23 18:45:14 +00:00
|
|
|
name = sys.argv[1][:-2]
|
|
|
|
|
2017-04-21 14:39:03 +00:00
|
|
|
out = StringIO()
|
2007-05-17 18:29:30 +00:00
|
|
|
|
2006-03-23 18:45:14 +00:00
|
|
|
selfheader = '#include "' + name + '.h"'
|
|
|
|
|
2017-04-21 14:39:03 +00:00
|
|
|
out.write( "/* Do not edit this file. It was automatically generated. */\n" )
|
|
|
|
out.write( "\n" )
|
2006-03-23 18:45:14 +00:00
|
|
|
|
2017-04-21 14:39:03 +00:00
|
|
|
out.write( "#ifndef HEADER_" + os.path.basename(name) + "\n")
|
|
|
|
out.write( "#define HEADER_" + os.path.basename(name) + "\n")
|
2011-12-26 21:35:57 +00:00
|
|
|
is_blank = False
|
2006-03-23 18:45:14 +00:00
|
|
|
for line in file.readlines():
|
|
|
|
line = line[:-1]
|
|
|
|
if state == ANY:
|
|
|
|
if line == '/*{':
|
|
|
|
state = COPY
|
|
|
|
elif line == selfheader:
|
|
|
|
pass
|
2011-12-26 21:35:57 +00:00
|
|
|
elif line.find("#include") == 0:
|
|
|
|
pass
|
2006-06-06 20:28:09 +00:00
|
|
|
elif line.find("htop - ") == 0 and line[-2:] == ".c":
|
2017-04-21 14:39:03 +00:00
|
|
|
out.write(line[:-2] + ".h\n")
|
2006-06-06 20:28:09 +00:00
|
|
|
elif line.find("static ") != -1:
|
|
|
|
if line[-1] == "{":
|
|
|
|
state = SKIP
|
|
|
|
static = 1
|
|
|
|
else:
|
|
|
|
state = SKIPONE
|
|
|
|
elif len(line) > 1:
|
|
|
|
static = 0
|
|
|
|
equals = line.find(" = ")
|
2018-01-31 02:52:08 +00:00
|
|
|
if line[:7] == "#define":
|
|
|
|
if line[-1:] == "\\":
|
|
|
|
state = COPYDEFINE
|
|
|
|
out.write( line + "\n")
|
|
|
|
elif line[-3:] == "= {":
|
2017-04-21 14:39:03 +00:00
|
|
|
out.write( "extern " + line[:-4] + ";\n" )
|
2006-06-06 20:28:09 +00:00
|
|
|
state = SKIP
|
|
|
|
elif equals != -1:
|
2017-04-21 14:39:03 +00:00
|
|
|
out.write("extern " + line[:equals] + ";\n" )
|
2015-01-22 01:27:31 +00:00
|
|
|
elif line.startswith("typedef struct"):
|
|
|
|
state = SKIP
|
2006-06-06 20:28:09 +00:00
|
|
|
elif line[-1] == "{":
|
2017-04-21 14:39:03 +00:00
|
|
|
out.write( line[:-2].replace("inline", "extern") + ";\n" )
|
2006-06-06 20:28:09 +00:00
|
|
|
state = SKIP
|
|
|
|
else:
|
2018-01-31 02:52:08 +00:00
|
|
|
out.write( line + "\n" )
|
2011-12-26 21:35:57 +00:00
|
|
|
is_blank = False
|
|
|
|
elif line == "":
|
|
|
|
if not is_blank:
|
2017-04-21 14:39:03 +00:00
|
|
|
out.write( line + "\n")
|
2011-12-26 21:35:57 +00:00
|
|
|
is_blank = True
|
2006-03-23 18:45:14 +00:00
|
|
|
else:
|
2017-04-21 14:39:03 +00:00
|
|
|
out.write( line + "\n")
|
2011-12-26 21:35:57 +00:00
|
|
|
is_blank = False
|
2018-01-31 02:52:08 +00:00
|
|
|
elif state == COPYDEFINE:
|
|
|
|
is_blank = False
|
|
|
|
out.write( line + "\n")
|
|
|
|
if line[-1:] != "\\":
|
|
|
|
state = ANY
|
2006-03-23 18:45:14 +00:00
|
|
|
elif state == COPY:
|
2011-12-26 21:35:57 +00:00
|
|
|
is_blank = False
|
2006-03-23 18:45:14 +00:00
|
|
|
if line == "}*/":
|
|
|
|
state = ANY
|
|
|
|
else:
|
2017-04-21 14:39:03 +00:00
|
|
|
out.write( line + "\n")
|
2006-03-23 18:45:14 +00:00
|
|
|
elif state == SKIP:
|
2011-12-26 21:35:57 +00:00
|
|
|
is_blank = False
|
2006-03-23 18:45:14 +00:00
|
|
|
if len(line) >= 1 and line[0] == "}":
|
2006-06-06 20:28:09 +00:00
|
|
|
if static == 1:
|
|
|
|
state = SKIPONE
|
|
|
|
else:
|
|
|
|
state = ANY
|
|
|
|
static = 0
|
2006-03-23 18:45:14 +00:00
|
|
|
elif state == SKIPONE:
|
2011-12-26 21:35:57 +00:00
|
|
|
is_blank = False
|
2006-03-23 18:45:14 +00:00
|
|
|
state = ANY
|
|
|
|
|
2017-04-21 14:39:03 +00:00
|
|
|
out.write( "\n" )
|
|
|
|
out.write( "#endif\n" )
|
|
|
|
|
|
|
|
# only write a new .h file if something changed.
|
|
|
|
# This prevents a lot of recompilation during development
|
|
|
|
out.seek(0)
|
|
|
|
try:
|
2018-04-05 22:55:51 +00:00
|
|
|
with io.open(name + ".h", "r", encoding="utf-8") as orig:
|
2017-04-21 14:39:03 +00:00
|
|
|
origcontents = orig.readlines()
|
|
|
|
except:
|
|
|
|
origcontents = ""
|
|
|
|
if origcontents != out.readlines():
|
2018-04-05 22:55:51 +00:00
|
|
|
with io.open(name + ".h", "w", encoding="utf-8") as new:
|
2017-04-21 14:39:03 +00:00
|
|
|
print("Writing "+name+".h")
|
|
|
|
new.write(out.getvalue())
|
|
|
|
out.close()
|