-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstruct_constructors.py
executable file
·127 lines (111 loc) · 4.61 KB
/
struct_constructors.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/env python
import sys, re
class BlockType:
start_cre = re.compile(r'^typedef [a-z]+ {.*')
end_cre = re.compile(r'^}[ ]*(SaHpi.*);')
member_cre = re.compile(r'^[ ]*SaHpi[A-Za-z0-9]+[ ]+[A-Za-z0-9]+[\[A-Z_\]]*[ ]*;.*')
def __init__(self, name, block):
self.datatype = 'unknown'
self.name = name
self.block = block
self.members = []
def __str__(self):
str = self.name + '\n'
for m in self.members:
name = m.name
if m.datatype == 'array':
name += '[' + m.lenstr + ']'
str += ' %s %s (%s)\n' % (m.typename, name, m.datatype)
return str
def parse(self, blocks):
for b in self.block:
if self.member_cre.match(b):
self.members.append(Member(b, blocks))
class Struct(BlockType):
start_cre = re.compile(r'^typedef struct {.*')
def __init__(self, name, block):
BlockType.__init__(self, name, block)
self.datatype = 'struct'
class Union(BlockType):
start_cre = re.compile(r'^typedef union {.*')
def __init__(self, name, block):
BlockType.__init__(self, name, block)
self.datatype = 'union'
class Member:
member_cre = re.compile(r'^[ ]*(SaHpi[A-Za-z0-9]+)[ ]+([A-Za-z0-9]+)[ ]*;.*')
array_cre = re.compile(r'^[ ]*(SaHpi[A-Za-z0-9]+)[ ]+([A-Za-z0-9]+)\[([A-Z_]+)\][ ]*;.*')
def __init__(self, bline, blocks):
match = self.member_cre.match(bline)
if match:
try:
self.datatype = blocks[match.groups()[0]].datatype
except KeyError:
self.datatype = 'native'
else:
match = self.array_cre.match(bline)
if match:
self.datatype = 'array'
self.lenstr = match.groups()[2]
else:
print 'ERROR: Could not match member line: %s' % bline
sys.exit(1)
self.typename = match.groups()[0]
self.name = match.groups()[1]
class HeaderParser:
def __init__(self, filename, types):
self.filename = filename
self.types = types
def parse(self):
blocks = {}
header_file = open(self.filename, 'r')
line = header_file.readline()
while line:
for T in self.types:
if T.start_cre.match(line):
block = []
bline = header_file.readline()
match = T.end_cre.match(bline)
while not match:
block.append(bline[:-1])
bline = header_file.readline()
match = T.end_cre.match(bline)
blockname = match.groups()[0]
blocks[blockname] = T(blockname, block)
line = header_file.readline()
for block in blocks.values():
block.parse(blocks)
return blocks
if __name__ == '__main__':
blocks = HeaderParser(sys.argv[1], [Struct, Union]).parse()
for block in blocks.values():
extends = '%%extend %s {\n' % (block.name)
extends += '\t%s(' % (block.name)
args = []
for m in block.members:
if m.datatype == 'array':
args.append('%s %s[%s] = NULL' % (m.typename, m.name, m.lenstr))
elif m.datatype in ['struct', 'union']:
args.append('%s *%s = NULL' % (m.typename, m.name))
else:
args.append('%s %s = 0' % (m.typename, m.name))
extends += ', '.join(args)
extends += ') {\n'
extends += '\t\t%s *o;\n' % (block.name)
extends += '\t\to = (%s *)malloc(sizeof(%s));\n' % (block.name, block.name)
extends += '\t\tmemset(o, 0, sizeof(%s));\n' % block.name
args = []
for m in block.members:
# Need and exception here for SaHpiGuidT. It is obscurely defined.
if m.typename == 'SaHpiGuidT':
args.append('\t\tif (%s) memcpy(o->%s, %s, sizeof(%s));' % (m.name, m.name, m.name, m.typename))
elif m.datatype == 'array':
args.append('\t\tif (%s) memcpy(o->%s, %s, sizeof(%s)*%s);' % (m.name, m.name, m.name, m.typename, m.lenstr))
elif m.datatype in ['struct', 'union']:
args.append('\t\tif (%s) o->%s = *%s;' % (m.name, m.name, m.name))
else:
args.append('\t\to->%s = %s;' % (m.name, m.name))
extends += '\n'.join(args)
extends += '\n\t\treturn o;\n'
extends += '\t}\n};\n'
print extends
#print block