MakeHeader.py 2.12 KB
Newer Older
Hisham Muhammad's avatar
Hisham Muhammad committed
1
#!/usr/bin/env python
Hisham Muhammad's avatar
Hisham Muhammad committed
2
3
4
5
6
7
8
9
10

import os, sys, string

ANY=1
COPY=2
SKIP=3
SKIPONE=4

state = ANY
11
static = 0
Hisham Muhammad's avatar
Hisham Muhammad committed
12
13
14
15
16
17
18
19
20
21
22
23

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)

24
25
print("Generating "+name+".h")

Hisham Muhammad's avatar
Hisham Muhammad committed
26
27
28
29
30
selfheader = '#include "' + name + '.h"'

out.write( "/* Do not edit this file. It was automatically generated. */" )
out.write( "" )

31
32
out.write( "#ifndef HEADER_" + os.path.basename(name) )
out.write( "#define HEADER_" + os.path.basename(name) )
Hisham Muhammad's avatar
Hisham Muhammad committed
33
is_blank = False
Hisham Muhammad's avatar
Hisham Muhammad committed
34
35
36
37
38
39
40
for line in file.readlines():
   line = line[:-1]
   if state == ANY:
      if line == '/*{':
         state = COPY
      elif line == selfheader:
         pass
Hisham Muhammad's avatar
Hisham Muhammad committed
41
42
      elif line.find("#include") == 0:
         pass
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
      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[-1] == "{":
60
            out.write( line[:-2].replace("inline", "extern") + ";" )
61
62
63
            state = SKIP
         else:
            out.write( line )
Hisham Muhammad's avatar
Hisham Muhammad committed
64
65
66
67
68
         is_blank = False
      elif line == "":
         if not is_blank:
            out.write( line )
         is_blank = True
Hisham Muhammad's avatar
Hisham Muhammad committed
69
70
      else:
         out.write( line )
Hisham Muhammad's avatar
Hisham Muhammad committed
71
         is_blank = False
Hisham Muhammad's avatar
Hisham Muhammad committed
72
   elif state == COPY:
Hisham Muhammad's avatar
Hisham Muhammad committed
73
      is_blank = False
Hisham Muhammad's avatar
Hisham Muhammad committed
74
75
76
77
78
      if line == "}*/":
         state = ANY
      else:
         out.write( line )
   elif state == SKIP:
Hisham Muhammad's avatar
Hisham Muhammad committed
79
      is_blank = False
Hisham Muhammad's avatar
Hisham Muhammad committed
80
      if len(line) >= 1 and line[0] == "}":
81
82
83
84
85
         if static == 1:
            state = SKIPONE
         else:
            state = ANY
         static = 0
Hisham Muhammad's avatar
Hisham Muhammad committed
86
   elif state == SKIPONE:
Hisham Muhammad's avatar
Hisham Muhammad committed
87
      is_blank = False
Hisham Muhammad's avatar
Hisham Muhammad committed
88
89
90
91
      state = ANY

out.write( "" )
out.write( "#endif" )