1 #! /usr/bin/env python2.7
3 # A. V. Shiryaev, 2012.09
4 # simple *.h '#define' macro translator
10 _p1
= re
.compile('^#define\s+([^\s]+)\s+([A-Za-z0-9]+)\s*$')
11 _p2
= re
.compile('^#define\s+([^\s]+)\s+([^\s]+)\s+\/\*([^\*]+)\*\/\s*$')
30 s
.append("%s,%s" % (si
, si
+1))
32 s
.append("%s..%s" % (si
, si
+ sn
- 1))
41 s
.append("%s,%s" % (si
, si
+1))
43 s
.append("%s..%s" % (si
, si
+ sn
- 1))
44 return '{' + ','.join(s
) + '}'
49 if x
.startswith('(') and x
.endswith(')') and (x
.count('(') == 1) and (x
.count(')') == 1):
52 if x
.startswith('0x'): # fix HEX
54 if x
[2] in ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9'):
55 return x
[2:].upper() + "H"
57 return "0" + x
[2:].upper() + "H"
59 return mkset(int(x
, 16))
61 if x
[2] in ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9'):
62 return x
[2:].upper() + "X"
64 return "0" + x
[2:].upper() + "X"
67 elif x
[0] == '0': # fix OCT
71 return mkset(int(x
, 8))
73 return fixVal("0x%x" % (int(x
, 8),), tip
)
88 return fixVal("0x%x" % (int(y
),), tip
)
92 def p (nIndents
, sym
, val
, comment
, val1
):
93 r
= [ "%s%s* = %s;" % (INDENT
*nIndents
, sym
, val
) ]
94 if (val1
!= None) and (val
!= val1
):
95 r
.append( "(* %s *)" % (val1
,) )
97 r
.append( "(* %s *)" % (comment
,) )
101 return (len(s
) > 1) and s
.startswith('0') # HEX or OCT
104 if len(sys
.argv
) == 5:
105 nIndents
= int(sys
.argv
[1])
106 if sys
.argv
[2] == '*':
109 level
= int(sys
.argv
[2])
111 assert tip
in ('i', 's', 'c')
113 fh
= open(sys
.argv
[4], 'r')
115 for line
in fh
.readlines():
116 if line
.startswith("#if"):
119 elif line
.startswith("#endif"):
122 elif (level
== None) or (st
== level
):
126 val
= fixVal(r
.group(2), tip
)
127 if isBits(r
.group(2)):
128 val1
= fixVal(r
.group(2), 's')
131 comment
= r
.group(3).strip()
132 p(nIndents
, sym
, val
, comment
, val1
)
137 val
= fixVal(r
.group(2), tip
)
138 if isBits(r
.group(2)):
139 val1
= fixVal(r
.group(2), 's')
143 p(nIndents
, sym
, val
, comment
, val1
)
145 print "usage: %s nIndents level type file.h" % (sys
.argv
[0],)
147 print " number: selected level"
148 print " *: all levels"
152 print " c: shortchar"
154 if __name__
== '__main__':