htop/scripts/MakeHeader.py

94 lines
2.2 KiB
Python
Raw Permalink Normal View History

2014-11-20 01:27:21 +00:00
#!/usr/bin/env python
2006-03-23 18:45:14 +00:00
import os, sys, string
ANY=1
COPY=2
SKIP=3
SKIPONE=4
state = ANY
static = 0
2006-03-23 18:45:14 +00:00
file = open(sys.argv[1])
name = sys.argv[1][:-2]
out = open(name + ".h", "w")
class writer:
def __init__(self, file):
self.file = file
def write(self, text):
self.file.write(text + "\n")
out = writer(out)
print("Generating "+name+".h")
2006-03-23 18:45:14 +00:00
selfheader = '#include "' + name + '.h"'
out.write( "/* Do not edit this file. It was automatically generated. */" )
out.write( "" )
out.write( "#ifndef HEADER_" + os.path.basename(name) )
out.write( "#define HEADER_" + os.path.basename(name) )
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
elif line.find("htop - ") == 0 and line[-2:] == ".c":
out.write(line[:-2] + ".h")
elif line.find("static ") != -1:
if line[-1] == "{":
state = SKIP
static = 1
else:
state = SKIPONE
elif len(line) > 1:
static = 0
equals = line.find(" = ")
if line[-3:] == "= {":
out.write( "extern " + line[:-4] + ";" )
state = SKIP
elif equals != -1:
out.write("extern " + line[:equals] + ";" )
elif line.startswith("typedef struct"):
state = SKIP
elif line[-1] == "{":
out.write( line[:-2].replace("inline", "extern") + ";" )
state = SKIP
else:
out.write( line )
2011-12-26 21:35:57 +00:00
is_blank = False
elif line == "":
if not is_blank:
out.write( line )
is_blank = True
2006-03-23 18:45:14 +00:00
else:
out.write( line )
2011-12-26 21:35:57 +00:00
is_blank = False
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:
out.write( line )
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] == "}":
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
out.write( "" )
out.write( "#endif" )