1 |
#!/usr/bin/env python
|
2 |
|
3 |
import sys
|
4 |
import re
|
5 |
from optparse import OptionParser
|
6 |
|
7 |
parser = OptionParser()
|
8 |
parser.add_option("-r", "--read", action="store_true", dest="readMode")
|
9 |
|
10 |
(options, args) = parser.parse_args()
|
11 |
|
12 |
inputName = args[0]
|
13 |
tree = args[1]
|
14 |
|
15 |
try:
|
16 |
input = file(inputName, "r")
|
17 |
except:
|
18 |
sys.exit(1)
|
19 |
|
20 |
prog = re.compile('[ ]*([a-zA-Z_ ]+)[ ]+([a-zA-Z_][a-zA-Z0-9_]*);')
|
21 |
|
22 |
output = ""
|
23 |
|
24 |
for line in input:
|
25 |
result = prog.match(line)
|
26 |
if not result:
|
27 |
raise RuntimeError("Unrecognized input pattern " + line)
|
28 |
|
29 |
typeName = result.group(1).strip()
|
30 |
varName = result.group(2)
|
31 |
|
32 |
unsigned = False
|
33 |
if "unsigned" in typeName:
|
34 |
unsigned = True
|
35 |
typeName = typeName[len("unsigned"):].strip()
|
36 |
if len(typeName) == 0:
|
37 |
typeName = "int"
|
38 |
|
39 |
if typeName == "char":
|
40 |
typeId = "B"
|
41 |
elif typeName == "short":
|
42 |
typeId = "S"
|
43 |
elif typeName == "int":
|
44 |
typeId = "I"
|
45 |
elif typeName == "long":
|
46 |
typeId = "L"
|
47 |
elif typeName == "float":
|
48 |
typeId = "F"
|
49 |
elif typeName == "double":
|
50 |
typeId = "D"
|
51 |
else:
|
52 |
raise RuntimeError("Unknown type " + typeName)
|
53 |
|
54 |
if unsigned:
|
55 |
typeId = typeId.lower()
|
56 |
|
57 |
if options.readMode:
|
58 |
output += tree + '->SetBranchAddress("' + varName + '", &' + varName + ');\n'
|
59 |
else:
|
60 |
output += tree + '->Branch("' + varName + '", &' + varName + ', "' + varName + '/' + typeId + '");\n'
|
61 |
|
62 |
print output
|