blob: 02bfc5202ba64c95005327b2682f83ecc968f447 [file] [log] [blame]
Damien George55baff42014-01-21 21:40:13 +00001import argparse
2import re
Damien Georgefdf0da52014-03-08 15:03:25 +00003import sys
Damien George1976bae2014-01-24 22:22:00 +00004
5# codepoint2name is different in Python 2 to Python 3
6import platform
7if platform.python_version_tuple()[0] == '2':
8 from htmlentitydefs import codepoint2name
9elif platform.python_version_tuple()[0] == '3':
10 from html.entities import codepoint2name
Damien George55baff42014-01-21 21:40:13 +000011
Damien Georgea71c83a2014-02-15 11:34:50 +000012# add some custom names to map characters that aren't in HTML
13codepoint2name[ord('.')] = 'dot'
Paul Sokolovsky4aee1192014-02-18 00:06:37 +020014codepoint2name[ord(':')] = 'colon'
15codepoint2name[ord('/')] = 'slash'
Damien Georgea71c83a2014-02-15 11:34:50 +000016
Damien George55baff42014-01-21 21:40:13 +000017# this must match the equivalent function in qstr.c
18def compute_hash(qstr):
19 hash = 0
20 for char in qstr:
21 hash += ord(char)
22 return hash & 0xffff
23
24def do_work(infiles):
25 # read the qstrs in from the input files
Paul Sokolovskyab5d0822014-01-24 00:22:00 +020026 qstrs = {}
Damien George55baff42014-01-21 21:40:13 +000027 for infile in infiles:
28 with open(infile, 'rt') as f:
29 line_number = 0
30 for line in f:
31 line_number += 1
32 line = line.strip()
33
34 # ignore blank lines and comments
35 if len(line) == 0 or line.startswith('//'):
36 continue
37
38 # verify line is of the correct form
Paul Sokolovskyab5d0822014-01-24 00:22:00 +020039 match = re.match(r'Q\((.+)\)$', line)
Damien George55baff42014-01-21 21:40:13 +000040 if not match:
Damien Georgefdf0da52014-03-08 15:03:25 +000041 print('({}:{}) bad qstr format, got {}'.format(infile, line_number, line), file=sys.stderr)
Damien George55baff42014-01-21 21:40:13 +000042 return False
43
44 # get the qstr value
45 qstr = match.group(1)
Paul Sokolovskyab5d0822014-01-24 00:22:00 +020046 ident = re.sub(r'[^A-Za-z0-9_]', lambda s: "_" + codepoint2name[ord(s.group(0))] + "_", qstr)
Damien George55baff42014-01-21 21:40:13 +000047
48 # don't add duplicates
Paul Sokolovskyab5d0822014-01-24 00:22:00 +020049 if ident in qstrs:
Damien George55baff42014-01-21 21:40:13 +000050 continue
51
Damien George1976bae2014-01-24 22:22:00 +000052 # add the qstr to the list, with order number to retain original order in file
53 qstrs[ident] = (len(qstrs), ident, qstr)
Damien George55baff42014-01-21 21:40:13 +000054
55 # process the qstrs, printing out the generated C header file
56 print('// This file was automatically generated by makeqstrdata.py')
Dave Hylands7a996b12014-01-21 15:28:27 -080057 print('')
Damien George1976bae2014-01-24 22:22:00 +000058 for order, ident, qstr in sorted(qstrs.values(), key=lambda x: x[0]):
Damien George55baff42014-01-21 21:40:13 +000059 qhash = compute_hash(qstr)
60 qlen = len(qstr)
Paul Sokolovskyab5d0822014-01-24 00:22:00 +020061 print('Q({}, (const byte*)"\\x{:02x}\\x{:02x}\\x{:02x}\\x{:02x}" "{}")'.format(ident, qhash & 0xff, (qhash >> 8) & 0xff, qlen & 0xff, (qlen >> 8) & 0xff, qstr))
Damien George55baff42014-01-21 21:40:13 +000062
63 return True
64
65def main():
66 arg_parser = argparse.ArgumentParser(description='Process raw qstr file and output qstr data with length, hash and data bytes')
67 arg_parser.add_argument('files', nargs='+', help='input file(s)')
68 args = arg_parser.parse_args()
69
70 result = do_work(args.files)
71 if not result:
Damien Georgefdf0da52014-03-08 15:03:25 +000072 print('exiting with error code', file=sys.stderr)
Damien George55baff42014-01-21 21:40:13 +000073 exit(1)
74
75if __name__ == "__main__":
76 main()